Sei sulla pagina 1di 38

Chapter 3: Processes

Chapter 3: Processes

„ Process Concept
„ Process Scheduling
„ Operations on Processes
„ Cooperating Processes
„ Interprocess Communication
„ Communication in Client-Server Systems

Operating System Concepts - 7th Edition, Feb 7, 2006 3.2 Silberschatz, Galvin and Gagne ©2005
Process Concept

„ An operating system executes a variety of programs:


z Batch system – jobs
z Time-shared systems – user programs or tasks
„ Textbook uses the terms job and process almost
interchangeably
„ Process – a program in execution; process execution must
progress in sequential fashion
„ A process is the unit of work within the system.
„ Systems consist of a collection of processes: operating-
system processes and user processes.

Operating System Concepts - 7th Edition, Feb 7, 2006 3.3 Silberschatz, Galvin and Gagne ©2005
Process Concept (cont’d)
„ A process executes a program, but a program is not a process.
z A program is passive ( a set of instructions).
z A process is active (a program in execution).
z Multiple processes may execute the same program
„ A process contains all of the state for a program in execution
z An address space
z The code (called text sometimes) for the executing program
z The data for the executing program
z An execution stack encapsulating the state of procedure calls
z The program counter (PC) indicating the next instruction
z A set of general-purpose registers with current values
z A set of operating system resources
 Open files, network connections, etc.
„ A process may contain a Heap , which is a memory that is allocated during
process run-time.
„ A process is named using its process ID (PID)

Operating System Concepts - 7th Edition, Feb 7, 2006 3.4 Silberschatz, Galvin and Gagne ©2005
Process in Memory

Operating System Concepts - 7th Edition, Feb 7, 2006 3.5 Silberschatz, Galvin and Gagne ©2005
Process State

„ A process has an execution state that indicates what it is currently


doing
„ As a process executes, it changes state
z new: The process is being created
z running: Instructions are being executed on a CPU
z waiting: The process is waiting for some event to occur
 It cannot make progress until event is signaled (disk completes)
z ready: The process is waiting to be assigned to a CPU
 Ready to execute, but another process is executing on the CPU
z terminated: The process has finished execution
„ Unix command “ps”: STAT column indicates execution state

Operating System Concepts - 7th Edition, Feb 7, 2006 3.6 Silberschatz, Galvin and Gagne ©2005
Diagram of Process State

Operating System Concepts - 7th Edition, Feb 7, 2006 3.7 Silberschatz, Galvin and Gagne ©2005
Process Control Block (PCB)
„ Each process is represented in the OS by a PCB
„ PCB includes Information associated with each process
z Process state: any of the above states.
z Program counter: next instruction to be executed
z CPU registers: must be saved during state changes
z CPU scheduling information such as priority
z Memory-management information: base register, page tables, segment
tables
z Accounting information such as CPU cycles or time.
z I/O status information: allocated devices, open files
„ The PCB is where the OS keeps all of a process’ hardware execution state
(PC, SP, regs, etc.) when the process is not running
z This state is everything that is needed to restore the hardware to the
same configuration it was in when the process was switched out of the
hardware

Operating System Concepts - 7th Edition, Feb 7, 2006 3.8 Silberschatz, Galvin and Gagne ©2005
Process Control Block (PCB)

Operating System Concepts - 7th Edition, Feb 7, 2006 3.9 Silberschatz, Galvin and Gagne ©2005
PCBs and Hardware State

„ When a process is running, its hardware state (PC, SP, regs, etc.)
is in the CPU
z The hardware registers contain the current values
„ When the OS stops running a process, it saves the current values
of the registers into the process’ PCB
„ When the OS is ready to start executing a new process, it loads the
hardware registers from the values stored in that process’ PCB
z What happens to the code that is executing?
„ The process of changing the CPU hardware state from one
process to another is called a context switch
z This can happen 100 or 1000 times a second!

Operating System Concepts - 7th Edition, Feb 7, 2006 3.10 Silberschatz, Galvin and Gagne ©2005
CPU Switch From Process to Process

Operating System Concepts - 7th Edition, Feb 7, 2006 3.11 Silberschatz, Galvin and Gagne ©2005
Context Switch

„ When the CPU is switched to another process, the system must


save the state of the old process and load the previously saved
state for the new process. This is called context switching.
„ The context is represented in the PCB of the process
z Value of CPU registers
z Process state
z Memory-management information
„ Context-switch time is overhead; the CPU does no useful work
while switching between processes, so it should be done fast.
„ Time dependent on hardware support.
z Memory speed
z No of registers
z Special instructions

Operating System Concepts - 7th Edition, Feb 7, 2006 3.12 Silberschatz, Galvin and Gagne ©2005
Process Scheduling

„ Multiprogramming: the goal is to have some process running at all


time to maximize the CPU utilization
„ Time-sharing: the goal is to switch the CPU among processes so
frequently that users can interact with each program while it is
running
„ Done by process scheduler.
z Note that for single-processor system, there will never be more
that one running process at the same time.

Operating System Concepts - 7th Edition, Feb 7, 2006 3.13 Silberschatz, Galvin and Gagne ©2005
Process Scheduling Queues
How does the OS keep track of processes?
„ The OS maintains a collection of queues that represent the state of
all processes in the system
„ Typically, the OS has one queue for each state
z Ready, waiting, etc.
„ Each PCB is queued on a state queue according to its current state
„ As a process changes state, its PCB is unlinked from one queue and
linked into another
„ Examples:
z Job queue – set of all processes in the system
z Ready queue – set of all processes residing in main memory,
ready and waiting to execute
 Generally stored as linked list of PCB’s
z Device queues – set of processes waiting for an I/O device
 Each device has its own device queue

Operating System Concepts - 7th Edition, Feb 7, 2006 3.14 Silberschatz, Galvin and Gagne ©2005
Process Queues

Operating System Concepts - 7th Edition, Feb 7, 2006 3.15 Silberschatz, Galvin and Gagne ©2005
Representation of Process Scheduling
„ New processes in ready
queue
„ Wait until selected for
execution (dispatched)
„ Then,
z I/O request Î I/O queue
z Create new sub process
Îwait for finishing
z Removed forcibly from CPU
by interrupt Î back to ready
queue
„ After waiting, the process
eventually becomes ready
and moved to the ready
queue. This process
continues until the process
terminates.

Operating System Concepts - 7th Edition, Feb 7, 2006 3.16 Silberschatz, Galvin and Gagne ©2005
Schedulers

„ A process moves among various queues. The OS must


select processes from these queues. This is carried out by
the appropriate scheduler.
„ Long-term scheduler (or job scheduler) – selects which
processes should be brought into the ready queue from a
mass-storage device (typically, a disk)
„ Short-term scheduler (or CPU scheduler) – selects
which process should be executed next and allocates CPU
to it
„ Medium-term scheduler may be used in time-sharing
systems with swapping.( remove process from memory to
decrease degree of multiprogramming, then restore the
process later)

Operating System Concepts - 7th Edition, Feb 7, 2006 3.17 Silberschatz, Galvin and Gagne ©2005
Schedulers (cont’d)

„ Frequency of execution is the main distinction among different types of


schedulers.
„ Short-term scheduler is invoked very frequently.
z (milliseconds) Î (must be fast)
„ Long-term scheduler is invoked very infrequently.
„ (seconds, minutes) Î (may be slower)
z The long-term scheduler controls the degree of multiprogramming.( No
of processes in memory)
z It is important for the long-term scheduler to select a good process mix.
„ A process can be described as either:
z I/O-bound process – spends more time doing I/O than computations;
many short CPU bursts.
z CPU-bound process – spends more time doing computations; few very
long CPU bursts.

Operating System Concepts - 7th Edition, Feb 7, 2006 3.18 Silberschatz, Galvin and Gagne ©2005
Addition of Medium Term Scheduling

Operating System Concepts - 7th Edition, Feb 7, 2006 3.19 Silberschatz, Galvin and Gagne ©2005
Schedulers (Cont.)

„ Short-term scheduler is invoked very frequently (milliseconds) ⇒


(must be fast)
„ Long-term scheduler is invoked very infrequently (seconds,
minutes) ⇒ (may be slow)
„ The long-term scheduler controls the degree of multiprogramming
„ Processes can be described as either:
z I/O-bound process – spends more time doing I/O than
computations, many short CPU bursts
z CPU-bound process – spends more time doing computations;
few very long CPU bursts

Operating System Concepts - 7th Edition, Feb 7, 2006 3.20 Silberschatz, Galvin and Gagne ©2005
Process Creation

„ Parent process create children processes, which, in turn create other


processes, forming a tree of processes
z (Unix: ps “PPID” field)
z What creates the first process (Unix: init (PID 0 or 1))
„ Processes are identified by a unique process identifier (pid)
„ Resource sharing
z Parent and children share all resources
z Children share subset of parent’s resources
z Parent and child share no resources
z Ex: Unix: Process User ID is inherited – children of your shell
execute with your privileges
„ Execution
z Parent and children execute concurrently
z Parent waits until children terminate

Operating System Concepts - 7th Edition, Feb 7, 2006 3.21 Silberschatz, Galvin and Gagne ©2005
Process Creation (Cont.)
„ Address space
z Child duplicate of parent (same program code and date)
z Child has a new program loaded into it
„ UNIX examples
z Fork() system call creates new process
 same address space (easy communication between them)
 Executes concurrently
 Return value from fork distinguish which process is which
– Returns the child’s PID to the parent, “0” to the child
z fork()
 Creates and initializes a new PCB
 Creates a new address space
 Initializes the address space with a copy of the entire contents of the
address space of the parent
 Initializes the kernel resources to point to the resources used by
parent (e.g., open files)
 Places the PCB on the ready queue

Operating System Concepts - 7th Edition, Feb 7, 2006 3.22 Silberschatz, Galvin and Gagne ©2005
Process Creation

Operating System Concepts - 7th Edition, Feb 7, 2006 3.23 Silberschatz, Galvin and Gagne ©2005
Process Creation :NT

„ The system call on NT for creating a process is called,


surprisingly enough, CreateProcess:
„ BOOL CreateProcess(char *prog, char *args) (simplified)
„ CreateProcess
z Creates and initializes a new PCB
z Creates and initializes a new address space
z Loads the program specified by “prog” into the address
space
z Copies “args” into memory allocated in address space
z Initializes the hardware context to start execution at main
(or wherever specified in the file)
z Places the PCB on the ready queue

Operating System Concepts - 7th Edition, Feb 7, 2006 3.24 Silberschatz, Galvin and Gagne ©2005
Fork Example

int main(int argc, char *argv[])


{
char *name = argv[0];
int return_pid = fork();
if (return_pid == 0) {
printf(“Child of %s has PID %d\n”, name, getpid());
return 0;
} else {
printf(“I’m the parent, My child PID=%d\tMy PID=”, return_pid, getpid());
return 0;
}
}
What does this program print?

Operating System Concepts - 7th Edition, Feb 7, 2006 3.25 Silberschatz, Galvin and Gagne ©2005
Fork Example (contd)

sunv880% gcc fork1.c


sunv880% a.out
I'm the Parent, My Child PID=24369 My PID =24368
Child of a.out has PID=24369

Operating System Concepts - 7th Edition, Feb 7, 2006 3.26 Silberschatz, Galvin and Gagne ©2005
Duplicating Address Spaces

Operating System Concepts - 7th Edition, Feb 7, 2006 3.27 Silberschatz, Galvin and Gagne ©2005
Divergence

Operating System Concepts - 7th Edition, Feb 7, 2006 3.28 Silberschatz, Galvin and Gagne ©2005
Example (Contd)

sunv880% a.out
I'm the Parent, My Child PID=24386 My PID =24385
Child of a.out has PID=24386

sunv880% a.out
Child of a.out has PID=24389
I'm the Parent, My Child PID=24389 My PID =24388

