operating systems cse 411 process management sept. 13 2006 - lecture 4 instructor: bhuvan urgaonkar

24
Operating Systems Operating Systems CSE 411 CSE 411 Process Management Process Management Sept. 13 2006 - Lecture 4 Sept. 13 2006 - Lecture 4 Instructor: Bhuvan Instructor: Bhuvan Urgaonkar Urgaonkar

Upload: aubrey-briggs

Post on 18-Jan-2018

216 views

Category:

Documents


0 download

DESCRIPTION

CPU/Process Management : Objectives & Challenges Scheduling –Determine the order in which processes get to use the CPU –Prevent starvation, ensure fairness, be efficient Allow processes to communicate efficiently and securely Synchronization –When things need to be done in a certain order Prevent deadlocks –A situation where no-one can make progress –More later

TRANSCRIPT

Page 1: Operating Systems CSE 411 Process Management Sept. 13 2006 - Lecture 4 Instructor: Bhuvan Urgaonkar

Operating SystemsOperating SystemsCSE 411CSE 411

Process ManagementProcess ManagementSept. 13 2006 - Lecture 4Sept. 13 2006 - Lecture 4

Instructor: Bhuvan UrgaonkarInstructor: Bhuvan Urgaonkar

Page 2: Operating Systems CSE 411 Process Management Sept. 13 2006 - Lecture 4 Instructor: Bhuvan Urgaonkar

• Last class– Definition of a process– Process states– Process related data structures

• PCB– Process layout in memory

• Today– More about processes– Begin CPU scheduling

Page 3: Operating Systems CSE 411 Process Management Sept. 13 2006 - Lecture 4 Instructor: Bhuvan Urgaonkar

CPU/Process Management : Objectives & Challenges

• Scheduling– Determine the order in which processes get to use the

CPU– Prevent starvation, ensure fairness, be efficient

• Allow processes to communicate efficiently and securely

• Synchronization– When things need to be done in a certain order

• Prevent deadlocks– A situation where no-one can make progress– More later

Page 4: Operating Systems CSE 411 Process Management Sept. 13 2006 - Lecture 4 Instructor: Bhuvan Urgaonkar

Data Structures for CPU Management

• Already seen: PCBs, one per process• Several linked lists

– List of ready (runnable) processes– List of waiting (blocked) processes

• Often multiple lists, one per event– List of running processes??

• Que: Why linked lists? Why not better data structures?

Page 5: Operating Systems CSE 411 Process Management Sept. 13 2006 - Lecture 4 Instructor: Bhuvan Urgaonkar

Re-entrant Kernels• Kernel control path• Multiple kernel control paths may

be active simultaneously• Reason: Efficiency• How to achieve this?

Page 6: Operating Systems CSE 411 Process Management Sept. 13 2006 - Lecture 4 Instructor: Bhuvan Urgaonkar

Interleaved Interrupt Handling

User

Excp Intr

Time

USERMODE

KERNELMODE

UserUser

Intr

Intr

Page 7: Operating Systems CSE 411 Process Management Sept. 13 2006 - Lecture 4 Instructor: Bhuvan Urgaonkar

Data Structures for CPU Management (contd.)

• Kernel-level stacks for processes

Page 8: Operating Systems CSE 411 Process Management Sept. 13 2006 - Lecture 4 Instructor: Bhuvan Urgaonkar

Linked List Example from Linux

struct list_head {struct list_head *next, *prev;

};

struct runqueue { [….] struct list_head migration_queue; [….]};

nextprev

nextprev

runqueue

Page 9: Operating Systems CSE 411 Process Management Sept. 13 2006 - Lecture 4 Instructor: Bhuvan Urgaonkar

Linked List in Linux (2.4 onwards)

• A linked list (what we normally encounter)

• A linked list in Linux• Why?

Page 10: Operating Systems CSE 411 Process Management Sept. 13 2006 - Lecture 4 Instructor: Bhuvan Urgaonkar

Process Operations:Process Creation

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

• Resource sharing– 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

Page 11: Operating Systems CSE 411 Process Management Sept. 13 2006 - Lecture 4 Instructor: Bhuvan Urgaonkar

Process Creation (contd.)

• Address space– Child duplicate of parent– Child has a program loaded into it

• 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 12: Operating Systems CSE 411 Process Management Sept. 13 2006 - Lecture 4 Instructor: Bhuvan Urgaonkar

Process Creation (contd.)

Page 13: Operating Systems CSE 411 Process Management Sept. 13 2006 - Lecture 4 Instructor: Bhuvan Urgaonkar

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 14: Operating Systems CSE 411 Process Management Sept. 13 2006 - Lecture 4 Instructor: Bhuvan Urgaonkar

A tree of processes on a typical Solaris

Page 15: Operating Systems CSE 411 Process Management Sept. 13 2006 - Lecture 4 Instructor: Bhuvan Urgaonkar

Process Operations:Process Termination

• Process executes last statement and asks the operating system to delete it (exit)– Output data from child to parent (via wait)– Process’ resources are deallocated by operating

system• Parent may terminate execution of children processes

(abort)– Child has exceeded allocated resources– Task assigned to child is no longer required– If parent is exiting

• Some operating system do not allow child to continue if its parent terminates

– All children terminated - cascading termination

Page 16: Operating Systems CSE 411 Process Management Sept. 13 2006 - Lecture 4 Instructor: Bhuvan Urgaonkar

CPU Scheduling

Page 17: Operating Systems CSE 411 Process Management Sept. 13 2006 - Lecture 4 Instructor: Bhuvan Urgaonkar

CPU Scheduling• OS implements a CPU scheduling algorithm

that decides which process to run when (plus itself!)

• Needs to be online• Set of processes changes dynamically• Needs to be fast, efficient• May need to implement specified policies

– E.g., Run certain processes at a higher priority

Page 18: Operating Systems CSE 411 Process Management Sept. 13 2006 - Lecture 4 Instructor: Bhuvan Urgaonkar

Timers• CPU is a temporal resource

– Other temporal resources? Non-temporal?• OS needs to be aware of the passage of time• Several clocks might be present

– E.g., Intel: Real time clock, Time Stamp Counter, Programmable Interval Timer

– RTC: Battery operated– TSC: All Intel CPUs have a CLK input pin, which receives the clock

signal of an external oscillator• 64-bit TSC register, can be read using the rdtsc assembly instruction• Incremented at each clock signal• E.g., 400 MHz clock => TSC incremented every 2.5 nanosec

– PIT: Like an alarm, the OS sets the frequency at which it interrupts

• Linux programs PIT to send interrupt on IRQ0 every tick time units• Linux/Intel: ~10 msec, Linux/Alpha: ~1 msec• jiffies: a variable in Linux that stores # ticks since startup

Page 19: Operating Systems CSE 411 Process Management Sept. 13 2006 - Lecture 4 Instructor: Bhuvan Urgaonkar

Choosing the Right Scheduling

Algorithm/Scheduling Criteria

• CPU utilization – keep the CPU as busy as possible• Throughput – # of processes that complete their

execution per time unit• Turnaround time – amount of time to execute a

particular process• Waiting time – amount of time a process has been

waiting in the ready queue• Response time – amount of time it takes from

when a request was submitted until the first response is produced, not output (for time-sharing environment)

• Fairness

Page 20: Operating Systems CSE 411 Process Management Sept. 13 2006 - Lecture 4 Instructor: Bhuvan Urgaonkar

When is the scheduler invoked?

• CPU scheduling decisions may take place when a process:1. Switches from running to waiting

state2. Switches from running to ready state3. Switches from waiting to ready4. Terminates

• Scheduling only under 1 and 4: nonpreemptive scheduling

• All other scheduling is preemptive

Page 21: Operating Systems CSE 411 Process Management Sept. 13 2006 - Lecture 4 Instructor: Bhuvan Urgaonkar

Dispatcher• Dispatcher module gives control of the CPU

to the process selected by the short-term scheduler; this involves:– switching context– switching to user mode– jumping to the proper location in the

user program to restart that program• Dispatch latency – time it takes for the

dispatcher to stop one process and start another running

Page 22: Operating Systems CSE 411 Process Management Sept. 13 2006 - Lecture 4 Instructor: Bhuvan Urgaonkar

Example from Linux 2.6.xasmlinkage void __sched schedule(void){

[ . . . ]prepare_arch_switch(rq, next);prev = context_switch(rq, prev, next);barrier();

finish_task_switch(prev);[ . . . ]

}

task_t * context_switch(runqueue_t *rq, task_t *prev, task_t *next){

struct mm_struct *mm = next->mm;struct mm_struct *oldmm = prev->active_mm;

/* Here we just switch the register state and the stack. */ switch_to(prev, next, prev);

return prev;}

#define switch_to(prev,next,last) \ asm volatile(SAVE_CONTEXT \ "movq %%rsp,%P[threadrsp](%[prev])\n\t" /* saveRSP */ \

"movq %P[threadrsp](%[next]),%%rsp\n\t" /* restore RSP */ \"call __switch_to\n\t" \".globl thread_return\n" \"thread_return:\n\t" \"movq %%gs:%P[pda_pcurrent],%%rsi\n\t" \"movq %P[thread_info](%%rsi),%%r8\n\t" \LOCK "btr %[tif_fork],%P[ti_flags](%%r8)\n\t" \"movq %%rax,%%rdi\n\t" \"jc ret_from_fork\n\t" \RESTORE_CONTEXT \

: "=a" (last) \: [next] "S" (next), [prev] "D" (prev), \

[threadrsp] "i" (offsetof(struct task_struct, thread.rsp)), \[ti_flags] "i" (offsetof(struct thread_info, flags)),\

[tif_fork] "i" (TIF_FORK), \[thread_info] "i" (offsetof(struct task_struct, thread_info)), \[pda_pcurrent] "i" (offsetof(struct x8664_pda, pcurrent)) \: "memory", "cc" __EXTRA_CLOBBER)

Page 23: Operating Systems CSE 411 Process Management Sept. 13 2006 - Lecture 4 Instructor: Bhuvan Urgaonkar

First-Come, First-Served Scheduling

(FCFS)Process Burst TimeP1 24 P2 3 P3 3

• Suppose that the processes arrive in the order: P1 , P2 , P3 The Gantt Chart for the schedule is:

• Waiting time for P1 = 0; P2 = 24; P3 = 27• Average waiting time: (0 + 24 + 27)/3 = 17

P1 P2 P3

24 27 300

Page 24: Operating Systems CSE 411 Process Management Sept. 13 2006 - Lecture 4 Instructor: Bhuvan Urgaonkar

FCFS Scheduling (Cont.)

Suppose that the processes arrive in the order P2 , P3 , P1

• The Gantt chart for the schedule is:

• Waiting time for P1 = 6; P2 = 0; P3 = 3• Average waiting time: (6 + 0 + 3)/3 = 3• Much better than previous case• Convoy effect short process behind long process

P1P3P2

63 300