473,385 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Need some assistance

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.

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.
Jul 1 '09 #1
6 2152
Jibran
30
You need to declare your function prototypes outside the main function.
Jul 2 '09 #2
donbock
2,426 Expert 2GB
@stayit
This is the same error message that was explained in your other thread: A little help with this code!
Jul 2 '09 #3
stayit
14
@Jibran
How would I do that and what should I include? Just a little confused.
Jul 2 '09 #4
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.
Jul 2 '09 #5
donbock
2,426 Expert 2GB
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.
Jul 2 '09 #6
stayit
14
Apologies. I am using the C-Free 4 IDE.
Jul 3 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
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...
0
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...
2
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...
0
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...
0
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...
1
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
0
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...
46
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...
1
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...
3
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...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
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,...
0
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...
0
Oralloy
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,...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.