CS 134: Operating Systems Processes 1 / 25 Overview CS34 - - PowerPoint PPT Presentation

cs 134 operating systems
SMART_READER_LITE
LIVE PREVIEW

CS 134: Operating Systems Processes 1 / 25 Overview CS34 - - PowerPoint PPT Presentation

CS34 2013-05-17 CS 134: Operating Systems Processes CS 134: Operating Systems Processes 1 / 25 Overview CS34 Overview 2013-05-17 Processes Processes in Unix Implementation States Overview Threads Concepts Uses Models Design


slide-1
SLIDE 1

CS 134: Operating Systems

Processes

1 / 25

CS 134: Operating Systems

Processes

2013-05-17

CS34

slide-2
SLIDE 2

Overview

Processes Processes in Unix Implementation States Threads Concepts Uses Models Design

2 / 25

Overview

Processes Processes in Unix Implementation States Threads Concepts Uses Models Design

2013-05-17

CS34 Overview

slide-3
SLIDE 3

Processes

Processes & Concurrency

What is a process? What is concurrency?

3 / 25

Processes & Concurrency

What is a process? What is concurrency?

2013-05-17

CS34 Processes Processes & Concurrency

slide-4
SLIDE 4

Processes

User’s View of Processes

A fundamental OS abstraction

◮ But details vary from OS to OS:

◮ Batch system—Jobs ◮ Time-shared systems—User programs or tasks

◮ Common idea: Process = “A program in execution” ◮ Processes have a degree of independence from each other

◮ Possibly only allowed communicate through designated

mechanisms

◮ One errant processes should not affect other unrelated ones 4 / 25

User’s View of Processes

A fundamental OS abstraction

◮ But details vary from OS to OS: ◮ Batch system—Jobs ◮ Time-shared systems—User programs or tasks ◮ Common idea: Process = “A program in execution” ◮ Processes have a degree of independence from each other ◮ Possibly only allowed communicate through designated mechanisms ◮ One errant processes should not affect other unrelated ones

2013-05-17

CS34 Processes User’s View of Processes

slide-5
SLIDE 5

Processes

Class Exercise

What makes up a process? (“A process has. . . ”)

◮ In general ◮ On a typical POSIX system

5 / 25

Class Exercise

What makes up a process? (“A process has. . . ”)

◮ In general ◮ On a typical POSIX system

2013-05-17

CS34 Processes Class Exercise

slide-6
SLIDE 6

Processes Processes in Unix

Components of a Process (Unix)

◮ Execution state

◮ Registers ◮ Program counter ◮ Program status word ◮ Stack pointer

◮ Scheduling information

◮ Process state ◮ Priority ◮ Class, etc.

◮ Memory

◮ Text area ◮ Data area ◮ Stack area

◮ Security/Authentication Info

◮ User ID ◮ Group ID

◮ I/O State

◮ File descriptors ◮ Working directory ◮ Root directory

◮ Event Notifications

◮ Signals waiting ◮ Signal mask ◮ Time of next alarm

◮ Other

◮ Process ID ◮ Parent process ◮ Process group ◮ Controlling terminal ◮ Start time ◮ CPU time ◮ Children’s CPU time 6 / 25

Components of a Process (Unix)

◮ Execution state ◮ Registers ◮ Program counter ◮ Program status word ◮ Stack pointer ◮ Scheduling information ◮ Process state ◮ Priority ◮ Class, etc. ◮ Memory ◮ Text area ◮ Data area ◮ Stack area ◮ Security/Authentication Info ◮ User ID ◮ Group ID ◮ I/O State ◮ File descriptors ◮ Working directory ◮ Root directory ◮ Event Notifications ◮ Signals waiting ◮ Signal mask ◮ Time of next alarm ◮ Other ◮ Process ID ◮ Parent process ◮ Process group ◮ Controlling terminal ◮ Start time ◮ CPU time ◮ Children’s CPU time

2013-05-17

CS34 Processes Processes in Unix Components of a Process (Unix)

slide-7
SLIDE 7

Processes Processes in Unix

Processes under UNIX

Processes:

◮ Create with fork ◮ Exit with exit ◮ Replace “process image” with execve

Multiple processes may be active at any one time (compare w/ uniprogrammed system)

7 / 25

Processes under UNIX

Processes:

◮ Create with fork ◮ Exit with exit ◮ Replace “process image” with execve

Multiple processes may be active at any one time (compare w/ uniprogrammed system)

2013-05-17

CS34 Processes Processes in Unix Processes under UNIX

slide-8
SLIDE 8

Processes Processes in Unix

Class Question

If there’s fork, should we have join?

8 / 25

