• No results found

Process, Threads & Process Scheduling

N/A
N/A
Protected

Academic year: 2020

Share "Process, Threads & Process Scheduling"

Copied!
61
0
0

Loading.... (view fulltext now)

Full text

(1)

Processes and Threads

1. PROCESSES

1.1. The Process Model 1.2. Process Creation 1.3. Process Termination 1.4 Process Hierarchies 1.5. Process States

1.7 Modeling Multiprogramming 2. THREADS

2.1. Thread Usage

2.2 The Classical Thread Model 2.4. Implement threads in user space 2.5 Implement threads in kernel space

3. SCHEDULING

(2)

1.1. The Process Model

• • •

Multiprogramming of four programs

Conceptual model of 4 independent, sequential Only one program active at any instant

(3)

1.1. The Process Model (cont)

• Foreground processes

– Associated with the user

Background processes •

– –

Daemons

Not associated with any user

(4)

1.2. Process Creation

Principal events that cause process creation

1. System initialization

– Boot time

2. Execution of a process creation system

– network date fetch – buffer - execute

3. 4.

User request to create a new process Initiation of a batch job

(5)

1.2. Process Creation (cont)

• Unix

– Fork()

• Creates copy of parent

• Then execve is called to change its content exectute

Windows

– Win32 function call CreateProcess() • Does creation and loading of program

to

(6)

1.3. Process Termination

Conditions which terminate processes

1. 2. 3. 4.

Normal exit (voluntary) Error exit (voluntary) Fatal error (involuntary)

(7)

1.4 Process Hierarchies

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

Forms a hierarchy

– UNIX calls this a "process group"

processes •

• Windows has no

– all processes are

– Though in some • It can be passed

concept of process

created equal

cases handle() is used

to other processes

(8)

1.5. Process States

• Possible process states

– –

Running (actually using the CPU at that instant). Blocked (runnable; temporarily stopped to let another process run)

Ready (unable to run until some external event happens)

(9)

1.5. Process States (cont)

• Lowest layer of process-structured OS

– handles interrupts, scheduling

Above that layer are different sequential

(10)

Implementation of Processes (1)

(11)

Implementation of Processes (2)

Skeleton of what lowest level of OS does when interrupt occurs

(12)

2. THREADS

• is a light-weight process

• is the smallest sequence of instructions

programmed

(13)

The Thread Model

• •

(14)

Differences b/w process and thread

• processes are typically independent, while threads exist

of a process

as subsets

• processes carry considerably more state information than threads, whereas multiple threads within a process share process state as well as memory and other resources

• processes have separate

their address space

(15)

Differences b/w process and thread

• processes interact only through system-provided inter-process communication mechanisms

context switching between threads in the same process is typically

between processes.

(16)

Threads

The Thread Model (1)

(17)

The Thread Model (2)

(18)

Thread Usage (1)

(19)

Thread Usage (2)

(20)

Thread Usage (3)

• Rough outline of code for previous

(a) Dispatcher thread (b) Worker thread

(21)

Thread Usage (4)

(22)

Implementing Threads in User Space

• Own private thread table

– – –

TCB ( thread control block)

Analogous to PCB or process table It is managed by Run-time system

• State is saved in this TCB

(23)

Implementing Threads in User Space

• When thread does something which cause it to blocked

– Calls run-time, and run-time sees if it needs to be blocked

It stores thread registers in thread table (TCB) Finds next thread to run in ready queue

– – – –

Reload register’s with new thread’s New thread starts running

saved values

• Key features

(24)

Implementing Threads in User Space

(25)

Implementing Threads in the Kernel

(26)

Single Thread vs. Multithread

• Single Threaded Server –

– – –

Main loop of web-server gets a request Examines it

Complete If waiting

• Server

it before next one for disk

is idle

• Does not process incoming requests

As result, only few requests/sec can be processed

• Multithread

– Each thread has its own work

(27)

Finite-State Machine Server

Non-Blocking system call •

– When a request come in one & only thread

• Can be found from cache

• If not, non-blocking disk operation is started

Server records the state of current request Fetches next event

• May be a new request

• Reply from disk about previous request

Process next request

• New request started

examines it

– –

• If reply from disk, relevant recorded state is fetched table and reply is processed

from the

(28)

Finite State Machine

A design in which each computation has a saved state and there exists some set of

events that can occur to change the state called a finite-state machine.

Uses non-blocking system call

is

• Best to use when be processed.

large amount of data is to

– – –

Reads a block Process it

Write it back

(29)

Scheduling

• Problem

– One or more processes are in ready state

– Decide which one to proceed for processing

Scheduling •

– That part of Operating System which decides which process runs next

The algorithm it uses called Scheduling Algorithm

(30)

Scheduling

Introduction to Scheduling (1)

• Bursts of CPU usage alternate with periods of I/O

– a CPU-bound process

– an I/O bound process

(31)

When to Schedule

• When a process is

– – – –

Created Exits

(32)

Scheduling

Non-Preemptive •

– Algorithm picks a process and lets it run until it blocks ( either on I/O or waiting for

process )

