Smoking Cessation in the Military: Challenges, Solutions, And Issues for Women Smokers
Homework Analysis - SJTUfwu/teaching/res2016/OS-HW-Analysis-2016.pdf · Homework Analysis Homework1...
Transcript of Homework Analysis - SJTUfwu/teaching/res2016/OS-HW-Analysis-2016.pdf · Homework Analysis Homework1...
Operating System
Spring, 2016
Fan Wu & Yuanyuan Li
Homework Analysis
Homework Analysis Homework1 Problem 1&2
Frequent Mistakes
Homework2 Problems 1&2
Frequent Mistakes
Homework 1: P1
Ready Queue: P1P3
Ready Queue: P1P3
top
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
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).
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
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.
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.
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;
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);}
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);}
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).
Functional HW2: P2
Mutual exclusion conditions
Read the description carefully and double check your solution.
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);
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);
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);
Dead Lock Free Refer to Slide06 P26
Avoid using
Please review your course slides.
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……
Frequent Mistakes Dead Lock
Starvation (we allow starvation here)
Busy Waiting
using while loops
Miss some question