Chapter-3
Chapter-3
Process Concept
An operating system executes a variety of programs that run as a process.
Multiple parts
Process in Memory
Chapter-3 1
Process State
• As a process executes, it changes state
Chapter-3 2
Threads
So far, process has a single thread of execution
Must then have storage for thread details, multiple program counters in PCB
Process Scheduling
Process scheduler selects among available processes for next execution on
CPU core
Goal -- Maximize CPU use, quickly switch processes onto CPU core
Ready queue – set of all processes residing in main memory, ready and
waiting to execute
Chapter-3 3
Wait queues – set of processes waiting for an event (i.e., I/O)
Context Switch
Chapter-3 4
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-switch time is pure overhead; the system does no useful work while
switching
• The more complex the OS and the PCB è the longer the context switch
• Some hardware provides multiple sets of registers per CPU è multiple contexts
loaded at once
Operations on Processes
• System must provide mechanisms for:
Process creation
Process termination
Process Creation
Parent process create children processes, which, in turn create other
processes, forming a tree of processes
• Execution options
Chapter-3 5
Chapter-3 6
• Some operating systems do not allow child to exists if its parent has terminated. If a
process terminates, then all its children must also be terminated.
The parent process may wait for termination of a child process by using the
wait()system call. The call returns status information and the pid of the
terminated process
pid = wait(&status);
Foreground process
Visible process
Service process
Background process
Empty process
Chapter-3 7
Interprocess Communication
Processes within a system may be independent or cooperating
Information sharing
Computation speedup
Modularity
Convenience
Shared memory
Message passing
Chapter-3 8
Producer-Consumer Problem
• Paradigm for cooperating processes:
send(message)
Chapter-3 9
receive(message)
• Implementation issues:
Is the size of a message that the link can accommodate fixed or variable?
Shared memory
Hardware bus
Network
• Logical:
Direct or indirect
Synchronous or asynchronous
Direct Communication
• Processes must name each other explicitly:
Chapter-3 10
• Properties of communication link
Indirect Communication
• Messages are directed and received from mailboxes (also referred to as ports)
• Operations
Delete a mailbox
• Mailbox sharing
• Solutions
Chapter-3 11
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
• Blocking is considered synchronous
A valid message, or
Null message
Buffering
Queue of messages attached to the link.
1. Zero capacity – no messages are queued on a link. Sender must wait for
receiver (rendezvous)
2. Bounded capacity – finite length of n messages Sender must wait if link full
Chapter-3 12
Also used to open an existing segment
ftruncate(shm_fd, 4096);
Reading and writing to shared memory is done by using the pointer returned by
mmap().
mach_port_allocate()
Send and receive are flexible; for example four options if mailbox full:
Wait indefinitely
Wait at most n milliseconds
Return immediately
Temporarily cache a message
Chapter-3 13
4The server creates two private communication ports and returns the
handle to one of them to the client.
The client and server use the corresponding port handle to send
messages or callbacks and to listen for replies.
Pipes
Acts as a conduit allowing two processes to communicate
Issues:
• Ordinary pipes – 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 – can be accessed without a parent-child relationship.
Chapter-3 14
Ordinary Pipes
Ordinary Pipes allow communication in standard producer-consumer style
Consumer reads from the other end (the read-end of the pipe)
Named Pipes
Named Pipes are more powerful than ordinary pipes
Communication is bidirectional
Sockets
A socket is defined as an endpoint for communication
Chapter-3 15
Concatenation of IP address and port – a number included at start of message
packet to differentiate network services on a host
All ports below 1024 are well known, used for standard services
Chapter-3 16