0% found this document useful (0 votes)
31 views27 pages

Lecture 5-6 ch3 Precesses

Uploaded by

osamhhong
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views27 pages

Lecture 5-6 ch3 Precesses

Uploaded by

osamhhong
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Chapter 3: Processes

Lecture 5

Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
Chapter 3: Processes
Process Concept
Process Scheduling
Operations on Processes
Interprocess Communication
Examples of IPC Systems
Communication in Client-Server Systems

Operating System Concepts – 9th Edition 3.2 Silberschatz, Galvin and Gagne ©2013
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
To explore interprocess communication using shared memory
and message passing
To describe communication in client-server systems

Operating System Concepts – 9th Edition 3.3 Silberschatz, Galvin and Gagne ©2013
Process
 A program in execution
 An instance of a program running on a computer
 The entity that can be assigned to and executed on a processor
 A unit of activity characterized by the execution of a sequence of
instructions, a current state, and an associated set of system instructions

4
Process Management

 A process is a program in execution. It is a unit of


work within the system.
 Program is a
 passive entity,
 active entity.
 Process needs resources to accomplish its task
 CPU, memory, I/O, files
 Initialization data
 Process termination requires reclaim of any reusable
resources
5
Process Management

 Single-threaded process has one program counter


specifying location of next instruction to execute
 Process executes instructions sequentially, one at a
time, until completion
 Multi-threaded process has one program counter
per thread
 Typically system has many processes, some user,
some operating system running concurrently on
one or more CPUs
 Concurrency by multiplexing the CPUs among the
processes / threads
6
Process Image (in Memory)

Contains temporary data


(function var, return address,
local var)

(optional) Memory dynamically


allocated during process runtime

Contains global var

Program code 7
Process States
 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 (such as I/O)
 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)
 Each process is represented in the OS by PCB. It is data structure called
the process control block (PCB) that holds all this stuff
 The PCB is identified by an integer process ID (PID)
 Only one PCB active at a time
 OS keeps all of a process’s hardware execution state in the PCB when the
process isn’t running
 PC, SP, registers, etc.
Process Control Block (PCB)
Information associated with each process
 Process state: new, ready, running, waiting, halted.
 Program counter: the address of the next instruction to be
executed for this process.
 CPU registers: accumulators, Index registers, SP,.. Must be saved
when an interrupt occurs to allow the process to be continued
correctly afterward.
 CPU scheduling information : ex. process priority.
 Memory-management information : ex. the value of the base and
limit registers.
 Accounting information : ex. process number.
 I/O status information : ex. List of I/O devices allocated to the
process , list of open files.
CPU Switch From Process to Process
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
The act of switching the CPU from one process to another
is called a context switch.
Context switching is an essential part of a multitasking
operating system features.
CPU Switch From Process to Process
Process Scheduling Queues
 The process scheduler selects an available process( from a set of several
available processes) for program execution on the CPU.
 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 (each device
has its own device queue)
 Processes migrate among the various queues
Representation of Process Scheduling
(queuing diagram)
Schedulers
 Long-term scheduler (or job scheduler) – selects which processes
should be brought into the ready queue and load them into memory.
 Short-term scheduler (or CPU scheduler) – selects which process should
be executed next and allocates CPU
 Medium Term Scheduler( swapping scheduler ) - It removes the
processes from the memory. It reduces the degree of multiprogramming
 Short-term scheduler is invoked very frequently (milliseconds)  (must
be fast)
 Long-term scheduler is invoked very infrequently (seconds, minutes) 
(may be slow)
 The long-term scheduler controls the degree of multiprogramming (the
number of processes in memory).
Operations on Processes

 System must provide mechanisms for:


 process creation,
 process termination,
 and so on as detailed next
Operations on the processes

1-Process Creation
 A process may create several new processes, via a create-process system call.
 Parent process create children processes, which, in turn create other
processes, forming a tree of processes
 Most O.S. identify process and managed via a unique process identifier (pid)
(an integer number).
 A process will need resources (CPU time, memory , files,…) to accomplish its
task. When a process creates a subprocess, that subprocess may be able to
obtain its resources directly from O.S. or
 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 some or all children terminate
Process Creation (Cont)
 Address space
 Child duplicate of parent (it has the same program and data as the parent).
 Child has a new program loaded into it
 UNIX examples
 fork system call creates new process (copy of the address space of the original
process).
 exec system call used after a fork to replace the process’ memory space with a
new program
 Wait() the parent waits for the child process to complete.
 Exit().
A tree of processes on a typical Linux
2- Process Termination
 A Process terminates when it finishes executing its last statement and
asks the operating system to delete it by using the (exit) system call.
 Return a value from child to parent (via wait)
 Process’ resources are deallocated by operating system
 Parent may terminate execution of children processes (abort)
 Child has exceeded allocated resources
 Task assigned to child is no longer required
 If parent is exiting
 Some operating system do not allow child to continue if its parent terminates
 All children terminated - cascading termination
Interprocess Communication
 Processes executing concurrently :
 Independent :process cannot affect or be affected by the execution of another
process
 Cooperating :process can affect or be affected by other processes, including
sharing data
 Reasons for cooperating processes:
 Information sharing (ex.: shared file)
 Computation speedup (break up process into sub tasks to run faster and can be
achieved only if the computer has multiple processing elements – CPUs or I/O
channels)
 Modularity (dividing system functions into separate processes or threads)
 Convenience (individual user may work on many tasks at the same time could be
editing, printing, and compiling in parallel)
Interprocess Communication
 Mechanism for processes to communicate and to synchronize their
actions
 Two models of IPC:
1) shared memory
cooperating processes exchange information by reading and writing data
to a shared region of memory.
* allows maximum speed and convenience of communication.
* faster than message passing (system calls only to establish the region. All
accesses are routine memory accesses, no assistance from the kernel).
2) message passing
messages are exchanged between the cooperating processes
* useful for exchanging smaller amounts of data.
* easier to implement than is shared memory for intercomputer communications.
* implemented using system calls (more time, kernel intervention).
Communications Models

message passing shared memory


Shared memory system

 IPC using shared memory requires communicating processes to establish


region of shared memory.
 The region resides in the address space of the process creating the shared
memory segment.
 Shared memory requires that two or more processes agree to remove the
restriction.
 Exchange information by reading and writing data in the shared area.
 The processes are responsible for ensuring that they are not writing to the
same location simultaneously.
Producer-Consumer Problem

 A common Paradigm for cooperating processes, producer process produces


information that is consumed by a consumer process.
 Ex. The assembler produce object modules, which are consumed by the
loader.
 Ex. Web server produces(provides) HTML files and images, which are
consumed (read) by the web browser requesting the resource.
 One solution to the producer-consumer problem uses shared memory.
 To Allow the P and C processes to run concurrently, we must have a buffer
that can be filled by the P and emptied by the C.
 This buffer reside in a region of memory that is shared by the P and C
processes.
Interprocess Communication – Message Passing

 Provides a Mechanism for processes to communicate and to synchronize their


actions.
 Message system – processes communicate with each other without resorting
to shared variables.
 IPC message-passing facility provides two operations:
 send(message) – message size fixed or variable
 receive(message)
 If processes P and Q wish to communicate, they need to:
 establish a communication link between them
 exchange messages via send/receive
 Implementation of communication link :
 physical (e.g., shared memory, hardware bus)
 logical (e.g., logical properties)

You might also like