Connecting Tech Pros Worldwide Forums | Help | Site Map

Need some assistance

Newbie
 
Join Date: Jun 2009
Posts: 14
#1: Jul 1 '09
Expand the code so that the parent creates two children and then waits until both children and then waits until both children exit.

Note: The code might have something to do in UNIX.

Expand your solution further so the program accepts an integer as a command line argument. The program then creates that number of child processes and assigns each child a ranodm number of seconds to sleep. Finally, the parent processes report as each child exits.

Expand|Select|Wrap|Line Numbers
  1. #include         <stdio.h>
  2. #define DELAY     2
  3.  
  4. main ()
  5. {
  6.     int newpid;
  7.     void child_code(), parent_code();
  8.     printf("before: mypid is %d\n", getpid());
  9.     if ( (newpid = fork()) == -1 )
  10.             perror("fork");
  11.     else if ( newpid == 0 )
  12.             child_code(DELAY);
  13.     else
  14.             parent_code(newpid);
  15. }
  16. void child_code(int delay)
  17. {
  18.     printf("child %d here. will sleep for %d seconds\n", getpid(), 
  19.         delay);
  20.         sleep(delay);
  21.         printf("child done. about to exit\n");
  22.         exit(17);
  23. }
  24. void parent_code(int childpid)
  25. {
  26.     int wait_rv;
  27.     wait_rv = wait(NULL);
  28.     printf("done waiting for %d. Wait returned:%d\n", childpid,
  29.         wait_rv);
  30. }
Note: The line printf("before: mypid is %d\n", getpid()); has an error: implicit declaration of function 'int wait(...)'

Please advise.

Thanks.

Newbie
 
Join Date: Oct 2008
Posts: 27
#2: Jul 2 '09

re: Need some assistance


You need to declare your function prototypes outside the main function.
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 831
#3: Jul 2 '09

re: Need some assistance


Quote:

Originally Posted by stayit View Post

The line printf("before: mypid is %d\n", getpid()); has an error: implicit declaration of function 'int wait(...)'

This is the same error message that was explained in your other thread: A little help with this code!
Newbie
 
Join Date: Jun 2009
Posts: 14
#4: Jul 2 '09

re: Need some assistance


Quote:

Originally Posted by Jibran View Post

You need to declare your function prototypes outside the main function.

How would I do that and what should I include? Just a little confused.
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,180
#5: Jul 2 '09

re: Need some assistance


wait is a posix function (the UNIX clue tells us this). If you google "posix wait" then you will get the help pages describing the function, how to use it and what headers you need to include.

From the question I would say you will need to read an understand most of the man (help) page on that function.
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 831
#6: Jul 2 '09

re: Need some assistance


How did you come up with line 7? It contains a pair of old-style (pre-ANSI) function declarations. This kind of function declaration went out of style in 1990. I suggest you read up on "function prototypes".

I notice that you only include <stdio.h>; but I see the following functions being called: getpid, fork, sleep, exit, wait. None of these functions are declared in <stdio.h>; however you only report a compiler error for 'wait'. Did you include other headers or did you get additional errors?

Functions getpid, fork, sleep, and wait are not part of the Standard C Library. You must have learned how to use them from some environment-specific documentation. That documentation should tell you everything about how to use them correctly.

It is not uncommon for similar functions to be provided by several environments. If you don't tell us what environment you're using then we have to guess. If we guess wrong then our advice is wrong. You refer to "UNIX", but a clear declarative sentence ("My program runs on the UNIX operating system") would have been clearer.
Newbie
 
Join Date: Jun 2009
Posts: 14
#7: Jul 3 '09

re: Need some assistance


Apologies. I am using the C-Free 4 IDE.
Reply