Amirkabir University of
Technology
(Tehran Polytechnic)
Department of Computer Engineering and Information
Technology
Processes ()فرآیند
Hamid R. Zarandi
[email protected]
Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2018/10/04
Operating Systems
Definition
Process
o A program in execution; process execution must progress in sequential
fashion
In time-sharing sys: unit of work
o All processes are executed concurrently
Process vs. Job?
o Passive: program
o Active: process
Program becomes process when executable file loaded into memory
One program can be several processes
o Question?
java program
Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/09/12 2
Operating Systems
Process in memory
Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/09/12 3
Operating Systems
Process state
As a process executes, it changes state
o new: The process is being created
o running: Instructions are being executed
o waiting: The process is waiting for some event to occur
o ready: The process is waiting to be assigned to a processor
o terminated: The process has finished execution
Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/09/12 4
Operating Systems
Process Control Block (PCB)
How to manage processes?
Information associated with each process
(also task control block)
o Process state
o Program counter
o CPU registers – contents of all process-centric registers
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
Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/09/12 5
Operating Systems
CPU switch from process to process
Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/09/12 6
Operating Systems
Process representation in Linux
Represented by the C structure task_struct
pid t_pid; /* process identifier */
long state; /* state of the process */
unsigned int time_slice /* scheduling information */
struct task_struct *parent; /* this process’s parent */
struct list_head children; /* this process’s children */
struct files_struct *files; /* list of open files */
struct mm_struct *mm; /* address space of this process */
Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/09/12 7
Operating Systems
Process scheduling
Process scheduler selects among
available processes for next execution
on CPU
Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/09/12 8
Operating Systems
Diagram representation of process scheduling
Queueing diagram
represents queues,
resources, flows
Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/09/12 9
Operating Systems
Schedulers
Short-term scheduler (or CPU scheduler)
o selects which process should be executed next and allocates CPU
Sometimes the only scheduler in a system
Short-term scheduler is invoked frequently (milliseconds) (must be fast)
Long-term scheduler (or job scheduler)
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
Processes:
o I/O-bound
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
Long-term scheduler strives for good process mix
Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/09/12 10
Operating Systems
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
Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/09/12 11
Operating Systems
Context switch ()تعویض متن
When CPU switches to another process, the system must save the state of the
old process and load the saved state for the new process via a context switch
Context of a process represented in the PCB
Context-switch time is overhead; the system does no useful work while
switching
o The more complex the OS and the PCB the longer the context switch
Time dependent on hardware support
o Some hardware provides multiple sets of registers per CPU multiple contexts loaded at
once
Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/09/12 12
Operating Systems
Process creation
Parent vs. Child
Generally, process identified and managed via a process identifier (pid)
Resource sharing options
o Parent and children share all resources
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
Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/09/12 13
Operating Systems
A tree of processes in Linux
Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/09/12 14
Operating Systems
Process creation
Address space
o Child duplicate of parent
o Child has a program loaded into it
UNIX examples
o fork() system call creates new process
o exec() system call used after a fork() to replace the process’ memory space with a new
program
Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/09/12 15
Operating Systems
Process creation with C
POSIX Windows
Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/09/12 16
Operating Systems
Process termination
Child Parent
o Process’ resources are deallocated when:
exit(n)
return() in main()
o Catch exit status wait()
pid = wait(&status);
Parent Child
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
Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/09/12 17
Operating Systems
Problems of process termination
zombie process
o No parent waiting
orphan process
o Parent termination without wait
Multi process example: Chrome Browser
o Browser, Renderer, Plugins, etc
Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/09/12 18
Operating Systems
Interprocess communication (IPC)
Process:
o independent vs. cooperating
Cooperating process:
o Shared memory
o Message passing
Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/09/12 19
Operating Systems
Circular buffer & producer-consumer problem
#define BUFFER_SIZE 10
typedef struct {
. . .
} item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
item next_produced; item next_consumed;
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 */
} }
Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/09/12 20
Operating Systems
Message passing
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
Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/09/12 21
Operating Systems
Synchronization
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
Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/09/12 22
Operating Systems
POSIX examples of shared memory: (sender->receiver)
Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/09/12 23
Operating Systems
Local procedure calls in Windows
Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/09/12 24
Operating Systems
Communications in client-server systems
Sockets
Remote Procedure Calls (windows)
Pipes
Remote Method Invocation (Java)
Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/09/12 25
Operating Systems
Sockets
A socket is defined as an endpoint for communication
Concatenation of IP address and port – a number included at start of message packet to
differentiate network services on a host
The socket 161.25.19.8:1625 refers to port 1625 on host 161.25.19.8
Communication consists between a pair of sockets
All ports below 1024 are well known, used for standard services
Special IP address 127.0.0.1 (loopback) to refer to system on which process is running
Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/09/12 26
Operating Systems
Socket communication
Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/09/12 27
Operating Systems
Sockets in Java
Three types of sockets
o Connection-oriented (TCP)
o Connectionless (UDP)
o MulticastSocket class–
data can be sent to multiple
recipients
Consider this “Date” server:
Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/09/12 28
Operating Systems
Execution of RPC (Remote Procedure Call)
Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/09/12 29
Operating Systems
Pipes
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.
Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/09/12 30
Operating Systems
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
Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/09/12 31
Operating Systems
Ordinary pipe (POSIX), parent-child
Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/09/12 32
Operating Systems
Ordinary pipe (windows), parent
Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/09/12 33
Operating Systems
Ordinary pipe (windows), child
Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/09/12 34
Operating Systems
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
Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/09/12 35
Operating Systems
Questions?
Hamid R. Zarandi Amirkabir Univ. of Tech. (Tehran Polytechnic) 2015/09/12 36