Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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
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
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
Operating System Concepts - 7th Edition, Feb 7, 2006 3.12 Silberschatz, Galvin and Gagne ©2005
Process Scheduling
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
Operating System Concepts - 7th Edition, Feb 7, 2006 3.17 Silberschatz, Galvin and Gagne ©2005
Schedulers (cont’d)
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.)
Operating System Concepts - 7th Edition, Feb 7, 2006 3.20 Silberschatz, Galvin and Gagne ©2005
Process Creation
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
Operating System Concepts - 7th Edition, Feb 7, 2006 3.24 Silberschatz, Galvin and Gagne ©2005
Fork Example
Operating System Concepts - 7th Edition, Feb 7, 2006 3.25 Silberschatz, Galvin and Gagne ©2005
Fork Example (contd)
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
Operating System Concepts - 7th Edition, Feb 7, 2006 3.29 Silberschatz, Galvin and Gagne ©2005
Why fork()?
Operating System Concepts - 7th Edition, Feb 7, 2006 3.30 Silberschatz, Galvin and Gagne ©2005
Process Creation (contd)
Operating System Concepts - 7th Edition, Feb 7, 2006 3.31 Silberschatz, Galvin and Gagne ©2005
Process Creation (contd)
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)
Operating System Concepts - 7th Edition, Feb 7, 2006 3.36 Silberschatz, Galvin and Gagne ©2005
Process Summary
Operating System Concepts - 7th Edition, Feb 7, 2006 3.37 Silberschatz, Galvin and Gagne ©2005
End of Chapter 3