OS_Module2_Unit1

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 53

Study Material

Department of Computer Science & Engineering

Semester /
Course Code : BCS303 III / II
Year :

Academic
Course Title : Operating Systems 2024-25
Year :
Module 2
Process Management :
Process concept
The Process
Process State
PCB

Process scheduling
Scheduling Queues
Schedulers
Context Switch

Operations on processes
Process Creation
Process Termination

Inter Process communication


Shared Memory
Message Passing
Naming
Synchronization 2
Buffering
Process Concept
Process Concept
 What to call CPU activities?
 Batch system executes jobs
 Time-shared systems has user programs or tasks
 Even on a single-user system such as Microsoft Windows,
 a user may be able to run several programs at one time: a word processor, a Web browser and an e-
mail package.
 And even if the user can execute only one program at a time,
 the operating system may need to support its own internal programmed activities, such as memory
management.
 In many respects, all these activities are similar, so we call all of them processes.
The Process
 Process – a program in execution.
 process execution must progress in sequential fashion
 Process is more than the program code, also called text section
 It also includes:
 Current activity represented by value of program counter
 The contents of 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
The Process
 Program is passive entity( a file containing list of instructions) stored on disk (executable file), process
is active (with program counter specifying the next instruction to execute)
 Program becomes process when executable file loaded into memory
 Two common techniques for loading executable files are double-clicking an icon representing the
executable file and entering the name of the executable file on the command line (as in prog. exe or a.
out.)
 Although two processes may be associated with the same program, they are nevertheless considered
two separate execution sequences.
 For instance, several users may be running different copies of the mail program, or the same user
may invoke many copies of the Web browser program.
 Each of these is a separate process; and although the text sections are equivalent, the data, heap, and
stack sections vary.
Process in Memory
Process State
 As a process executes, it changes state.
 The state of a process is defined in part by the current activity of that process.
 Each process may be in one of the following states:
 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
 It is important to realize that only one process can be running on any processor at any instant.
 Many processes may be ready and waiting.
Process State
Process Control Block (PCB)
 Each process is represented using PCB. (also called task control block)
 It contains information associated with each process
 Process state – The state may be new, ready, running, waiting, halted, and so on.
 Program counter – The counter indicates the address of the next instruction to be executed for this
process.
 CPU registers –
 The registers vary in number and type, depending on the computer architecture.
 They include accumulators, index registers, stack pointers, and general-purpose registers, plus
any condition-code information.
 Along with the program counter, this state information must be saved when an interrupt occurs,
to allow the process to be continued correctly afterward
Process Control Block (PCB)
 CPU scheduling information- This information includes a process priority, pointers to scheduling
queues, and any other scheduling parameters
 Memory-management information – This information may include such information as the value of
the base and limit registers, the page tables, or the segment tables, depending on the memory system
used by the operating system
 Accounting information – This information includes the amount of CPU and real-time used, time
limits, account numbers, job or process numbers, and so on.
 I/O status information – This information includes the list of I/O devices allocated to the process, a
list of open files, and so on.
Process Control Block (PCB)
CPU Switch From Process to Process
Threads
 A process is a program that performs a single thread of execution.
 For example, when a process is running a word-processor program, a single thread of instructions is
being executed.
 This single thread of control allows the process to perform only one task at a time.
 The user cannot simultaneously type in characters and run the spell checker within the same process, for
example.
 Modern operating systems have extended the process concept to allow a process to have multiple
threads of execution and thus to perform more than one task at a time.
 On a system that supports threads, the PCB is expanded to include information for each thread.
Process Scheduling
Process Scheduling
 Multiprogramming wants to maximize CPU use.
 Time sharing wants to quickly switch CPU among processes
 The process scheduler selects an available process for program execution on the CPU.
 For a single-processor system, there will never be more than one running process. If there are more
processes, the rest will have to wait until the CPU is free and can be rescheduled.
Scheduling Queues
 Maintains scheduling queues of processes
 Job queue – set of all processes in the system.
 As processes enter the system, they are put in job queue.
 Ready queue – set of all processes residing in main memory, ready and waiting to execute.
 Generally stored as a linked list.
A ready-queue header contains pointers to the first and final PCBs in the list.
 Each PCB includes a pointer field that points to the next PCB in the ready queue.
Ready Queue And Various I/O Device Queues
Scheduling Queues
 Device queues – a set of processes waiting for an I/O device.
 When a process is allocated the CPU, it executes for a while and eventually quits, is interrupted,
