Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. void forkexample() {
  2. pid_t *pids = malloc(10 * sizeof(pid_t));
  3. for (int i = 0; i < 10; i++) {
  4. pid_t pid = fork();
  5. if (pid < 0) {
  6. return;
  7. } else if (pid > 0) {
  8. pids[i] = pid;
  9. } else {
  10. printf("elo1 %d \n", getpid());
  11. sleep(i);
  12. printf("elo2 %d \n", getpid());
  13. exit(1);
  14. }
  15. }
  16. for (int i = 0 ; i < 10 ; i++) {
  17. int status;
  18. waitpid(pids[i], &status, 0);
  19. printf("status: %d \n", WEXITSTATUS(status));
  20. }
  21.  
  22.  
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement