0% found this document useful (0 votes)
13 views31 pages

Process Control in Systems Programming

This document provides an overview of process control in Unix-like systems. It discusses the creation of new processes using fork(), process identifiers, process termination using exit(), waiting for process termination using wait() and waitpid(), avoiding race conditions, and replacing the current process with a new program using exec(). Key points include that fork() duplicates the calling process, returning 0 to the child and the child's PID to the parent, and that exec() replaces the current process rather than creating a new one.

Uploaded by

chaitanya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views31 pages

Process Control in Systems Programming

This document provides an overview of process control in Unix-like systems. It discusses the creation of new processes using fork(), process identifiers, process termination using exit(), waiting for process termination using wait() and waitpid(), avoiding race conditions, and replacing the current process with a new program using exec(). Key points include that fork() duplicates the calling process, returning 0 to the child and the child's PID to the parent, and that exec() replaces the current process rather than creating a new one.

Uploaded by

chaitanya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

CS2690

Systems Programming

Process Control

Hua Li

Process Control

Page 1 of 41

CS2690

Systems Programming

Process Control

Hua Li

Main Topics

Process Control
Creation of New Processes.
Executing [Link] executing various jobs and
tasks.
Process Termination.i.e abnormal or normal.

Page 2 of 41

CS2690

Systems Prog

Process Control

Hua Li

Process Identifiers
Every process has a unique process ID, a nonnegative integer.
There are some special processes.
Process ID 0 is usually the scheduler process and is often known
as the swapper.
It is part of the kernel and is known as a system process.

Page 3 of 41

CS2690

Systems Programming

Process Control

Hua Li

Process ID 1 is usually called the init process and is invoked by


the kernel ..
init usually reads the system-dependent initialization files (the
/etc/rc* files) and brings the system to a certain state (such as multiuser).ie makes system ready for multiple process execution.

In addition to the process ID, there are other identifiers for


every process.

Page 4 of 41

CS2690

Systems Programming

Process Control

Hua Li

The following functions return these identifiers.

#include <sys/types.h>
#include <unistd.h>
pid_t getpid(void);
Returns: process ID of calling process
pid_t getppid(void);
Returns: parent process ID of calling process

Page 5 of 41

CS2690

Systems Programming

Process Control

Hua Li

uid_t getuid(void);
Returns: real user ID of calling process
uid_t geteuid(void);
Returns: effective user ID of calling process
gid_t getgid(void);
Returns: real group ID of calling process

Page 6 of 41

CS2690

Systems Programming

Process Control

Hua Li

gid_t getegid(void);
Returns: effective group ID of calling process

fork Function
The only way a new process is created by the Unix kernel is
when an existing process calls the fork function.

Page 7 of 41

CS2690

Systems Programming

Process Control

Hua Li

#include <sys/types.h>
#include <unistd.h>
pid_t fork(void);
Returns: 0 in child, process ID of child in parent, -1 on error

The new process created by fork is called the child process.

Page 8 of 41

CS2690

Systems Programming

Process Control

Hua Li

The return value in the child is 0 while the return value in the
parent is the process ID of the new child.
The reason the childs process ID is returned to the parent is
because a process can have more than one child, so there is no
function that allows a process to obtain the process IDs of its
children.
The reason fork returns 0 to the child is because a process can
have only a single parent, so the child can always call getppid to
obtain the process ID of its parent.
Process ID 0 is always in use by the swapper, so its not
possible for 0 to be the process ID of a child.

Page 9 of 41

CS2690

Systems Programming

Process Control

Hua Li

Both the child and parent continue executing with the


instruction that follows the call to fork.
The child is a copy of the parent.
For example, the child gets a copy of the parents data space,
heap, and stack.
Note that this is a copy for the child the parent and child do
not share these portions of [Link] vitual memory.
Often the parent and child share the text code, if it is [Link] cant change read only format.

Page 10 of 41

CS2690

Systems Programming

Process Control

Hua Li

File Sharing
One characteristic of fork is that all descriptors that are open in
the parent are duplicated in the child.
Consider a process that has three different files opened for
standard input, standard output, and standard error.
On return from fork we have the arrangement shown in the
following figure:

Page 16 of 41

CS2690

Systems Programming

Process Control

Hua Li

Page 17 of 41

CS2690

Systems Programming

Process Control

Hua Li

It is important that the parent and child share the same file
offset(file_mode).
If both parent and child write to the same descriptor, without
any form of synchronization, their output will be intermixed.
This is not the normal mode of operation.
There are two normal cases for handling the descriptors after a
fork.
(1) The parent waits for the child to complete.

Page 18 of 41

CS2690

Systems Programming

Process Control

Hua Li

In this case, the parent does not need to do anything with its
descriptors.
When the child terminates, any of the shared descriptors that the
child read from or wrote to will have their file offsets updated
accordingly.

(2) The parent and child each go their own way.


Here, after the fork, the parent closes the descriptors that it doesnt
need and the child does the same thing.
This way neither interferes with the others open descriptors.
This scenario is often the case with network servers.

Page 19 of 41

CS2690

Systems Programming

Process Control

Hua Li

The two main reasons for fork to fail are


(a) if there are already too many processes in the system (which
usually means something else is wrong), or
(b) if the total number of processes exceeds the systems limit.

There are two uses for fork:


(1) When a process wants to duplicate itself so that the parent and
child can each execute different sections of code at the same time.

Page 20 of 41

CS2690

Systems Programming

Process Control

Hua Li

This is common for network servers -- the parent waits for a service
request from a client.
When the request arrives, the parent calls fork and lets the child
handle the request.
The parent goes back to waiting for the next service request to
arrive.

(2) When a process wants to execute a different program.


This is common for shells.
In this case the child does an exec right after it returns from the fork.

Page 21 of 41

CS2690

Systems Programming

Process Control

Hua Li

vfork Function
The function vfork has the same calling sequence and same
return values as fork.
vfork is intended to create a new process when the purpose of
the new process is to exec a new program.
vfork creates the new process, just like fork, without fully
copying the address space of the parent into the child, child just
calls exec (or exit) right after the vfork.

Page 22 of 41

CS2690

Systems Programming

Process Control

Hua Li

Instead, while the child is running, until it calls either exec or


exit, the child runs in the address space of the parent.
Another difference between the two functions is that vfork
guarantees that the child runs first, until the child calls exec or
exit.

exit Functions
(1) Normal termination:
(a) Executing a return from the main function.

Page 23 of 41

CS2690

Systems Programming

Process Control

Hua Li

This is equivalent to calling exit.

(b) Calling the exit function.

(2) Abnormal termination:


(a) Calling abort.
It generates the SIGABRT signal.

(b) When the process receives certain signals.

Page 24 of 41

CS2690

Systems Programming

Process Control

Hua Li

Examples of signals generated could be:


(1) the process references a memory location not within its
address space
(2) dividing by 0.

wait and waitpid Functions


#include <sys/types.h>
#include <sys/wait.h>
pid_t wait(int *statloc);
pid_t waitpid(pid_t pid, int *statloc, int options);

Page 25 of 41

CS2690

Systems Programming

Process Control

Hua Li

Both return: process ID if OK, 0 (see later), or 1 on error

The differences between these two functions are:


wait can block the caller until a child process terminates, while
waitpid has an option that prevents it from blocking.
waitpid doesnt wait for the first child to terminate -- it has a
number of options that control which process it waits for.

Page 26 of 41

CS2690

Systems Programming

Process Control

Hua Li

The interpretation of the pid argument for waitpid depends on


its value:
pid = = -1 waits for any child process. In this respect, waitpid is
equivalent to wait.
pid > 0 waits for the child whose process ID equals pid.
pid = = 0 waits for any child whose process group ID equals that
of the calling process.
pid < -1 waits for any child whose process group ID equals the
absolute value of pid.

Page 27 of 41

CS2690

Systems Programming

Process Control

Hua Li

waitpid returns the process ID of the child that terminated, and


its termination status is returned through statloc.

Page 28 of 41

CS2690

Systems Programming

Process Control

Hua Li

Race Conditions
A race condition occurs when multiple processes are trying to
do something with shared data and the final outcome depends on
the order in which the processes run.

In general we cannot predict which process runs first.

Page 34 of 41

CS2690

Systems Programming

Process Control

Hua Li

Even if we knew which process would run first, what happens


after that process starts running depends on the system load and
the kernels scheduling algorithm.
To avoid race conditions and to avoid polling, some form of
signaling is required between multiple processes.
Signals can be used for this (we will describe one way in the
section of signals).

Page 35 of 41

CS2690

Systems Programming

Process Control

Hua Li

exec Functions
When a process calls one of the exec functions, that process is
completely replaced by the new program, and the new program
starts executing at its main function.
The process ID does not change across an exec because a new
process is not created.
exec merely replaces the current process (its text, data, heap,
and stack segments) with a brand new program from disk.

Page 36 of 41

CS2690

Systems Programming

Process Control

Hua Li

With fork we can create new processes, and with the exec
functions we can initiate new programs.
The exit function and the two wait functions handle termination
and waiting for termination.

#include <unistd.h>
int execl(const char *pathname, const char *arg0, /* (char *)
0 */ );
int execv(const char *pathname, char *const argv []);

Page 37 of 41

CS2690

Systems Programming

Process Control

Hua Li

int execle(const char *pathname, const char *arg0,


/* (char *) 0, char *const envp[] */ );
int execve(const char *pathname, char *const argv[], char *const
envp[]);
int execlp(const char *filename, const char *arg0, . . . /* (char *)
0 */ );
int execvp(const char *filename, char *const argv[]);

All six return: -1 on error, no return on success

Page 38 of 41

CS2690

Systems Programming

Process Control

Hua Li

The first difference in these functions is that the first four take
a pathname argument while the last two take a filename
argument.
The next difference concerns the passing of the argument list.
The functions execl, execlp, and execle require each of the
command-line arguments to the new program to be specified as
separate arguments.
For the other three functions (execv, execvp, and execve) we
have to build an array of pointers to the arguments, and the address
of this array is the argument to these three functions.

Page 39 of 41

CS2690

Systems Programming

Process Control

Hua Li

The final difference is the passing of the environment list to the


new program.
The two functions whose name ends in an e (execle and execve)
allow us to pass a pointer to an array of pointers to the
environment strings.
The other four functions, however, use the environ variable in the
calling process to copy the existing environment for the new
program.

Page 40 of 41

CS2690

Systems Programming

Process Control

Hua Li

system Function
#include <stdlib.h>
int system(const char *cmdstring);
System Function can be used to execute a command string
within a program
system (date > file);
( system is implemented by calling fork, exec, and waitpid.)

Page 41 of 41

You might also like