Processes, Schedulers, Threads

49
Processes, Schedulers, Threads Sorin Manolache [email protected]

description

Processes, Schedulers, Threads. Sorin Manolache [email protected]. Last on TTIT61. The OS consists of A user interface for controlling programs (starting, interrupting) A set of device drivers for accessing the hardware - PowerPoint PPT Presentation

Transcript of Processes, Schedulers, Threads

Page 1: Processes, Schedulers, Threads

Processes, Schedulers, Threads

Sorin Manolache

[email protected]

Page 2: Processes, Schedulers, Threads

2S. Manolache, Process programming and operating systems, Processes, schedulers, threads

Last on TTIT61

The OS consists ofA user interface for controlling programs (starting,

interrupting)A set of device drivers for accessing the hardwareA set of system calls as a program interface to hardware

(and not only, we’ll see later)Process scheduler that schedules process execution

and manages the process stateMemory managementFile systemOthers

Page 3: Processes, Schedulers, Threads

3S. Manolache, Process programming and operating systems, Processes, schedulers, threads

Lecture Plan

1. What is an operating system? What are its functions? Basics of computer architectures. (Part I of the textbook)

2. Processes, threads, schedulers (Part II , chap. IV-VI)

3. Synchronisation (Part II, chap. VII)

4. Primary memory management. (Part III, chap. IX, X)

5. File systems and secondary memory management (Part III, chap. XI, XII, Part IV)

6. Security (Part VI)

Page 4: Processes, Schedulers, Threads

4S. Manolache, Process programming and operating systems, Processes, schedulers, threads

Outline

The concept of “process”Memory layout of a processProcess state and state transition diagramProcess Control Blocks

Context switchesOperations on processes (create, terminate, etc.)

ThreadsMotivation, user vs. kernelMulti-threading models, threading issues

CPU schedulingCriteria, algorithms

Page 5: Processes, Schedulers, Threads

5S. Manolache, Process programming and operating systems, Processes, schedulers, threads

What Is a Process?

In the previous lecture, I’ve used “processes” and “programs” interchangeably hoping you will not notice

A program is a passive entity, we use it to refer to the executable file on the disk (or memory stick, etc., from where it is eventually loaded in the main memory)

Definition:A process is an active entity, it is an executing program,

it is an instance of the program

We can have several processes of the same program

Page 6: Processes, Schedulers, Threads

6S. Manolache, Process programming and operating systems, Processes, schedulers, threads

Memory Layout of a Process

The text segment contains the code of the program

The data segment contains the global variables

The stack segment contains the return addresses of function calls, and in most OS the local variables

The heap contains the dynamically allocated memory. It is typically at the other end of the stack segment.

Text (code)

Data

Stack

Mem

ory

Page 7: Processes, Schedulers, Threads

7S. Manolache, Process programming and operating systems, Processes, schedulers, threads

The Data Segment(s)

A process may contain several data segments:Read-only data: printf(“%d\n”, i)

“%d\n” is read-only Initialised data: int a = 20;

20 goes into the executable file on the disk. When the program is loaded into memory, the memory location corresponding to ‘a’ is initialised with 20

Uninitialised data: int b; No space for ‘b’ is reserved for the executable file on

the disk. It is just specified in the executable file header that the uninitialised data segment is X bytes long. When the program is loaded into memory, a segment of X bytes is reserved for uninitialised data.

Page 8: Processes, Schedulers, Threads

8S. Manolache, Process programming and operating systems, Processes, schedulers, threads

The Stack Segment

int fact(int n) { int xp;1000: if (n == 0)1001: return 1;1002: xp = fact(n – 1);1003: xp *= n;1004: return xp; }

main() { int rlt;1005: rlt = fact(3);1006: printf(“%d\n, rlt); }

rlt31006

xp

21003

xp

11003xp01003xp

main

fact

fact

fact

fact

= 1

= 1

= 2

= 6

Page 9: Processes, Schedulers, Threads

9S. Manolache, Process programming and operating systems, Processes, schedulers, threads

Process Execution

A register of the CPU, the program counter (PC), contains the address of the next instruction to execute (it points in the code segment)

Another register of the CPU, the stack pointer (SP), contains the address of the of the top of the stack (it points in the stack segment)

Page 10: Processes, Schedulers, Threads

10S. Manolache, Process programming and operating systems, Processes, schedulers, threads

Segment Sharing

Can two processes of the same program share the code segment? What would we gain/lose if yes?

Can two processes, not necessarily of the same program, share the data segment? Why would we (not) want that?

Can two processes, not necessarily of the same program, share the stack segment?

Page 11: Processes, Schedulers, Threads

11S. Manolache, Process programming and operating systems, Processes, schedulers, threads

Outline

The concept of “process”Memory layout of a processProcess state and state transition diagramProcess Control Blocks

Context switchesOperations on processes (create, terminate, etc.)

ThreadsMotivation, user vs. kernelMulti-threading models, threading issues

CPU schedulingCriteria, algorithms

Page 12: Processes, Schedulers, Threads

12S. Manolache, Process programming and operating systems, Processes, schedulers, threads

Process States

RunningReady

Waiting

New

Terminated

preemption

dispatch

I/O, waitI/O,

event completion

admitted

exit

Page 13: Processes, Schedulers, Threads

13S. Manolache, Process programming and operating systems, Processes, schedulers, threads

Outline

The concept of “process”Memory layout of a processProcess state and state transition diagramProcess Control Blocks

Context switchesOperations on processes (create, terminate, etc.)

ThreadsMotivation, user vs. kernelMulti-threading models, threading issues

CPU schedulingCriteria, algorithms

Page 14: Processes, Schedulers, Threads

14S. Manolache, Process programming and operating systems, Processes, schedulers, threads

Process Control Block (PCB)

Is a memory area in the OS kernel memoryOne for each processContains the data needed by the OS in order to manage

the process to which the PCB corresponds

It is also called the context of the processWhen the OS switches the process that runs on the CPU,

we say that it performs a context switch

Page 15: Processes, Schedulers, Threads

15S. Manolache, Process programming and operating systems, Processes, schedulers, threads

Contents of the PCB

Program counter value, stack pointer value, and the value of all other registers

Memory management information (base + limit registers, translation tables)

CPU scheduling information (process priority, pointers to scheduling queues)

Accounting information (CPU time used, real time used, etc)

I/O status (devices allocated to the process, list of open files, etc)

Page 16: Processes, Schedulers, Threads

16S. Manolache, Process programming and operating systems, Processes, schedulers, threads

Context Switch

Process A Process B

A running

B running

A running

Context switch

Context switch

Save state of A into PCBA

Load state of B into PCBB

Save state of B into PCBB

Load state of A into PCBA

Page 17: Processes, Schedulers, Threads

17S. Manolache, Process programming and operating systems, Processes, schedulers, threads

Ready Queues

Ready queue

I/O queue I/O request

Time slice expired

Wait for interrupt

CPU

I/O

Interrupt occurs

Page 18: Processes, Schedulers, Threads

18S. Manolache, Process programming and operating systems, Processes, schedulers, threads

Scheduling

The scheduler is the routine that selects a process from the ready queue. This is the short-term scheduler. It runs at least one time every 100ms.

Page 19: Processes, Schedulers, Threads

19S. Manolache, Process programming and operating systems, Processes, schedulers, threads

Long-Term Scheduler

Degree of multiprogramming: the number of processes in memory at the same time

If this degree is stable number of newly created processes over a time interval is roughly equal to the number of processes that terminated in the same interval

A long-term scheduler runs whenever a processes terminates in order to decide which new process to bring in the memory

Has to select an appropriate process mixToo many CPU-bound processes devices under-utilised,

process execution times much longer than if the mix was more balanced

Too many device-bound processes under-utilised CPU

Page 20: Processes, Schedulers, Threads

20S. Manolache, Process programming and operating systems, Processes, schedulers, threads

Outline

The concept of “process”Memory layout of a processProcess state and state transition diagramProcess Control Blocks

Context switchesOperations on processes (create, terminate, etc.)

ThreadsMotivation, user vs. kernelMulti-threading models, threading issues

CPU schedulingCriteria, algorithms

Page 21: Processes, Schedulers, Threads

21S. Manolache, Process programming and operating systems, Processes, schedulers, threads

Operations on Processes

Creation: system call called fork in UnixTermination: system call called exitLoading of new process image (code, data, stack

segments): system call called exec in UnixWaiting for the termination of a child: system call called

wait or waitpid in Unix

man –s 2 fork/exit/exec/wait/waitpid

Page 22: Processes, Schedulers, Threads

22S. Manolache, Process programming and operating systems, Processes, schedulers, threads

Fork

Fork creates a “clone” of the invoking process. The invoking process will be the parent, and the “clone” will be the child. One child has exactly one parent, a parent may have 0 or more children

The child inherits the resources of the parent (set of open files, scheduling priority, etc.)

Page 23: Processes, Schedulers, Threads

23S. Manolache, Process programming and operating systems, Processes, schedulers, threads

CoW

However, it has its own memory space. Its memory space contains the same data as the memory space of the parent, just that it is a copy.

Parent and child do not share data and stack segments. Each has its own copy that it can modify independently.

Should parent and child have different copies of read-only segments? Do they share the code segment?

Actually, in modern Unixes, data segments are allocated in a lazy manner, i.e. only if one of them starts writing, will the data segment copied.

This lazy copying technique is called “copy-on-write” (CoW)

Page 24: Processes, Schedulers, Threads

24S. Manolache, Process programming and operating systems, Processes, schedulers, threads

Code Example

pid = fork();

if (pid == 0) {

printf(“I am the child. My ID is %d and my parent’s ID is %d\n”, getpid(), getppid());

execlp(“/bin/ls”, “ls”, “-l”, “/home/TTIT61”, 0);

exit(0);

} else {

printf(“I am the parent. My child’s ID is %d\n”, pid);

waitpid(pid, &status, 0);

}

Page 25: Processes, Schedulers, Threads

25S. Manolache, Process programming and operating systems, Processes, schedulers, threads

Co-operating Processes

Parent and child processes have separated memory spaces, it is as if they are not aware that the other process exists.

Sometimes this is not desirable, we would like to pass data from one process to the other

E.g.:gzip –dc nachos-3.4.tar.gz | tar xf –Mail composer + spell checker

Page 26: Processes, Schedulers, Threads

26S. Manolache, Process programming and operating systems, Processes, schedulers, threads

Inter-Process Communication Mechanisms

Pipes (gzip –dc nachos-3.4.tar.gz | tar xf -)Signals (kill -9 pid)Message queuesSemaphores, condition variables, locks, etc.Shared memory segmentsNetwork sockets (http, ftp, X windows, etc.)

Page 27: Processes, Schedulers, Threads

27S. Manolache, Process programming and operating systems, Processes, schedulers, threads

Outline

The concept of “process”Memory layout of a processProcess state and state transition diagramProcess Control Blocks

Context switchesOperations on processes (create, terminate, etc.)

ThreadsMotivation, user vs. kernelMulti-threading models, threading issues

CPU schedulingCriteria, algorithms

Page 28: Processes, Schedulers, Threads

28S. Manolache, Process programming and operating systems, Processes, schedulers, threads

Context Switch

Process A Process B

A running

B running

A running

Context switch

Context switch

Performancebottleneck

Page 29: Processes, Schedulers, Threads

29S. Manolache, Process programming and operating systems, Processes, schedulers, threads

Threads

Also known as lightweight processesThe do share the data segment

Do they share the stack segment?

Page 30: Processes, Schedulers, Threads

30S. Manolache, Process programming and operating systems, Processes, schedulers, threads

Single vs. Multi-Threaded Processes

code data files

stack registers

code data files

stack registers stack registers

Page 31: Processes, Schedulers, Threads

31S. Manolache, Process programming and operating systems, Processes, schedulers, threads

Advantages of Threads

Resource sharing (memory segments)Faster creation and destruction (30 times on Solaris 2)

application is much more responsiveFaster context switch (5 times on Solaris 2)

Page 32: Processes, Schedulers, Threads

32S. Manolache, Process programming and operating systems, Processes, schedulers, threads

User Threads and Kernel Threads

Kernel threads: threads that are visible by the OSThey are a scheduling unitThread creation, scheduling, management is done in kernel

space slightly slower than user threads If a kernel thread blocks (on I/O, for example), the kernel is

able to schedule a different kernel thread or process rather efficient

User threads: implemented by a thread library at the user levelThey are not a scheduling unitCreation, scheduling, management is done by the user

(library) faster than kernel threads If a user thread blocks, all user threads belonging to the

scheduling unit (encapsulating process) block

Page 33: Processes, Schedulers, Threads

33S. Manolache, Process programming and operating systems, Processes, schedulers, threads

Multi-Threading Models

Many-to-one

k

User threads

Page 34: Processes, Schedulers, Threads

34S. Manolache, Process programming and operating systems, Processes, schedulers, threads

Multi-Threading Models

One-to-one

k

User threads

k k

Page 35: Processes, Schedulers, Threads

35S. Manolache, Process programming and operating systems, Processes, schedulers, threads

Multi-Threading Models

Many-to-many

k

User threads

k

Page 36: Processes, Schedulers, Threads

36S. Manolache, Process programming and operating systems, Processes, schedulers, threads

Threading Issues

Fork and exec?Should the child process be multi-threaded, or should

only the calling thread be cloned in a new process?exec invoked by one thread replaces the entire process

Signals? Which thread should get the signal?

Page 37: Processes, Schedulers, Threads

37S. Manolache, Process programming and operating systems, Processes, schedulers, threads

Outline

The concept of “process”Memory layout of a processProcess state and state transition diagramProcess Control Blocks

Context switchesOperations on processes (create, terminate, etc.)

ThreadsMotivation, user vs. kernelMulti-threading models, threading issues

CPU schedulingCriteria, algorithms

Page 38: Processes, Schedulers, Threads

38S. Manolache, Process programming and operating systems, Processes, schedulers, threads

CPU Scheduling

Why scheduling?For using resources efficiently

I/O is very much slower than the CPU (CPUs run at billions of instructions per second, hard disk and network accesses take milliseconds)

