Is the child process in foreground or background on fork in C

6.8k Views Asked by At

When I perform a fork() in C on Linux, is the newly created child a foreground or a background process?

If it is foreground by default, is there any way to force it to be created as a background process?

I read through quite a few links about fork, but whether it's foreground or background is not mentioned anywhere.

http://en.wikipedia.org/wiki/Fork_(system_call)

http://linux.die.net/man/2/fork

http://www.csl.mtu.edu/cs4411.ck/www/NOTES/process/fork/create.html

3

There are 3 best solutions below

3
On

Simply set the sequence as background or foreground using the commands I detailed below. Use the pid = fork() to ensure you execute in the parent or child whichever you want to set first. In the parent, pid will be the PID of the child and in the child it will be 0. Use it like this :

if(pid)
// Parent
else
// Child

Process

A process pid is put into the process group pgid by

setpgid(pid, pgid);

If pgid == pid or pgid == 0 then this creates a new process group with process group leader pid. Otherwise, this puts pid into the already existing process group pgid. A zero pid refers to the current process. The call setpgrp() is equivalent to setpgid(0,0).

Foreground

Among the process groups in a session at most one can be the foreground process group of that session. The tty input and tty signals (signals generated by ^C, ^Z, etc.) go to processes in this foreground process group.

A process can determine the foreground process group in its session using tcgetpgrp(fd), where fd refers to its controlling tty. If there is none, this returns a random value larger than 1 that is not a process group ID.

A process can set the foreground process group in its session using tcsetpgrp(fd,pgrp), where fd refers to its controlling tty, and pgrp is a process group in the its session, and this session still is associated to the controlling tty of the calling process.

How does one get fd? By definition, /dev/tty refers to the controlling tty, entirely independent of redirects of standard input and output. (There is also the function ctermid() to get the name of the controlling terminal. On a POSIX standard system it will return /dev/tty.) Opening the name of the controlling tty gives a file descriptor fd.

Background

All process groups in a session that are not foreground process group are background process groups. Since the user at the keyboard is interacting with foreground processes, background processes should stay away from it. When a background process reads from the terminal it gets a SIGTTIN signal. Normally, that will stop it, the job control shell notices and tells the user, who can say fg to continue this background process as a foreground process, and then this process can read from the terminal. But if the background process ignores or blocks the SIGTTIN signal, or if its process group is orphaned (see below), then the read() returns an EIO error, and no signal is sent. (Indeed, the idea is to tell the process that reading from the terminal is not allowed right now. If it wouldn't see the signal, then it will see the error return.)

When a background process writes to the terminal, it may get a SIGTTOU signal. May: namely, when the flag that this must happen is set (it is off by default). One can set the flag by

% stty tostop

and clear it again by

% stty -tostop

and inspect it by

% stty -a
2
On

On Linux and many Unix like systems, you may want to use daemon(3) to create a background, daemonized process.

You may also want to close(2) or redirect all of stdin, stdout, stderr (perhaps to /dev/null, see null(4)) using dup2(2).

Otherwise, take care about controlling terminals, process groups, job control, session ids. See setpgrp(2) (and setpgid) and the NOTES section. Use tcsetpgrp(3), setsid(2)

Read also the tty demystified page && tty(4)

0
On

Are you trying to change the nice value in code? Whenever you fork a process it is in the "background." Depending on what you mean, and what you're trying to do.