„ Why is the output in a different order?

Operating System Concepts - 7th Edition, Feb 7, 2006 3.29 Silberschatz, Galvin and Gagne ©2005
Why fork()?

„ Very useful when the child…


z Is cooperating with the parent
z Relies upon the parent’s data to accomplish its task
z Example: Web server
while (1) {
int sock = accept();
if ((child_pid = fork()) == 0) {
Handle client request
} else {
Close socket
}
}

Operating System Concepts - 7th Edition, Feb 7, 2006 3.30 Silberschatz, Galvin and Gagne ©2005
Process Creation (contd)

„ How do we actually start a new program?


z usually exec() system call used after a fork to replace the
process’ memory space with a new program (destroying the
memory image of the program containing the exec system call
int exec(char *prog, char *argv[])
„ exec()
z Stops the current process
z Loads the program “prog” into the process’ address space
z Initializes hardware context and args for the new program
z Places the PCB onto the ready queue
z Note: It does not create a new process
„ What does it mean for exec to return?
„ What does it mean for exec to return with an error?

Operating System Concepts - 7th Edition, Feb 7, 2006 3.31 Silberschatz, Galvin and Gagne ©2005
Process Creation (contd)

„ fork() is used to create a new process, exec is


used to load a program into the address space
„ What happens if you run “exec csh” in your shell?
„ What happens if you run “exec ls” in your shell?
Try it.
„ fork() can return an error. Why might this happen?

Operating System Concepts - 7th Edition, Feb 7, 2006 3.32 Silberschatz, Galvin and Gagne ©2005
Another Example:
C Program Forking Separate Process
int main()
{
pid_t pid;
/* fork another process */
pid = fork();
if (pid < 0) { /* error occurred */
fprintf(stderr, "Fork Failed");
exit(-1);
}
else if (pid == 0) { /* child process */
execlp("/bin/ls", "ls", NULL);
}
else { /* parent process */
/* parent will wait for the child to complete */
wait (NULL);
printf ("Child Complete");
exit(0);
}
}

Operating System Concepts - 7th Edition, Feb 7, 2006 3.33 Silberschatz, Galvin and Gagne ©2005
A tree of processes on a typical Solaris

Operating System Concepts - 7th Edition, Feb 7, 2006 3.34 Silberschatz, Galvin and Gagne ©2005
Process Termination
„ Process executes last statement and asks the operating system to delete
it (exit)
z Output data from child to parent (via wait)
z Process’ resources are deallocated by operating system
„ Essentially, free resources and terminate
z Terminate all threads (next lecture)
z Close open files, network connections
z Allocated memory (and VM pages out on disk)
z Remove PCB from kernel data structures, delete
„ Parent may terminate execution of children processes (abort)
z Child has exceeded allocated resources
z Task assigned to child is no longer required
z If parent is exiting
 Some operating system do not allow child to continue if its parent
terminates
– All children terminated - cascading termination

Operating System Concepts - 7th Edition, Feb 7, 2006 3.35 Silberschatz, Galvin and Gagne ©2005
Process Termination (Contd)

„ Often it is convenient to pause until a child process has finished


z Think of executing commands in a shell
„ Use wait() (WaitForSingleObject)
z Suspends the current process until a child process ends
z waitpid() suspends until the specified child proces

Operating System Concepts - 7th Edition, Feb 7, 2006 3.36 Silberschatz, Galvin and Gagne ©2005
Process Summary

„ What are the units of execution?


Processes
„ How are those units of execution represented?
Process Control Blocks (PCBs)
„ How is work scheduled in the CPU?
Process states, process queues, context switches
„ What are the possible execution states of a process?
New, Running, ready, waiting, Terminated
„ How does a process move from one state to another?
Scheduling, I/O, creation, termination
„ How are processes created?
CreateProcess (NT), fork/exec (Unix)

Operating System Concepts - 7th Edition, Feb 7, 2006 3.37 Silberschatz, Galvin and Gagne ©2005
End of Chapter 3

Potrebbero piacerti anche