Processes
Process Control
Unix manages multiple concurrent processes. Each process has memory allocation for its exclusive use, referred to at the address space of the process and is composed of:
- Text – Machine instructions for procedures and function in the program
- Data – that values of variables, arrays, and structures
- Stack – to manage function calls, returns, parameter passing, returned value
Each process is also assign system resources necessary for the UNIX kernel (the central part of the operating system) to manage.
Each process is represented by an entry in the process table which is manipulated by the kernel to manage all processes.
A process usually goes through a number of states:
- Running – The process is executing
- Asleep – Waiting for an event to occur, e.g., I/O completion by a peripheral device
- Ready – Scheduled for CPU service
- Zombie – Process has terminated. Last state of a process
Process Table
A system-wide process table is maintained in the UNIX kernel to control all processes. There is one table entry for each existing process. Each process entry contains info such as:
- PIDprocess ID
- UID
- State
- Event
- Size
- Locations
- Priority
- Signals
- Accounting
For more details see table on page 348.
Displaying process status
To display information about existing processes use the UNIX command:
ps
To display all processes use option –a. Use option –l for long.
Process Creation: fork
The fork system call is used inside a C program to create another process. The process that calls the fork is called the parent process and the newly created process is called the child process. After the fork call the child and parent processes run concurrently. To invoke fork use:
int pid;
pid = fork();
The child process is a copy of the parent process except for the following:
- The child process has a unique PID (process identification)
- The child process has a different PIPD (parent process identification = PID or creator)
The fork is called by the parent, but returns in both the parent and child. In the parent it returns the PID of the child process, whereas in the child it returns 0. If fork fails, no child process is created and it returns –1.
Example C program using fork.
As a second example using fork, the following program uses the child to compute the partial sums and the parent to compute the partial product from an array Numbers of integers. Both child and parent have access to their own copies of the Numbers and other variables such as Answer.
Program Execution: execv
An execv (exc1, execve) call transforms the calling process into a new process constructed from an executable file (either an a.out file of an executable text file).
Example C program to implement a simple shell using fork and execv calls.
Synchronization of Parent and Child Processes
The parent process can wait for the child process to terminate before proceeding further using the system call:
#include <sys/wait.h>
int pid = wait(union wait *status)
Interprocess Communication (IPC)
The C library function popen creates a child process to execute the shcmd_string and extablishes a read or write stream (FILE *) to the child
FILE *popen(char *cmd_string, char mode)
where mode is
“r” for reading the standard output of given command
“w” for writing the standard input of given command
The stream created by popen can be shut down by
int pclose(FILE *stream)
Example C program: application of popen that counts total number of words in a list of files.
Interprocess Communication with pipe
A pipe is a direct in memory I/O channel between processes.
int pipe(int FileDescriptors[2])
establishes a pipe with a buffer and two descriptors
FileDescriptors[0] (for reading the pipe)
FileDescriptors[1] (for writing the pipe)
Example C program: A parent process writes the message “Hello Dolly” to a child process through a pope.
1