or waits for the occurrence of a particular event, such as the completion of an I/0 request.
 Suppose the process makes an I/O request to a shared device, such as a disk.
 Since there are many processes in the system, the disk may be busy with the I/0 request of some
other process.
 The process therefore may have to wait for the disk.
 Each device has its own device queue.
Scheduling Queues
 Queueing diagram: A common representation of process scheduling.
 Each rectangular box represents a queue.
 Two types of queues are present: the ready queue and a set of device queues.
 The circles represent the resources that serve the queues, and the arrows indicate the flow of processes
in the system.
Scheduling Queues
 A new process is initially put in the ready queue. It waits there until it is selected for execution, or is
dispatched.
 Once the process is allocated the CPU and is executing, one of several events could occur:
 The process could issue an I/0 request and then be placed in an I/0 queue.
 The process could create a new subprocess and wait for the subprocess’s termination.
 The process could be removed forcibly from the CPU, as a result of an interrupt, and be put back in
the ready queue.
 In the first two cases, the process eventually switches from the waiting state to the ready state and is
then put back in the ready queue.
 A process continues this cycle until it terminates, at which time it is removed from all queues and has
its PCB and resources deallocated.
Schedulers
 A process migrates among the various scheduling queues throughout its lifetime.
 The operating system must select, for scheduling purposes, processes from these queues in some
fashion. The selection process is carried out by the scheduler.
 Short-term scheduler (or CPU scheduler)
 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)
 selects processes from this pool and loads them into memory for execution
 Long-term scheduler is invoked infrequently (seconds, minutes)
 may be slow
 The long-term scheduler controls the degree of multiprogramming (the number of processes in
memory)
Schedulers
 Processes can be described as either:
 I/O-bound process – spends more time doing I/O than computations, many short CPU bursts
 CPU-bound process – spends more time doing computations; few very long CPU bursts
 Long-term scheduler strives for good process mix.
 The system with the best performance will thus have a combination of CPU-bound and I/O-bound
processes.
 On some systems, the long-term scheduler may be absent or minimal.
 no long-term scheduler but simply put every new process in memory for the short-term scheduler.
Schedulers
 Medium-term scheduler can be added if degree of multiple programming needs to decrease
 Remove process from memory, store on disk, bring back in from disk to continue execution:
swapping
 Swapping may be necessary to improve the process mix or because a change in memory
requirements has overcommitted available memory, requiring memory to be freed up.
Context Switch
 Interrupts cause the operating system to change a CPU from its current task and to run a kernel routine.
 When an interrupt occurs, the system needs to save the current context of the process running on the
CPU so that it can restore that context when its processing is done, essentially suspending the process
and then resuming it.
 Context of a process is represented in the PCB.
 it includes the value of the CPU registers, the process state and memory-management information.
 Switching the CPU to another process requires performing a state save of the current process and a state
restore of a different process. This task is known as a context switch
 Context switch time is overhead as the system does no useful work while switching
 The more complex the OS and the PCB, the longer the context switch
 Context switch time dependent on hardware support
 Some hardware provides multiple sets of registers per CPU, multiple contexts loaded at once.
Operations on Processes
Operations on Processes
 The processes in most systems can execute concurrently, and they may be created and deleted
dynamically.
 Thus, these systems must provide a mechanism for process creation and termination.
Process Creation
 A Process may create several new processes via a create-process system call during the course of
execution .
 The creating process is called the parent process
 A new process is called children of that process
 Each of these process in turn create other processes, forming a tree of processes
 Generally, a process is identified and managed via a process identifier (pid) which is generally an
integer number.
 a parent needs to know the identities of its children. Thus, when one process creates a new process, the
identity of the newly created process is passed to the parent.
A Tree of Processes in Linux
Process Creation
 A process need certain resources ( CPU time,memory ,files ,I/O devices ) to accomplish its task
 When a process creates a sub process , sub process may be able to obtain its resources directly from
operating system, or it may be constrained to a subset of the resources of the parent process.
 Resource sharing options
 Parent and children share all resources
 Children share subset of parent’s resources
 Restricting child process to subset of parents resource prevents any process from overloading the
system
 In addition to the various physical and logical resources that a process obtains when it is created,
initialization data (input) may be passed along by the parent process to the child process.
 For example, consider a process whose function is to display the contents of a file-say, img.jpg-on
the screen of a terminal.
 When it is created, it will get, the name of the file img.jpg, and it will use that file name, open the
