一、管道(PIPE)
1.1 函数
- int pipe(int file_descriptor[2]); //建立管道,该函数在数组上填上两个新的文件描述符后返回0,失败返回-1。
file_descriptor[0]用于读出数据,读取时必须关闭写入端,即close(file_descriptor[1]);
file_descriptor[1]用于写入数据,写入时必须关闭读取端,即close(file_descriptor[0]);
只能用于父子进程或者兄弟进程之间(具有亲缘关系的进程).比如fork或exec创建的新进程.
管道是半双工的,数据只能向一个方向流动;需要双方通信时,需要建立起两个管道.
- ssize_t read(int fd, void * buf, size_t count); //读数据file_descriptor[0]
- ssize_t write (int fd, const void * buf, size_t count); //写数据file_descriptor[1]
1.2 源码
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
typedef struct _IPC_MESSAGE{
int len;
char *body;
}IPC_MSG;
int ipc_read(int fd){
IPC_MSG rMsg;
read(fd,&rMsg.len,sizeof(int));
printf("[read]msg len:%d\n",rMsg.len);
int lenBody = rMsg.len - sizeof(int);
rMsg.body = (char*)malloc(lenBody);
//printf("[read]body len:%d\n",lenBody);
int ret = read(fd,(void*)rMsg.body,lenBody);
//printf("[read]body ret:%d\n",ret);
rMsg.body[lenBody] = '\0';
printf("[read]msg body:%s\n",rMsg.body);
return 0;
}
int ipc_write(int fd,char* buffer){
printf("[write]msg body:%s\n",buffer);
IPC_MSG wMsg;
wMsg.body = buffer;
wMsg.len = sizeof(int) + (strlen(wMsg.body)+1);
printf("[write]msg len:%d\n",wMsg.len);
write(fd,(void*)&wMsg.len,sizeof(int));
write(fd,(void*)wMsg.body,strlen(wMsg.body)+1);
}
int test_ipc_pipe(){
printf("Test for pipe\n");
pid_t pid;
int fd[2];
if (pipe(fd) == -1){
printf("fail to pipe\n");
return -1;
}
pid = fork();
if(pid < 0){
printf("fail to fork\n");
return -1;
}else if(pid == 0){ //子进程
printf("this is child process,will do read\n");
close(fd[1]); //读取时必须关闭写入端
ipc_read(fd[0]);
close(fd[0]);
}else if(pid > 0){ //父进程
printf("this is parent process,will do write\n");
close(fd[0]); //写入时必须关闭读取端
char *bufferWrite = "pipe:hello,world!";
ipc_write(fd[1],bufferWrite);
close(fd[1]);
}
}
int main(int argc,char* argv[]){
test_ipc_pipe();
return 0;
}
二、命名管道(FIFO)
2.1 函数
- int mkfifo(const char *filename,mode_t mode); //建立一个名字为filename的命名管道,参数mode为该文件的权限(mode%~umask),若成功则返回0,否则返回-1,错误原因存于errno中。
有名管道以FIFO文件形式存在于文件系统中,这样即使与FIFO创建进程不存在血缘关系的进程,只要访问该路径,就能够通 过FIFO通信
- int open(const char *filename, int flags); //O_RDONLY只读打开;O_WRONLY 只写打开;O_RDWR 可读可写打开
创建一个命名通道,我们必须用open函数来显示地建立连接到管道的通道
- ssize_t read(int fd, void * buf, size_t count); //fd为open时O_RDONLY方式
- ssize_t write (int fd, const void * buf, size_t count); //fd为open时O_WRONLY 方式
2.2 源码
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/msg.h>
//FIFO
typedef struct _IPC_MESSAGE{
int len; //消息的长度,而不是整个结构体的长度,即body的长度
char *body;
}IPC_MSG;
int ipc_read(int fd){
IPC_MSG rMsg;
read(fd,&rMsg.len,sizeof(int));
printf("[read]msg body len:%d\n",rMsg.len);
int lenBody = rMsg.len;
rMsg.body = (char*)malloc(lenBody);
//printf("[read]body len:%d\n",lenBody);
int ret = read(fd,(void*)rMsg.body,lenBody);
//printf("[read]body ret:%d\n",ret);
rMsg.body[lenBody] = '\0';
printf("[read]msg body:%s\n",rMsg.body);
return 0;
}
int ipc_write(int fd,char* buffer){
printf("[write]msg body:%s\n",buffer);
IPC_MSG wMsg;
wMsg.body = buffer;
wMsg.len = (strlen(wMsg.body)+1);
printf("[write]msg body len:%d\n",wMsg.len);
write(fd,(void*)&wMsg.len,sizeof(int));
write(fd,(void*)wMsg.body,strlen(wMsg.body)+1);
}
(1)send process
int test_ipc_fifo_snd(){
printf("Test for fifo\n");
const char* fifo_path = "/work/7_Learning/3_ipc/fifo";
if(mkfifo(fifo_path, 0666) < 0 && errno != EEXIST){
printf("fifo:%s error\n",fifo_path);
return -1;
}
char *bufferWrite = "fifo:hello,world!";
int wfd = open(fifo_path, O_WRONLY);
ipc_write(wfd,bufferWrite);
close(wfd);
}
int main(int argc,char* argv[]){
test_ipc_fifo_snd();
return 0;
}
(2)receive process
int test_ipc_fifo_rcv(){
printf("Test for fifo\n");
const char* fifo_path = "/work/7_Learning/3_ipc/fifo";
if(mkfifo(fifo_path, 0666) < 0 && errno != EEXIST){
printf("fifo:%s error\n");
return -1;
}
int rfd = open(fifo_path, O_RDONLY);
ipc_read(rfd);
close(rfd);
}
int main(int argc,char* argv[]){
test_ipc_fifo_rcv();
return 0;
}
三、信号 (signal)
省略
四、消息队列(Message queues)
4.1 函数
- int msgget(key_t key, int msgflag); //创建和访问一个消息队列
- int msgctl(int msqid, int cmd, struct msqid_ds *buf);
- int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);//把一条消息添加到消息队列中
- ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);//是从一个消息队列接受消息
注意1:msgp是一个指向准备发送消息的指针,但是消息的数据结构却有一定的要求,指针msgp所指向的消息结构一定要是以一个长整型成员变量开始的结构体,接收函数将用这个成员来确定消息的类型。所以消息结构要定义成这样:
struct my_message {
long int message_type;
/* The data you wish to transfer */
};
注意2:msgsz 是msgp指向的消息的长度,注意是消息的长度,而不是整个结构体的长度,也就是说msgsz是不包括长整型消息类型成员变量的长度。
4.2 源码
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/msg.h>
//MessageQueue head
const long int MSG_Q_HEAD_TYPE = 1;
typedef struct _IPC_MESSAGE_QUEUE_HEAD{
long int type; //硬性要求:一定要是以一个长整型成员变量开始,接收函数将用这个成员来确定消息的类型
int len;
}IPC_MESSAGE_QUEUE_HEAD;
//MessageQueue body
const long int MSG_Q_BODY_TYPE = 2;
typedef struct _IPC_MESSAGE_QUEUE_BODY{
long int type; //硬性要求:一定要是以一个长整型成员变量开始,接收函数将用这个成员来确定消息的类型
//char* body; //Segmentation fault (core dumped)
char body[32];
}IPC_MESSAGE_QUEUE_BODY;
(1)send process
int test_ipc_message_queue_snd(){
key_t key = 12345678;
int msgid = msgget(key, 0666 | IPC_CREAT);
if(-1 == msgid){
printf("msgget:%d\n",(int)key);
return -1;
}
char *bufferWrite = "Hello,world!OK";
IPC_MESSAGE_QUEUE_HEAD hMsg;
hMsg.type = MSG_Q_HEAD_TYPE;
hMsg.len = strlen(bufferWrite) + 1;
printf("[snd]len:%d\n",hMsg.len);
IPC_MESSAGE_QUEUE_BODY bMsg;
bMsg.type = MSG_Q_BODY_TYPE;
//bMsg.body = (char*)malloc(sizeof(char)*hMsg.len);
memset(bMsg.body,0,sizeof(bMsg.body));
strncpy(bMsg.body,bufferWrite,hMsg.len);
printf("[snd]body:%s\n",bMsg.body);
//先通知body的长度
int hmsgsz = sizeof(hMsg.len);
int hRet = msgsnd(msgid,&hMsg,hmsgsz,0);
//再真正通知body的内容
int bmsgsz =hMsg.len;
int bRet = msgsnd(msgid,&bMsg,bmsgsz,0);
printf("[snd]bRet:%d\n",bRet);
}
int main(int argc,char* argv[]){
test_ipc_message_queue_snd();
return 0;
}
(2)receive process
int test_ipc_message_queue_rcv(){
key_t key = 12345678;
int msgid = msgget(key, 0666 | IPC_CREAT);
if(-1 == msgid){
printf("msgget:%d\n",(int)key);
return -1;
}
IPC_MESSAGE_QUEUE_HEAD hMsg;
hMsg.type = MSG_Q_HEAD_TYPE;
IPC_MESSAGE_QUEUE_BODY bMsg;
bMsg.type = MSG_Q_BODY_TYPE;
//bMsg.body = NULL;
//先接收body的长度
int hmsgsz = sizeof(hMsg.len);
int hRet = msgrcv(msgid,&hMsg,hmsgsz,hMsg.type,0);
printf("[recv]len:%d\n",hMsg.len);
//再真正接收body内容
int bmsgsz =hMsg.len;
//bMsg.body = (char*)malloc(sizeof(char)*bmsgsz);
if(bMsg.body){
printf("[recv]begin to recv ...\n");
int bRet = msgrcv(msgid,&bMsg,bmsgsz,bMsg.type,0);
printf("[recv]bRet:%d\n",bRet);
bMsg.body[bmsgsz] = '\0';
printf("[recv]body:%s\n",bMsg.body);
}
}
int main(int argc,char* argv[]){
test_ipc_message_queue_rcv();
return 0;
}
五、共享内存
六、socket