1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Shell in C

  1. Apr 29, 2017 #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.
  2. jcsd
  3. Apr 29, 2017 #2


    Staff: Mentor

    I dont 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

    system("sh ls -al");

    Or something like that.
  4. Apr 29, 2017 #3


    User Avatar
    Science Advisor

    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: Apr 30, 2017
  5. Apr 30, 2017 #4

    jim mcnamara

    User Avatar

    Staff: Mentor

    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.
  6. Apr 30, 2017 #5


    Staff: Mentor

    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.
  7. May 2, 2017 #6
    Yeah, computer operating systems are like that as well.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted