基本概念
管道
FIFO
System V 的 IPC 机制
共享内存
信号量
===============================================================================
#include <unistd.h>
int pipe(int filedes[2]);
------------------------------------------------------------------------------
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#define MAXLINE 256
void err_sys (const char* info)
{
perror (info);
exit (1);
}
int mian (void)
{
int n, fd [2];
pid_t pid;
char line [MAXLINE];
if (pipe (fd) < 0)
err_sys ("pipe error");
if ( (pid = fork ()) < 0)
err_sys ("fork error");
else if (pid > 0) { // parent
close (fd [0]);
write (fd [1], "hello world\n", 12);
}
else { // child
close (fd [1]);
n = read (fd[0], line, MAXLINE);
write (STDOUT_FILENO, line, n);
}
exit (0);
}
===============================================================================
===============================================================================
* 标识符用来标识 IPC 对象.
* 利用 msgget, semget, shmget 等函数建立 IPC 对象时, 必须指定键. 指定建为
IPC_PRIVATE 时, 可建立新的 IPC 对象.
* 作为服务器的进程建立新的 IPC 对象之后, 可将返回的标识符写入一个预先约定
的文件, 从而可让客户获得标识符; 或者, 在关系进程之间, 可通过命令行参数
传递标识符.
* 两个进程可约定同一个键, 比如通过共同的头文件, 服务器负责事先建立新 IPC 对象,
客户只许通过键获得标识符.
* 两个进程可通过 ftok 函数生成键. 该函数利用一个文件名和项目 ID 工作.
===============================================================================
![]()