Processes & Threads CS 4410, Opera5ng Systems Fall 2016 - - PowerPoint PPT Presentation

processes threads
SMART_READER_LITE
LIVE PREVIEW

Processes & Threads CS 4410, Opera5ng Systems Fall 2016 - - PowerPoint PPT Presentation

Processes & Threads CS 4410, Opera5ng Systems Fall 2016 Cornell University Rachit Agarwal Anne Bracy See: Ch 3&4 in OSPP textbook The slides are the product of many rounds of teaching CS 4410 by Professors Sirer, Bracy, Agarwal,


slide-1
SLIDE 1

Processes & Threads

CS 4410, Opera5ng Systems

Fall 2016 Cornell University

Rachit Agarwal Anne Bracy

See: Ch 3&4 in OSPP textbook

The slides are the product of many rounds of teaching CS 4410 by Professors Sirer, Bracy, Agarwal, George, and Van Renesse. Some content from Markus Püschel at CMU.

slide-2
SLIDE 2

What is a Process?

2

  • An instance of a program
  • An abstrac5on of a computer:

Address Space + Execu5on Context + Environment A good abstracOon:

  • is portable and hides implementa5on details
  • has an intui5ve and easy-to-use interface
  • can be instan5ated many 5mes
  • is efficient and reasonably easy to implement
slide-3
SLIDE 3

Process Management

3

Can a program…

  • Create an instance of another program?
  • Wait for it to complete?
  • Stop or resume another running program?
  • Send it an asynchronous event?
slide-4
SLIDE 4

Who should be allowed to start a process?

4

Possibility #1: Only the kernel may start a process Possibility #2: User-level processes may start processes

slide-5
SLIDE 5

System Call Interface

5

System Call Interface Portable Operating System Kernel Portable OS Library Web Servers Compilers Source Code Control Web Browsers Email Databases Word Processing x86 ARM PowerPC 10Mbps/100Mbps/1Gbps Ethernet 802.11 a/b/g/n SCSI IDE Graphics Accelerators LCD Screens

Why so skinny? Example: Crea%ng a Process Windows: CreateProcess(…); UNIX fork + exec

slide-6
SLIDE 6

Beginning a Process via CreateProcess

6

Kernel has to:

  • Create & ini5alize PCB in the kernel
  • Create and ini5alize a new address space
  • Load the program into the address space
  • Copy arguments into memory in address space
  • Ini5alize hw context to start execu5on at “start”
  • Inform scheduler that new process is ready to run

[Windows]

slide-7
SLIDE 7

Abstract Life of a Process

7

New Runnable Running Zombie Waiting

admiSed done I/O OperaOon I/O CompleOon dispatch interrupt, descheduling

Details on Thursday.

slide-8
SLIDE 8

CreateProcess (Simplified)

8

System Call

if (!CreateProcess( NULL, // No module name (use command line) argv[1], // Command line NULL, // Process handle not inheritable NULL, // Thread handle not inheritable FALSE, // Set handle inheritance to FALSE 0, // No creation flags NULL, // Use parent's environment block NULL, // Use parent's starting directory &si, // Pointer to STARTUPINFO structure &pi ) // Ptr to PROCESS_INFORMATION structure )

[Windows]

slide-9
SLIDE 9

Fork + Exec

9

pid = fork(); if (pid==0) exec(B); else wait(pid); PC pid ? Program A pid = fork(); if (pid==0) exec(B); else wait(pid); PC pid Program A pid = fork(); if (pid==0) exec(B); else wait(pid); PC pid 42 Program A main() { ... } PC pid Program B

[UNIX]

Process 1

if and else both executed!

Process 1 Process 42 Process 42

slide-10
SLIDE 10

Beginning a Process via CreateProcess Fork

10

Kernel has to:

  • Create & ini5alize PCB in the kernel
  • Create and ini5alize a new address space
  • Load the program into the address space
  • Copy arguments into memory in address space
  • IniOalize the address space with a copy of the

enOre contents of the address space of the parent

  • Ini5alize hw context to start execu5on at “start”
  • Inherit execuOon context of parent (e.g. open files)
  • Inform scheduler that new process is ready to run

[UNIX]

slide-11
SLIDE 11

Code example

11

/* * Corresponds to Figure 3.5 in the textbook * */ #include <stdio.h> #include <unistd.h> int main() { int child_pid = fork(); if (child_pid == 0) { // child process printf("I am process #%d\n", getpid()); return 0; } else { // parent process. printf("I am the parent of process #%d\n", child_pid); return 0; } }

Possible outputs?

slide-12
SLIDE 12

CreaOng and Managing Processes

12

[UNIX]

fork

Create a child process as a clone of the current process. Returns to both parent and child.

exec (prog, args)

Run the application prog in the current process.

exit

Tell the kernel the current process is complete, and its data structures (stack, heap, code) should be garbage collected. Why not necessarily PCB?

wait(pid)

Pause until the child process has exited.

kill (pid, type)

Send an interrupt of a specified type to a process.

slide-13
SLIDE 13

QuesOons

13

  • Can UNIX fork() return an error? Why?
  • Can UNIX exec() return an error? Why?
  • Can UNIX wait() ever return immediately? Why?
slide-14
SLIDE 14

What is a Shell?

14

Job control system

  • runs programs on behalf of the user
  • allows programmer to create/manage set of programs
  • sh Original Unix shell (Stephen Bourne,

AT&T Bell Labs, 1977)

  • csh BSD Unix C shell (tcsh: enhanced csh

at CMU and elsewhere)

  • bash “Bourne-Again” Shell

Runs at user-level. What system calls does it use?

slide-15
SLIDE 15

Built-In UNIX Shell Commands

15

[UNIX]

jobs

List all jobs running in the background + all stopped jobs.

bg <job>

Run the application prog in the current process.

fg <job>

Change a stopped or running background job to a running in the foreground.

kill <job>

Terminate a job.

show in acOon, + exec

slide-16
SLIDE 16

Signals

16

[UNIX]

ID

Name Default Action Corresponding Event

2

SIGINT Terminate Interrupt (e.g., ctrl-c from keyboard)

9

SIGKILL Terminate Kill program (cannot override or ignore)

14

SIGALRM Terminate Timer signal

17

SIGCHLD Ignore Child stopped or terminated

20

SIGTSTP Stop until next SIGCONT Stop signal from terminal (e.g. ctrl-z from keyboard)

A virtualized interrupt. Allow applica5ons to behave like opera5ng systems. youtube?

slide-17
SLIDE 17

Sending a Signal

17

Kernel delivers a signal to a des5na5on process For one of the following reasons:

  • Kernel detected a system event (e.g. div-by-zero

(SIGFPE) or termina5on of a child (SIGCHLD))

  • A process invoked the kill system call reques5ng

kernel to send signal to another process

  • debugging
  • suspension
  • resump5on
  • 5mer expira5on
slide-18
SLIDE 18

Receiving a Signal

18

A des5na5on process receives a signal when it is forced by the kernel to react in some way to the delivery of the signal Three possible ways to react:

  • 1. Ignore the signal (do nothing)
  • 2. Terminate process (+ op5onal core dump)
  • 3. Catch the signal by execu5ng a user-level

func5on called signal handler

  • Like a hardware excep5on handler being called in response

to an asynchronous interrupt

show handler.c

slide-19
SLIDE 19

Signal Example

19

void int_handler(int sig) { printf("Process %d received signal %d\n", getpid(), sig); exit(0); } int main() { pid_t pid[N]; int i, child_status; signal(SIGINT, int_handler); for (i = 0; i < N; i++) // N forks if ((pid[i] = fork()) == 0) { while(1); //child infinite loop } for (i = 0; i < N; i++) { // parent continues executing printf("Killing proc. %d\n", pid[i]); kill(pid[i], SIGINT); } for (i = 0; i < N; i++) { pid_t wpid = wait(&child_status); if (WIFEXITED(child_status)) // parent checks for each child’s exit printf("Child %d terminated w exit status %d\n", wpid, WEXITSTATUS(child_status)); else printf("Child %d terminated abnormally\n", wpid); } exit(0); }

slide-20
SLIDE 20

Blocked Signals

20

A process can block the receipt of certain signals

  • Blocked signals can be delivered, but will not be

received un5l the signal is unblocked Kernel maintains pending and blocked bit vectors in the context of each process

  • blocked: represents the set of blocked signals

Can be set and cleared by using the

sigprocmask func5on

slide-21
SLIDE 21

Process Groups

Every process belongs to exactly one process group

Shell

Foreground job Background job #1 Background job #2 Child Child

getpgrp() Return process group of current process setpgid() Change process group of a process /bin/kill –9 21 Send SIGKILL to process 24818 /bin/kill –9 –20 Send SIGKILL to every process in process group 20 pid=20 pgid=20 pid=21 pgid=20 pid=22 pgid=20 Foreground process group 20 Background process group 32 Background process group 40 pid=32 pgid=32 pid=40 pgid=40 pid=10 pgid=10

slide-22
SLIDE 22

ImplemenOng a (really, really simple) Shell

22

void eval(char *cmdline) { char *argv[MAXARGS]; /* argv for execve() */ int bg; /* should the job run in bg or fg? */ pid_t pid; /* process id */ bg = parseline(cmdline, argv); if (!builtin_command(argv)) { if ((pid = Fork()) == 0) { /* child runs user job */ if (execve(argv[0], argv, environ) < 0) { printf("%s: Command not found.\n", argv[0]); exit(0); } } if (!bg) { /* parent waits for fg job to terminate */ int status; if (waitpid(pid, &status, 0) < 0) unix_error("waitfg: waitpid error"); } else /* otherwise, don’t wait for bg job */ printf("%d %s", pid, cmdline); } }

slide-23
SLIDE 23

And Now… Threads!

23

slide-24
SLIDE 24

Why Threads?

24

  • Program structure: expressing logically

concurrent tasks

  • Responsiveness: shioing work to run in the

background

  • Performance: managing I/O devices

Does mulO-threading only make sense

  • n mulOcore?
  • Performance: exploi5ng mul5ple processors
slide-25
SLIDE 25

Stack

What happens when…

25

Mail

Kernel

PCBs

0x00000000 0xFFFFFFFF

Apache wants to run mul5ple concurrent computa5ons?

Apache Emacs Apache

Two heavyweight address spaces for two concurrent computa5ons? What is dis5nct about these address spaces?

Heap

Data Insns Stack

Heap

Data Insns

slide-26
SLIDE 26

Stack

Idea!

26

Mail

Kernel

PCBs

0x00000000 0xFFFFFFFF

Apache Emacs Heap

Data Insns Stack

Eliminate duplicate address spaces and place concurrent computa5ons in the same address space.

slide-27
SLIDE 27

Process vs. Thread

27

Process:

  • Address Space
  • Shared I/O resources
  • One or more Threads:

Other terms for threads: Lightweight Process, Thread of Control, Task Not Shared:

  • Registers, PC, SP
  • Stack

Shared:

  • Code
  • Data
  • Privileges
slide-28
SLIDE 28

Stack 2

Thread Memory Layout

28

Data Insns Stack 1 Stack 3

PC

Thread 1 Thread 2

PC

Thread 3

PC SP SP SP

Process A

(Heap Shared but subdivided.)

slide-29
SLIDE 29

Simple Thread API

29

void thread_create (thread,func,arg)

Create a new thread, storing information about it in

  • thread. Concurrently with the calling thread, thread

executes the function func with the argument arg.

void thread_yield ()

Calling thread voluntarily gives up processor to let

  • ther thread(s) run. Scheduler can resume running

the calling thread whenever it chooses to do so.

int thread_join (thread)

Wait for thread to finish if it has not already done so; then return the value passed to thread_exit by that

  • thread. Note that thread_join may be called only once

for each thread.

void thread_exit (ret)

Finish the current thread. Store the value ret in the current thread’s data structure. If another thread is already waiting in a call to thread_join, resume it.

slide-30
SLIDE 30

Coding Example

30

process_share.c thread_share.c

slide-31
SLIDE 31

Threads

31

Lighter-weight than processes

  • process is an abstract computer (CPU, mem,

devices, …)

  • thread is an abstract core

Threads need to be mutually trus5ng (Why?) Ideal for concurrent programs where lots of code and data are shared

  • Servers, GUI code, …
slide-32
SLIDE 32

OpOon #1: Kernel Threads

32

  • Threads share a single

loca5on in memory

  • Separate PCBs (TCBs)

for each thread

  • PCBs have:
  • same: base & bound

register values

  • different: PC, SP,

registers

Mail

Kernel

PCBs

0x00000000 0xFFFFFFFF

Apache Emacs

Stack 2

Heap

Data Insns Stack 1

2 1

slide-33
SLIDE 33

MulO-threaded kernel with three kernel threads and two single-threaded user-level processes.

33

Each kernel thread has its own TCB and its own stack. Each user process has a stack at user-level for execu5ng user code and a kernel interrupt stack for execu5ng interrupts and system calls.

slide-34
SLIDE 34

A mulO-threaded kernel with 3 kernel threads and 2 user-level processes, each with 2 threads.

34

Each user-level thread has a user-level stack and an interrupt stack in the kernel for execu5ng interrupts and system calls.

slide-35
SLIDE 35

OpOon #2: User Threads

35

  • Build a mini-OS in user space
  • Real OS doesn’t know about

mul5ple threads

  • Single PCB
  • Generally more efficient than

kernel threads (Why?)

  • But kernel threads

simplify system call handling and scheduling (Why?)

Mail

Kernel

PCBs

0x00000000 0xFFFFFFFF

Apache Emacs

stack 2

Heap

Data Insns stack 1

“os” stack

slide-36
SLIDE 36

ImplemenOng User Threads (4411-P1!)

36

User process supports:

  • Thread Control Block (TCB) table
  • one entry per thread
  • “context switch” opera5ons
  • save/restore thread state in TCB
  • much like kernel-level context switches
  • yield() opera5on:
  • thread releases core, allows another thread to

use it

  • Automa5c pre-emp5on not always supported
  • Thread scheduler
slide-37
SLIDE 37

User Threads & System Calls

37

  • With user threads, a process may have mul5ple

systems calls outstanding simultaneously (one per thread)

  • Kernel PCB must support this (Why?)
slide-38
SLIDE 38

CooperaOve vs. PreempOve MulOthreading

38

Coopera5ve: thread runs un5l it yields control to another thread — yield is in the code itself + beser control of scheduling + simpler reasoning about shared resources – starva5on (slow interfaces) – mul5-core → reasoning not simpler aferall! [Not common these days] Mul5-threading == preemp5ve mul5-threading except in 11-P1 “Non-PrempOve MulOtasking”

slide-39
SLIDE 39

MulOthreading Programming Model

39

Rule #1: don’t presume to know the schedule Shared Resources → Synchroniza5on Masers! thread_share.c revisited (Next Week!)