The &

This is a discussion on The & within the Linux Programming forums, part of the Platform Specific Boards category; I'm writing a simple linux shell for mostly the heck of it and to see how they work and all ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    6

    The &

    I'm writing a simple linux shell for mostly the heck of it and to see how they work and all and so far I have things like redirection, piping, etc working but I seem to be at a sticking point. How would one implement the & command i'm going to call it in a shell? Like when you type $emacs blah & . I'm not sure how to implement the & part. Thanks.

  2. #2
    twm
    twm is offline
    root
    Join Date
    Sep 2003
    Posts
    232
    >How would one implement the & command i'm going to call it in a shell?
    First you need to find the & command in the string, but that's easy, so I'll assume you've already done it. Once you've determined that the new process is to be run in the background, you can simply fork (like you normally would), and then call setpgid to 0. But, because nobody likes a zombie you're not done yet. You need to wait for the child processes using waitpid (it's a LOT harder if you've implemented job control though). For all of this, try using a flag that says whether the new job is going to be in the background or not.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    6
    Ok thanks for your reply. I'll try this and see if I can get it. And yes the finding of the & part in what the user typed in is easy and I was able to do that, past that part though I was not sure. When I finally get down to executing things, I fork and in the child section (pid == 0) I would put the exe... function to execute things. I guess the wait thing is what I was missing pretty much.

    On with the show...

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21