ceng 334 – operating systems 02- processes asst. prof. yusuf sahillioğlu computer eng. dept,,...

49
CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept, , Turkey

Upload: roger-holland

Post on 11-Jan-2016

232 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

CENG 334 – Operating Systems

02- Processes

Asst. Prof. Yusuf Sahillioğlu

Computer Eng. Dept, , Turkey

Page 2: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

System Calls2 / 49

Programming interface to the services provided by the OS.

Page 3: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

Processes3 / 49

Process: a program (piece of code) in execution. Program is passive entity, process is active.

Program becomes process when executable file loaded into memory.

We will learn process scheduling, creation/termination, communication.

Page 4: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

Process in Memory4 / 49

//temporary data (function parameters, return //addresses, local variables); grows downwards.

//dynamically allocated memory; grows upwards.

//global variables.

//program code (current line pointed by Program Counter).

Address space

0x0000000

0xFFFFFFF

Page 5: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

Process Execution5 / 49

How can several processes run on one CPU? OS makes this happen by ensuring:

Fair scheduling: each process gets fair chance to run. Protection: processes do not modify each others properties.

Process states: new: The process is being created. running: Instructions are being executed. waiting: The process is waiting for some event to occur. ready: The process is waiting to be assigned to a processor. terminated: The process has finished execution.

Page 6: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

Process States6 / 49

Page 7: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

Process Control Block (PCB)7 / 49

When switching from process to process (context switch), we need to know information associated with each process: Process ID //unique identifier User ID //who is running? Process state //ready, waiting? Program counter //code line CPU registers //computations storage CPU scheduling information //priority of this process Memory-management information //max memo given to

this process Accounting information //total CPU time, memo usage I/O status information //is I/O ready?

Page 8: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

Process Control Block (PCB)8 / 49

Contents of PCB (not all shown below):

Calculations ‘till the interrupt stored here so that I can resume efficiently.

Page 9: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

Context Switch9 / 49

Important ‘cos it allows new processes run by the processor. Overhead ‘cos while switching the context no work is done

for the processes.

Page 10: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

Context Switch10 / 49

Context switch is kernel code. Process is user code.

Process A Process B

user code

kernel code

user code

kernel code

user code

context switch

context switch

Time

Page 11: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

Context Switch11 / 49

Context overhead in Ubuntu 9.04 is 5.4 usecs on a 2.4GHz Pentium 4.

This is about 13.200 CPU cycles. Don’t panic; not quite that many instructions since CPI > 1

Page 12: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

Process Representation in Linux12 / 49

PCB for a process is in the structure task_struct.

Use ps command to see the started processes in the system.

struct task_struct {long state; /* state of the process */….pid_t pid; /* identifier of the process */…unisgned int time_slice; /* scheduling info */…struct files_struct *files; /* open files this process is working on */….struct task_struct *parent; /* this process’s parent */…

}

Page 13: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

Process Scheduling13 / 49

Scheduler interleaves processes in order to give every process the illusion of having its own CPU, aka concurrency. Even with one CPU (instruction executer), you can multitask: music, code,

download, ..

Process scheduler selects among available processes for next execution on CPU.

Maintains scheduling queues of processes Job queue – set of all processes in the system Ready queue – set of all processes ready to execute Device queues – set of processes waiting for an I/O device //scanf(“%d”,

&number); Processes migrate among the various queues

Page 14: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

Process Scheduling14 / 49

e.g., sleep(1000);

Page 15: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

Schedulers15 / 49

Long-term scheduler (or job scheduler): selects which processes should be brought into the ready queue (from the job queue). Controls the degree of multitasking (all ready processes execute).

Short-term scheduler (or CPU scheduler): selects which process should be executed next and allocates CPU. Sometimes the only scheduler in a system.

Page 16: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

First Come First Served Scheduling16 / 49

An unfair non-preemptive CPU scheduler, aka FCFS or FIFO. Idea: run until done!

Example:

Page 17: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

First Come First Served Scheduling17 / 49

An unfair non-preemptive CPU scheduler, aka FCFS or FIFO. Idea: run until done!

Example:

Page 18: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

Round Robin Scheduling18 / 49

A fair preemptive CPU scheduler. Idea: each process gets a small amount of CPU time (time

quantum). Usually 10-100 milliseconds. Comments on this value?

Example with quantum = 20:

Page 19: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

Shortest Job Next19 / 49

An unfair non-preemptive CPU scheduler. Idea: run the shortest jobs first.

Run time estimate is an issue

A variant: Shortest Remaining Time Next. Still needs those estimates Preemptive version of Shortest Job Next.

Page 20: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

Priority Scheduling20 / 49

An unfair non-preemptive CPU scheduler. Idea: e.g., admin jobs favored!

Page 21: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

Process Creation21 / 49

One process can create, or fork, another process. The original process is the parent. //original variables, assignments, .. New process is the child. //copy of parent with its own address space.

Page 22: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

Why do we fork?22 / 49

Exxample usages of fork(): Your shell uses fork to run programs you invoke from the command line.

Google Chrome uses fork to handle each page within a separate process. This will prevent client-side code on one page from bringing your whole browser down.

Web servers like Apache use fork to create multiple server processes, each of which handles requests in its own address space. If one dies or leaks memory, others are unaffected, so it functions as a mechanism for fault tolerance.

Your word processor uses fork to print a document while you can still look/edit different pages.

Unix system starts with a process called init. It then forks new processes. Without fork you could only run init in Unix systems, which totally blows.

Your virus forks new processes to draw annoying stuff on screen, to produce weird noises, etc., once it is settled to the system (common work). When user obeys you, you may want to kill some annoying child processes as a reward.

Page 23: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

UNIX fork Mechanism23 / 49

fork() system call creates a new process (already discussed 2 slides ago).

This creates an exact duplicate of the parent process. Creates and initializes a new PCB; Creates a new address

space. Copies entire content of parent’s address space into the

child. Initializes CPU (registers) & OS resources (open files) to a copy of the

parent’s. Places new PCB on ready queue.

PC

Registers

PID 4109State: Running

Process calls fork() PC

Registers

PID 4110State: Ready

copy state

PC

Registers

PID 4277State: Ready

PC

Registers

PID 4391State: Ready

Ready queueAdd to the end of the queue

PC

Registers

PID 4110State: Ready

Page 24: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

Process-related Unix Commands24 / 49

pstree –p #print processes with the parent-child hierarchy. ps #print snapshot of current processes started in this shell. ps –A #print all processes regardless of who started, which shell started, etc.

ps –A | grep firefo #print all processes whose name has `firefo’ string.

ps –A | grep –v firefo #print all processes not containing`firefo’ string.

top #print all process with CPU/memo info; like Win.TaskManager

./myprog & #start myprog as a new process in the background.

kill 777 #kill/terminate the process whose ID is 777.

Page 25: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

Process-related C Library25 / 49

void main() { int pid = fork(); if (pid == 0) print(“hello from child”); else print(“hello from parent”);}

Dead simple fork() program:

Cooler ones in live coding session now.

Page 26: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

Process-related C Library26 / 49

Zombie process: is a process that has completed execution but still has an entry in process table (ps). (zombie is in terminated state). Entry needed to allow the parent process to read its child’s exit

status. read: exit status is read via wait() system call. After wait() is

executed, zombie entry is removed from process table. This operation is called reaping.

What if parent does not reap? Child will be reaped by init process.

When does parent not reap? Terminates before child. Forgets calling wait(), i.e., forgets doing reaping. Don’t panic ‘cos init process does the reaping eventually.

wait() system call: suspends execution of the calling process until one of its children terminates. waitpid() system call: suspends execution of the calling process until the child specified by pid

terminates. waitpid(-1, &status, 0); == wait(&status)

Page 27: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

Process-related C Library27 / 49

Orphan process: is a process whose parent has completed execution but it has not.

execlp(“\bin\ls”, “ls”, NULL) Does not fork a new process. Rather replaces the address space & CPU state of the current

process p. Loads the new address space from executable file and starts it from

main(). When executable finishes, it does not return to the calling process p.

p is reaped, i.e., not a zombie.

So, to start a new program use fork() followed by exec() which is what system() call does. which is what init process does when you double click on Chrome browser.

There are variants of execlp() which again load and run programs.

Page 28: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

Interprocess Communication (IPC)28 / 49

Cooperating vs. independent.

By definition processes are isolated. Own address space, own resources, etc.

They may need to communicate to do interesting stuff. Info sharing. Computation speedup. Modularity (app is divided into sub-tasks)

cook cut vegetables

read newspaper

Page 29: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

Models of IPC29 / 49

Shared memory. Read write through memeory. Shared board on the refrigerator.

Message passing. send() and receive() messages. Blocking/synchronous:

send(): postman knocks on your door and waits ‘till you open.

receive(): You (client) wait on the waiter (server) to order food.

Non-blocking/asynchronous: send(): Lazy postman knocks and leaves. receive(): You occasionally look for the waiter and

order if he is there; otherwise continue chatting.

Page 30: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

Models of IPC30 / 49

Page 31: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

Shared Memory31 / 49

One process creates shared memory w/ unique address/key. It and the other processes attach to this shared memory.

Page 32: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

Shared Memory (in C)32 / 49

shmget() to create shared memory. shmat() to attach to it. shmdt() to detach from it.

See the code of a use-case study: Producer-Consumer problem. Producer process produces info that is consumed by a consumer

process. e.g. print utility places data and printer fetches data to print.

Page 33: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

Shared Memory33 / 49

Advantages: good for sharing large amount of data. very fast.

Limitation: no synchronization provided. applications must create their own.

Alternative: mmap() instead of shmget().

Page 34: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

Message Passing34 / 49

There is a message queue (created msgget()) where msgs written (msgsend()) and read (msgrcv()).

Producer-consumer style.

Message queues are more versatile than pipes. Can transmit messages as struct’ured entities. Priority/urgency can be specified in msgs.

Page 35: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

Message Passing35 / 49

Producer: Consumer:

Page 36: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

More Models of IPC36 / 49

Pipe. Allow communication in a producer-consumer style. Producer writes to one end (the write-end of the pipe [1]). Consumer reads from the otherend (the read-end of the

pipe [0]). Unidirectional. Fixed size: Pipe fills up after writing 10K to it without

reading anything out. FIFO: anything can be written to the pipe, and read from

the other end in the order it came in.

Page 37: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

Pipe37 / 49

ls | sort | grep sq #output of ls process becomes input to sort process,

#whose output becomes input to grep process.

Result: msqconsumer.c msqproducer.c

ls sort grep

Page 38: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

Pipe; behind the scenes38 / 49

File descriptors. We know FILE* from stdio.h; fopen, fwrite, fclose, .. Corresponding low-level system calls: open, write, close, ..

File descriptors are simply int’s that are analogous to FILE*’s. FILE* fPtr; int fd = fileno(fPtr); //fd is the file descriptor

associated //with fPtr file stream (> 2).

stdin is 0, stdout is 1, stderr is 2. //standard streams.

Page 39: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

Pipe; behind the scenes39 / 49

pipe() returns a pair of file descriptors. One connected to the write-end of the pipe, fd[1]. Other connected to the read-end of the pipe, fd[0]. See this in action..

Page 40: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

Pipe; behind the scenes40 / 49

dup() duplicates an open file descriptor. ls -1 | wc l #count the number of lines in the output of ls -

1, #which means count the number of files in the

folder.

exec() & dup() combo to implement shell’s | support in C. exec(P) replaces the currently running process with P.

Connect the stdout of the ls to the stdin of the wc.

Page 41: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

Pipe41 / 49

ls | sort | grep sq #output of ls process becomes input to sort process,

#whose output becomes input to grep process.

Result: msqconsumer.c msqproducer.c

Page 42: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

Signals42 / 49

A limited form of interprocess communication. Light notify of an event or exception. Small msg: only info in a signal is its ID & the fact that it

arrived.

When a signal is sent, OS interrupts process’s normal execution flow. If process has a user-level signal handler, that routine is

executed. Else default signal handler is executed.

Page 43: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

Signal Send43 / 49

Signal is sent from the kernel, sometimes at the request of another process.

Kernel sends a signal for one of the following reasons: Kernel has detected a system event such as divide-by-

zero (SIGFPE) or the termination of a child process (SIGCHLD).

Another process has invoked the kill system call to explicitly request the kernel to send a signal to the destination process.

Page 44: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

Signal Receive44 / 49

A destination 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: Ignore the signal (do nothing). Terminate the process (with optional core dump (for

debugging)). Catch the signal by executing a user-level function called

signal handler.

Page 45: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

A Signal Example45 / 49

Process p1 sends SIGSTOP to stop process p2. //hold on! To continue, p2 has to receive signal SIGCONT. //pick up

where left off

How does the process know to do this when it receives a certain signal? Many signals are predefined and the process has a default

signal handler to deal with it.

Page 46: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

Default vs. User-level Signal Handler46 / 49

SIGINT, for example, is the interrupt signal that a process receives when the user hits ^C.

The default signal handler for SIGINT causes the process to exit. You can override the SIGINT to do whatever you want (or nothing

at all) – this is called the user-level signal handler. See this in action.. //catching a signal via sigaction

You cannot override SIGKILL (ID=9) and SIGSTOP. By default SIGKILL terminates the process:

kill –SIGKILL 2614 #terminate process w/ ID 2614 kill –9 2614 #same: terminate process w/ ID 2614

kill 2614 #what signal is sent? SIGTERM kill –SIGTERM 2614 #same as above; SIGTERM can be

overridn

Page 47: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

Signal Send47 / 49

kill is one way to send signals to a process (see prev. slide). Recall that kill can be used from shell: kill –SIGCONT 2614 and from C code (kill() system call): kill(getpid(), SIGCONT)

raise is a way to raise a signal within the same process.

Page 48: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

User-defined Signals48 / 49

Not all signals are predefined. You can send a custom signal to a process via SIGUSR1 and

SIGUSR2.

My cd player program might respond to SIGUSR1 by advancing to the next track: kill –SIGUSR1 7482 #pid of my cd player is 7482

Page 49: CENG 334 – Operating Systems 02- Processes Asst. Prof. Yusuf Sahillioğlu Computer Eng. Dept,, Turkey

Coming soon49 / 49

Threads. parallel work on the same process for efficiency. share registers, memory, and other resources. all about synchronization: