How to Use the Linux wait Command for a Program Process

In summary: They're designed to be user-friendly, but they're really just a collection of shell scripts. Yeah, computer operating systems are like that as well. They're designed to be user-friendly, but they're really just a collection of shell scripts.
  • #1
Quatros
17
1
Moved from technical forum, so no template
I want to know how to implement a wait(linux) command for my program. Our professor allowed us to generate any command with system to generate a process. However, when I attempt to do this, I can see the processes that are kids on my terminal, however I can't make them wait.
 
Physics news on Phys.org
  • #2
I don't think parent has successfully gotten their kids to wait but they try.

So are you sure the command is being executed via system? There are some commands which are actually shell builtins that require you to use system to invoke the shell and the shell invokes your
command.

system("sh ls -al");

Or something like that.
 
  • #3
Quatros said:
I want to know how to implement a wait(linux) command for my program. Our professor allowed us to generate any command with system to generate a process. However, when I attempt to do this, I can see the processes that are kids on my terminal, however I can't make them wait.
Let's be clear what you're trying to do. Are you writing a program in C, which forks a child process, and then the parent process must wait for the child? If so, then a combination of execl() and wait() [or waitpid(), or waitid()] should suffice, provided you check both return values carefully. If you use system(), then you can simply let system() return and look at its return codes. Check your Linux man page for system(). Mine has an example that does pretty much what (I think) you want.

If that's not what you're trying to do, then please explain more clearly.

(Btw, if this is homework, then you should post this in the "Engineering & Computer Science" homework forum, and use the homework template. I.e., post the question verbatim -- exactly as it was given to you.)
[Edit: I see this thread has now been moved.]
 
Last edited:
  • #4
The children do not wait with either what strangerep showed you, or jedishfru showed. The PARENT process waits for the kids.

If you have "kids" that are supposed to wait, then each kid has to block on one of these:

An event like an I/O wait on a pipe - usually the kid reads from stdin on a pipe that the parent writes to.

A mutex or a semaphore is controlled by the parent, there is a shared memory object or a stream or other object used for inteprocess communication that the parent writes and maybe reads, and the child read and maybe writes. The kids are usually threads (lightweight proceses), part of the pthread library.

So you to need to inform us what your kids are really doing.
 
  • #5
jim mcnamara said:
The children do not wait with either what strangerep showed you, or jedishfru showed. The PARENT process waits for the kids.

To be clear, I was talking about real parents and their inability to get their kids to wait on anything. Parenting can be like herding cats.
 
  • Like
Likes jim mcnamara
  • #6
jedishrfu said:
To be clear, I was talking about real parents and their inability to get their kids to wait on anything. Parenting can be like herding cats.
Yeah, computer operating systems are like that as well.
 
  • Like
Likes jedishrfu

FAQ: How to Use the Linux wait Command for a Program Process

What is the Linux wait command for a program process?

The Linux wait command is used to wait for a specific process or group of processes to complete before executing the next command. It can also be used to check the exit status of a process and to pause a script until a process or series of processes have finished running.

How do I use the wait command in Linux?

To use the wait command, you need to know the PID (Process ID) of the process or group of processes you want to wait for. You can find the PID by using the ps command. Once you have the PID, you can use the wait command followed by the PID to wait for the process to complete.

Can I use the wait command for multiple processes?

Yes, you can use the wait command for multiple processes by specifying the PIDs of all the processes you want to wait for. You can also use the wait command with the wait keyword, which will wait for all child processes to finish before continuing.

What happens if the process I am waiting for never finishes?

If the process you are waiting for never finishes, the wait command will continue to wait indefinitely. To avoid this, you can use the -t option with the wait command, which will set a timeout and stop waiting if the process does not finish within the specified time.

Can I use the wait command in a shell script?

Yes, the wait command can be used in a shell script to wait for a process or group of processes to finish before executing the next command. This is particularly useful for scripts that require certain processes to complete before moving on to the next step.

Back
Top