Class Question

If there’s fork, should we have join?

2013-05-17

CS34 Processes Processes in Unix Class Question

slide-9
SLIDE 9

Processes Processes in Unix

Processes under UNIX

The environment you interact with is made up of processes

root init mach_init syslogd kextd configd diskarbitrationd notifyd netinfod update dynamic_pager SystemStarter TabletDriverRelauncher KernelEventAgent nobody mDNSResponder cron coreservicesd distnoted lookupd crashreporterd SecurityServer cupsd loginwindow nfsiod TabletDriver rpc.lockd xinetd ntpd automount automount ATSServer WindowServer DirectoryService melissa pbs Dock SystemUIServer Finder System MouseWorks UniversalAccess Safari Terminal TeXShop AppleSpell Keynote Preview TextEdit p1 login

  • tcsh

man sh sh less p2 login

  • tcsh

ssh p3 login

  • tcsh

ps p4 login

  • tcsh

p5 login

  • tcsh

p6 login

  • tcsh

9 / 25

Processes under UNIX

The environment you interact with is made up of processes

root init mach_init syslogd kextd configd diskarbitrationd notifyd netinfod update dynamic_pager SystemStarter TabletDriverRelauncher KernelEventAgent nobody mDNSResponder cron coreservicesd distnoted lookupd crashreporterd SecurityServer cupsd loginwindow nfsiod TabletDriver rpc.lockd xinetd ntpd automount automount ATSServer WindowServer DirectoryService melissa pbs Dock SystemUIServer Finder System MouseWorks UniversalAccess Safari Terminal TeXShop AppleSpell Keynote Preview TextEdit p1 login
  • tcsh
man sh sh less p2 login
  • tcsh
ssh p3 login
  • tcsh
ps p4 login
  • tcsh
p5 login
  • tcsh
p6 login
  • tcsh

2013-05-17

CS34 Processes Processes in Unix Processes under UNIX

slide-10
SLIDE 10

Processes Implementation

Process Implementation

How does the OS implement the process abstraction?

10 / 25

Process Implementation

How does the OS implement the process abstraction?

2013-05-17

CS34 Processes Implementation Process Implementation

slide-11
SLIDE 11

Processes Implementation

Process Implementation (cont’d)

The OS needs to maintain a process image for each process:

◮ Process’s address space, containing:

◮ Program code ◮ Program data ◮ Processor stack

◮ Housekeeping information (PCB)

◮ One of most important is process state 11 / 25

Process Implementation (cont’d)

The OS needs to maintain a process image for each process:

◮ Process’s address space, containing: ◮ Program code ◮ Program data ◮ Processor stack ◮ Housekeeping information (PCB) ◮ One of most important is process state

2013-05-17

CS34 Processes Implementation Process Implementation (cont’d)

slide-12
SLIDE 12

Processes States

A Two-State Process Model

Simplest model for processes:

Not Running Running

12 / 25

A Two-State Process Model

Simplest model for processes:

Not Running Running

2013-05-17

CS34 Processes States A Two-State Process Model

slide-13
SLIDE 13

Processes States

A Four-State Process Model

More useful model for processes: Ready Running Blocked Finished

13 / 25

A Four-State Process Model

More useful model for processes: Ready Running Blocked Finished

2013-05-17

CS34 Processes States A Four-State Process Model

slide-14
SLIDE 14

Processes States

A Five-State Process Model

Five states can model additional needs of batch systems:

Ready Running Blocked Finished New

Scheduler queues:

◮ Ready queue: Processes ready and waiting to execute. ◮ New queue: Processes waiting to be created

14 / 25

A Five-State Process Model

Five states can model additional needs of batch systems:

Ready Running Blocked Finished New

Scheduler queues:

◮ Ready queue: Processes ready and waiting to execute. ◮ New queue: Processes waiting to be created

2013-05-17

CS34 Processes States A Five-State Process Model

slide-15
SLIDE 15

Threads Concepts

Generalizing Processes

Simple view of process is Address space + Thread of execution Does the mapping need to be one-to-one?

15 / 25

Generalizing Processes

Simple view of process is Address space + Thread of execution Does the mapping need to be one-to-one?

2013-05-17

CS34 Threads Concepts Generalizing Processes

slide-16
SLIDE 16

Threads Concepts

Possible Mappings

  • ne process
  • ne thread
  • ne process

multiple threads

  • multiple processes
  • ne thread per process
  • multiple processes

multiple threads per process

  • 16 / 25

Possible Mappings

  • ne process
  • ne thread
  • ne process

multiple threads

  • multiple processes
  • ne thread per process
  • multiple processes

multiple threads per process

  • 2013-05-17

