04/09/23
Operating Systems
Process in memory
Processes
Operating Systems Operating Systems
Definition Process state
Process As a process executes, it changes state
o A program in execution; process execution must progress in sequential o new: The process is being created
fashion
In time-sharing sys: unit of work
o running: Instructions are being executed
o All processes are executed concurrently o waiting: The process is waiting for some event to occur
Process vs. Job? o ready: The process is waiting to be assigned to a processor
o Passive: program o terminated: The process has finished execution
o Active: process
Program becomes process when executable file loaded into memory
One program can be several processes
o Question?
java program
04/09/23
Operating Systems Operating Systems
Process Control Block (PCB) Process representation in Linux
How to manage processes? Represented by the C structure task_struct
Information associated with each process
pid t_pid; /* process identifier */
(also task control block) long state; /* state of the process */
o Process state unsigned int time_slice /* scheduling information */
struct task_struct *parent; /* this process’s parent */
o Program counter struct list_head children; /* this process’s children */
struct files_struct *files; /* list of open files */
o CPU registers – contents of all process-centric registers struct mm_struct *mm; /* address space of this process */
o CPU scheduling info. – priorities, scheduling queue pointers
o Memory-management info. – memory allocated to the process
o Accounting info. – CPU used, clock time elapsed since start, time
limits
o I/O status info. – I/O devices allocated to process, list of open files
Operating Systems Operating Systems
CPU switch from process to process Process scheduling
Process scheduler selects among
available processes for next execution
on CPU
04/09/23
Operating Systems Operating Systems
Diagram representation of process scheduling Example of standard API
Medium-term scheduler
o Can be added if degree of multiple programming needs to decrease
o Remove process from memory, store on disk, bring back in from disk to continue execution:
swapping
Queueing diagram
represents queues,
resources, flows
Operating Systems Operating Systems
Schedulers Context switch
Short-term scheduler (or CPU scheduler) When CPU switches to another process, the system must save the state of the
o selects which process should be executed next and allocates CPU old process and load the saved state for the new process via a context switch
Sometimes the only scheduler in a system
Short-term scheduler is invoked frequently (milliseconds) (must be fast)
Long-term scheduler (or job scheduler) Context of a process represented in the PCB
o selects which processes should be brought into the ready queue
Long-term scheduler is invoked infrequently (seconds, minutes) (may be slow)
The long-term scheduler controls the degree of multiprogramming Context-switch time is overhead; the system does no useful work while
Processes: switching
o I/O-bound o The more complex the OS and the PCB the longer the context switch
spends more time doing I/O than computations, many short CPU bursts
o CPU-bound
spends more time doing computations; few very long CPU bursts Time dependent on hardware support
Long-term scheduler strives for good process mix o Some hardware provides multiple sets of registers per CPU multiple contexts loaded at
once
04/09/23
Operating Systems Operating Systems
Process creation Process creation
Parent vs. Child Address space
o Child duplicate of parent
o Child has a program loaded into it
Generally, process identified and managed via a process identifier (pid)
UNIX examples
Resource sharing options o fork() system call creates new process
o Parent and children share all resources o exec() system call used after a fork() to replace the process’ memory space
with a new program
o Children share subset of parent’s resources
o Parent and child share no resources
Execution options
o Parent and children execute concurrently
o Parent waits until children terminate
Operating Systems Operating Systems
A tree of processes in Linux Process creation with C
POSIX Windows
04/09/23
Operating Systems Operating Systems
Process termination Interprocess communication (IPC)
Child Parent Process:
o Process’ resources are deallocated when: o independent vs. cooperating
exit(n)
return() in main()
o Catch exit status wait() Cooperating process:
pid = wait(&status); o Shared memory
Parent Child o Message passing
o abort()
o Why?
Child has exceeded allocated resources
Task assigned to child is no longer required
The parent is exiting and the operating systems does not allow a child to continue if its
parent terminates
Operating Systems Operating Systems
Problems of process termination Circular buffer & producer-consumer problem
#define BUFFER_SIZE 10
zombie process typedef struct {
. . .
o No parent waiting } item;
orphan process item buffer[BUFFER_SIZE];
o Parent termination without wait int in = 0;
int out = 0;
Multi process example: Chrome Browser item next_produced; item next_consumed;
o Browser, Renderer, Plugins, etc while (true) { while (true) {
while (in == out) ; /* do nothing */
/* produce an item in next produced */
while (((in + 1) % BUFFER_SIZE) == out) next_consumed = buffer[out];
; /* do nothing */ out = (out + 1) % BUFFER_SIZE;
buffer[in] = next_produced;
in = (in + 1) % BUFFER_SIZE; /* consume the item in next consumed */
} }
04/09/23
Operating Systems Operating Systems
Message passing POSIX examples of shared memory: (sender->receiver)
Direct communication (unidirectional)
o send (P, message) – send a message to process P
o receive(Q, message) – receive a message from process Q
Indirect communication (uni & bidirectional)
o Messages are directed and received from mailboxes (or ports)
o Can be used by multiple processes
o Primitives are defined as:
send(A, message) – send a message to mailbox A
receive(A, message) – receive a message from mailbox A
Operating Systems Operating Systems
Synchronization Local procedure calls in Windows
Blocking vs. non-blocking
Blocking is considered synchronous
o Blocking send
o Blocking receive
Non-blocking is considered asynchronous
o Non-blocking send
o Non-blocking receive
The receiver receives
A valid message
Null message
Different combinations possible
o If both send and receive are blocking, we have a rendezvous
04/09/23
Operating Systems Operating Systems
Communications in client-server systems Socket communication
Sockets
Remote Procedure Calls (windows)
Pipes
Remote Method Invocation (Java)
Operating Systems Operating Systems
Sockets Sockets in Java
A socket is defined as an endpoint for communication Three types of sockets
o Connection-oriented (TCP)
Concatenation of IP address and port – a number included at start of message packet to
differentiate network services on a host o Connectionless (UDP)
o MulticastSocket class–
The socket 161.25.19.8:1625 refers to port 1625 on host 161.25.19.8 data can be sent to multiple
recipients
Communication consists between a pair of sockets
All ports below 1024 are well known, used for standard services Consider this “Date” server:
Special IP address 127.0.0.1 (loopback) to refer to system on which process is running
04/09/23
Operating Systems Operating Systems
Execution of RPC (Remote Procedure Call) Ordinary Pipes
Ordinary Pipes allow communication in standard producer-consumer style
Producer writes to one end (the write-end of the pipe)
Consumer reads from the other end (the read-end of the pipe)
Ordinary pipes are therefore unidirectional
Require parent-child relationship between communicating processes
Windows calls these anonymous pipes
See Unix and Windows code samples in textbook
Operating Systems Operating Systems
Pipes Ordinary pipe (POSIX), parent-child
Acts as a conduit allowing two processes to communicate
Issues:
o Is communication unidirectional or bidirectional?
o In the case of two-way communication, is it half or full-duplex?
o Must there exist a relationship (i.e., parent-child) between the communicating processes?
o Can the pipes be used over a network?
Ordinary pipes
o cannot be accessed from outside the process that created it. Typically, a parent process creates a pipe and uses it
to communicate with a child process that it created.
Named pipes
o can be accessed without a parent-child relationship.
04/09/23
Operating Systems Operating Systems
Ordinary pipe (windows), parent Named pipes
Named Pipes are more powerful than ordinary pipes (?)
Communication is bidirectional
No parent-child relationship is necessary between the communicating
processes
Several processes can use the named pipe for communication
Provided on both UNIX and Windows systems
Operating Systems Operating Systems
Ordinary pipe (windows), child Questions?