Homework Analysis - SJTUfwu/teaching/res2016/OS-HW-Analysis-2016.pdf · Homework Analysis Homework1...

20
Operating System Spring, 2016 Fan Wu & Yuanyuan Li Homework Analysis

Transcript of Homework Analysis - SJTUfwu/teaching/res2016/OS-HW-Analysis-2016.pdf · Homework Analysis Homework1...

Page 1: Homework Analysis - SJTUfwu/teaching/res2016/OS-HW-Analysis-2016.pdf · Homework Analysis Homework1 ... smoking and signal(smoking). Non-smokers can notenter the lounge if some smokers

Operating System

Spring, 2016

Fan Wu & Yuanyuan Li

Homework Analysis

Page 2: Homework Analysis - SJTUfwu/teaching/res2016/OS-HW-Analysis-2016.pdf · Homework Analysis Homework1 ... smoking and signal(smoking). Non-smokers can notenter the lounge if some smokers

Homework Analysis Homework1 Problem 1&2

Frequent Mistakes

Homework2 Problems 1&2

Frequent Mistakes

Page 3: Homework Analysis - SJTUfwu/teaching/res2016/OS-HW-Analysis-2016.pdf · Homework Analysis Homework1 ... smoking and signal(smoking). Non-smokers can notenter the lounge if some smokers

Homework 1: P1

Ready Queue: P1P3

Page 4: Homework Analysis - SJTUfwu/teaching/res2016/OS-HW-Analysis-2016.pdf · Homework Analysis Homework1 ... smoking and signal(smoking). Non-smokers can notenter the lounge if some smokers

Ready Queue: P1P3

top

Page 5: Homework Analysis - SJTUfwu/teaching/res2016/OS-HW-Analysis-2016.pdf · Homework Analysis Homework1 ... smoking and signal(smoking). Non-smokers can notenter the lounge if some smokers

We specify that: when a preempted process and new

coming process need to be put in the ready queue at the same time, we prefer to put the new coming process in front of the preempted process in the ready queue.

Ready Queue: P3P1

top

Page 6: Homework Analysis - SJTUfwu/teaching/res2016/OS-HW-Analysis-2016.pdf · Homework Analysis Homework1 ... smoking and signal(smoking). Non-smokers can notenter the lounge if some smokers

Homework 1: P2 MultilevelFeedback Queue Scheduling

Q0: P2Q1: P1Q2:

Q0: P5Q1: P4, P2Q2:

If a process does not use up its quantum in the current level, it will keep its current queuing level and be put in to the end of the queue.

(queue 0: quantum 8), (queue 1: quantum 16), (queue 2: FCFS).

Page 7: Homework Analysis - SJTUfwu/teaching/res2016/OS-HW-Analysis-2016.pdf · Homework Analysis Homework1 ... smoking and signal(smoking). Non-smokers can notenter the lounge if some smokers

Frequent Mistakes, Attention Calculation!

Distinct turn around time and waiting time.

Average.

Double check your Gantt Chart!

You should know how FCFS/SJF/Priority/RR/Multilevel Feedback Queue work.

You should know what is “preemptive”.

You should know definition of context switch: the process switch not caused by a voluntary yielding of CPU from the running process

Page 8: Homework Analysis - SJTUfwu/teaching/res2016/OS-HW-Analysis-2016.pdf · Homework Analysis Homework1 ... smoking and signal(smoking). Non-smokers can notenter the lounge if some smokers

Homework 2: P1 Smokers can enter the lounge at any time if no non-smokers are waiting to enter the lounge.

If smokers in the lounge want to smoke, they must make sure that non-smokers are absent.

If there are some non‐smokers who are waiting to enter the lounge, the smokers canot enter the lounge any more.

The smokers in the lounge will finally finish smoking and signal(smoking).

Non-smokers can not enter the lounge if some smokers are smoking in the lounge.

Page 9: Homework Analysis - SJTUfwu/teaching/res2016/OS-HW-Analysis-2016.pdf · Homework Analysis Homework1 ... smoking and signal(smoking). Non-smokers can notenter the lounge if some smokers

Functional HW2: P1

Smokers cannot smoke in front of non-smokers.

Non-smokers cannot enter when smokers are smoking.

In this problem, and we allow that the smokers may be starved

Read the description carefully and double check your solution.

Similar to reader-writer problem.

Page 10: Homework Analysis - SJTUfwu/teaching/res2016/OS-HW-Analysis-2016.pdf · Homework Analysis Homework1 ... smoking and signal(smoking). Non-smokers can notenter the lounge if some smokers

Variables and Semaphores

Variable: int smokingCount=nonSmoCount=0;//the number of smokers(smoking) and nonsmokers in the lounge.

Semaphore: Semaphore mutexSmoker=mutexNonSmoker =smoking=enter=1;

Page 11: Homework Analysis - SJTUfwu/teaching/res2016/OS-HW-Analysis-2016.pdf · Homework Analysis Homework1 ... smoking and signal(smoking). Non-smokers can notenter the lounge if some smokers

Pseudo code

nonSmoker:

enterlounge(false)

{

wait(enter);

wait(mutexNonSmoker);

if(nonSmoCount==0)

wait(smoking);

nonSmoCount++;

signal(mutexNonSmoker);

signal(enter);

}

chat()

leaveLounge(false){wait(mutexNonSmoker);nonSmoCount‐‐;if(nonSmoCount==0)signal(smoking);signal(mutexNonSmoker);}

Page 12: Homework Analysis - SJTUfwu/teaching/res2016/OS-HW-Analysis-2016.pdf · Homework Analysis Homework1 ... smoking and signal(smoking). Non-smokers can notenter the lounge if some smokers

Pseudo code

Smokers:

enterlounge(true);

{

wait(enter);

signal(enter);

}

//chat()

smoke()

leaveLounge(true);

smoke(){wait(mutexSmoker)if(smokingCount==0)wait(smoking);smokingCount++;signal(mutexSmoker);//smokewait(mutexSmoker);smokingCount‐‐;If(smokingCount==0)signal(smoking);signal(mutexSmoker);}

Page 13: Homework Analysis - SJTUfwu/teaching/res2016/OS-HW-Analysis-2016.pdf · Homework Analysis Homework1 ... smoking and signal(smoking). Non-smokers can notenter the lounge if some smokers

Homework 2: P2 Searchers can work together. First searcher wait(delete) in case there is a deleter.

Last searcher signal(delete)

Inserters are mutually exclusive, but safe with searchers. Inserters have nothing to do with searchers.

Deleter blocks any other user. Deleter should block searchers and inserters after him (to avoid starvation).

In this problem, and we allow that the smokers may be starved (however you should be aware of the starvation).

Page 14: Homework Analysis - SJTUfwu/teaching/res2016/OS-HW-Analysis-2016.pdf · Homework Analysis Homework1 ... smoking and signal(smoking). Non-smokers can notenter the lounge if some smokers

Functional HW2: P2

Mutual exclusion conditions

Read the description carefully and double check your solution.

Page 15: Homework Analysis - SJTUfwu/teaching/res2016/OS-HW-Analysis-2016.pdf · Homework Analysis Homework1 ... smoking and signal(smoking). Non-smokers can notenter the lounge if some smokers

Pseudo code: Deleters

Deleter should have 2 semaphores

Block after processes

Blocked by current search or inserters

#For deleterwait(del);wait(sch);//deleting……signal(sch);signal(del);

Page 16: Homework Analysis - SJTUfwu/teaching/res2016/OS-HW-Analysis-2016.pdf · Homework Analysis Homework1 ... smoking and signal(smoking). Non-smokers can notenter the lounge if some smokers

Pseudo code: Searchers&Inserters

#For searcher:

wait(del);

wait(mutex);

prev=active;

active++;

signal(mutex);

if(prev==0)

wait(sch);

signal(del);

//searching……

wait(mutex);

active--;

current=active;

signal(mutex);

if(current==0)

signal(sch);

#For inserter:wait(ins);wait(del);wait(mutex);prev=active;active++;signal(mutex);if(prev==0)wait(sch);

signal(del);//inserting……

wait(mutex);active--;current=active;signal(mutex);if(current==0)signal(sch);

signal(ins);

Page 17: Homework Analysis - SJTUfwu/teaching/res2016/OS-HW-Analysis-2016.pdf · Homework Analysis Homework1 ... smoking and signal(smoking). Non-smokers can notenter the lounge if some smokers

Mutual Exclusion: protect critical section

you should know that the counters should be protected.

wait(mutex);prev=active;active++;signal(mutex);

wait(mutexSmoker);if(smokingCount==0)wait(smoking);smokingCount++;signal(mutexSmoker);

Page 18: Homework Analysis - SJTUfwu/teaching/res2016/OS-HW-Analysis-2016.pdf · Homework Analysis Homework1 ... smoking and signal(smoking). Non-smokers can notenter the lounge if some smokers

Dead Lock Free Refer to Slide06 P26

Avoid using

Please review your course slides.

Page 19: Homework Analysis - SJTUfwu/teaching/res2016/OS-HW-Analysis-2016.pdf · Homework Analysis Homework1 ... smoking and signal(smoking). Non-smokers can notenter the lounge if some smokers

Starvation Free

Deleter should have 2 semaphores

Block after processes

Blocked by current search or inserters

Different from non-smokers, non-smokers are not blocked by non-smokers.

#For deleterwait(del);wait(sch);//deleting……signal(sch);signal(del);#For searcher:

wait(del);

wait(mutex);

prev=active;

active++;

signal(mutex);

if(prev==0)

wait(sch);

signal(del);

//searching……

Page 20: Homework Analysis - SJTUfwu/teaching/res2016/OS-HW-Analysis-2016.pdf · Homework Analysis Homework1 ... smoking and signal(smoking). Non-smokers can notenter the lounge if some smokers

Frequent Mistakes Dead Lock

Starvation (we allow starvation here)

Busy Waiting

using while loops

Miss some question