教甄◆電腦科專業題庫下載題庫

上一題
以下的程式中fork()為UNIX的Systemcall, main() { int pid; pid=fork(); printf("My process ID is %dn",pid); pid=fork(); printf("My process ID is %dn",pid); } 請問其執行結果會印出幾行文字?
(A)2
(B)4
(C)6
(D)以上皆非。


答案:登入後觀看
難度: 適中
1F
Eric Juan 高二上 (2012/07/05)

why?
2F
Hsi-chia Tsen 高一上 (2012/09/24)
在 UNIX 中有所謂的 fork() 功能,這個功能主要目的是產生兩個一模一樣的 process 在電腦中同時執行,藉由這種方法可以做出 Concurrent Programming 的效果,所以第一個fork(); 會製造出兩個process。

 int pid;
 pid=fork();
 printf("My process ID is %dn",pid);
所以第一次會印出兩行,同時產生兩個一樣的Process,接著再出現一次

 pid=fork();
 printf("My process ID is %dn",pid);
所以兩個成四個一樣的列印出printf("My process ID is %dn",pid); 的Process,

加上一開始印出的兩行,共6行。

3F
安身立命 國二下 (2016/03/29)
fork 分叉
concurrent 同時發生的

以下的程式中fork()為UNIX的Systemcall, main() {..-阿摩線上測驗