操作系统作业4姓名,学号.docx
《操作系统作业4姓名,学号.docx》由会员分享,可在线阅读,更多相关《操作系统作业4姓名,学号.docx(7页珍藏版)》请在冰豆网上搜索。
![操作系统作业4姓名,学号.docx](https://file1.bdocx.com/fileroot1/2022-10/1/ee812fe9-e672-40db-8813-2e8e7c4b9f18/ee812fe9-e672-40db-8813-2e8e7c4b9f181.gif)
橾作系统课柱作业
操作系统作业4
姓名,学号
1.Why istheseparationofmechanismandpolicydesirable?
Answer:
Mechanismandpolicymustbeseparatetoensurethatsystemsareeasytomodify.Notwosysteminstallationsarethesame,soeachinstallationmaywanttotunetheoperatingsystemtosuititsneeds.Withmechanismandpolicyseparate,thepolicymaybechangedatwillwhilethemechanismstaysunchanged.Thisarrangementprovidesamoreflexiblesystem.
2.·whatarethetwomodelsofinterprocesscommunication?
Whatarethestrengthsandweaknessesofthetwoapproaches?
Answer:
Thehvomodelsofintetprocesscommunicationaremessage-passingmodelandtheshared-memorymodel.Messagepassingisusefttlforexchangingsmalleramountsof
da,tabecausenoconflictsne,edbeavoided.Itisalsoeasiertoimplementthanisshared
memoryforintercomputercommunication.Sharedn1emoryallowsmaximumspeedandconvenienceofcommunication,sinceitcanbedoneatmemorytransferspeedswhenittakesplacewithinacomputer.However,thismethodcompromisesonprotectionandsynchronizationbel\veentheprocessessharingmemory
3.Includingtheinitialparentprocess,bowmanyprocessesarecreatedbytheprogramshowninFigurel?
#include#include
int main()
{
int i;
for (i一O;i<4;i++)
fork();
}
returnO;
Figure1:
ProgramforQuestion3.
2
Answer:
16processesarecreated.
Hint:
YoumayincludeprintfOstatementstobetterunderstandbowmanyprocesseshavebeencreated.
4..Explainthecircumstancesunderwhichthelineofcodemarkedprintf("LINEJ")inFigure2willbereached
#include#include#include
int main()
{
pid_t pid;
I*forkachildprocess*Ipid=fork();
if (pid}
elseif (pid==0) { I*childprocess*I
execlp("/bin/ls","ls",NULL);printf("LINEJ");
}
else{/*parentprocess*I
I*parentwillw吐t for the childtocomplete*I
w吐t(NULL);
}
printf("ChildComplete");
}
returnO;
Fi驴re2:
ProgramforQuestion4.
橾作系统课柱作业
3
Answer:
ThecalltoexecOreplacestheaddressspaceoftheprocesswiththeprogramspecifiedastheparametertoexecQ.IfthecalltoexecOsucceeds,thenewprogramisnowrunningandcontrolfromthecalltoexec()neverreturns.ltithisscenario,thelineprintf("LineJ");wouldneverbeperformed.However,ifanerroroccmsinthecalltoexecO,thefunctionren1rnscontrolandthereforthelineprintf("LineJ");wouldbeperformed.
5.UsingtheprograminFigure3,identifythevaluesofpidat1inesA,B,C,andD.(Assumethattheactualpidsoftheparentandchildare2600and2603,respectively.)
#includes
#include#include
intmain{)
{
picLtpid,pidl;
I*forkachildprocess寒/pid frok();
if (pid<0){/•erroroccurred•/fprintf(stderr,"ForkFaile心);return1;
}
elseif(pidsc 0){/•childprocess•/pidl•getpid();
printf("child:
pid=¼d",pid);/亭A*Iprintf("child:
pid1=¼d",p过1);/•B•I
}
else{/•parentprocess•/pidl=getpid();
}
printf("parent:
pid=i.d",pid);/拿C拿/printf("parent:
p工di•i.d",pidl);/*D•I11ait(NULL);
}
returnO;
Figure3:
ProgramforQuestion5.
Answer:
A=0,B=2603,C=260,3D=2600
4
橾作系统课柱作业
6.UsingtheprogramshowninFigure4,explainwhattheoutputwillbeat
linesXandY.
#include<芍s/types.h>#include#include
#defineSIZE5
int nums[SIZE]={0,1,2,3,4};
int main()
{
inti;
pid_t pid;
pid=fork();
if (pid=0){
for (i=O;inums[i] •=-i;
printf("CHILD:
%d11,nums [i]);/•LINEX•/
}
}
elseif (pid>0){
wait(NULL);
for (i =O;i}
rpintf("PARENT:
加 ",nums[i]);I*LINEY•/
}
returnO;
Figure4:
ProgramforQuestion6.
Answer:
Becausethechildisacopyoftheparent,anychangesthechildmakeswilloccurinitscopyofthedataandwon'tbereflectedintheparent.Asaresult,thevaluesoutputbythechildatlineXare0,-1,-4,-9,-16.ThevaluesoutputbytheparentatlineYare0,I,2,3,4
5
操作系统课杠作业
7.Whichofthefollowingcomponentsofprogramstatearesharedacrossthreadsinamultithreadedprocess?
a.Registervalues
b.Heapmemory
c.Globalvariables
d.Stackmemory
Answer:
Thethreadsofamultithreadedprocessshareheapmemoryandglobalvariables.Eachthreadhasitsseparatesetofregistervaluesandaseparatestack.
8.Asystemwithtwodual-coreprocessorshasfourprocessorsavailableforscheduling.ACPU-intensiveapplicationisrunningonthissystem.Allinputisperformed