一、基本概要
1、概念:
- 进程空间是独立的,包含文本段、数据段和系统数据段
- 多个进程没有共享的用户空间,进程是操作系统资源分配的最小单元
- 可以利用Linux内核实现多个进程间的通信
2、常用方式:
- 传统Unix系统中的通信方式:
- 管道
- 信号
- SYS V分支中的通信方式:
- 消息队列
- 共享内存
- 信号灯
- BSD分支中的通信方式:
- 本地域套接字
二、管道
1、概念:
- 分类:
- 有名管道
- 无名管道
- 只能用于具有亲缘关系的进程间通信
2、无名管道:
-
原理
- 无名管道是一段内核缓存区
- 父进程通过pipe创建无名管道
- 只有该父进程的子进程才能继承得到这两个文件描述符,才能实现通信
- 只能用于具有亲缘关系的进程间通信

函数接口:
- pipe

管道操作特性:
- 管道中至少有一个写端:
- 读取数据时,如果管道中有数据则直接读出
- 读取数据时,如果管道中没有数据则阻塞等待有数据写入才能读出
- 管道中没有写端:
- 读取数据时,管道中有数据则直接读出
- 读取数据时,管道中没有数据不阻塞等待直接向下执行
- 管道中至少有一个读端:
- 写入数据时,如果管道没有写满则直接写入
- 写入数据时,如果管道存满则阻塞等待有数据读出才能继续写入
- 管道中没有读端:
- 写入数据时,会发生管道破裂的信号导致进程任务异常退出
3、有名管道
与无名管道区别:
- 有名管道有名字,可以通过名字找到该管道
- 可以用于任意进程间的通信
- 进程间通信最简单、易实现的方式
- 有名管道必须读写两端同时加入才能继续向下执行

操作方式:
- 进程一使用open以读、写、读写方式打开管道文件
- 进程二使用open以读、写、读写方式打开管道文件
- 两个进程可以通过向管道文件中读写数据实现进程的通信
函数接口:
- mkfifo

多任务实现进程间通信:
编写clientA.c和clientB.c实现利用有名管道完成两个进程的聊天功能
head.c
#ifndef __HEAD_H__
#define __HEAD_H__#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <math.h>
#include <dirent.h>
#include <sys/wait.h>
#include <pthread.h>
#include <semaphore.h>
#include <signal.h>
#define my_fgets(filename) do { \
fgets(filename, sizeof(filename), stdin); \
filename[strlen(filename)-1] = '\0'; \
} while (0)#endif
makefile
LIBS+=-lpthread
all:clientA clientB
clientA:clientA.c
gcc $^ -o $@ $(LIBS)clientB:clientB.c
gcc $^ -o $@ $(LIBS)
clientA.c
#include "../head.h"
int fd_rw1 = 0;
int fd_rw2 = 0;char tmpbuff[4096] = {0};
void *thread_rtow(void *arg)
{
while(1)
{
memset(tmpbuff,0,sizeof(tmpbuff));
my_fgets(tmpbuff);
write(fd_rw2,tmpbuff,sizeof(tmpbuff));
}
return NULL;
}
void *thread_wtor(void *arg)
{
while(1)
{
memset(tmpbuff,0,sizeof(tmpbuff));
read(fd_rw1,tmpbuff,sizeof(tmpbuff));
printf("RECV:%s\n",tmpbuff);
}
}int main(void)
{
pthread_t tid1;
pthread_t tid2;
mkfifo("./rtow",0777);
mkfifo("./wtor",0777);fd_rw1 = open("./rtow",O_RDWR);
if(-1 == fd_rw1)
{
perror("fail to open");
return -1;
}fd_rw2 = open("./wtor",O_RDWR);
if(-1 == fd_rw2)
{
perror("fail to open");
return -1;
}
pthread_create(&tid1,NULL,thread_rtow,NULL);
pthread_create(&tid2,NULL,thread_wtor,NULL);pthread_join(tid1,NULL);
pthread_join(tid2,NULL);close(fd_rw1);
close(fd_rw2);return 0;
}
clientB.c
#include "../head.h"
int fd_rw1 = 0;
int fd_rw2 = 0;char tmpbuff[4096] = {0};
void *thread_rtow(void *arg)
{
while(1)
{
memset(tmpbuff,0,sizeof(tmpbuff));
my_fgets(tmpbuff);
write(fd_rw1,tmpbuff,sizeof(tmpbuff));
}
return NULL;
}
void *thread_wtor(void *arg)
{
while(1)
{
memset(tmpbuff,0,sizeof(tmpbuff));
read(fd_rw2,tmpbuff,sizeof(tmpbuff));
printf("RECV:%s\n",tmpbuff);
}
}int main(void)
{
pthread_t tid1;
pthread_t tid2;
mkfifo("./rtow",0777);
mkfifo("./wtor",0777);fd_rw1 = open("./rtow",O_RDWR);
if(-1 == fd_rw1)
{
perror("fail to open");
return -1;
}fd_rw2 = open("./wtor",O_RDWR);
if(-1 == fd_rw2)
{
perror("fail to open");
return -1;
}
pthread_create(&tid1,NULL,thread_rtow,NULL);
pthread_create(&tid2,NULL,thread_wtor,NULL);pthread_join(tid1,NULL);
pthread_join(tid2,NULL);close(fd_rw1);
close(fd_rw2);return 0;
}
1315

被折叠的 条评论
为什么被折叠?



