tags:

views:

126

answers:

2

This section of the glibc manual explains job control, but unless I am missing it, it doesn't explain how to implement the ability to move a job that has already launched into the background or foreground.

Can anyone give me a basic example, overview, or a link that explains how this works?

I don't mean how to do this in a shell, but rather how to implement it when writing a shell.

A: 

I don't mean to be a smart ass, but have you tried looking in:

http://ftp.gnu.org/pub/gnu/bash/bash-4.0.tar.gz

;-)

-Oisin

x0n
+4  A: 

There are two aspects of foreground/background in the Posix *nix operating systems.

One is absurdly simple: the shell or some other parent process either is or is not waiting (via some variation on wait(2)) for the child to finish. If the parent is waiting, it's foreground. If it's not, it's background. In the original versions of *nix, this was the entire story. As you can see, the shell actually did less work to implement the magic % command & pattern than just % command, yet few other systems could do this.

Now, that leaves a few loose ends. Processes have controlling terminals and terminals have process groups. There are session leaders and process group leaders. It is nice to have signals directed simultaneously at just, say, one connected pipeline in the background. Jobs in the background that attempt terminal I/O get a signal. When users log out the OS may want to reclaim a virtual or physical terminal for security and in order to give the next user unobstructed access. So the entire story gets rather more complex. 4.1BSD introduced essentially the present-day full-blown job control mechanism.

I've given you the keywords to search for if you want to know at a more detailed level than this.

DigitalRoss
I kind of got that much ( nice summary though ). More specifically, I think what I am most confused about how I would 'cancel' the wait that is blocking for the foreground job to move into the background (Handle Ctrl-Z).
Kyle Brandt
Basically, the implementation of fg %jobid or bg %jobid commands.
Kyle Brandt
The fg, I think I have. I just return the process group to the foreground and do a block wait on it.
Kyle Brandt
Oh, sorry! We get a lot of really basic questions, but rereading yours I should have realized you were writing a shell. I think you are correct about fg. For bg, I believe you trap `SIGTSTP` using `sigaction()` without specifying `SA_RESTART`. Then, you return from the trap handler to the `wait*()` call, which will will cause the `wait*()` to return with EINTR, because you did not give `SA_RESTART` to `sigaction()`.
DigitalRoss
Actually, it looks like I can just add WUNTRACED and it will block until the job is stopped or it terminates...?
Kyle Brandt