操作系统作业4姓名,学号Word格式.docx
《操作系统作业4姓名,学号Word格式.docx》由会员分享,可在线阅读,更多相关《操作系统作业4姓名,学号Word格式.docx(7页珍藏版)》请在冰豆网上搜索。
stdio.h>
#include<
unistd.h>
int main()
{
int i;
for (i一O;
i<
4;
i++)
fork();
}
returnO;
Figure1:
ProgramforQuestion3.
2
16processesarecreated.
Hint:
YoumayincludeprintfOstatementstobetterunderstandbowmanyprocesseshavebeencreated.
4..Explainthecircumstancesunderwhichthelineofcodemarkedprintf("
LINEJ"
)inFigure2willbereached
#include<
sys/types.h>
#include<
stdio.h>
unistd.h>
int main()
pid_t pid;
I*forkachildprocess*Ipid=fork();
if (pid<
O){I*erroroccurred*Ifprintf(stderr,"
ForkFailed"
);
return1;
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"
Fi驴re2:
ProgramforQuestion4.
橾作系统课柱作业
3
ThecalltoexecOreplacestheaddressspaceoftheprocesswiththeprogramspecifiedastheparametertoexecQ.IfthecalltoexecOsucceeds,thenewprogramisnowrunningandcontrolfromthecalltoexec()neverreturns.ltithisscenario,thelineprintf("
LineJ"
wouldneverbeperformed.However,ifanerroroccmsinthecalltoexecO,thefunctionren1rnscontrolandthereforthelineprintf("
);
wouldbeperformed.
5.UsingtheprograminFigure3,identifythevaluesofpidat1inesA,B,C,andD.(Assumethattheactualpidsoftheparentandchildare2600and2603,respectively.)
#includes<
ys/types.h>
stdio.h>
unistd.h>
intmain{)
picLtpid,pidl;
I*forkachildprocess寒/pid frok();
0){/•erroroccurred•/fprintf(stderr,"
ForkFaile心);
elseif(pidsc 0){/•childprocess•/pidl•getpid();
printf("
child:
pid=¼
d"
pid);
/亭A*Iprintf("
child:
pid1=¼
p过1);
/•B•I
else{/•parentprocess•/pidl=getpid();
parent:
pid=i.d"
/拿C拿/printf("
p工di•i.d"
pidl);
/*D•I11ait(NULL);
returnO;
Figure3:
ProgramforQuestion5.
A=0,B=2603,C=260,3D=2600
4
6.UsingtheprogramshowninFigure4,explainwhattheoutputwillbeat
linesXandY.
芍s/types.h>
#include<
#defineSIZE5
int nums[SIZE]={0,1,2,3,4};
inti;
pid=fork();
if (pid=0){
for (i=O;
i<
SIZE;
i++){
nums[i] •=-i;
printf("
CHILD:
%d11,nums [i]);
/•LINEX•/
elseif (pid>
0){
wait(NULL);
for (i =O;
i++)
rpintf("
PARENT:
加 "
nums[i]);
I*LINEY•/
Figure4:
ProgramforQuestion6.
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
Thethreadsofamultithreadedprocessshareheapmemoryandglobalvariables.Eachthreadhasitsseparatesetofregistervaluesandaseparatestack.
8.Asystemwithtwodual-coreprocessorshasfourprocessorsavailableforscheduling.ACPU-intensiveapplicationisrunningonthissystem.Allinputisperformed