0% found this document useful (0 votes)
61 views

Process and Threads

The document discusses processes and threads, defining a process as a program under execution that includes the program code and associated data. It describes process states as running, ready, or blocked, and the transitions between these states. Key concepts covered include process creation, termination, states, and control, as well as the benefits of using threads within a process.

Uploaded by

UTSAV KHANDELWAL
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)
61 views

Process and Threads

The document discusses processes and threads, defining a process as a program under execution that includes the program code and associated data. It describes process states as running, ready, or blocked, and the transitions between these states. Key concepts covered include process creation, termination, states, and control, as well as the benefits of using threads within a process.

Uploaded by

UTSAV KHANDELWAL
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/ 35

Unit-2

Process and Thread


Looping
 Outline
• Definition of process
• Process relationship
• Process states
• Process state transitions
• Process control
• Process control block
• Context switching
• Threads
• Concept of multithreads
• Benefits of threads
• Types of threads
Section - 1
Background
1. A computer platform consists of a collection of hardware resources, such as the processor, main memory, I/O
modules, timers, disk drives, and so on.
2. Computer applications are developed to perform some task. Typically, they accept input from the outside
world, perform some processing, and generate output.
3. It is inefficient for applications to be written directly for a given hardware platform. The principal reasons for
this are as follows:
a) Numerous applications can be developed for the same platform.Thus, it makes sense to develop common routines for
accessing the computer’s resources.
b) The processor itself provides only limited support for multiprogramming. Software is needed to manage the sharing of the
processor and other resources by multiple applications at the same time.
c) When multiple applications are active at the same time, it is necessary to protect the data, I/O use, and other resource use of
each application from the others.

1. The OS was developed to provide a convenient, feature-rich, secure, and consistent interface for applications
to use. The OS is a layer of software between the applications and the computer hardware (Figure 2.1) that
supports applications and utilities.
2. We can think of the OS as providing a uniform, abstract representation of resources that can be requested and
accessed by applications. Resources include main memory, network interfaces, file systems, and so on. Once
the OS has created these resource abstractions for applications to use, it must also manage their use.

4
What is Process?
Program Process

5
What is Process?
 Process is a program under execution.
 Process is an abstraction of a running program.
 Process is an instance of an executing program, including the current values of the program
counter, registers & variables.
 The entity that can be assigned to and executed on a processor.
 Two essential elements of a process are program code:
 Program code
 Set of Data associated with code

6
Process Creation
1. System initialization
2. Execution of a process creation system call (fork) by running process
3. A user request to create a new process
4. Initialization of batch process

7
Process Creation
1. System initialization
• At the time of system (OS) booting various processes are created
• Foreground and background processes are created
• Background process – that do not interact with user e.g. process to accept mail
• Foreground Process – that interact with user

8
Process Creation
2. Execution of a process creation system call (fork) by running process
• Running process will issue system call (fork) to create one or more new process to help it.
• A process fetching large amount of data and execute it will create two different processes
one for fetching data and another to execute it.

P1
P3
P2

9
Process Creation (Cont…)
3. A user request to create a new process
• Start process by clicking an icon (opening word file by double click) or by typing command.

10
Process Creation (Cont…)
4. Initialization of batch process
• Applicable to only batch system found on large mainframe
• The OS is provided with a batch job control stream, usually on tape or disk. When the OS is
prepared to take on new work, it will read the next sequence of job control commands.

11
Process Termination
Normal completion: The process executes an OS service call to indicate that it has completed running.{exit system call}
Time limit exceeded: The process has run longer than the specified total time limit. There are a number of possibilities for the type of
time that is measured. These include total elapsed time (“wall clock time”), amount of time spent executing, and, in the case of an
interactive process, the amount of time since the user last provided any input.
Memory unavailable: The process requires more memory than the system can provide.
Bounds violation: The process tries to access a memory location that it is not allowed to access.
Protection error: The process attempts to use a resource such as a file that it is not allowed to use, or it tries to use it in an improper
fashion, such as writing to a read only file.
Arithmetic error: The process tries a prohibited computation, such as division by zero, or tries to store numbers larger than the
hardware can accommodate.
Time overrun: The process has waited longer than a specified maximum for a certain event to occur.
I/O failure: An error occurs during input or output, such as inability to find a file, failure to read or write after a specified maximum
number of tries (when, for example, a defective area is encountered on a tape), or invalid operation (such as reading from the line
printer).
Invalid instruction: The process attempts to execute a nonexistent instruction (often a result of branching into a data area and
attempting to execute the data).
Privileged instruction: The process attempts to use an instruction reserved for the operating system.
Data misuse: A piece of data is of the wrong type or is not initialized.
Operator or OS intervention: For some reason, the operator or the operating system has terminated the process (for example, if a
deadlock exists).
Parent termination: When a parent terminates, the operating system may automatically terminate all of the offspring of that parent.
Parent request: A parent process typically has the authority to terminate any of its offspring.

