IPC Review

362 阅读1分钟
#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

- 1、FIFO 在文件系统中作为一个特殊的文件而存在,但 FIFO 中的内容却存放在内存中。

- 2、当使用 FIFO 的进程退出后,FIFO 文件将继续保存在文件系统中以便以后使用。

- 3、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, 大进程会减慢整个进程的运行速度