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.
6 2152
You need to declare your function prototypes outside the main function.
@Jibran
How would I do that and what should I include? Just a little confused.
Banfa 9,065
Expert Mod 8TB
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.
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.
Apologies. I am using the C-Free 4 IDE.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: lagunasun |
last post by:
not shitass responses. People like this david fellow, degrade the board.
again, here is my msg, and thanks again for any assistance!
I am going from learning c++ console apps to windows, and need...
|
by: Jawahar |
last post by:
All
I had posted this in the remote assistance group and could not get any
replies so I thought that I could try in the developer group
Thanks
One of the issues we face when helping our remote...
|
by: CSDunn |
last post by:
Hello,
I need some assistance with error handling in an Access 2003 Project
form. The project is has a data source connection to a SQL Server 2000
database. The main form is named...
|
by: Joe Ross |
last post by:
(Apologies in advance if there is a better forum for asking advice on this
topic).
Our ASP.NET application occasionally starts spitting out OutOfMemory
exceptions. When this happens, the memory...
|
by: mwalk66 |
last post by:
I have been able to create a web based company phone book by retrieving
the last name, first name, email address and telephone # from active
directory. However I need serious assistance in...
|
by: Chris |
last post by:
Hi,
How can I post a web request like
http://site.com/lookupvalue.aspx?id=xxxxx
and then parse the request ex
Home
|
by: serpius |
last post by:
Hello Everyone,
I am a beginner in VB.Net 2003 and am taking classes in beginning VB.net. To make a long story short, I am looking for real samples of coding in Console Apps.
I am the type of...
|
by: Bruce W. Darby |
last post by:
This will be my very first VB.Net application and it's pretty simple. But
I've got a snag in my syntax somewhere. Was hoping that someone could point
me in the right direction.
The history:
My...
|
by: DR |
last post by:
What ports do i need to unblock on client and server (running msvsmon.exe)
to debug remotely from my client box with visual studio 2005 pro?
When I attach to remote process a connection shows up...
|
by: gaurav92K |
last post by:
sir i am working in a company . there are many pc. i want to use remote assistance. i configure all group policy which are related remote assistance.and i enable service through remote in system...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |