wait1
TRANSCRIPT
-
7/28/2019 wait1
1/6
#1
#2
GIDForums> Computer Programming Forums> C Programming Language
wait() and waitpid() functions
User Name User Name Remember Me?
Password Log in
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
Thread Tools Search this Thread Rate Thread
20-Sep-2006, 10:22
groberts1980New Member
Join Date: Sep 2006Posts: 2
wait() and waitpid() functions
This is my first post here, so hello everybody. I have a question about a C program I'm writing for an Operating Systems class. I
basically need to write a little shell that will take commands (ls, ls -l, ./program.c, cat prog.c >text.txt, etc).
After fork() is called, I have an if and else to determine whether the parent or child is running. In the parent branch, I need it to
wait for the child to finish if the user did not specify the & (background) argument.
I'm having trouble figuring out the wait() and waitpid() functions. Here is the a snippet of the code in question:
CPP / C++ / C Code:
cpid = fork();
if(cpid) { //parent if(param.Background ==0) {
//use wait function here }}else { //child
execvp(param.FirstArg, param.ArgVector); return(0);}
When I run the program, the parent does not wait for the child to complete. In the shell, I'm running a program called slow.c, that
prints out the process id of itself 10 times, in one second intervals. Hence the name "slow." When it runs, the prompt for the parent
comes right back up, when it should wait for slow to finish before coming back.
Any help on this would be most humbly appreciated.
Last edited by LuciWiz : 20-Sep-2006 at 15:15. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
20-Sep-2006, 16:37
davekw7xOutstanding Member
Join Date: Feb 2004Location: Left Coast, USAPosts: 5,764
Re: wait() and waitpid() functions
Quote:
Originally Posted by groberts1980
In the parent branch, I need it to wait for the child to finish
funct ions in the family execvp(), execl(), execv(), etc., never return unless there was an error. When you invoke execvp() it
Guide to Process RulesFree 20 page Process Rules Guide. Example Business Rules Included!www.progress.com
converted by Web2PDFConvert.com
http://www.gidforums.com/member.php?u=1284http://www.gidforums.com/member.php?u=1284http://www.gidforums.com/showpost.php?p=49738&postcount=1http://www.gidforums.com/memberlist.phphttp://www.gidforums.com/calendar.phphttp://www.gidforums.com/search.phphttp://www.gidforums.com/search.phphttp://www.gidforums.com/search.phphttp://www.gidforums.com/http://www.gidforums.com/f-36.htmlhttp://www.web2pdfconvert.com/?ref=PDFhttp://www.web2pdfconvert.com/?ref=PDFhttp://www.gidforums.com/member.php?u=1284http://www.gidforums.com/showpost.php?p=49758&postcount=2http://www.gidforums.com/member.php?u=10924http://www.gidforums.com/showpost.php?p=49738&postcount=1http://www.gidforums.com/forumdisplay.php?do=markreadhttp://www.gidforums.com/search.php?do=getdailyhttp://www.gidforums.com/search.phphttp://www.gidforums.com/calendar.phphttp://www.gidforums.com/memberlist.phphttp://www.gidforums.com/faq.phphttp://www.gidforums.com/register.phphttp://www.gidforums.com/f-41.htmlhttp://www.gidforums.com/f-36.htmlhttp://www.gidforums.com/http://www.gidforums.com/ -
7/28/2019 wait1
2/6
replaces the current process image with a new process, whose path is given in the first argument. But, and I hate to repeat myself,
it never returns. See footnote.
For starters, you could consider:
CPP / C++ / C Code:
#include #include #include #include #include int main(){ int i;
pid_t cpid;pid_t child_pid;cpid = fork();
switch (cpid) { case -1: printf("Fork failed; cpid == -1\n"); break;
case0: child_pid = getpid(); for (i = 0; i < 10; i++) {
printf("%d: this is the child, pid = %d\n", i, child_pid)sleep(1);
}exit(0);
default: rintf "This is the arent: waitin for %d to finish\n" c
Output:
Code:
0: this is the child, pid = 3562This is the parent: waiting for 3562 to finish1: this is the child, pid = 35622: this is the child, pid = 35623: this is the child, pid = 35624: this is the child, pid = 35625: this is the child, pid = 35626: this is the child, pid = 35627: this is the child, pid = 3562
8: this is the child, pid = 35629: this is the child, pid = 3562Ttttthat's all, folks
I'm not sure exactly what you need, but if you want to put see the "sleep" stuff in a process invoked by the child:
CPP / C++ / C Code:
/** file slow.c*/
#include #include
#include
int main(){
pid_t pid; int i;
pid = getpid(); for (i = 0; i < 10; i++) {
printf("%d: This is slow --- pid = %d\n", i, pid);sleep(1);
} return0;}
You could, maybe, do something like:
converted by Web2PDFConvert.com
http://www.web2pdfconvert.com/?ref=PDFhttp://www.web2pdfconvert.com/?ref=PDF -
7/28/2019 wait1
3/6
#3
CPP / C++ / C Code:
#include #include #include #include #include int main(){ int i;
pid_t cpid;pid_t child_pid;cpid = fork();
switch (cpid) { case -1: printf("Fork failed; cpid == -1\n"); break;
case0: child_pid = getpid();printf("This is the child: pid = %d\n", child_pid);system("./slow");exit(0);
default: printf("This is the parent: waiting for %d to finish\n", cpwaitpid(cpid, NULL, 0);rintf "Ttttthat's all folks\n" ;
Output:Code:
This is the child: pid = 3838This is the parent: waiting for 3838 to finish0: This is slow --- pid = 38391: This is slow --- pid = 38392: This is slow --- pid = 38393: This is slow --- pid = 38394: This is slow --- pid = 38395: This is slow --- pid = 38396: This is slow --- pid = 38397: This is slow --- pid = 38398: This is slow --- pid = 38399: This is slow --- pid = 3839Ttttthat's all, folks
Results are from Linux Fedora Core 5.
Regards,
Dave
Footnote:
"Well, did he ever return?
No, he never returned and his fate is st ill unknown. "
Charlie on the MTA-- -Jacqueline Steiner and Bess Lomax
Sung by the Kingston Trio, 1959
"Was it really 1959??? Sometimes it seems like yesterday. Where does the time go?"
---davekw7x
20-Sep-2006, 17:34
groberts1980New Member
Join Date: Sep 2006Posts: 2
Re: wait() and waitpid() functions
So because it does not return from execvp, I do not need a return statement?
We have to use exec, cannot use system() calls.
I'm running it like this, and it still is not waiting. When the program first runs, it takes a line of input and passes it into a function,
which fills up the param struct. If the input contains Background = 1.
If the parent runs, and does not have to wait for the child (& operator in child), it should go back and print the prompt, waiting for
more input.
converted by Web2PDFConvert.com
http://www.web2pdfconvert.com/?ref=PDFhttp://www.web2pdfconvert.com/?ref=PDFhttp://www.gidforums.com/member.php?u=10924http://www.gidforums.com/showpost.php?p=49764&postcount=3 -
7/28/2019 wait1
4/6
#4
CPP / C++ / C Code:
cpid = fork();switch (cpid){ case -1:
printf("Fork failed; cpid == -1\n"); break;
case0: //child if(param.Input != NULL) //redirect input
{fp = freopen(param.Input,"r",stdin);
} if(param.Output != NULL) //redirect output
{fp = freopen(param.Output,"w",stdout);
}execvp(param.FirstArg, param.ArgVector);
break;
default: //parent if(param.Background)
{waitpid(cpid,NULL,0);
20-Sep-2006, 18:57
davekw7xOutstanding Member
Join Date: Feb 2004Location: Left Coast, USAPosts: 5,764
Re: wait() and waitpid() functions
Quote:
Originally Posted by groberts1980
So because it does not return from execvp, I do not need a return statement?
The only time it returns is if there is an error. Therefore, I would usually expect something like:
CPP / C++ / C Code:
execvp(*args, args);perror(*args);exit(1);
Quote:
Originally Posted by groberts1980
We have to use exec, cannot use system() calls.
That was just an illustration, where I used system() instead of the execvp that you had indicated. Playing with simple stuff like this
might give you a better feel for the whole forking process. (It does for me.)
Quote:
Originally Posted by groberts1980
When the program first runs, it takes a line of input and passes it into a function, which fills up the param struct.
That sounds OK for starters.
Quote:
Originally Posted by groberts1980
If the input contains Background = 1.
If the parent runs, and does not have to wait for the child (& operator in child), it should go back and print the prompt,
waiting for more input.
But your problem was when it does have to wait, right?
I would think of it like this:
converted by Web2PDFConvert.com
http://www.web2pdfconvert.com/?ref=PDFhttp://www.web2pdfconvert.com/?ref=PDFhttp://www.gidforums.com/member.php?u=1284http://www.gidforums.com/showpost.php?p=49766&postcount=4 -
7/28/2019 wait1
5/6
Code:
loopget lineparse line into argumentsexecute the command
end loop
I would think that the big loop would be in main(), and I would have a separate function for parsing, and another function to execute
the command after the command line had been parsed into arguments.
If you are going to call execvp(), you need the args as an array of pointers to char. I assume that is in your struct , somehow. Sothe execute() function looks something like the main() from my previous example. Here's an execute() function whose arguments are
already in the form you need to call execvp.
The child calls exevp() and for this one, the parent always waits for the child to finish. You can easily fix it for the background
situation where you don't want to wait.
CPP / C++ / C Code:
/** execute--spawn a child process and execute the program.*/int execute(char **args){
pid_t cpid;
cpid = fork();
switch (cpid) { case -1: perror("fork"); break; /* maybe you want to return something indicating error
case0: execvp(*args, args); /* this is the child */perror(*args);exit(1); /* major system error: fuggedaboudit */
default: waitpid(cpid, NULL, 0); /* TODO: parent shouldn't wait if it'
} return0; /* or whatever value your main() program would like to see */}
Of course, you might have a parameter that determines whether to wait. (Or, bett er yet, your parameter might be your struct, and
inside the execute() function it would figure out what the arguments to execvp are and whether to wait. There might be more error
chec king and reporting also.
I hope this helps.
Regards,
Dave
Previous Thread | Next Thread
Recent GIDBlog Windows XP ?You may be a victim of software counterfeiting? by LocalTech
Thread Tools Search this Thread
Show Printable Version
Email this Page
Search this Thread:
Go
Advanced Search
Rate This Thread
Rate This Thread:
5 : Excellent Go
Ads by Google
BPM Process
Function DJ
Child
Ads by Google
MSSQL Function
Process Mapping
Cell Function
converted by Web2PDFConvert.com
http://www.web2pdfconvert.com/?ref=PDFhttp://www.web2pdfconvert.com/?ref=PDFhttp://www.gidforums.com/search.phphttp://www.gidforums.com/sendmessage.php?do=sendtofriend&t=11552http://www.gidforums.com/printthread.php?t=11552http://localtech.gidblog.com/2011/07/15/windows-xp-%e2%80%9cyou-may-be-a-victim-of-software-counterfeiting%e2%80%9d/http://gidblog.com/http://www.gidforums.com/t-11552.html?goto=nextnewesthttp://www.gidforums.com/t-11552.html?goto=nextoldest -
7/28/2019 wait1
6/6
Posting Rules
You may not post new threadsYou may not post repliesYou may not post attachmentsYou may not edit your posts
vB code is OnSmilies are On[IMG] code is OnHTML code is Off
Forum Jump
C Programming Language Go
Network Sites: GIDNetwork GIDWebHosts GIDSearch Learning Journal by J de Silva, The
All times are GMT -6. The time now is 12:17.
Contact Us - GIDForums - Archive - Top
vBulletin, Copyright 2000 - 2011, Jelsoft Enterprises Ltd.
http://www.web2pdfconvert.com/?ref=PDFhttp://www.web2pdfconvert.com/?ref=PDFhttp://www.gidforums.com/gid_archive.phphttp://www.gidforums.com/http://www.gidforums.com/sendmessage.phphttp://www.desilva.biz/http://www.gidsearch.com/http://www.gidwebhosts.com/http://www.gidnetwork.com/http://www.gidforums.com/misc.php?do=bbcode#imgcodehttp://www.gidforums.com/misc.php?do=showsmilieshttp://www.gidforums.com/misc.php?do=bbcode