#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
int main() {
int fh[2];
pipe(fh);
FILE *reader = fdopen(fh[0], "r");
FILE *writer = fdopen(fh[1], "w");
pid_t p = fork();
if (p > 0) {
int score;
fscanf(reader, "Score %d", &score);
printf("The child says the score is %d\n", score);
} else {
fprintf(writer, "Score %d", 10 + 10);
fflush(writer);
}
return 0;
}
複製代碼
If parent want to write and read from child. It need two pipes.
SIGPIPE if no one listening.
If pipe is full and there are no readers, all write will fail until we got new reader. 1.Increase pipe size. 2.Keep read
Pipe is thread safe, kernel will have inner mutex, but if there are
Pipe lifetime: once all procsses closed, pipe will be freed
Named pipe FIFO
- 一、FIFO 在文件系統中做爲一個特殊的文件而存在,但 FIFO 中的內容卻存放在內存中。
- 二、當使用 FIFO 的進程退出後,FIFO 文件將繼續保存在文件系統中以便之後使用。
- 三、FIFO 有名字,不相關的進程能夠經過打開命名管道進行通訊。
int mkfifo(const char *pathname, mode_t mode);
FIFO will block直到有read出現(若是Program 1的open是read和write,他不會等reader)
複製代碼
若是存在prioritization,會出現餓死的狀況
A process will assign to ready queue when it is able to use CPU
turnaround time = end - arrival
response time = start - arrival
wait time = turnaround time - 等菜時間
Convoy Effect 聽從 FCFS, 大進程會減慢整個進程的運行速度
複製代碼