You are on page 1of 2

CS3103 Operating Systems Programming Assignment 1

Goals
Bycompletingthisassignment,youshouldknowhowtodevelopaprograminLinuxplatformandtomake
systemcallsrelatedtoprocessmanagement.

Introduction
YouneedtoimplementaC++programcalledBPthatallowstheusertorunaprocessatthebackground.
Whiletheprocessisrunningatthebackground,theusercaninputcommandtodisplayinformationofthe
backgroundprocessorkillthebackgroundprocess.

Requirements
1. YourBPneedstoshowthepromptforuserinputasfollows.
cs3103-01:/home/lec/vlee> BP
BP >

2. BPacceptsthefollowingcommandsfromtheuserandtakesthecorrespondingaction.

BP >bg [name of executable file] [a list of arguments]


Action:BPrunstheexecutablefilewithalistofargumentsatthebackgroundandcontinuesto
acceptinputfromtheuser.Inthefollowingexample,BPrunstheexecutablefiledemowithalistof
arguments:running 2 5atthebackground.
BP >bg demo running 2 5

BP >bginfo
Action:BPdisplaysthepidandnameofthebackgroundprocessonthescreen.Forexample,
123: demo

BP >bgkill
Action:BPterminatesthebackgroundprocessanddisplaysamessageonthescreen.Forexample,
123: demo killed

BP >exit
Action:BPexecutesbgkill,ifthereisanybackgroundprocess,andexits.

3. Incasetheuserinputsacommandwhenthemostrecentbackgroundprocesshascompleted,BP
displaysamessageonthescreen.Forexample,
123: demo completed

4. Incasetheuserinputsacommandwhenthereisnobackgroundprocess,BPdisplaysthefollowing
messageonthescreen.
No background process

5. Iftheuserinputsanewbackgroundprocesswhilethereisarunningbackgroundprocess,BPrejectsthe
requestanddisplaysamessageonthescreen.Forexample,
123: demo is running, no new background process allowed.


CS3103 Operating Systems Programming Assignment 1

Hints
Youmayusefork()andexecvp()sothattheparentprocessacceptsuserinputandthechild
processexecutesthebackgroundprocess.
Whenyouwillusefork(),itisimportantthatyoudonotcreateafork()bomb,whicheasily
eatsupalltheresourcesallocatedtoyou.Ifthishappens,youcantrytousethecommandkill
toterminateyourprocesses(http://cslab.cs.cityu.edu.hk/supports/unixstartupguide).However,if
youcannotlogintoyouraccountanymore,youneedtoaskCSLabforhelptokillyourprocesses.
YoumaysendtheTERMsignalviathekill()systemcalltoterminateaprocess.
Youmayusewaitpid()andtheWNOHANGoptiontocheckthestateofthebackgroundprocess.
Youshouldstudythemanpagesofthesystemcallsbeforecallingtheminyourprogram.For
example,thefollowingcommanddisplaysthemanpagesofwaitpid()inSection2.
cs3103-01:/home/lec/vlee> man 2 waitpid

Helperprograms
demo.cpp
ThisdemoprogramcanbeusedtoactasabackgroundprocessfortestingyourBPasitsexecution
canbevisualizedbydisplayingawordeveryfewsecondsanumberoftimes.
Thisprogramtakesthreearguments,word,intervalandtimes.
Thefirstargumentwordisasinglewordtobedisplayedrepeatedly.
Thesecondargumentintervalisthenumberofsecondsbetweentwoconsecutivedisplaysof
theword.
Thethirdoptionalargumenttimesisthenumberoftimesthewordtobedisplayed.Ifthis
argumentismissing,thewordwillbedisplayedinfinitenumberoftimes.
Forexample,thefollowingcommanddisplaysthewordrunning5timesin2secondinterval.
cs3103-01:/home/lec/vlee> demo running 2 5

args.cpp
Thisexampleprogramshowsyouhowtodisplayalistofallargumentspassedtoit.
Tocompiletheprogram,usethefollowingcommand.
cs3103-01:/home/lec/vlee> g++ args.cpp -lreadline -lhistory -o
args

Marking
YouprogramwillbetestedontheLinuxservercs310301.Ifanexecutablefilecannotbegenerated
andrunningsuccessfullyontheLinuxserver,itwillbeconsideredasunsuccessful.
Markingscheme:
o bg 30%
o bginfo 20%
o bgkill 20%
o exitanderrorhandling 20%
o programmingstyleandinprogramcomments 10%

Submission
Thisassignmentistobedoneindividually.Youareencouragedtodiscussthedesignofyoursolution
withyourclassmatesbutyoumustimplementtheprogramonyourown.
Eachsubmissionconsistsoftwofiles:asourceprogramfile(.cppfile)andatextfilecontainingall
possibleoutputsproducedbyyourprogram(.txtfile).
UseyourstudentIDtonameyoursubmittedfiles,suchas5xxxxxxx.cpp,5xxxxxxx.txt.
SubmitthefilestoCanvas.
Thedeadlineis10:00a.m.,16FEB17(Thursday).Nolatesubmissionwillbeaccepted.