我爲了使不同proccesses運行,但打印的代碼counfuses我一個簡單的message.The結果使用fork()..看看代碼:fork()和標準錯誤與終端
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <math.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <time.h>
int main(void)
{
fork();
fork();
fork();
fprintf(stderr,"hello world\n");
}
,輸出爲:
[email protected]:~/OS$ ./main
hello world
hello world
hello world
hello world
hello world
hello world
[email protected]:~/OS$ hello world
hello world
[email protected]:~/OS$
請注意,我在終端的第一行執行程序,但輸出是不是我的預期。請幫幫我!提前致謝!如果fprintf用printf(「......」)改變,也會發生同樣的情況
編輯:我不明白爲什麼打印是這種方式。在終端線旁邊有6個,之後有1個...
你看過'fork'函數的工作原理嗎?嘗試猜猜爲什麼你使用3把叉子8次'hello world'。提示,8 = 2^3。 –
我知道這個事實!我的問題不是8打印,而是它們被終端命令打亂。我不覺得它合理的程序打印6之前,終端線(馬里奧@ Ubuntu的:〜/ OS $你好世界)旁邊和一個.. – JmRag