Chapter 3: Processes
Chapter 3: Processes
Chapter 3: Processes
Chapter 3: Processes
Process Concept
Process Scheduling
Operations on Processes
Interprocess Communication
Examples of IPC Systems
Communication in Client-Server Systems
Objectives
To introduce the notion of a process -- a program in execution, which forms the basis of all
computation
To describe the various features of processes, including scheduling, creation and termination,
and communication
A process includes:
program counter
stack
data section
The Process
Multiple parts
The program code, also called text section
Current activity including program counter, processor registers
Stack containing temporary data
Function parameters, return addresses, local variables
Data section containing global variables
Heap containing memory dynamically allocated during run time
Program is passive entity, process is active
Program becomes process when executable file loaded into memory
Execution of program started via GUI mouse clicks, command line entry of its name, etc
One program can be several processes
Consider multiple users executing the same program
Process in Memory
Process State
As a process executes, it changes state
new: The process is being created
running: Instructions are being executed
waiting: The process is waiting for some event to occur
ready: The process is waiting to be assigned to a processor
terminated: The process has finished execution
Diagram of Process State
Process Control Block (PCB)
Information associated with each process
Process state
Program counter
CPU registers
CPU scheduling information
Memory-management information
Accounting information
I/O status information
Process Control Block (PCB)
CPU Switch From Process to Process
Process Scheduling
Maximize CPU use, quickly switch processes onto CPU for time sharing
Process scheduler selects among available processes for next execution on CPU
Maintains scheduling queues of processes
Job queue – set of all processes in the system
Ready queue – set of all processes residing in main memory, ready and waiting to
execute
Device queues – set of processes waiting for an I/O device
Processes migrate among the various queues
Process Representation in Linux
Represented by the C structure task_struct
Long-term scheduler (or job scheduler) – selects which processes should be brought into the
ready queue
Short-term scheduler (or CPU scheduler) – selects which process should be executed next and
allocates CPU
Sometimes the only scheduler in a system
Schedulers (Cont.)
Short-term scheduler is invoked very frequently (milliseconds) (must be fast)
Context-switch time is overhead; the system does no useful work while switching
The more complex the OS and the PCB -> longer the context switch
Resource sharing
Parent and children share all resources
Children share subset of parent’s resources
Parent and child share no resources
Execution
Parent and children execute concurrently
Parent waits until children terminate
Process Creation (Cont.)
Address space
Child duplicate of parent
Child has a program loaded into it
UNIX examples
fork system call creates new process
exec system call used after a fork to replace the process’ memory space with a new program
Process Creation
C Program Forking Separate Process
#include <sys/types.h>
#include <studio.h>
#include <unistd.h>
int main()
{
pid_t pid;
/* fork another process */
pid = fork();
if (pid < 0) { /* error occurred */
fprintf(stderr, "Fork Failed");
return 1;
}
else if (pid == 0) { /* child process */
execlp("/bin/ls", "ls", NULL);
}
else { /* parent process */
/* parent will wait for the child */
wait (NULL);
printf ("Child Complete");
}
return 0;
}
A Tree of Processes on Solaris
Process Termination
Process executes last statement and asks the operating system to delete it (exit)
Output data from child to parent (via wait)
Process’ resources are deallocated by operating system
Shared data
#define BUFFER_SIZE 10
typedef struct {
...
} item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
Solution is correct, but can only use BUFFER_SIZE-1 elements
Bounded-Buffer – Producer
while (true) {
/* Produce an item */
while (((in = (in + 1) % BUFFER SIZE count) == out)
; /* do nothing -- no free buffers */
buffer[in] = item;
in = (in + 1) % BUFFER SIZE;
}
Bounded Buffer – Consumer
while (true) {
while (in == out)
; // do nothing -- nothing to consume
Solutions
Allow a link to be associated with at most two processes
Allow only one process at a time to execute a receive operation
Allow the system to select arbitrarily the receiver. Sender is notified who the receiver was.
Synchronization
Message passing may be either blocking or non-blocking
Pipes
The client-side stub locates the server and marshalls the parameters
The server-side stub receives this message, unpacks the marshalled parameters, and performs the
procedure on the server
Execution of RPC
Pipes
Acts as a conduit allowing two processes to communicate
Issues
Is communication unidirectional or bidirectional?
In the case of two-way communication, is it half or full-duplex?
Must there exist a relationship (i.e. parent-child) between the communicating processes?
Can the pipes be used over a network?
Ordinary Pipes
Ordinary Pipes allow communication in standard producer-consumer style
Consumer reads from the other end (the read-end of the pipe)
Communication is bidirectional