Professional Documents
Culture Documents
Process Concept
■ An operating system executes a variety of programs:
● Batch system – jobs(or process)
● Time-shared systems – user programs or tasks
■ Execution of program started via GUI mouse clicks, command line entry of its
name, etc
I/O request
process
created quantum
expired block
ready
ed
I/O complete
Process State (continued)
• While a program is undergoing the transformation into an active process, it is
said to be in the new state
• When the transformation completes, the operating system puts the process in
a queue of processes that are ready to run (the ready state)
• Eventually the process scheduler selects a process to run; when the process
is actually executing on the CPU, it is in the running state
6
Process State (continued)
• A process in the blocked state is waiting for an event and is not eligible to be
picked for execution
– A process can voluntarily move to the blocked state by executing a call
such as sleep()
– More commonly, a process moves to the blocked state when it performs
an I/O request via a system call
• A context switch is the act of removing one process from the running state
and replacing it with another
– The process context is the information that the operating system needs
about the process and its environment to restart it after a context switch
– It includes such things as the executable code, stack, registers, program
counter, and the memory used for static and dynamic variables
7
Process Control Block (PCB)
Information associated with each process (also
called task control block)
• Column headings
UID – user ID STIME – starting time of the process
PID – process ID TTY – controlling terminal
PPID – parent process ID TIME – cumulative execution time
C – (obsolete) CMD – command name
10
ps Utility
• Example output produced by ps -ef
11
Creating a Process
UNIX Process Creation
• A process creates another process by calling the fork() function
– The calling process is called the parent and the created process is
called the child
– The fork function copies the parent's memory image so that the new
process receives a copy of the address space of the parent
– Both processes continue at the instruction directly after the statement
containing the fork() call (executing in their respective memory
images)
#include <unistd.h>
pid_t fork(void);
• The return value from the fork() function is used to determine which
process is the parent and which is the child; the child gets the value 0 while
the parent gets the child's process ID
– When the fork() call fails, it returns –1 and sets errno (a child is not
created)
Fork Example (Simple)
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main(void)
{
pid_t childPID;
childPID = fork();
if (childPID == -1)
{
perror("Fork failed");
return 1;
} // End if
else if (childPID == 0) // This is the child
{
printf("My PID is %ld; I am the child\n", getpid());
} // End else if
else // This is the parent
{
printf("My PID is %ld; I am the parent of %ld\n", getpid(),
childPID);
} // End else
return 0;
} // End main
3.4 The wait() Function
The wait() Function
• When a process creates a child, both parent and child proceed with
execution from the point of the fork
• The parent process can run the wait() or waitpid() functions to block its
execution until the child process finishes
• The wait() function causes the caller (i.e., the parent) to suspend
execution until a child's status becomes available or until the caller receives
a signal
– A process status most commonly becomes available after process
termination
#include <sys/wait.h>
• It takes one parameter, a pointer to the location for returning the status of the
process. The function returns either the process ID that terminated or –1
(and sets errno)
16
The wait() Function
• If a child process terminates and its parent does not wait for it, it becomes a
zombie
– Zombies stay in the system(process table) until they are waited for
– I.e the process that has completed execution but still has an entry in the
process table
17
Overlaying a Child Process
The exec Family of Functions
• The fork() function creates a copy of the calling process, but many
applications require the child process to execute code that is different
than that of the parent
if (childpid == 0)
• "/bin/ls" const char *path
{ • "ls" const char *arg0
execl("/bin/ls", "ls", "-l", NULL);
perror("Child failed when running execl");
• "-1" const char *arg1
return 1; • NULL NULL pointer
} // End if
if (childpid != wait(NULL))
{
perror("Parent failed to wait due to signal or error");
return 1;
} // End if
return 0;
} // End main
exec Functions (Behind the scenes)
• Each of the exec functions copies a new executable into the process
image
• The program text, variables, stack and heap are overwritten
• The new process inherits the environment (i.e., the environment
variables and values) unless the original process called execle() or
execve()
• Files that are open at the time of an exec call are usually still open
afterward
• Many attributes are inherited by a process after an exec call; below are
some of them
– process ID
– parent process ID
– current working directory
– time used so far
– resource limits
– controlling terminal
execl("/bin/ls", "/bin/ls", "-r", "-t", "-l", NULL );
Pass Current
Library Call Search PATH
Argument List Environment
Name automatic?
Variables
execl list yes no
execv array yes no
execle list no no
execve array no no
execlp list yes yes
execvp array yes yes
Things to remember about exec*
• this system call simply replaces the current process with a new
program -- the pid does not change
• the exec() is issued by the calling process and what is exec'ed is
referred to as the new program -- not the new process since no new
process is created
• it is important to realize that control is not passed back to the calling
process unless an error occurred with the exec() call
• in the case of an error, the exec() returns a value back to the calling
process
• if no error occurs, the calling process is lost