Вы находитесь на странице: 1из 3

CS3103 Operating Systems Programming Assignment 2

Introduction
Considerafactorywith36machines,whicharearrangedin6rowsand
6columns(i.e.,6machinesineachrow).Themanagerofthefactory
assignsqualitycheck(QC)taskstothesemachines.Eachmachine
takessometimetofinishitsQCtaskandrecordstheresultonthe
resultsheet.

Tasks
UsingPthreads,designandimplementamultithreadedC++program
underLinuxthatworksasfollows:

Amasterthreadiscreatedtomodelthemanagerofthefactory.
o ThemanagerhastoassignN(36)QCtaskstomachinesinrowmajororderintheformof
[row,column]where1row6and1column6(Hint1).
o ThemanagerhastowaituntilalltheQCtasksarefinished.
o WhenalltheQCtasksarefinished,themanagerhastochecktheresultsheettofindoutthe
numberofunqualifiedproducts(Hint4).
EachQCtaskismodeledwithaworkerthread(createdbythemasterthread).
o EachQCtaskisassignedamachinenumberintheformof[row,column]where1row
6and1column6(Hint1).
o Eachmachinetakessometimes,rangingfrom5to10seconds,tofinishitsQCtask(Hint2).
o WhenamachinefinishesitsQCtask,ithastorecordtheresult(Qstandsforqualifiedand
Ustandsforunqualified)ontheresultsheet,accordingtoitsmachinenumber(Hint4).
Suppose,onaverage,90%oftheproductsarequalified(Hint3).

Hints
1. Passingparameterstoeachthread:Whenthemasterthread(manager)createstheNworkerthreads
(QCtasks),itpassestoeachworkerthreadamachinenumberinrowmajororder((1,1),(1,2),(1,3),(1,
4),(1,5),,(6,6)).Thissteprequirespassingtwonumbers(rowandcolumn)toeachworkerthread.
2. SimulatingthetimetakentofinishaQCtask:TosimulatethetimetakenforamachinetofinishitsQC
task,youcanmaketheworkerthreadsleepforarandomperiodoftime.Forexample,
sleep(rand()%6+5)canbeusedtosimulatethetimetakentofinishaQCtask,rangingfrom5to10
seconds(http://linux.die.net/man/3/sleep,
http://www.tutorialspoint.com/cplusplus/cpp_numbers.htm).
3. Simulatingtheproportionofqualifiedproducts:Tosimulateonaverage90%oftheproductsare
qualified,youcancheckthecondition:rand()%100 < 90ineachworkerthread.Iftrue,the
productisqualified;otherwise,theproductisunqualified.
4. Returningresultstothemasterthread:TheresultsheetisusedtorecordtheresultsoftheQCtasks.
Onewaytohandlethisistocreateamatrix(resultsheet)thatisaccessibletoallthreads.Ifaproductis
qualified,theworkerthreadsetsitscorrespondingentrytoQ.Otherwise,theworkerthreadsetsthe
entrytoU.AdefaultentryofIindicatesanidlemachine.WhenallQCtasksarefinished,themaster
threadcountsthenumberofentrieswithUinthematrixtodeterminethenumberofunqualified
products.
5. Passingcommandlineargumentstoyourprogram:Topasscommandlinearguments(whichare
characterarrays)intoyourprogramandconverttheargumentsintonumbers,read
http://www.site.uottawa.ca/~lucia/courses/213105/labs/Lab3/CommandLineArguments.html.
CS3103 Operating Systems Programming Assignment 2

Flowchart(forreference)

Masterthread Workerthread

Start

Input,
e.g.,qc30

False
Errorhandle Inputislegal?
Start
Ture

Use
pthread_create() Usesleepto
createworker simulatetimetaken
threads tofinishaQC task

Utilizepthead_join() checkrand()%100 <


towaituntilallthe 90 todetermine
worker threadsare whethermark
finished correspondingentry
toQ orU

Checktheresult
sheet,printthe
numberof End
unqualified
products

End


CS3103 Operating Systems Programming Assignment 2

Programrequirementsandmarkingscheme
Designanduseofmultithreading(60%)
o Complete,correctandthreadsafemultithreadeddesignandimplementationincluding
creationandterminationofmasterthread
creationandterminationofworkerthreads
passingparameterstoworkerthreads
returningresultstomasterthread
o Nonmultithreadedimplementation(0%)
Programcorrectness(30%)
o Completeandcorrectimplementationofotherfeaturesincluding
correctlogicandcodingofthreadfunctions
passingN(numberofQCtasks)totheprogramonthecommandline
timetakentofinishaQCtask
onaverage90%ofproductsarequalified
programinputandoutputconformtotheformatofthesamplebelow
successfulprogramtermination
o Failtopasstheg++complieronourcourseLinuxservercs310301.cs.cityu.edu.hkto
generatearunnableexecutablefile(0%)
Programmingstyleanddocumentation(10%)
o Goodprogrammingstyle
o Clearcommentsintheprogramtodescribethedesignandlogic(noneedtosubmita
separatefilefordocumentation)
o Programcompletedinonefile
o Unreadableprogramwithoutanycomment(0%)

Input/Outputsample
>qc 30
QQQQUQ
QQQQQQ
QUQQQQ
QQQQQQ
QQQQUQ
IIIIII
3 products are unqualified.

Submission
Thisassignmentcanbedoneindividuallyorinagroupoftwomembers.Thegroupingmustremain
theSAMEforProgrammingAssignment3.
Eachsubmissionconsistsoftwofiles:asourceprogramfile(.cppfile)andatextfilecontaining
differentinput/outputsamplesgeneratedbyyourprogram(.txtfile).
UseyourstudentID(s)tonameyoursubmittedfiles,suchas5xxxxxxx.cpp,5xxxxxxx.txtforindividual
submission,or5xxxxxxx_5yyyyyyy.cpp,5xxxxxxx_5yyyyyyyy.txtforgroupsubmission.OnlyONE
submissionisrequiredforeachgroup.
SubmitthefilestoCanvas.
Thedeadlineis10:00a.m.,8MAR17(Wednesday).Nolatesubmissionwillbeaccepted.

Questions?
ContactourTAMr.ZHOUZimengatzmzhou4c@my.cityu.edu.hkorthecourselecturer.