fork download
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. void *fact(void* t) {
  6. int i, n = *((int *)t);
  7. for (i = n; i > 1; i--)
  8. n = n * (i);
  9. printf("factorial of %d = %d\n", *((int *)t), n);
  10. return NULL;
  11. }
  12.  
  13. int main() {
  14. pthread_t thread_id;
  15. int n, ret;
  16. printf("Enter number: ");
  17. scanf("%d", &n);
  18. ret = pthread_create(&thread_id, NULL, &fact, &n);
  19. if (ret != 0) {
  20. perror("Error");
  21. exit(EXIT_FAILURE);
  22. }
  23. pthread_exit(0);
  24. }
  25.  
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
Enter number: factorial of 22002 = 0