12
Process Termination
1. Normal exit (voluntary)
• Terminated because process has done its work.

13
Process Termination
2. Error exit (voluntary)
• The process discovers a fatal error e.g. user types the command cc foo.c to compile the
program foo.c and no such file exists, the compiler simply exit.

14
Process Termination
3. Fatal error (involuntary)
• An error caused by a process often due to a program bug e.g. executing an illegal instruction,
referencing nonexistent memory or divided by zero.

15
Process Termination
4. Killed by another process (involuntary)
• A process executes a system call telling the OS to kill some other process using kill system
call.

16
Process State
1. Running – Process is actually using the
CPU Running
2. Ready – Process is runnable,
temporarily stopped to let another
process to run
3. Blocked – process is unable to run until Blocked Ready
some external event happens

Processes are always either executing (running) or waiting to


execute (ready) or waiting for an event (blocked) to occur.

20
Process State Blocked
Running
Ready
• Running – Process is actually using the
CPU
• Ready – Process is runnable, temporarily
stopped to let another process to run
Running
• Blocked – process is unable to run until
some external event happens
• Processes are always either executing
(running), waiting to execute (ready) or
waiting for an event (blocked) to occur. Blocked Ready

21
Process State Transitions
• When and how these transitions occur
(process moves from one state to another)?
1. Process blocks for input or waits for an
event (i.e. printer is not available)
2. Scheduler picks another process Running
• End of time-slice or
pre-emption. 1
3
2
3. Scheduler picks this process
4. Input becomes available, event arrives Blocked Ready
(i.e. printer become available) 4

22
Queue Diagram
Process is
Scheduled Process is
completed
Ready Queue to run
Admit Dispatch Exit
Processor

Time-out

Event Wait
Event
Occurs Blocked Queue

If all processes were always ready to execute, then the queuing discipline would be effective. The queue is a first-in-first-out
list and the processor operates in round-robin fashion on the available processes (each process in the queue is given a certain
amount of time, in turn, to execute and then returned to the queue, unless blocked)
23
Five State Process Model and Transitions
Dispatch
Admit Release
New Ready Running Exit
Time-out

Event
Occurs Event
Wait
Blocked

 New – process is being created{Acquiring resources, the process itself is not in main memory. That is, the code of the program to be
executed is not in main memory, and no space has been allocated for the data associated with that program. While the process is in the New state, the
program remains in secondary storage, typically disk storage.}
 Ready – process is waiting to run (runnable), temporarily stopped to let another process run
 Running – process is actually using the CPU
 Blocked/Waiting – unable to run until some external event happens, such as the completion
of an I/O operation
 Exit (Terminated) – process has finished the execution{a process is terminated when it reaches a natural completion
point, when it aborts due to an unrecoverable error, or when another process with the appropriate authority causes the process to abort. Termination moves the
process to the exit state. At this point, the process is no longer eligible for execution}

24
State transition
 Null  New: A new process is created to execute a program.
 New  Ready: The OS will move a process from the New state to the Ready state when it is
prepared to take on an additional process. Most systems set some limit based on the
number of existing processes or the amount of virtual memory committed to existing
processes. This limit assures that there are not so many active processes as to degrade
performance.
 Ready  Running: When it is time to select a process to run, the OS chooses one of the
processes in the Ready state. This is the job of the scheduler or dispatcher.
 Running  Exit: The currently running process is terminated by the OS if the process
indicates that it has completed, or if it aborts.

25
State transition
 Running  Ready: The most common reason for this transition is that the running process
has reached the maximum allowable time for uninterrupted execution;
• virtually all multiprogramming operating systems impose this type of time discipline. There
are several other alternative causes for this transition, which are not implemented in all
operating systems.
• Of particular importance is the case in which the OS assigns different levels of priority to
different processes. Suppose, for example, that process A is running at a given priority level,
and process B, at a higher priority level, is blocked. If the OS learns that the event upon which
process B has been waiting has occurred, moving B to a ready state, then it can interrupt
process A and dispatch process B. We say that the OS has preempted process A.
• Finally, a process may voluntarily release control of the processor. An example is a
background process that performs some accounting or maintenance function periodically

26
State transition
 Running  Blocked: A process is put in the Blocked state if it requests something for which
it must wait. A request to the OS is usually in the form of a system service call;
• that is, a call from the running program to a procedure that is part of the operating system
code.
• For example, a process may request a service from the OS that the OS is not prepared to
perform immediately.
• It can request a resource, such as a file or a shared section of virtual memory, that is not
immediately available. Or the process may initiate an action, such as an I/O operation, that
must be completed before the process can continue.
• When processes communicate with each other, a process may be blocked when it is waiting
for another process to provide data or waiting for a message from another process.

27
State transition
 Blocked  Ready: A process in the Blocked state is moved to the Ready state when the
event for which it has been waiting occurs.
 Ready  Exit: For clarity, this transition is not shown on the state diagram. In some
