![]() |
|
#1
|
|||
|
|||
wait() and waitpid() functionsThis 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:
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
|
|
#2
|
|||
|
|||
Re: wait() and waitpid() functionsQuote:
functions in the family execvp(), execl(), execv(), etc., never return unless there was an error. When you invoke execvp() it 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:
Output: Code:
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:
You could, maybe, do something like: CPP / C++ / C Code:
Output: Code:
Results are from Linux Fedora Core 5. Regards, Dave Footnote: "Well, did he ever return? No, he never returned and his fate is still 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 |
|
#3
|
|||
|
|||
Re: wait() and waitpid() functionsSo 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 <text.txt, it assigns "param->Input = text.txt". If they put a & argument at the end of the line of input, it sets param->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. CPP / C++ / C Code:
|
|
#4
|
||||
|
||||
Re: wait() and waitpid() functionsQuote:
CPP / C++ / C Code:
Quote:
Quote:
Quote:
But your problem was when it does have to wait, right? I would think of it like this: Code:
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. So the 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:
Of course, you might have a parameter that determines whether to wait. (Or, better 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 checking and reporting also. I hope this helps. Regards, Dave |
Recent GIDBlog
Toyota - 2008 November Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The