944,044 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 63388
  • C RSS
Mar 25th, 2007
0

fork() and wait()

Expand Post »
I am supposed to create a chain of N processes, where N is the command line parameter. Each process creates a child process, prints out its own PID and its parent's PID, and then waits for its child to terminate by calling the wait() function.

Here is my code:
  1. #include <stdio.h>
  2. int main(int argc, char *argv[])
  3. {
  4.  
  5. int number = atoi(argv[1]);
  6. int i;
  7. int pid;
  8.  
  9. for (i=0; i<number-1; i++)
  10. {
  11.  
  12. pid=fork();
  13. if (pid==0)
  14. wait();
  15.  
  16. else
  17. {
  18. printf("I am the child\n");
  19. printf("my pid=%d\n", getpid());
  20. printf("and my parent pid=%d\n", getppid());
  21. }
  22.  
  23. }
  24. }
Any ideas why it's not working properly?

Example output:

[balhoffe1@gator labfork]$ ./lab2 4
I am the child
my pid=6381
and my parent pid=5670
I am the child
my pid=6381
and my parent pid=5670
I am the child
my pid=6382
and my parent pid=6381
I am the child
my pid=6381
and my parent pid=5670
[balhoffe1@gator labfork]$ I am the child
I am the child
my pid=6384
and my parent pid=1
my pid=6382
and my parent pid=1
I am the child
my pid=6383
and my parent pid=1
Last edited by ~s.o.s~; Mar 25th, 2007 at 1:50 pm. Reason: Added code tags, learn to use them.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
rwagnes is offline Offline
36 posts
since Dec 2006
Mar 25th, 2007
0

Re: fork() and wait()

# if (pid==0)
# wait();
fork() returns 0 in the child, not the parent.

Your if/else is the wrong way round.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Mar 25th, 2007
0

Re: fork() and wait()

thanks! that helped alot.
Reputation Points: 10
Solved Threads: 0
Light Poster
rwagnes is offline Offline
36 posts
since Dec 2006
Dec 18th, 2009
-1
Re: fork() and wait()
it might be because you havent defined pid of type pid_t.

you need to do the following in order to be able to use pid:

  1. #include<sys/types.h>
  2.  
  3. pid_t pid1;
  4.  
  5. pid1=fork();

you can then use wait(pid).

Click to Expand / Collapse  Quote originally posted by rwagnes ...
I am supposed to create a chain of N processes, where N is the command line parameter. Each process creates a child process, prints out its own PID and its parent's PID, and then waits for its child to terminate by calling the wait() function.

Here is my code:
  1. #include <stdio.h>
  2. int main(int argc, char *argv[])
  3. {
  4.  
  5. int number = atoi(argv[1]);
  6. int i;
  7. int pid;
  8.  
  9. for (i=0; i<number-1; i++)
  10. {
  11.  
  12. pid=fork();
  13. if (pid==0)
  14. wait();
  15.  
  16. else
  17. {
  18. printf("I am the child\n");
  19. printf("my pid=%d\n", getpid());
  20. printf("and my parent pid=%d\n", getppid());
  21. }
  22.  
  23. }
  24. }
Any ideas why it's not working properly?

Example output:

[balhoffe1@gator labfork]$ ./lab2 4
I am the child
my pid=6381
and my parent pid=5670
I am the child
my pid=6381
and my parent pid=5670
I am the child
my pid=6382
and my parent pid=6381
I am the child
my pid=6381
and my parent pid=5670
[balhoffe1@gator labfork]$ I am the child
I am the child
my pid=6384
and my parent pid=1
my pid=6382
and my parent pid=1
I am the child
my pid=6383
and my parent pid=1
Reputation Points: 7
Solved Threads: 0
Light Poster
shakunni is offline Offline
29 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: Program for network Scan
Next Thread in C Forum Timeline: merge sort & quick sort how its nlogn





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


Follow us on Twitter


© 2011 DaniWeb® LLC