Transcript 下載/瀏覽
包含最初父行程,圖3.16中的程式建立了多少行程?
#include<stdio.h>
#include<unistd.h>
int main()
{
/* fork a child process */
fork();
/* fork another child process */
fork();
/* and fork another */
fork();
return 0;
}
↑圖3.16 建立多少行程?
3.10 ANS
Ans:8個行程
使用圖3.17中的程式,以辨認pid的值在行A、B、C和D。
(假定實際父行程與子行程的pids分別為2600和2603)
#include<sys/types.h>
#include<stdio.h>
#include<unistd.h>
int main()
{
pid_t pid,pid1;
/* fork a child process */
pid = fork();
if (pid < 0) { /* error process */
fprintf(stderr, "Fork Failed");
return 1;
}
else if (pid == 0) { /* child process */
pid1 = getpid();
printf("child: pid = %d\n",pid); /* A */
printf("child: pid1 = %d\n",pid1); /* B */
}
else { /* parent process */
pid1 = getpid();
printf("parent: pid = %d\n",pid); /* C */
printf("parent: pid1 = %d\n",pid1); /* D */
wait(NULL);
}
return 0;
}
↑圖3.17 pid值為何?
3.11 ANS
Ans: A=0
B=2603
C=2603
D=2600
使用圖3.18解釋 Line A 將輸出什麼?
#include<sys/types.h>
#include<stdio.h>
#include<unistd.h>
int value = 5;
int main()
{
pid_t pid;
pid = fork();
if (pid == 0) { /* child process */
value += 15;
return 0;
}
else if (pid > 0) { /* parent process */
wait(NULL);
printf("PARENT: value = %d",value); /* LINE A */
return 0;
}
}
↑圖3.18 Line A 將輸出 什麼?
3.12 ANS
Ans:
PARENT: value = 5