CS34 Threads Concepts Possible Mappings

slide-17
SLIDE 17

Threads Concepts

Threads

Motivation:

◮ Traditional processes: Virtual uniprocessor machine ◮ Multithreaded processes: Virtual multiprocessor machine

17 / 25

Threads

Motivation:

◮ Traditional processes: Virtual uniprocessor machine ◮ Multithreaded processes: Virtual multiprocessor machine

2013-05-17

CS34 Threads Concepts Threads

slide-18
SLIDE 18

Threads Uses

Uses of Threads

Various reasons why people use threads

◮ Performing foreground and background work ◮ Supporting asynchronous processing ◮ Speeding execution ◮ Organizing programs

18 / 25

Uses of Threads

Various reasons why people use threads

◮ Performing foreground and background work ◮ Supporting asynchronous processing ◮ Speeding execution ◮ Organizing programs

2013-05-17

CS34 Threads Uses Uses of Threads

slide-19
SLIDE 19

Threads Uses

Uses of Threads—Example

Dispatcher thread Worker thread Web page cache Kernel Network connection Web server process User space Kernel space

/* Dispatcher Thread */ for ( ; ; ) { url = get_next_request(); handoff_work(url); } /* Worker Thread */ \\ for ( ; ; ) { url = wait_for_work(); page = look_in_cache(url); if (page == NULL) page = generate_page(url); send_page(page); }

19 / 25

Uses of Threads—Example

Dispatcher thread Worker thread Web page cache Kernel Network connection Web server process User space Kernel space

/* Dispatcher Thread */ for ( ; ; ) { url = get_next_request(); handoff_work(url); } /* Worker Thread */ \\ for ( ; ; ) { url = wait_for_work(); page = look_in_cache(url); if (page == NULL) page = generate_page(url); send_page(page); }

2013-05-17

CS34 Threads Uses Uses of Threads—Example

slide-20
SLIDE 20

Threads Uses

Class Exercise

Can an application implement threads without built-in thread support in the OS? If so, what does it need from the from the OS to support threads?

20 / 25

Class Exercise

Can an application implement threads without built-in thread support in the OS? If so, what does it need from the from the OS to support threads?

2013-05-17

CS34 Threads Uses Class Exercise

slide-21
SLIDE 21

Threads Models

Model for User Threads

P

  • User

Space

  • Threads

Library

  • Kernel

Space

  • Pure user-level
  • Key:

P

  • User-level thread
  • Kernel-level thread
  • Process
  • Class Exercise

What are the pros and cons of this approach?

21 / 25

Model for User Threads

P

  • User
Space
  • Threads
Library
  • Kernel
Space
  • Pure user-level
  • Key:

P

  • User-level thread
  • Kernel-level thread
  • Process
  • Class Exercise

What are the pros and cons of this approach?

2013-05-17

CS34 Threads Models Model for User Threads

So, maybe we should put the threads in the kernel?

slide-22
SLIDE 22

Threads Models

Model for User Threads

P

  • User

Space

  • Threads

Library

  • Kernel

Space

  • Pure user-level
  • Key:

P

  • User-level thread
  • Kernel-level thread
  • Process
  • + No kernel overhead for thread library calls

+ Own scheduler = Application-specific scheduling policy? − I/O issues − Can’t (easily) take advantage of multiprocessing

21 / 25

Model for User Threads

P

  • User
Space
  • Threads
Library
  • Kernel
Space
  • Pure user-level
  • Key:

P

  • User-level thread
  • Kernel-level thread
  • Process
  • + No kernel overhead for thread library calls

+ Own scheduler = Application-specific scheduling policy? − I/O issues − Can’t (easily) take advantage of multiprocessing

2013-05-17

CS34 Threads Models Model for User Threads

So, maybe we should put the threads in the kernel?

slide-23
SLIDE 23

Threads Models

Model for Kernel-Level Threads

P

  • User

Space

  • Kernel

Space

  • Pure kernel-level
  • Key:

P

  • User-level thread
  • Kernel-level thread
  • Process
  • Class Exercise

What are the pros and cons of this approach?

22 / 25

Model for Kernel-Level Threads

P
  • User
Space
  • Kernel
Space
  • Pure kernel-level
  • Key:
P
  • User-level thread
  • Kernel-level thread
  • Process
  • Class Exercise

What are the pros and cons of this approach?

2013-05-17

CS34 Threads Models Model for Kernel-Level Threads

slide-24
SLIDE 24

Threads Models

Model for Kernel-Level Threads

P

  • User

Space

  • Kernel

Space

  • Pure kernel-level
  • Key:

