process control

27
Process Control Process identifiers Process creation fork and vfork wait and waitpid Race conditions exec functions system function

Upload: kesia

Post on 12-Jan-2016

26 views

Category:

Documents


0 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Process Control

Process Control

Process identifiers Process creation

fork and vfork wait and waitpid

Race conditions exec functions system function

Page 2: Process Control

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.

Page 3: Process Control

Process Identifiers Functionspid_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

Page 4: Process Control

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

Page 5: Process Control

Process Creation

Child process gets a copy of the parent’s data space, heap and stack

Text segment is shared Modern implementations use copy-

on-write for data because a fork is often followed by an exec

Page 6: Process Control

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)

Page 7: Process Control

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)

Page 8: Process Control

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

Page 9: Process Control

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

Page 10: Process Control

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

Page 11: Process Control

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

Page 12: Process Control

exit functions

Normal termination Return from main Calling exit Calling _exit or _Exit

Abnormal termination Calling abort Receiving some signals

Page 13: Process Control

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

Page 14: Process Control

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.

Page 15: Process Control

wait and waitpid 4 mutually exclusive macros for

interpreting the status argument WIFEXITED(status)

WEXITSTATUS(status) WIFSIGNALED(status)

WTERMSIG(status) WCOREDUMP(status)

WIFSTOPPED(status) WSTOPSIG(status)

WIFCONTINUED(status)

Page 16: Process Control

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

Page 17: Process Control

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

Page 18: Process Control

Advantages of waitpid

Can wait for a specified process Has a non-blocking option Supports job control with the

options argument

Page 19: Process Control

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

Page 20: Process Control

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

Page 21: Process Control

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, ...);

Page 22: Process Control

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

Page 23: Process Control

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 set-user-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

Page 24: Process Control

Changing Effective IDs

int seteuid(uid_t euid);int setegid(gid_t egid); Sets only effective IDs, not real or

saved

Page 25: Process Control

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

Page 26: Process Control

User Identification

char *getlogin(void); Returns a string containing the

user’s login name or NULL on error

Page 27: Process Control

Process Times

clock_t times(struct tms *buf); struct tms

clock_t tms_utime clock_t tms_stime clock_t tms_cutime clock_t 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);