944,033 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 6072
  • C RSS
Jun 8th, 2006
0

waitpid w/ multiple processes

Expand Post »
Hi!
I'm trying to find out what is wrong with the next code which I'm trying to run in Linux:

  1. int e1;
  2. int e2;
  3. pid_t pid1; // pid of child
  4. pid_t pid2;
  5. pid_t w1; // return code of wait()
  6. pid_t w2;
  7.  
  8. // child process 1
  9. pid1 = fork();
  10. if (pid1 == 0) {
  11. // some code...
  12. exit(e1);
  13. }
  14.  
  15. // child process 2
  16. pid2 = fork();
  17. if (pid2 == 0) {
  18. // some code
  19. exit(e2);
  20. }
  21.  
  22. // wait for children to terminate
  23. w1 = waitpid(pid1, &e1, WEXITED);
  24. if (w1 == -1) perror("");
  25. w2 = waitpid(pid2, &e2, WEXITED);
  26. if (w2 == -1) perror("");

The problem here is that even if the program compiles all right and runs all the way to end and does what supposed in the child processes, I get "invalid argument" error message from waitpid's and the flow of program just happily passes those waits. What's that? What I don't do right? I have also tried _exit() but with no different results.

I have printed out pids to see what they are and they seem to be ok.

All help appreciated!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rhp405 is offline Offline
3 posts
since Jun 2006
Jun 9th, 2006
0

Re: waitpid w/ multiple processes

WEXITED can be only used with waitid(), not waitpid(). Hum... Ok, it works better when I put
  1. waitpid(pid, &status, 0);
Just as it says in manual. :I

Or wait a minute.

It waits only process number 1, and not number 2. Why?
Last edited by rhp405; Jun 9th, 2006 at 3:34 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rhp405 is offline Offline
3 posts
since Jun 2006
Jun 9th, 2006
0

Re: waitpid w/ multiple processes

> It waits only process number 1, and not number 2. Why?
Because you asked it to.

If you use -1 as the pid, then it will wait for any child, and return with the pid of the child which ended.
Put that into a loop until all your children have ended, then carry on as normal.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jun 20th, 2006
0

Re: waitpid w/ multiple processes

Ok, here's the solution.
When you have a pipe open in parent process, you have to close the pipe before waiting. It has to do with the file descriptor counters of the pipe ends, they have to be zeroes to not cause a child to block on them (in particular, the read end). The counters are the key if you want to look for more information.

The code which works is here:
  1. close(pipefd[0]);
  2. close(pipefd[1]);
  3.  
  4. // wait for children to terminate
  5.  
  6. w1 = waitpid(pid1, &status1, 0);
  7. if (w1 == -1) perror("");
  8. printf("%d terminated: %d\n", pid1, w1);
  9.  
  10. w2 = waitpid(pid2, &status2, 0);
  11. if (w2 == -1) perror("");
  12. printf("%d terminated: %d\n", pid2, w2);
This way you can wait for specific pid's one at a time, as is done in this example.

Thanks to my professor.
Last edited by rhp405; Jun 20th, 2006 at 6:19 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rhp405 is offline Offline
3 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: ostream - how to insert a line of text
Next Thread in C Forum Timeline: Base64 Decoder





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC