Chapter 9 : Process Management

Chapter 9 : Process Management

Contents

9.1. Fork, exec and wait

9.1.1. fork

To initiate a new process, first `fork' to produce a duplicate of the current process.

A typical child might be

A typical parent might be

9.1.2. Variants of `exec'

The value of the firs argument in execl or of argv[0] in execv is entirely up to you.

exec calls do not return. They are therefore typically followed by code such as

9.1.3. Wait

wait returns the pid (numeric process identifier) of the dying process:

If you give a parameter (`int *'), wait will return the process's exit status in i.

To control two children

9.2. Pipes

A pipe is a one way input/out channel, which must be opened BEFORE the second process is forked off.

The argument to pipe is the name of an array of int variable of length two. Variable subscript [0] is the read end ( stdin is stream 0) and variable subscript [1] the write end ( stdout is stream 1).

9.2.1. Parent writes to child

9.2.2. Parent read child's standard output

9.2.3. Two-way communication

This cannot be done with stdio.

9.3. Interrupts (signals)

We need two facilities: one to send a signal to another process (different signals, numbered 0 to 15, are possible), and another to say (early in a program) "if signal number so-and-so is received, do this".

defines mnemonics for signals.

9.3.1. Sending signals

To send to given process a given signal

A library function to send a signal SIGALRM to the current process in 60 seconds permits you to write

You must trap the signal (see below).

9.3.2. Receiving signals

On receipt of the numbered signal, we require that a particular function `sigproc' is executed.

Use the format

signal delivers as its result a pointer to the trap function previously associated with this signal.

9.3.3. To ignore a given signal

9.3.4. To save and reset the previous signal proc

9.3.5. Long jumps

To control interrupts so that, for example in a menu driven system, an interrupt brings you back to the menu, we need jumps (goto's!) back to the start of the loop if an interrupt occurs.

Copyright Eric Foxley 1996


Notes converted from troff to HTML by an Eric Foxley shell script, email errors to me!