Crisp Reading Notes on Latest Technology Trends and Basics


Review of Concepts


pid_t p;
p = fork();
if (p == (pid_t) -1)
/* ERROR */
else if (p == 0)
/* CHILD */
/* PARENT */


  • A process is terminated normally by the exit “C” library call, or an _exit system call
  • It also returns by executing return(n) from the main function
  • The status of the system call may be collected by the parent using the wait system call

Collecting Status

pid_t p;
int status;
p = wait(&status);
  • A process that has terminated, but not yet been waited-for is called a “zombie”.
  • A zombie process stores a 2-byte status.
  • On the other hand, if the parent dies first, theinit process inherits the child, and becomes its parent.


  • Signals may force abnormal termination of a process.
  • The default action on receipt of a signal by a process is as follows
    • Terminate the process
    • Terminate the process with a core-dump
    • Ignore the signal


  • The default action on all signals except SIGKILL and SIGSTOP can be overridden
  • The SIGSTOP signal pauses the given process
  • The SIGCONT signal continues the process from where it left off.
  • SIGTTIN (input requested by background process) and SIGTTOUT (output requested by background process), also cause a process to pause.
  • Several related processes may be configured as part of the same process group.
  • An example of this is when we pipe several processes together

Process Group

Process group

% cat paper | ideal | pic | tbl | eqn | ditroff > out
  • A process group is created with the setpgid(pid, pgid) call
  • Only the process or its parent can set the process group-id.
    • The parent must set the process group-id before the child does the exec, and may move it within the session only
  • A process may set itself as a process-group leader by giving itself as the process-group-id
  • A session leader cannot set the process group-id
  • An example to set the process group

Example of setting a process group

p = fork();if (p == (pid_t) -1) {/* ERROR */

} else if (p == 0) {    /* CHILD */

setpgid(0, pgid);

} else {                /* PARENT */

setpgid(p, pgid);


Signaling and Waiting

  • One can signal to all members of a process group as part of the kill system call
kill( -getpgid(), signal );
  • One can also wait for all children of a specified process group
wait( 0, &status ); // By default, wait waits for all processes of the// process group of the current processwait( -pid, &status ); // Waits for pid of the current process

Foreground Process Groups

  • Among the several process-groups associated with a session, at most one is called a “foreground process group”.
  • The terminals associated with a “session” send their input and signals to the processes associated with the “foreground process group”.
  • A process can get / set the “foreground process group” associated with its session using
    • fd denotes the controlling terminal of the process
pid = tcgetpgrp(fd);tcsetpgrp(pgid,fd);
  • The file descriptor is obtained by opening the device /dev/tty. This works irrespective of the redirects of standard inputs and standard outputs.
  • All process groups, except “foreground process group”, are called “background process groups”.
  • Since the foreground process group is interacting with the terminal, the background processes stay out of this
    • When they try to read the Terminal input, they get a SIGTTIN signal. If this signal is blocked or ignored, then the function returns an error
  • Similarly, when they try to read the Terminal output, they get a SIGTTOU signal.
    • If background processes are allowed to write to the terminal, verified by an stty setting
    • then, they may be allowed to write to the terminal.

Background Process Group

stty -tostop

Orphaned Process Group

  • Whenever a Process Group Leader dies, it leaves the entire process group orphaned.
  • When the process leader dies, all the members of the process group are sent SIGHUP or SIGCONT.
  • Each process group belongs to a unique session.
  • A session is often created by a user-login
  • The terminal on which one is logged in becomes the controlling tty of the session.
  • By convention, the session-id is the process-id of the first member of the session, also known as “Session Leader”.
  • One can obtain the session-id with the getsessid() call.
  • The id can be changed with a setsessid() calls
  • The setsid creates a new session with the specified process-id, and makes it the leader of a group.


What are sessions

Creation and Propagation of Sessions

p = fork();
if (p) exit(0);
pid = setsid();

Controlling Terminal

Getting a Controlling terminal

  • System V specifies that the first tty that is opened by the process becomes its controlling tty.
  • As per BSD, a control tty is explicitly set using the process,
  • Linux uses a combination of these techniques.
ioctl(fd, TIOCSCTTY, …);

Getting rid of the controlling terminal

  • When the controlling terminal is not needed, as for a daemon,
    • we need to detach the process from the controlling tty.
  • This is done using the function setsid(),
  • Or by the function ioctl with flag TIOCNOTTY
if ((fork()) != 0)
if ((fork()) != 0)

The daemon function

  • The daemon function enables the process to close the controlling terminals, and to start as a system daemon
int daemonize( int nochdir, int noclose )// Specifies whether the current directory should be changed// and whether or not the stdin, stdout and stderr file descriptors

should be closed


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Tag Cloud

%d bloggers like this: