Need some assistance 
July 1st, 2009, 11:24 PM
| | Newbie | | Join Date: Jun 2009
Posts: 14
| |
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. - #include <stdio.h>
-
#define DELAY 2
-
-
main ()
-
{
-
int newpid;
-
void child_code(), parent_code();
-
printf("before: mypid is %d\n", getpid());
-
if ( (newpid = fork()) == -1 )
-
perror("fork");
-
else if ( newpid == 0 )
-
child_code(DELAY);
-
else
-
parent_code(newpid);
-
}
-
void child_code(int delay)
-
{
-
printf("child %d here. will sleep for %d seconds\n", getpid(),
-
delay);
-
sleep(delay);
-
printf("child done. about to exit\n");
-
exit(17);
-
}
-
void parent_code(int childpid)
-
{
-
int wait_rv;
-
wait_rv = wait(NULL);
-
printf("done waiting for %d. Wait returned:%d\n", childpid,
-
wait_rv);
-
}
Note: The line printf("before: mypid is %d\n", getpid()); has an error: implicit declaration of function 'int wait(...)'
Please advise.
Thanks.
Last edited by Banfa; July 2nd, 2009 at 09:16 AM.
Reason: A
| 
July 2nd, 2009, 01:33 AM
| | Newbie | | Join Date: Oct 2008
Posts: 27
| | | re: Need some assistance
You need to declare your function prototypes outside the main function.
| 
July 2nd, 2009, 05:03 AM
| | Expert | | Join Date: Mar 2008 Location: Naperville, Illinois U.S.
Posts: 804
Provided Answers: 3 | | | re: Need some assistance Quote:
Originally Posted by stayit 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! | 
July 2nd, 2009, 07:20 PM
| | Newbie | | Join Date: Jun 2009
Posts: 14
| | | re: Need some assistance Quote:
Originally Posted by Jibran 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.
| 
July 2nd, 2009, 07:55 PM
|  | AdministratorVoR | | Join Date: Feb 2006 Location: South West UK
Posts: 6,117
Provided Answers: 6 | | | 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.
| 
July 2nd, 2009, 09:50 PM
| | Expert | | Join Date: Mar 2008 Location: Naperville, Illinois U.S.
Posts: 804
Provided Answers: 3 | | | 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.
| 
July 3rd, 2009, 01:02 AM
| | Newbie | | Join Date: Jun 2009
Posts: 14
| | | re: Need some assistance
Apologies. I am using the C-Free 4 IDE.
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,662 network members.
|