43
K. Salah Operating Systems 1 Module 2.0: Processes and Threads Process Concept Trace of Processes Process Context Context Switching Threads ULT KLT

Module 2.0: Processes and Threads

  • Upload
    chione

  • View
    42

  • Download
    0

Embed Size (px)

DESCRIPTION

Module 2.0: Processes and Threads. Process Concept Trace of Processes Process Context Context Switching Threads ULT KLT. Process. Also called a task. Useful and Important Concept: Process = program in execution - PowerPoint PPT Presentation

Citation preview

Page 1: Module 2.0:  Processes and Threads

K. Salah Operating Systems 1

Module 2.0: Processes and Threads

• Process Concept

• Trace of Processes

• Process Context

• Context Switching

• Threads

– ULT

– KLT

Page 2: Module 2.0:  Processes and Threads

K. Salah Operating Systems 2

Process

• Also called a task.

• Useful and Important Concept:Process = program in execution

• A process is not the same as a program. Program is a passive entity, whereas process is active. Process consists of an executable program, associated data, and execution context.

• Modern (multiprogramming) operating systems are structured around the concept of a process.

• Multiprogramming OS supports execution of many concurrent processes. OS issues tend to revolve around management of processes:

– How are processes created/destroyed?– How to manage resource requirements of a process during its execution:

cpu time, memory, I/O, communication, ... ?– How to avoid interference between processes?– How to achieve cooperation and communication between processes?

Page 3: Module 2.0:  Processes and Threads

K. Salah Operating Systems 3

Program Creation

• Program (say, C program) is edited

• It is compiled into assembly language, which may consist of several modules.

• Assembly language modules are assembled into machine language.

• External references (i.e., to procedures and data in another module) are resolved. This is called linking, which creates a load module.

• Load or image module is stored as a file in file system and may be executed at a later time by loading into memory to be executed.

Page 4: Module 2.0:  Processes and Threads

K. Salah Operating Systems 4

Process creation and termination

• Consider a simple disk operating system (like MS-DOS, typically supports only one process at a time)

• User types command like “run foo” at Keyboard (I/O device driver for keyboard, screen)

• Command is parsed by command shell

• Executable program file (load module) “foo” is located on disk (file system, I/O device driver for disk)

• Contents are loaded into memory and control transferred ==> process comes alive! (device driver for disk, relocating loader, memory management)

• During execution, process may call OS to perform I/O: console, disk, printer, etc. (system call interface, I/O device drivers)

• When process terminates, memory is reclaimed. (memory management)

Page 5: Module 2.0:  Processes and Threads

K. Salah Operating Systems 5

Two Types

• Processes can be described as either:

– I/O-bound process – spends more time doing I/O than computations many short CPU bursts Long I/O burst Ex: vi

– CPU-bound process spends more time doing computations Heavy number crunching few very long CPU bursts Ex: simulation

Page 6: Module 2.0:  Processes and Threads

K. Salah Operating Systems 6

Trace of Processes

Page 7: Module 2.0:  Processes and Threads

K. Salah Operating Systems 7

Trace of processes (cont.)

Page 8: Module 2.0:  Processes and Threads

K. Salah Operating Systems 8

Trace of processes (cont.)

Page 9: Module 2.0:  Processes and Threads

K. Salah Operating Systems 9

Page 10: Module 2.0:  Processes and Threads

K. Salah Operating Systems 10

Example

If parent chooses to wait until the child executes (but not always the

case).

Page 11: Module 2.0:  Processes and Threads

K. Salah Operating Systems 11

Multiprogramming/Timesharing Systems

• They provide interleaved execution of several processes to give an illusion of many simultaneously executing processes.

• Computers can be a single-processor or multi-processor machine.

• The OS must keep track of the state for each active process and make sure that the correct information is properly installed when a process is given control of the CPU.

Page 12: Module 2.0:  Processes and Threads

K. Salah Operating Systems 12

Multiprogramming (multiple processes)

• For each process, the O.S. maintains a data structure, called the process control block (PCB). The PCB provides a way of accessing all information relevant to a process:

– This data is either contained directly in the PCB, or else the PCB contains pointers to other system tables.