P

  • User-level thread
  • Kernel-level thread
  • Process
  • Now we have kernel overheads:

◮ Kernel data structures ◮ Mode switch to kernel

22 / 25

Model for Kernel-Level Threads

P
  • User
Space
  • Kernel
Space
  • Pure kernel-level
  • Key:
P
  • User-level thread
  • Kernel-level thread
  • Process
  • Now we have kernel overheads:

◮ Kernel data structures ◮ Mode switch to kernel

2013-05-17

CS34 Threads Models Model for Kernel-Level Threads

slide-25
SLIDE 25

Threads Models

Hybrid Thread Schemes

P

  • P
  • User

Space

  • Threads

Library

  • Kernel

Space

  • Combined
  • Key:

P

  • User-level thread
  • Kernel-level thread
  • Process
  • Class Exercise

What are the pros and cons of this approach?

23 / 25

Hybrid Thread Schemes

P

  • P
  • User
Space
  • Threads
Library
  • Kernel
Space
  • Combined
  • Key:

P

  • User-level thread
  • Kernel-level thread
  • Process
  • Class Exercise

What are the pros and cons of this approach?

2013-05-17

CS34 Threads Models Hybrid Thread Schemes

slide-26
SLIDE 26

Threads Models

Traditional vs. Multithreaded Processes

Single-Threaded Process Model

  • Process

Control Block

  • User

Address Space

  • User

Stack

  • Kernel

Stack

  • 24 / 25

Traditional vs. Multithreaded Processes

Single-Threaded Process Model

  • Process

Control Block

  • User

Address Space

  • User

Stack

  • Kernel

Stack

  • 2013-05-17

CS34 Threads Models Traditional vs. Multithreaded Processes

slide-27
SLIDE 27

Threads Models

Traditional vs. Multithreaded Processes

Single-Threaded Process Model

  • Process

Control Block

  • User

Address Space

  • User

Stack

  • Kernel

Stack

  • Multithreaded

Process Model

  • Process

Control Block

  • User

Address Space

  • User

Stack

  • Kernel

Stack

  • User

Stack

  • Kernel

Stack

  • User

Stack

  • Kernel

Stack

  • Thread

Control Block

  • Thread
  • Thread
  • Thread
  • Thread

Control Block

  • Thread

Control Block

  • Class Question

But what’s per-process and what’s per-thread?

24 / 25

Traditional vs. Multithreaded Processes

Single-Threaded Process Model

  • Process

Control Block

  • User

Address Space

  • User

Stack

  • Kernel

Stack

  • Multithreaded

Process Model

  • Process
Control Block
  • User
Address Space
  • User
Stack
  • Kernel
Stack
  • User
Stack
  • Kernel
Stack
  • User
Stack
  • Kernel
Stack
  • Thread
Control Block
  • Thread
  • Thread
  • Thread
  • Thread
Control Block
  • Thread
Control Block
  • Class Question

But what’s per-process and what’s per-thread?

2013-05-17

CS34 Threads Models Traditional vs. Multithreaded Processes

slide-28
SLIDE 28

Threads Design

Per-Process vs. Per-Thread—You Decide. . .

◮ Execution state

◮ Registers ◮ Program counter ◮ Program status word ◮ Stack pointer

◮ Scheduling information

◮ Process state ◮ Priority ◮ Class, etc.

◮ Memory

◮ Text area ◮ Data area ◮ Stack area

◮ Security/Authentication Info

◮ User ID ◮ Group ID

◮ I/O State

◮ File descriptors ◮ Working directory ◮ Root directory

◮ Event Notifications

◮ Signals waiting ◮ Signal mask ◮ Time of next alarm

◮ Other

◮ Process ID ◮ Parent process ◮ Process group ◮ Controlling terminal ◮ Start time ◮ CPU time ◮ Children’s CPU time 25 / 25

Per-Process vs. Per-Thread—You Decide. . .

◮ Execution state ◮ Registers ◮ Program counter ◮ Program status word ◮ Stack pointer ◮ Scheduling information ◮ Process state ◮ Priority ◮ Class, etc. ◮ Memory ◮ Text area ◮ Data area ◮ Stack area ◮ Security/Authentication Info ◮ User ID ◮ Group ID ◮ I/O State ◮ File descriptors ◮ Working directory ◮ Root directory ◮ Event Notifications ◮ Signals waiting ◮ Signal mask ◮ Time of next alarm ◮ Other ◮ Process ID ◮ Parent process ◮ Process group ◮ Controlling terminal ◮ Start time ◮ CPU time ◮ Children’s CPU time

2013-05-17

CS34 Threads Design Per-Process vs. Per-Thread—You Decide. . .