Created
October 2, 2020 05:49
-
-
Save y56/2a0a5f6a81c0d978d035a2eb892f09c1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
關於緩衝區 | |
上課的時候老師提到了一個例子,讓我們回去自己試試 | |
#include <stdio.h> | |
#include <unistd.h> | |
int main(){ | |
pid_t pid; | |
//第一種 | |
printf("start..."); | |
//第二種 | |
//printf("start...\n"); | |
pid = fork(); | |
if(pid==0){ | |
printf("child process\n"); | |
} | |
else if (pid>0){ | |
printf("parent process\n"); | |
} | |
else{ | |
//error 這裡不作處理 | |
} | |
} | |
可以看到我們在fork()呼叫前寫了一局printf,兩種情況唯一的區別就是多了個’\n’。 | |
輸出結果: | |
第一種: | |
start…parent process | |
start…child process | |
第二種: | |
start… | |
parent process | |
child process | |
看到這個結果我就覺得可能是’\n’能重新整理/清除緩衝區之類的原因吧。然後問了下老師。 | |
我:pcb中是否存在類似輸出緩衝區的資訊,而換行可以重新整理/清空緩衝區?所以執行fork()時,"start…"還在緩衝區沒有真正地被輸出在顯示器上,就被複制到子程序的pcb中的緩衝區去了。 | |
老師:如果不強制清除緩衝區,想當於子程序不僅copy了父程序的記憶體、PCB,還copy了緩衝區的內容。\n 或者flush都是強制清除緩衝區,所以加\n就是清除之後再執行下一條指令。如果不加\n,提交到緩衝區就接著執行下一條指令了。 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment