fork download
  1. #include <stdio.h>
  2. #include <pthread.h>
  3. #include <unistd.h>
  4.  
  5. void* printFoo(void* arg)
  6. {
  7. printf("foo");
  8. return NULL;
  9. }
  10. void* printBar(void* arg)
  11. {
  12. printf("bar");
  13. return NULL;
  14. }
  15.  
  16. int main() {
  17. int n = 10; // number of times to print "foobar"
  18. pthread_t t1, t2;
  19.  
  20. // create threads
  21. for(int i = 0;i<n;i++)
  22. {
  23. if(i%2==0)
  24. {
  25. pthread_create(&t1, NULL, printFoo, NULL);
  26. pthread_join(t1, NULL);
  27. }
  28. else
  29. {
  30. pthread_create(&t2, NULL, printBar, NULL);
  31. pthread_join(t2, NULL);
  32. }
  33. }
  34. return;
  35. }
  36.  
Success #stdin #stdout 0s 5284KB
stdin
10
aba
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
stdout
foobarfoobarfoobarfoobarfoobar