Or it voluntarily releases the CPU It cant be forcibly suspended

another

– –

• Preemptive

– Algorithm picks a process and let it maximum of some fixed time.

run for a

(33)

Introduction to Scheduling (2)

(34)

First Come First Serve

• The process that requests the CPU first, gets the CPU first

• New job queue

is added to the end of the job

• Non-preemptive

(35)

Shortest Job First

• Non-Preemptive

(36)

Shortest Job First

An example of shortest job first scheduling

• Average turnaround time

– (a)

– (b)

(37)

Shortest Remaining Time Next

Pre-emptive Scheduling

• Chooses process whose remaining is short

run time

• Every time new process comes in, current process and new process run-time are

(38)

Scheduling in Interactive Systems

• • • • • • • Round-Robin Scheduling Priority Scheduling Multiple Queues

Shortest Process Next Guaranteed Scheduling Lottery Scheduling

(39)

Round-Robin

Scheduling

• Used in interactive systems

• Quantum

(40)

Round-Robin Scheduling

• Round Robin Scheduling

– –

(a) (b)

list of list of

runnable processes

(41)

Round-Robin Scheduling

• If is

a process blocks or finished before quantum elapsed then process switching is done.

• It is easy to implement.

(42)

Round-Robin Scheduling

• Setting the quantum

– too short causes too many process switches lowers the CPU efficiency

and

– too long may cause poor response to short interactive requests.

• Quantum around 20-50 msec is recommended

(43)

Priority Queue

Some processes are more important than others. •

• •

This leads to assigning priority to each process. E.g. University hierarchy

(44)

Priority Queue

Each process is assigned apriority, and the

run-nable process with the highest priority allowed to run.

is

• A process with high priority may run indefinitely without intervention.

• To avoid this:

– 1) Decrease priority of process on each clock

– 2) Fix a max quantum on higher priority task

cycle

(45)

Priority Queue

• Can be assigned

– Statically

– Dynamically

• Statically

– Fix priority or time on each task by judging them statically ( whatever information is

provided)

E.g. background processes 20msec and foreground processes is given 40msec

(46)

Priority Queue

• Dynamically

– Better algorithm is to use formula 1/f where ‘f’ is fraction of the last quantum used by the process.

Examples: •

– Process that used only 1 msec of its

50

50 msec quantum would get priority

– Process that ran

2,

25 msec before blocking would get priority

– Process that used the whole quantum would get

(47)

Priority Queue

• Dynamically

– Better algorithm is to use formula 1/f where ‘f’ is fraction of the last quantum used by the process.

Examples: •

– Process that used only 1 msec of its

50

50 msec quantum would get priority

– Process that ran

2,

25 msec before blocking would get priority

– Process that used the whole quantum would get

(48)

Scheduling in Interactive Systems

Priority Queue

(49)

Multiple Queues

Every class has a priority and quantum And each class has number of processes

Once a process is run in a certain class, change its class to lower one so that it could give

change to other processes to run. Example: • • • • – –

A process with 100msec time needed to execute

(50)

Multiple Queues

(51)
(52)
(53)
(54)
(55)
(56)
(57)

Shortest Process Next

Similar to SJF •

• Shortest Job First response

(SJF) has minimum average

• Needs to runnable

figure out which of the currently processes is the shortest one

(58)

Guaranteed Scheduling

Make a promise of CPU time and fulfill each process.

• it to

• If there are N users logged including you then you will receive about 1/N of the CPU power.

(59)

Guaranteed Scheduling

• Tracking of CPU time utilized by each process is required.

(60)

Lottery Scheduling

Give processes lottery tickets for various system resources specially CPU time. Lets say 20 msec lottery prize.

• When a scheduling decision is to made, one lottery ticket is chosen at random and the process holding the ticket gets the resource.

• So some processes can be given more tickets to increase their chance of winning.

(61)

Lottery Scheduling

• Simpler to implement compared to Guaranteed scheduling

• Highly responsive as if one process blocks it can transfer it lottery tickets to other

References

Related documents

While labour market arrangements, such as part-time opportunities (when well-paid and protected), have a larger impact on the outcomes of women with higher educational

l  LTS to model processes as state machines - sequences of atomic actions. l  FSP to specify processes using prefix “->”, choice ” | ”

Surely, the two cases explored in this thesis are just a snapshot that could be  taken  in  nearly  every  Arab  country  from  Morocco  to  Oman.  The  core 

cliques problem asks for a vertex partition by γ -quasi-cliques with the minimum number of

This research uses a single case study, the Bentley School District (pseudonym), to examine the implementation, impact and results the district has had using the FFT as an

The topological analysis through the QTAIM and ELF methods was evaluated for the linear CH 3 X· · ·HX and HX· · ·HX dimer arrangements (QTAIM molecular graphs are shown in

But within a process, all threads share the same address space and other process resources like file descriptors and others... Threads

Kernel threads can access both kernel virtual memory and, if a user process is running, the user virtual memory of the running process.. However, even in the kernel, an attempt