Module 2 Process & Thread - 2014

33
Module 2 - Process Hemang Kothari Assistant Professor Computer Engineering Department MEFGI, Rajkot. Email: [email protected] Slides: http://www.slideshare.net/hemangkothari Computer Engineering Department - MEFGI 1 06/22/2022

description

Process, Process States, Threads, Multi Programming, Multithreading

Transcript of Module 2 Process & Thread - 2014

Page 1: Module 2 Process & Thread - 2014

04/11/2023 Computer Engineering Department - MEFGI 1

Module 2 - Process

Hemang KothariAssistant Professor

Computer Engineering Department MEFGI, Rajkot.

Email: [email protected]: http://www.slideshare.net/hemangkothari

Page 2: Module 2 Process & Thread - 2014

04/11/2023 Computer Engineering Department - MEFGI 2

Content

• Process.• Process States.• Process Control Block.• Threads. • Types of Threads. • Multithreading.

Page 3: Module 2 Process & Thread - 2014

04/11/2023 Computer Engineering Department - MEFGI 3

Process• A program in execution.• Process components: Every thing that interacts with its

current activity includes:• Program code• Program counter• Registers• Main memory• Program stack( temp var, parameters, etc.)• Data section ( global variables , etc….)

Page 4: Module 2 Process & Thread - 2014

04/11/2023 Computer Engineering Department - MEFGI 4

Doubt – Program Vs. Process• A process invokes or initiates a

program. It is an instance of a program that can be multiple and running the same application.

• Example:- Notepad is one program and can be opened twice.

• A process is a sequence of tasks.

Page 5: Module 2 Process & Thread - 2014

04/11/2023 Computer Engineering Department - MEFGI 5

Process

• Multiprogramming is rapid switching back and forth between processes

Page 6: Module 2 Process & Thread - 2014

04/11/2023 Computer Engineering Department - MEFGI 6

Process Creation

Principal events that cause process creationSystem initialization : Execution of a process creation

System.

1. User request to create a new process2. Initiation of a batch job

Page 7: Module 2 Process & Thread - 2014

04/11/2023 Computer Engineering Department - MEFGI 7

Process Termination

Conditions which terminate processes1. Normal exit (voluntary)2. Error exit (voluntary)3. Fatal error (involuntary)4. Killed by another process (involuntary)

Page 8: Module 2 Process & Thread - 2014

04/11/2023 Computer Engineering Department - MEFGI 8

Process Hierarchies

• Parent creates a child process, child processes can create its own process

• Forms a hierarchy– UNIX calls this a "process group"

• Windows has no concept of process hierarchy– all processes are created equal

Page 9: Module 2 Process & Thread - 2014

04/11/2023 Computer Engineering Department - MEFGI 9

Process• Daemons: processes that are running in the background mode (e.g.

checking email)• The only Unix system call to create a new process is fork. After that new

process has same memory image, same open files and etc. as its (only one) parent

• kill is the system call to terminate a process • In Unix all the processes in the system belong to a single tree, with init

at the root

Page 10: Module 2 Process & Thread - 2014

04/11/2023 Computer Engineering Department - MEFGI 10

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.– ready: The process is waiting to be assigned to a processor.– terminated: The process has finished execution.

Page 11: Module 2 Process & Thread - 2014

04/11/2023 Computer Engineering Department - MEFGI 11

Page 12: Module 2 Process & Thread - 2014

04/11/2023 Computer Engineering Department - MEFGI 12

Process States (2)

• Lowest layer of process-structured OS– handles interrupts, scheduling

• Above that layer are sequential processes

Page 13: Module 2 Process & Thread - 2014

04/11/2023 Computer Engineering Department - MEFGI 13

Process Implementation • Process table is used to maintain process information

( one for each process )• Process table is array of structure• Another name for process table is process control block

(PCB)• PCB is the key to multiprogramming• PCB must be saved when a process is switched from

running to ready or blocked states

Page 14: Module 2 Process & Thread - 2014

04/11/2023 Computer Engineering Department - MEFGI 14

PCB Fields

Page 15: Module 2 Process & Thread - 2014

04/11/2023 Computer Engineering Department - MEFGI 15

PCB is a key to Multiprogramming

Page 16: Module 2 Process & Thread - 2014

04/11/2023 Computer Engineering Department - MEFGI 16

Process Implementation

1. Hardware stacks program counter, etc.2. Hardware loads new program counter from interrupt vector.3. Assembly language procedure saves registers.4. Assembly language procedure sets up new stack.5. C interrupt service runs (typically reads and butters input).6. Scheduler decides which process is to run next.7. C procedure returns to the assembly code.8. Assembly language procedure starts up new current process.

Page 17: Module 2 Process & Thread - 2014

04/11/2023 Computer Engineering Department - MEFGI 17

ReviewIn theory, with three states, there could be six transition, two out of each state. However, only four transitions are shown. Are there any circumstances in which either or both of the missing transitions might occur?

1. The transition from blocked to running is conceivable. Suppose that a process is blocked on I/O and the I/O finishes. If the CPU is otherwise idle, the process could go directly from blocked to running.

2. The other missing transition, from ready to blocked, is impossible. A ready process cannot do I/O or anything else that might block it. Only a running process can block.

Page 18: Module 2 Process & Thread - 2014

04/11/2023 Computer Engineering Department - MEFGI 18

Review • Suppose that you were to design an advanced computer architecture that

did process switching in hardware, instead of having interrupts. What information would the CPU need? Describe how the hardware process switching might work.

You could have a register containing a pointer to the current process table entry. When I/O completed, the CPU would store the current machine state in the current process table entry. Then it would go to the interrupt vector for the interrupting device and fetch a pointer to another process table entry (the service procedure). This process would then be started up.

Page 19: Module 2 Process & Thread - 2014

04/11/2023 Computer Engineering Department - MEFGI 19

Thread - Motivation• Threads are very useful in modern programming whenever a

process has multiple tasks to perform independently of the others.• This is particularly true when one of the tasks may block, and it is

desired to allow the other tasks to proceed without blocking.• For example in a word processor, a background thread may check

spelling and grammar while a foreground thread processes user input ( keystrokes ), while yet a third thread loads images from the hard drive, and a fourth does periodic automatic backups of the file being edited.

Page 20: Module 2 Process & Thread - 2014

04/11/2023 Computer Engineering Department - MEFGI 20

Thread• Each process has a single address space that contains program

text, data and stack. Multiple threads of control within a process that share a same address space are called threads.

• Threads are lightweight processes that have their own program counter, registers and state.

• Threads allow multiple executions to take place in the same process environment.

• Multiple threads running in parallel in one process share program address, open files and other resources. Multiple process running in parallel in one computer share physical memory, disk and printers.

Page 21: Module 2 Process & Thread - 2014

Threads

Items shared by threads in a process Items private to each thread

Page 22: Module 2 Process & Thread - 2014

Threads• Threads in three process (a) and one process (b)

Page 23: Module 2 Process & Thread - 2014

04/11/2023 Computer Engineering Department - MEFGI 23

Multi - Threading

• Multithreading is the ability of a program or an operating system process to manage its use by more than one user at a time and to even manage multiple requests by the same user without having to have multiple copies of the programming running in the computer.

Page 24: Module 2 Process & Thread - 2014

04/11/2023 Computer Engineering Department - MEFGI 24

More Details

Page 25: Module 2 Process & Thread - 2014

04/11/2023 Computer Engineering Department - MEFGI 25

Page 26: Module 2 Process & Thread - 2014

Achieves concurrency without the overhead of using multiple processes

Threads within the same process can exchange

information through their common address space and have access to the shared resources of the process

Threads in different processes can exchange

information through shared memory that has been set

up between the two processes

How Sharing Will Work

Page 27: Module 2 Process & Thread - 2014

04/11/2023 Computer Engineering Department - MEFGI 27

Advantages of Thread• Responsiveness - One thread may provide rapid response

while other threads are blocked or slowed down doing intensive calculations.

• Resource sharing - By default threads share common code, data, and other resources, which allows multiple tasks to be performed simultaneously in a single address space.

• Economy - Creating and managing threads ( and context switches between them ) is much faster than performing the same tasks for processes.

Page 28: Module 2 Process & Thread - 2014

04/11/2023 Computer Engineering Department - MEFGI 28

One More Advantage

• Scalability i.e. Utilization of multiprocessor architectures - A single threaded process can only run on one CPU, no matter how many may be available, whereas the execution of a multi-threaded application may be split amongst available processors. ( Note that single threaded processes can still benefit from multi-processor architectures when there are multiple processes contending for the CPU, i.e. when the load average is above some certain threshold. )

Page 29: Module 2 Process & Thread - 2014

04/11/2023 Computer Engineering Department - MEFGI 29

Types of Thread• There are two types of threads to be managed in a modern

system: User threads and kernel threads.• User threads are supported above the kernel, without kernel

support. These are the threads that application programmers would put into their programs.

• Kernel threads are supported within the kernel of the OS itself. All modern OSes support kernel level threads, allowing the kernel to perform multiple simultaneous tasks and/or to service multiple kernel system calls simultaneously.

Page 30: Module 2 Process & Thread - 2014

04/11/2023 Computer Engineering Department - MEFGI 30

Review

• On all current computers, at least part of the interrupt handlers are written in assembly language. Why?

• Generally, high-level languages do not allow one the kind of access to CPU hardware that is required. For instance, an interrupt handler may be required to enable and disable the interrupt servicing a particular device, or to manipulate data within a process’ stack area. Also, interrupt service routines must execute as rapidly as possible

Page 31: Module 2 Process & Thread - 2014

04/11/2023 Computer Engineering Department - MEFGI 31

Review • Can a thread ever be preempted by a clock interrupt? If so,

under what circumstances? If not, why not?• User-level threads cannot be preempted by the clock unless

the whole process’ quantum has been used up. Kernel-level threads can be preempted individually. In the latter case, if a thread runs too long, the clock will interrupt the current process and thus the current thread. The kernel is free to pick a different thread from the same process to run next if it so desires.

Page 32: Module 2 Process & Thread - 2014

04/11/2023 Computer Engineering Department - MEFGI 32

Review

• In a system with threads, is there one stack per thread or one stack per process when user-level threads are used? What about when kernel-level threads are used?

• Each thread calls procedures on its own, so it must have its own stack for the local variables, return addresses, and so on. This is equally true for user-level threads as for kernel-level threads.

Page 33: Module 2 Process & Thread - 2014

04/11/2023 Computer Engineering Department - MEFGI 33

Thanks

“I know what I have given you... I do not know what you have received.”