file, and write the contents out. It may also get the name of the output device.
Process Creation
 When a process creates a new process ,two possibilities exist in terms of Execution
 The Parent continues to execute concurrently with its children
 The parent waits until some or all of its children have terminated
 There are also two possibilities in terms of the address space of the new process
 The Child process is a duplicate of the parent process (it has the same program and data as the
parent)
 The child process has a new program loaded into it
Process Creation
 UNIX examples
 fork() system call creates new process.
 The new process consists of a copy of the address space of the original process.
 This mechanism allows the parent process to communicate easily with its child process.
 Both processes continue execution at the instruction after the fork (), with one difference: the return code for
the fork() is zero for the new (child) process, whereas the (nonzero) process identifier of the child is returned
to the parent.
 exec() system call used after a fork() to replace the process memory space with a new program.
 loads a binary file into memory (destroying the memory image of the program containing the exec() system
call) and starts its execution.
 the two processes can communicate and then go their separate ways.
 The parent can then create more children; or, if it has nothing else to do while the child runs, it can issue
await() system call to move itself off the ready queue until the termination of the child.
Process Creation
Process Termination
 Process executes last statement and then asks the operating system to delete it using the exit() system
call.
 Returns status data (an integer value) from child to parent (via wait())
 Process resources (Physical and Virtual memory ,open files ,I/O buffers) are deallocated by
operating system
 Termination of process can occur in any circumstances
 A process can cause the termination of another process via an appropriate system call.
 such a system call can be invoked only by the parent of the process that is to be terminated.
Process Termination
 A parent may terminate the execution of one of its children for a variety of reasons, such as:
 The child has exceeded its usage of some of the resources that it has been allocated.
 The task assigned to the child is no longer required.
 The parent is exiting, and the operating system does not allow a child to continue if its parent
terminates.
 Some OS do not allow a child to exist if its parent has terminated
 if a process terminates (either normally or abnormally), then all its children must also be terminated.
 This phenomenon, referred to as cascading termination, is normally initiated by the operating
system.
Process Termination
 UNIX examples
 exit(): terminates a process
 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
Inter Process Communication
Interprocess Communication
 Processes within a system may be independent or cooperating
 Cooperating process can affect or be affected by other processes.
 any process that shares data with other processes is a cooperating process.
 Reasons for providing an environment to cooperate processes:
 Information sharing : same information(file)
 Computation speedup
 Modularity
 Convenience
Interprocess Communication
 Cooperating processes need interprocess communication (IPC): allow to exchange of data and
information.
 Two models of IPC
 Shared memory
 region of memory that is shared is established.
 Processes can then exchange information by reading and writing data to the shared region.
 useful for exchanging smaller amounts of data, because no conflicts need be avoided.
 allows maximum speed and convenience of communication.
 system calls are required only to establish shared-memory regions. After that, all accesses
are treated as routine memory accesses, and no assistance from the kernel is required.
 Message Passing
 communication takes place through message exchange.
 easier to implement for inter-computer communication.
 implemented using system calls, require the more time-consuming task of kernel
Communications Models
(a) Message passing. (b) Shared memory
Shared Memory System
 Interprocess communication using shared memory requires communicating processes to establish a
region of shared memory
 Shared memory region resides in the address space of the process creating the shared memory segment.
Other processes that wish to communicate using this shared memory segment must attach it to their
address space
 Normally, the operating system tries to prevent one process from accessing another process’s memory.
This shared system removes this restriction.
 The information can be exchanged by two or more processes to remove this restriction allowing reading
and writing data in the shared areas and not under the control of the operating system.
 This system should ensure that processes are not writing to the same location simultaneously
Shared Memory System
Shared Memory System
 Two types of buffers can be used.
 The unbounded buffer places no practical limit on the size of the buffer.
 The consumer may have to wait for new items, but the producer can always produce new
items.
 The bounded buffer assumes a fixed buffer size.
 In this case, the consumer must wait if the buffer is empty, and the producer must wait if the
buffer is full.
Message Passing
 Mechanism for processes to communicate and to synchronize their actions
 Message system – processes communicate with each other without sharing the same address space.
 useful in a distributed environment, where the communicating processes may reside on different
computers connected by a network.
 Two operations:
 send(message)
 receive(message)
 The message size is either fixed or variable
Message Passing
 If processes P and Q wish to communicate, they need to:
 Establish a communication link between them
 Exchange messages via send/receive
 Implementation issues:
 How are links established?
 Can a link be associated with more than two processes?
 How many links can there be between every pair of communicating processes?
 What is the capacity of a link?
 Is the size of a message that the link can accommodate fixed or variable?
 Is a link unidirectional or bi-directional?
