processes - computer systems laboratory @ …csl.skku.edu/uploads/eee3052f17/3-process.pdf ·  ·...

29
EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong ([email protected]) Processes Jinkyu Jeong ([email protected]) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu

Upload: trinhxuyen

Post on 08-Apr-2018

218 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Processes - Computer Systems Laboratory @ …csl.skku.edu/uploads/EEE3052F17/3-process.pdf ·  · 2017-09-11EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong(jinkyu@skku.edu)

EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong ([email protected])

Processes

Jinkyu Jeong ([email protected])Computer Systems Laboratory

Sungkyunkwan Universityhttp://csl.skku.edu

Page 2: Processes - Computer Systems Laboratory @ …csl.skku.edu/uploads/EEE3052F17/3-process.pdf ·  · 2017-09-11EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong(jinkyu@skku.edu)

EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong ([email protected]) 2

OS Internals

Hardware

SystemCallInterface

shellshell

ls ps

HardwareControl(Interrupthandling,etc.)

FileSystemManagement

I/OManagement(devicedrivers)

MemoryManagement

ProcessManagement Protection

Kernelspace

Userspace trap

schedulerIPC

synchronization

Page 3: Processes - Computer Systems Laboratory @ …csl.skku.edu/uploads/EEE3052F17/3-process.pdf ·  · 2017-09-11EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong(jinkyu@skku.edu)

EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong ([email protected]) 3

What is a Process?

• An instance of a program in execution

• The basic unit of protection

• A process is identified using its process ID (PID)

• A process includes– CPU context (registers)– OS resources (address space, open files, etc.)– Other information (PID, state, owner, etc.)

Page 4: Processes - Computer Systems Laboratory @ …csl.skku.edu/uploads/EEE3052F17/3-process.pdf ·  · 2017-09-11EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong(jinkyu@skku.edu)

EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong ([email protected]) 4

Process vs. Program

Stack

Heap

program

Code

Data

code

data

PC

SP

Memory Disk

Page 5: Processes - Computer Systems Laboratory @ …csl.skku.edu/uploads/EEE3052F17/3-process.pdf ·  · 2017-09-11EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong(jinkyu@skku.edu)

EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong ([email protected]) 5

Running a Process

Code

Data

Fetch Iß Mem[PC]Decode IExecute IUpdate PC

PC

Page 6: Processes - Computer Systems Laboratory @ …csl.skku.edu/uploads/EEE3052F17/3-process.pdf ·  · 2017-09-11EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong(jinkyu@skku.edu)

EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong ([email protected]) 6

Running Multiple Processes

Code A

Data A

Code C

Data C

Code B

Data B

Page 7: Processes - Computer Systems Laboratory @ …csl.skku.edu/uploads/EEE3052F17/3-process.pdf ·  · 2017-09-11EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong(jinkyu@skku.edu)

EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong ([email protected]) 7

Interleaving Multiple Processes

Code A

Data A

Code C

Data C

Code B

Data B

Page 8: Processes - Computer Systems Laboratory @ …csl.skku.edu/uploads/EEE3052F17/3-process.pdf ·  · 2017-09-11EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong(jinkyu@skku.edu)

EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong ([email protected]) 8

Virtualizing the CPU

Code A

Data A

OS Code

OS Data

Code B

Data B

OS creates the illusion that each process has its own CPU (and memory)

Page 9: Processes - Computer Systems Laboratory @ …csl.skku.edu/uploads/EEE3052F17/3-process.pdf ·  · 2017-09-11EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong(jinkyu@skku.edu)

EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong ([email protected]) 9

Example

#include <sys/types.h>#include <unistd.h>

int main(){

int pid;

if ((pid = fork()) == 0)/* child */printf (“Child of %d is %d\n”,

getppid(), getpid());else

/* parent */printf (“I am %d. My child is %d\n”,

getpid(), pid);}

Page 10: Processes - Computer Systems Laboratory @ …csl.skku.edu/uploads/EEE3052F17/3-process.pdf ·  · 2017-09-11EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong(jinkyu@skku.edu)

EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong ([email protected]) 10

Example Output

% ./a.outI am 31098. My child is 31099.Child of 31098 is 31099.

% ./a.outChild of 31100 is 31101.I am 31100. My child is 31101.

Page 11: Processes - Computer Systems Laboratory @ …csl.skku.edu/uploads/EEE3052F17/3-process.pdf ·  · 2017-09-11EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong(jinkyu@skku.edu)

EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong ([email protected]) 11

Process Hierarchy

• Parent-child relationship– One process can create another

process– Unix calls the hierarchy a “process

group”– Windows has no concept of process

hierarchy

• Browsing a list of processes:– ps in Unix– Task Manager (taskmgr) in Windows

sh

$ cat file1 | wc

cat wc