• Processes (PCBs) are manipulated by two main components of the process subsystem in order to achieve the effects of multiprogramming:

– Scheduler: determines the order by which processes will gain access to the CPU. Efficiency and fair-play are issues here.

– Dispatcher: actually allocates CPU to process next in line as determined by the scheduler.

Page 13: Module 2.0:  Processes and Threads

K. Salah Operating Systems 13

Process Context

• The context (or image) of a process can be described by– contents of main memory– contents of CPU registers– other info (open files, I/O in progress, etc.)

• Main memory -- three logically distinct regions of memory:– code region: contains executable code (typically read-only)– data region: storage area for dynamically allocated data structure,

e.g., lists, trees (typically heap data structure) – stack region: run-time stack of activation records

• CPU registers: general registers, PC, SP, PSW, segmentation registers

• Other info:– open files table, status of ongoing I/O– process status (running, ready, blocked), user id, ...

Page 14: Module 2.0:  Processes and Threads

K. Salah Operating Systems 14

The Process Control Block (PCB)

• The PCB typically contains the following types of information:

• Process status (or state): new, ready to run, user running, kernel running, waiting, halted

– Program counter: where in program the process is executing

– CPU registers: contents of general-purpose register stack pointer, PSW, index registers

– Memory Management info: segment base and limit registers, page table, location of pages on disk, process size

– User ID, Group ID, Process ID, Parent PID, ...– Event Descriptor: when process is in the

“sleep” or waiting state– Scheduling info: process priority, size of CPU

quantum, length of current CPU burst

Page 15: Module 2.0:  Processes and Threads

K. Salah Operating Systems 15

PCB (cont.)

– List of pending signals

– Accounting info: process execution time, resource utilization

– Real and Effective User IDs: determine various privileges allowed the process such as file access rights

– More timers: record time process has spent executing in user and Kernel mode

– Array indicating how process wishes to react to signals

– System call info: arguments, return value, error field for current system call

– Pending I/O operation info: amount of data to transfer, addr in user memory, file offset, ...

– Current directory and root: file system environment of process

– Open file table: records files process has open

Page 16: Module 2.0:  Processes and Threads

K. Salah Operating Systems 16

Process States & Transitions

• Running

– User-running

– Kernel-running

• Ready

– Ready, suspend

– Ready

• Waiting (or blocked)

– Blocked

– Blocked, suspend

Zooming in

Suspend may swap out all or part of the process. Shared regions/segments

are not suspended.

Page 17: Module 2.0:  Processes and Threads

K. Salah Operating Systems 17

How queues are implemented?

Page 18: Module 2.0:  Processes and Threads

K. Salah Operating Systems 18

When to context switch

• Typically, hardware automatically saves the user PC and PSW when interrupt occurs, and takes new PC from interrupt vector.

• Interrupt handler may simply perform its function and then return to the same process that was interrupted (restoring the PC and PSW from the stack).

• Alternatively, may no longer be appropriate to resume execution of process that was running because: process has used up its current CPU quantum process has requested I/O and must wait for results process has asked to be suspended (sleep) for some amount of time a signal or error requires process be destroyed (killed) a “higher priority” process should be given the CPU

E.g., pressing ctrl-alt-delete

• In such a situation, a context switch is performed to install appropriate info for running a new process.

Page 19: Module 2.0:  Processes and Threads

K. Salah Operating Systems 19

Mechanics of a Context Switch

1 copy contents of CPU registers (general-purpose, SP, PC, PSW, etc.) into a save area in the PCB of running process

2 change status of running process from “running” to “waiting” (or “ready”)3 change a system variable running-process to point to the PCB of new

process to run4 copy info from register save area in PCB of new process into CPU registers

• Note: context switching is pure overhead and should be done as fast as possible often hardware-assisted - special instructions for steps 1 and 4 determining new process to run accomplished by consulting scheduler

queues step 4 will start execution of new process - known as dispatching.

Page 20: Module 2.0:  Processes and Threads

K. Salah Operating Systems 20

MULTIPROGRAMMING Through CONTEXT SWITCHING

Page 21: Module 2.0:  Processes and Threads

K. Salah Operating Systems 21

Process Creation

• Parent process creates children processes, which, in turn create other processes, forming a tree of processes

• Resource sharing models/types:– Parent and children share all resources– Children share subset of parent’s resources– Parent and child share no resources

• Execution– Parent and children execute concurrently– Parent waits until children terminate

• UNIX examples– fork system call creates new process– exec system call used after a fork to replace the process’

memory space with a new program

Page 22: Module 2.0:  Processes and Threads

K. Salah Operating Systems 22

C Program Forking Separate Process

int main()

{

Pid_t pid;

/* fork another process */

pid = fork();

if (pid < 0) { /* error occurred */

fprintf(stderr, "Fork Failed");

exit(-1);

}

else if (pid == 0) { /* child process */

execlp("/bin/ls", "ls", NULL);

}

else { /* parent process */

/* parent will wait for the child to complete */

wait (NULL);

printf ("Child Complete");

exit(0);

}

}

Page 23: Module 2.0:  Processes and Threads

K. Salah Operating Systems 23

A tree of processes on a typical Solaris

Page 24: Module 2.0:  Processes and Threads

K. Salah Operating Systems 24

Introduction to Threads

Parallelism at different levels Granularity of parallelism at processor level

Coarse-grain – processes Fine-grain -- threads

Multitasking OS can do more than one thing concurrently by running more than a single process

Processes can do several things concurrently be running more than a single thread.

Each thread is a different stream of control that can execute its instructions independently.A program (e.g. Browser) may consist of the following threads:

GUI thread I/O thread computation

Page 25: Module 2.0:  Processes and Threads

K. Salah Operating Systems 25

Processes and Threads

A typical process consists of a running program a bundle of resources (file descriptor table, address space)

A thread, called a lightweight process, has its own stack CPU Registers state All the other resources are shared by all threads of that process.

These include: open files virtual address space (code and data segments). child processes

Page 26: Module 2.0:  Processes and Threads

K. Salah Operating Systems 26

Processes vs. Threads

Page 27: Module 2.0:  Processes and Threads

K. Salah Operating Systems 27

Single and Multithreaded Processes

Page 28: Module 2.0:  Processes and Threads

K. Salah Operating Systems 28

Single Threaded and Multithreaded Process Models

Thread Control Block contains a register image, thread priority and thread state information.

Page 29: Module 2.0:  Processes and Threads

K. Salah Operating Systems 29

Benefits of Threads vs Processes

Takes less time to create a new thread than a process

Less time to terminate a thread than a process

Less time to switch between two threads within the same process

Since threads within the same process share memory and files, they can communicate with each other without invoking the kernel. However, it is necessary to synchronize the activities of various threads so that they do not obtain inconsistent views of the data.

Page 30: Module 2.0:  Processes and Threads

K. Salah Operating Systems 30

Example I: Web Browser

Page 31: Module 2.0:  Processes and Threads

K. Salah Operating Systems 31

Example II: Web Server

Page 32: Module 2.0:  Processes and Threads

K. Salah Operating Systems 32

Threads States

Three key states: running, ready, blocked

Generally, it does not make sense to have a suspend state for threads.

Because all threads within the same process share the same address space

Indeed: suspending (ie: swapping) a single thread involves suspending all threads of the same process

Termination of a process, terminates all threads within the process

Page 33: Module 2.0:  Processes and Threads

K. Salah Operating Systems 33

User-Level Threads (ULT)

• The kernel is not aware of the existence of threads

• All thread management is done by the application by using a thread library

• Thread switching does not require kernel mode privileges (no mode switch)

• Scheduling is application specific

Page 34: Module 2.0:  Processes and Threads

K. Salah Operating Systems 34

Threads library

• Contains code for:– creating and destroying threads– passing messages and data between threads– scheduling thread execution– saving and restoring thread contexts

• Three primary thread libraries:– POSIX Pthreads. The P stands for POSIX and run on unix, linux,

and MS Windows.– Cthreads– Win32 threads– Java threads

Page 35: Module 2.0:  Processes and Threads

K. Salah Operating Systems 35

Kernel activity for ULTs

• The kernel is not aware of thread activity but it is still managing process activity

• When a thread makes a system call, the whole process will be blocked

• but for the thread library that thread is still in the running state

• So thread states are independent of process states

Page 36: Module 2.0:  Processes and Threads

K. Salah Operating Systems 36

Advantages and inconveniences of ULT

• Advantages– Thread switching does not involve

the kernel: no mode switching thread_yield()

– Strong sharing of data with little blocking

No need for shared memory system calls

Excel sheets share a lot other than files

– Scheduling can be application specific: choose the best algorithm.

Run a garbage collection thread at convenient points

– ULTs can run on any OS. Only needs a thread library

Portable

• Inconveniences– Most system calls are blocking

and the kernel blocks processes. So all threads within the process will be unable to run

– The kernel can only assign processes to processors. Two threads within the same process cannot run simultaneously on two processors

Page 37: Module 2.0:  Processes and Threads

K. Salah Operating Systems 37

Improving blocking with ULT -- Advanced

• Use nonblocking I/O system calls

– Returns quickly without need to complete the full I/O operation

• Use asynchronous I/O system calls

– Setup a callback function and returns quick

– When I/O is completed a function is called (part of signal handling)

• Identify blocking system calls, and place a jacket or wrapper around them

– Needs to modify API or system call library

– If we know it will block, defer the thread and let other threads run first

Page 38: Module 2.0:  Processes and Threads

K. Salah Operating Systems 38

Kernel-Level Threads (KLT)

• All thread management is done by kernel

• No thread library but an API (I.e. system calls) to the kernel thread facility

• Kernel maintains context information for the process and the threads

• Switching between threads requires the kernel

• Scheduling on a thread basis

• Examples– Windows XP/2000– Solaris– Linux– Tru64 UNIX– Mac OS X

Page 39: Module 2.0:  Processes and Threads

K. Salah Operating Systems 39

Kernel Multithreading Models

• Many-to-One

• One-to-One

• Many-to-Many

Page 40: Module 2.0:  Processes and Threads

K. Salah Operating Systems 40

Advantages and inconveniences of KLT

• Advantages– the kernel can simultaneously

schedule many threads of the same process on many processors

– blocking is done on a thread level

– kernel routines can be multithreaded

• Inconveniences– thread switching within the

same process involves the kernel. We have 2 mode switches per thread switch: user to kernel and kernel to user.

– this results in a significant slow down due to:

Interrupt overhead due to mode switch

Updates to TCB info Cache pollution and

flushing to Process tables and page tables

Page 41: Module 2.0:  Processes and Threads

K. Salah Operating Systems 41

Combined ULT/KLT Approaches

• Thread creation done in the user space

• Bulk of scheduling and synchronization of threads done in the user space

• The programmer may adjust the number of KLTs

• May combine the best of both approaches

• Examples:– Solaris prior to version 9– Windows NT/2000 with the

ThreadFiber package

Page 42: Module 2.0:  Processes and Threads

K. Salah Operating Systems 42

Solaris: versatility

• We can use ULTs when logical parallelism does not need to be supported by hardware parallelism (we save mode switching)– Ex: Multiple windows but only one is active at any one time

– Excel sheet (sheet1, sheet2, etc)– Power point– Word processor

It is wise to have 2 KLTs under this situation. So if one window is blocked when making a system call, use the other KLT to run the other selected window). If the windows are doing a lot of blocking, use more KLTs.

– Reason is efficiency ULTs can be created, blocked, destroyed, without involving

the kernel Efficiency in terms of memory and data structure allocated in

kernel space Minimizing cache pollution and flushing

• If threads may block then we can specify two or more LWPs (or KLTs) to avoid blocking the whole application

Page 43: Module 2.0:  Processes and Threads

K. Salah Operating Systems 43

Further Readings

• What is the difference between RPC and RMI?

• What is meant by marshalling parameters?

• What is the idea behind a thread pool?

• What is hyperthreading?

• Answer this:– If you have CPU –bound application, when does it make sense to

use ULTs for them as opposed to KLTs? Example is a parallel array computation where you divide the

rows of its arrays among different threads– Answer:

use ULTs to minimize switching with uniprocessor Use KLTs for more concurrency with SMP