systems, a parent may terminate a child process at any time. Also, if a parent terminates, all
child processes associated with that parent may be terminated.

28
29
30
State diagram with suspend state

Suspend ready: Process that was initially in the ready state but were swapped out Suspend wait or suspend blocked: Similar to suspend ready
of main memory(refer Virtual Memory topic) and placed onto external storage by but uses the process which was performing I/O operation and
scheduler are said to be in suspend ready state. The process will transition back to lack of main memory caused them to move to secondary
ready state whenever the process is again brought onto the main memory. memory. When work is finished it may go to suspend ready.
31
Types of schedulers:
 Long term: performance: – Makes a decision about how many processes should be made to
stay in the ready state, this decides the degree of multiprogramming. Once a decision is
taken it lasts for a long time hence called long term scheduler.
 Short term: Context switching time: – Short term scheduler will decide which process to be
executed next and then it will call dispatcher. A dispatcher is a software that moves process
from ready to run and vice versa. In other words, it is context switching.
 Medium term: Swapping time: – Suspension decision is taken by medium term scheduler.
Medium term scheduler is used for swapping that is moving the process from main memory
to secondary and vice versa.

32
Multiprogramming: We have many processes ready to run. There are two types of
multiprogramming:
• Pre-emption: Process is forcefully removed from CPU. Pre-emption is also called as time
sharing or multitasking.
• Non pre-emption: Processes are not removed until they complete the execution.

Degree of multiprogramming:
The number of processes that can reside in the ready state at maximum decides the degree of
multiprogramming, e.g., if the degree of programming = 100, this means 100 processes can
reside in the ready state at maximum.

33
Process Control Block (PCB)
 A Process Control Block (PCB) is a data structure maintained by the operating system for
every process.
 PCB is used for storing the collection of information about the processes.
 The PCB is identified by an integer process ID (PID).
 A PCB keeps all the information needed to keep track of a process.
 The PCB is maintained for a process throughout its lifetime and is deleted once the process
terminates.
 The architecture of a PCB is completely dependent on operating system and may contain
different information in different operating systems.
 PCB lies in kernel memory space.

34
Process Control Block (PCB) contains
• Process ID/Identifier - Unique identification for each of the process in the operating
system.
• State - The current state of the process i.e., whether it is ready, running, waiting.
• Priority - Priority of a process relative to other process.
• Program Counter - Program Counter is a pointer to the address of the next instruction
to be executed for this process.
• Memory pointers: Includes pointers to the program code and data associated with this
process, plus any memory blocks shared with other processes.
• Context data: These are data that are present in registers in the processor while the
process is executing.
• I/O status information: Includes outstanding I/O requests, I/O devices (e.g., tape
drives) assigned to this process, a list of files in use by the process, and so on
• Accounting information - This includes the amount of CPU used for process execution,
time limits etc.

35
Dispatcher
Dispatcher module gives control of the CPU to the process selected by the short-term
scheduler; this involves: Context switching, Switching to user mode, Jumping to the proper
location in the user program to restart that program,
dispatch latency: time it takes for the dispatcher to stop one process and start another running.

• 100 ! Starting address of dispatcher program


• Shaded areas indicate execution of dispatcher process;
• first six instructions of process A are executed, followed by a time-out and the execution of
some code in the dispatcher, which executes six instructions before turning control to
process B.2 After four instructions are executed, process B requests an I/O action for which it
must wait. Therefore, the processor stops executing process B and moves on, via the
dispatcher, to process C.

36
Context switching
• Context switch means stopping one process and
restarting another process.
• When an event occur, the OS saves the state of an active
process and restore the state of new process.
• Context switching is purely overhead because system
does not perform any useful work while context switch.
• Sequence of action:
1. OS takes control (through interrupt)
2. Saves context of running process in the process PCB
3. Reload context of new process from the new process PCB
4. Return control to new process

37
Context Switching
The process of saving the context of one process and loading the context of another process is known as Context
Switching. In simple terms, it is like loading and unloading the process from the running state to the ready state.

When does context switching happen?


1. When a high-priority process comes to a ready state (i.e. with higher priority than the running process)
2. An Interrupt occurs
3. User and kernel-mode switch (It is not necessary though)
4. Preemptive CPU scheduling used.

Context Switch vs Mode Switch


A mode switch occurs when the CPU privilege level is changed, for example when a system call is made or a fault occurs.
The kernel works in more a privileged mode than a standard user task. If a user process wants to access things that are
only accessible to the kernel, a mode switch must occur. The currently executing process need not be changed during a
mode switch.
A mode switch typically occurs for a process context switch to occur. Only the kernel can cause a context switch.

CPU-Bound vs I/O-Bound Processes:


A CPU-bound process requires more CPU time or spends more time in the running state.
An I/O-bound process requires more I/O time and less CPU time. An I/O-bound process spends more time in the waiting
state.

38

You might also like