Page 12: Processes - Computer Systems Laboratory @ …csl.skku.edu/uploads/EEE3052F17/3-process.pdf ·  · 2017-09-11EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong(jinkyu@skku.edu)

EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong ([email protected]) 12

Process Creation• fork()– Creates a new process cloning the parent process• Parent inherits most of resources and privileges: open files, UID,

etc.• Child also duplicates the parent’s address space– Parent may either wait for the child to finish (using

wait()), or it may continue in parallel– Shells or GUIs use this system call internally

• exec()– Replaces the current process image with a new program– Windows: CreateProcess() = fork() + exec()

Page 13: Processes - Computer Systems Laboratory @ …csl.skku.edu/uploads/EEE3052F17/3-process.pdf ·  · 2017-09-11EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong(jinkyu@skku.edu)

EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong ([email protected]) 13

Process Termination

• Normal exit (voluntary)• Error exit (voluntary)• Fatal error (involuntary)– Segmentation fault – illegal memory access– Protection fault– Exceed allocated resources, etc.

• Killed by another process (involuntary)– By receiving a signal

• Zombie process: terminated, but not removed

Page 14: Processes - Computer Systems Laboratory @ …csl.skku.edu/uploads/EEE3052F17/3-process.pdf ·  · 2017-09-11EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong(jinkyu@skku.edu)

EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong ([email protected]) 14

Process Termination APIs• exit()– Caller process terminates its execution – Return from main() is identical– Parameter denotes whether this exit is normal (0) or error (1)

• kill()– Send a signal to a process– Parameters are directives to go to sleep, to die, and other useful

imperatives

• wait()– Wait for state changes in any child of the calling process• Usually termination of a child

– Release resources associated with the terminated child– waitpid() for a specific child

Page 15: Processes - Computer Systems Laboratory @ …csl.skku.edu/uploads/EEE3052F17/3-process.pdf ·  · 2017-09-11EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong(jinkyu@skku.edu)

EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong ([email protected]) 15

Simplified Shell

int main(void){char cmdline[MAXLINE];char *argv[MAXARGS];pid_t pid;int status;

while (getcmd(cmdline, sizeof(buf)) >= 0) {parsecmd(cmdline, argv);if (!builtin_command(argv)) {if ((pid = fork()) == 0) {

if (execv(argv[0], argv) < 0) {printf(“%s: command not found\n”, argv[0]);exit(0);

}}waitpid(pid, &status, 0);

}}

}

Page 16: Processes - Computer Systems Laboratory @ …csl.skku.edu/uploads/EEE3052F17/3-process.pdf ·  · 2017-09-11EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong(jinkyu@skku.edu)

EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong ([email protected]) 16

Process State Transitions

RunningReadyScheduled

Timesliceexhausted

I/Ooreventwait

Blocked

I/Ooreventcompletion

Created exit

Page 17: Processes - Computer Systems Laboratory @ …csl.skku.edu/uploads/EEE3052F17/3-process.pdf ·  · 2017-09-11EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong(jinkyu@skku.edu)

EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong ([email protected]) 17

Process State – Linux Example

RunnableSleepingTracedorStoppedUninterruptibleSleepZombie

R:S:T:

D:

Z:High-prioritytaskLow-prioritytaskSessionleaderIntheforegroundprocessgroupMulti-threaded

<:N:s:+:

l:

Page 18: Processes - Computer Systems Laboratory @ …csl.skku.edu/uploads/EEE3052F17/3-process.pdf ·  · 2017-09-11EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong(jinkyu@skku.edu)

EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong ([email protected]) 18

Implementing Processes• PCB (Process Control Block) or Process Descriptor– Each PCB represents a process– Contains all of the information about a process• CPU registers • PID, PPID, process group, priority, process state, signals• CPU scheduling information• Memory management information• Accounting information• File management information• I/O status information• Credentials

– task_struct in Linux• 3584 bytes as of Linux 4.4.0

Page 19: Processes - Computer Systems Laboratory @ …csl.skku.edu/uploads/EEE3052F17/3-process.pdf ·  · 2017-09-11EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong(jinkyu@skku.edu)

EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong ([email protected]) 19

Context Switch• The act of switching CPU from one process to another

• Administrative overhead– Saving and restoring registers and memory maps– Flushing and reloading the memory cache– Updating various tables and lists, etc.

• The overhead depends on hardware support– Multiple register sets in UltraSPARC– Advanced memory management techniques may require

extra data to be switched with each context

• 100s or 1000s of switches/sec typically

Page 20: Processes - Computer Systems Laboratory @ …csl.skku.edu/uploads/EEE3052F17/3-process.pdf ·  · 2017-09-11EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong(jinkyu@skku.edu)

EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong ([email protected]) 20

Context Switch – Linux Example

• Linux example– Total 1,693,515,228 ticks = 4704 hours = 196 days– Total 4,066,419,922 context switches– Roughly 240 context switches / sec

Page 21: Processes - Computer Systems Laboratory @ …csl.skku.edu/uploads/EEE3052F17/3-process.pdf ·  · 2017-09-11EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong(jinkyu@skku.edu)

EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong ([email protected]) 21

Performing Context Switch

handlethetrapcallswitch()routinesaveregs(A)toPCB(A)restoreregs(B)fromPCB(B)switchtok-stack(B)return-from-trap(intoB)

timerinterruptsaveregs(A)tok-stack(A)movetokernelmodejumptotraphandler

ProcessA…

Kernel Hardware Process

restoreregs(B)fromk-stack(B)movetousermodejumptoB’sIP

ProcessB…

Page 22: Processes - Computer Systems Laboratory @ …csl.skku.edu/uploads/EEE3052F17/3-process.pdf ·  · 2017-09-11EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong(jinkyu@skku.edu)

EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong ([email protected]) 22

Process State Queues

• The OS maintains a collection of queues that represent the state of all processes in the system– Ready queue (or run queue)– Wait queue(s): one queue for each type of event (device,

timer, message, …)

• Each PCB is queued onto a state queue according to its current state– As a process changes state, its PCB is migrated between

the various queues

Page 23: Processes - Computer Systems Laboratory @ …csl.skku.edu/uploads/EEE3052F17/3-process.pdf ·  · 2017-09-11EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong(jinkyu@skku.edu)

EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong ([email protected]) 23

Process State Queues

ready queue

disk I/O queue

time slice expired

fork a child

keyboard I/O queue

mutex wait queue

exitscheduled

disk interrupt

keyboard interrupt

mutex acquired

Page 24: Processes - Computer Systems Laboratory @ …csl.skku.edu/uploads/EEE3052F17/3-process.pdf ·  · 2017-09-11EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong(jinkyu@skku.edu)

EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong ([email protected]) 24

fork()

• Implementing fork()– Creates and initializes a new PCB– Creates and initializes a new address space– Initializes the address space with a copy of the entire

contents of the address space of the parent– Initializes the kernel resources to point to the resources

used by the parent (e.g. open files)– Places the new PCB on the ready queue– Returns the child’s PID to the parent, and zero to the

child

int fork()

Page 25: Processes - Computer Systems Laboratory @ …csl.skku.edu/uploads/EEE3052F17/3-process.pdf ·  · 2017-09-11EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong(jinkyu@skku.edu)

EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong ([email protected]) 25

exec()

• Implementing exec()– Wipe the process’ address space and release most of the

resources allocated to the process– Loads the new program “prog” into the its address space– Initializes hardware context and “args” for the new program

– exec() does not create a new process– What does it mean for exec() to return?

• Variants– exec(): execl(), execle(), execlp(), execv(), and execvp()

int execv(char *prog, char *argv[])

Page 26: Processes - Computer Systems Laboratory @ …csl.skku.edu/uploads/EEE3052F17/3-process.pdf ·  · 2017-09-11EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong(jinkyu@skku.edu)

EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong ([email protected]) 26

Inter-Process Communication

• Cooperating processes need to communicate with each other for– Information sharing– Computation speedup– Modularity– Convenience

• Two models of IPC– Shared memory– Message passing

Page 27: Processes - Computer Systems Laboratory @ …csl.skku.edu/uploads/EEE3052F17/3-process.pdf ·  · 2017-09-11EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong(jinkyu@skku.edu)

EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong ([email protected])

IPC Models

• Message passing • Shared memory

process A

message queue

kernel

(a) (b)

process A

shared memory

kernel

process B

m0 m1 m2 ...m3 mn

process B

process A

message queue

kernel

(a) (b)

process A

shared memory

kernel

process B

m0 m1 m2 ...m3 mn

process B

Page 28: Processes - Computer Systems Laboratory @ …csl.skku.edu/uploads/EEE3052F17/3-process.pdf ·  · 2017-09-11EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong(jinkyu@skku.edu)

EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong ([email protected]) 28

Policy vs. Mechanism

• Policy– What should be done?– Policy decisions must be made for all resource allocation

and scheduling problems– e.g. What is the next process to run?

• Mechanism– How to do something?– The tool for implementing a set of policies– e.g. How to make multiple processes run at once?

Page 29: Processes - Computer Systems Laboratory @ …csl.skku.edu/uploads/EEE3052F17/3-process.pdf ·  · 2017-09-11EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong(jinkyu@skku.edu)

EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong ([email protected]) 29

Separating Policy from Mechanism

• A key principle in operating system design

• Policies are likely to change depending on workloads and also across places or over time

• A general mechanism, separated from policy, is more desirable

• Allows to build a more modular OS

• Enables extensible systems – User-specific policies?