Process Control
Download
Report
Transcript Process Control
Process Control
Process identifiers
Process creation
fork and vfork
wait and waitpid
Race conditions
exec functions
system function
Process Identifiers
Every process has a PID
Unique non negative integer
PIDs are re-used
Two special PIDs
0 – scheduler process. A.K.A. swapper.
System process.
1 – init process. Parent of all other
processes.
Process Identifiers
Functions
pid_t getpid(void);
pid_t getppid(void);
uid_t getuid(void);
uid_t geteuid(void);
gid_t getgid(void);
gid_t getegid(void);
Shell command ps
Process Creation
fork function
Used to create new processes
Every process except swapper and init are created
with fork
pid_t fork(void);
Creates a copy of the current process
Returns 0 in the child
Returns the PID of the child in the parent
Returns -1 on error
Process Creation
Child process gets a copy of the
parent’s data space, heap and stack
Text segment is shared
Modern implementations use copy-onwrite for data because a fork is often
followed by an exec
Buffering and forking
Buffers are part of the data of the
parent and will be copied to the child
Line buffered vs fully buffered are
flushed at different times
May cause output of program to differ if
its re-directed to a file instead of screen
(changes from line buffered to fully
buffered)
File Sharing
File descriptors are copied from parent to
child
Important that file offsets changed by child
are updated for parent as well
See Fig. 8.2 on page 214
Normally the parent will wait on the child to
finish or close the descriptors that the child
will use (and child closes the ones parent will
use)
Inherited Properties
RUID RGID EUID EGID Supplementary GIDs
Process Group ID
Session ID
Controlling terminal
set-user-ID and set-group-ID flags
Current working directory
Root directory
File mode creation mask
Signal mask and dispositions
close-on-exec flag for any open file descriptors
Environment
Attached shared memory segment
Memory mappings
Resource limits
Differences
Return value from fork
Process IDs
Different parent process IDs
Child’s tms_utime tms_stime tms_cutime and
tms_cstime values are zero
File locks not inherited
Pending alarms cleared for child
Pending signals for child set to empty set
When fork fails
Too many processes in the system
Too many processes for the real user ID
CHILD_MAX specifies the max number of
processes per user
vfork
Creates a new processes with the
express purpose of exec-ing a new
program
New child process runs in parent’s
address space until exec or exit is called
vfork guarantees that the child will run
before the parent until exec or exit call
is reached
exit functions
Normal termination
Return from main
Calling exit
Calling _exit or _Exit
Abnormal termination
Calling abort
Receiving some signals
exit functions
Special cases
Orphaned process
If the parent of a process terminates before it does, that
process becomes an orphan
init becomes the parent of all orphaned processes
Zombie process
Child terminates, but parent has not yet called the wait
or waitpid function to retrieve its exit status
Status of zombie processes shown as “Z” by the ps
command
init always calls wait or waitpid for its children, so
orphaned processes never become zombies
wait and waitpid
pid_t wait(int *status);
pid_t waitpid(pid_t pid, int *status, int options);
Both return the PID of the child or -1 on error
status will hold the return status of the child
when the function returns.
wait and waitpid
4 mutually exclusive macros for interpreting
the status argument
WIFEXITED(status)
WIFSIGNALED(status)
WTERMSIG(status)
WCOREDUMP(status)
WIFSTOPPED(status)
WEXITSTATUS(status)
WSTOPSIG(status)
WIFCONTINUED(status)
wait and waitpid
options argument specifies the behavior of
the parent. Can be zero or binary OR of the
following constants
WNOHANG – do not block for child to terminate.
Function returns zero in this case
WUNTRACED – retrieves the status information for
the process of a child that has been stopped
WCONTINUED – same as WUNTRACED, but for
the specified child that is continued
wait and waitpid
pid argument has different meanings
depending on its value
pid > 0 – wait on child whose PID == pid
pid == -1 - waits for any child
pid == 0 – waits for any child where process
group ID equals the calling process group ID
pid < -1 - waits for any child where process
group ID equals absolute value of pid
Advantages of waitpid
Can wait for a specified process
Has a non-blocking option
Supports job control with the options
argument
Race Conditions
A race condition occurs when two or
more processes are using the same
resource with no synchronization. In
this case, the result is dependant on the
order in which the processes execute
Often solved with use of signals and
other mechanisms to be discussed later
exec Functions
6 versions of exec
Replaces the program in a process with
the binary image in a specified file
PID remains the same
All return -1 on error, no return on
success
exec Functions
int execl(const char *path, const char *arg, ...);
int execle(const char *path, const char *arg,...,
char * const envp[]);
int execv(const char *path, char *const argv[]);
int execvp(const char *file, char *const argv[]);
int execlp(const char *file, const char *arg, ...);
exec Functions
p – uses filename parameter and searches
the PATH for the executable
v – arguments to new program are contained
in a vector (array of strings)
l – arguments to new program are contained
in a list (separate parameters) with final entry
being NULL
e – function takes a vector of environment
values instead of using the current system
environment
Changing UID and GID
int setuid(uid_t uid);
int setgid(gid_t gid);
Rules for setting the ID
EUID = 0 then setuid sets RUID, EUID and setuser-ID to uid
EUID != 0 but uid = RUID or set-user-ID then
setuid sets EUID to uid. RUID and set-user-ID
remain unchanged
Otherwise, sets errno to EPERM (operation not
permitted) and return -1
Changing Effective IDs
int seteuid(uid_t euid);
int setegid(gid_t egid);
Sets only effective IDs, not real or
saved
System Function
int system(const char *command);
Executes a shell command string from
within a program
Causes a fork, exec and waitpid
Returns
-1 if fork failed
127 if exec failed
Exit status of shell otherwise
User Identification
char *getlogin(void);
Returns a string containing the user’s
login name or NULL on error
Process Times
clock_t times(struct tms *buf);
struct tms
clock_t
clock_t
clock_t
clock_t
tms_utime
tms_stime
tms_cutime
tms_cstime
The clock_t values are measured in seconds
To get clock ticks, find the clock ticks per
second with sysconf(_SC_CLK_TCK);