When a process makes a I/O request, it has to wait. In this time, the CPU would idle if the OS did not schedule a ready process on it.

Page 39: Processes, Schedulers, Threads

39S. Manolache, Process programming and operating systems, Processes, schedulers, threads

Non-Preemptive vs. Preemptive

If a scheduling decision is taken only when a process terminates or moves to the waiting state because of the unavailability of a resource, the scheduling is non-preemptive (Windows 3.1, Apple Macintosh)

If a scheduling decision is taken also when a process becomes ready to execute (moves from waiting or running state to ready state), the scheduling is preemptive

Page 40: Processes, Schedulers, Threads

40S. Manolache, Process programming and operating systems, Processes, schedulers, threads

Non-Preemptive vs. Preemptive

Non-preemptive scheduling requires no hardware support (timer). The OS is also less complex.

Preemptive leads to shorter response times.However, operations by the kernel on some data have to

be performed atomically (i.e. without being preempted while in the middle of managing that data) in order to avoid data inconsistancies

A common Unix solution is preemptive scheduling of processes and non-preemptable system calls.

However, the problem persists because of interrupts from the hardware, which may not be ignored.

Either disable interrupts or, better, fine-grained locking

Page 41: Processes, Schedulers, Threads

41S. Manolache, Process programming and operating systems, Processes, schedulers, threads

Dispatcher

Once the new process to run is selected by the scheduler, the dispatcherStops the currently running process (if any)Switches contextSwitches to user modeJumps to the proper location in the user program to

restart it

The time it takes the dispatcher to do that is the dispatch latency

Page 42: Processes, Schedulers, Threads

42S. Manolache, Process programming and operating systems, Processes, schedulers, threads

Scheduling Criteria

CPU utilisation – keep it as busy as possibleThe load can be between 0 and 100%. ‘uptime’ in Unix

indicates the average number of ready processes in the ready queue. Therefore it may be > 1

Throughput – number of finished processes per time unitTurnaround time – length of the time interval between the

process submission time and finishing time of a processWaiting time – length of time spent waiting in the ready

queueResponse time – length of the time interval between the

process submission time and the production of the first results

Page 43: Processes, Schedulers, Threads

43S. Manolache, Process programming and operating systems, Processes, schedulers, threads

First-Come First-Served

Simple

Non-preemptiveNon-minimal waiting timesConvoy effect

Page 44: Processes, Schedulers, Threads

44S. Manolache, Process programming and operating systems, Processes, schedulers, threads

Shortest Job First

Optimal w.r.t. waiting time

How could we know the length of the next CPU burst?Take the user-specified maximum CPU time

Cannot be implemented as the short-term scheduling algorithm. We cannot know the length of the next CPU burst. We can predict it with various methods (see exponential average, textbook, section 6.3.2)

Page 45: Processes, Schedulers, Threads

45S. Manolache, Process programming and operating systems, Processes, schedulers, threads

Priority Scheduling

Processes are given priorities offline, not by the OS

More flexible in the sense that priorities capture aspects such as importance of the job, (financial) reward, etc.

Starvation – low priority processes never get to the CPUCan be countered by aging, i.e. slowly modifying the

priorities of processes that waited for a long time

Page 46: Processes, Schedulers, Threads

46S. Manolache, Process programming and operating systems, Processes, schedulers, threads

Round-Robin Scheduling

Used in time-sharing systemsEvery time quantum (10—100ms), a new processes from

the ready queue is dispatchedThe old one is put at the tail of the ready queue

If the time quantum is very small, we get processor sharing, i.e. each of the n processes have the impression that they run alone on an n times slower processor too many context switches

Average waiting time is rather long

Page 47: Processes, Schedulers, Threads

47S. Manolache, Process programming and operating systems, Processes, schedulers, threads

Multi-Level Queue Scheduling

Processes are assigned to different queues, based on some properties (interactive or not, memory size, etc.)

There is a scheduling policy between queues, and a scheduling policy for each queue

System processes

Interactive processes

Interactive editing processes

Batch processes

Student processes

High priority

Low priority

Page 48: Processes, Schedulers, Threads

48S. Manolache, Process programming and operating systems, Processes, schedulers, threads

Further Reading

Operations on processes (man pages, http://www.linuxhq.com/guides/LPG/node7.html)

Signals in Unix (man signal, man sigaction)Pthreads (man pthreads)Multi-processor scheduling (section 6.4)Real-time scheduling (section 6.5)

Page 49: Processes, Schedulers, Threads

49S. Manolache, Process programming and operating systems, Processes, schedulers, threads

Summary

Processes are executing programsKernel manages them, their state, associated data (open

files, address translation tables, register values, etc.)Threads are lightweight processes, i.e. they share data

segments, are cheaper to spawn and terminateScheduler selects next process to run among the ready

onesVarious scheduling algorithms and criteria to evaluate them