Naming
 Processes that want to communicate must have a way to refer to each other.
 They can use either direct or indirect communication.
 Direct Communication
 each process that wants to communicate must explicitly name the recipient or sender of the
communication.
 the send() and receive() primitives are defined as:
 send (P, message) – send a message to process P
 receive(Q, message) – receive a message from process Q
 A communication link in this scheme has the following properties:
 A link is established automatically between every pair of processes that want to
communicate. The processes need to know only each other’s dentity to communicate.
 A link is associated with exactly two processes.
 Between each pair of processes, there exists exactly one link.
Naming
 Direct Communication
 This scheme exhibits symmetry in addressing as both the sender process and the receiver process
must name the other to communicate.
 A variant of this scheme employs asymmetry in addressing, only the sender names the recipient; the
recipient is not required to name the sender.
 In this scheme, the send() and receive() primitives are defined as follows:
 send(P, message) -Send a message to process P.
 receive (id, message) -Receive a message from any process; the variable id is set to the name of
the process with which communication has taken place.
 Disadvantage:
 limited modularity, changing the identifier of a process may necessitate examining all other
process definitions. All references to the old identifier must be found so that they can be
modified to the new identifier.
Naming
 Indirect Communication
 the messages are sent to and received from mailboxes, or ports.
 A mailbox can be viewed abstractly as an object into which messages can be placed by processes
and from which messages can be removed.
 Each mailbox has a unique identification.
 Two processes can communicate only if the processes have a shared mailbox
 The send() and receive 0 primitives are defined as follows:
 send (A, message) -Send a message to mailbox A.
 receive (A, message)-Receive a message from mailbox A.
 A communication link has the following properties:
 A link is established between a pair of processes only if both members of the pair have a shared
mailbox.
 A link may be associated with more than two processes.
 Between each pair of communicating processes, there may be a number of different links, with
each link corresponding to one mailbox.
Naming
 Mailbox sharing
 P1, P2, and P3 share mailbox A
 P1, sends; P2 and P3 receive
 Who gets the message?
 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.
Naming
 A mailbox may be owned either by a process or by the operating system.
 If the mailbox is owned by a process,
 then we distinguish between the owner and the user.
 each mailbox has a unique owner, there can be no confusion about which process should receive a
message sent to this mailbox.
 process that owns a mailbox terminates, the mailbox disappears.
 If a mailbox that is owned by the operating system has an existence of its own.
 It is independent and is not attached to any particular process.
 The operating system provide a mechanism that allows a process to do the following:
 Create a new mailbox.
 Send and receive messages through the mailbox.
 Delete a mailbox.
Synchronization
 Communication between processes takes place through calls to send() and receive () primitives.
 different design options for implementing each primitive.
 Message passing may be either blocking or nonblocking known as synchronous and asynchronous.
 Synchronous Message Passing: In synchronous message passing, the sender process waits for the
recipient process to receive and process the message before continuing its execution. This type of
message passing is also known as "blocking" message passing.
 Asynchronous Message Passing: In asynchronous message passing, the sender process does not wait
for the recipient process to receive and process the message. Instead, it continues its execution
immediately after sending the message. This type of message passing is also known as "non-
blocking" message passing.
Synchronization
 Blocking send. The sending process is blocked until the message is received by the receiving
process or by the mailbox.
 Nonblocking send. The sending process sends the message and resumes operation.
 Blocking receive. The receiver blocks until a message is available.
 Nonblocking receive. The receiver retrieves either a valid message or a null.
 Different combinations of send() and receive() are possible.
 When both send() and receive() are blocking, we have a rendezvous between the sender and the
receiver. The solution to the producer-consumer problem becomes trivial
 The producer invokes the blocking send() call and waits until the message is delivered to either the
receiver or the mailbox. When the consumer invokes receive(), it blocks until a message is available.
Buffering
 Whether communication is direct or indirect, messages exchanged by communicating processes reside
in a temporary queue. Such queues can be implemented in three ways:
 Zero capacity. The queue has a maximum length of zero.
 the link cannot have any messages waiting in it., the sender must block until the recipient
receives the message.
 Bounded capacity. The queue has a finite length n
 atmost n messages can reside in it.
 If the queue is not full when a new message is sent, the message is placed in the queue. The
link’s capacity is finite.
 If the link is full, the sender must block it until space is available in the queue.
 Unbounded capacity. The queue's length is potentially infinite
 any number of messages can wait in it. The sender never blocks.
 The zero-capacity case is referred to as a message system with no buffering; the other cases are
referred to as systems with automatic buffering.

You might also like