Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4. #include <unistd.h>
  5. #include <sys/wait.h>
  6.  
  7.  
  8. void* say_hello(void* data) {
  9. char *str;
  10. str = (char*)data;
  11.  
  12. printf("%s\n", str);
  13. // return;
  14. }
  15.  
  16. void main(int argc, char **argv) {
  17. pthread_t tp1, tp2, tc1, tc2;
  18. pid_t child_pid = 0;
  19.  
  20. printf("HELLO WORLD!\n\n");
  21. child_pid = fork();
  22. if (child_pid == 0) {
  23. pthread_create(&tc1, NULL, say_hello, "First thread from child process");
  24. pthread_create(&tc2, NULL, say_hello, "Second thread from child process");
  25.  
  26. pthread_join(tc1, NULL);
  27. pthread_join(tc2, NULL);
  28. } else {
  29. pthread_create(&tp1, NULL, say_hello, "First thread from parent process");
  30. pthread_create(&tp1, NULL, say_hello, "Second thread from parent process");
  31.  
  32. pthread_join(tp1, NULL);
  33. pthread_join(tp2, NULL);
  34. wait(0);
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement