1. 进程间的通信
(一)进程间的通信(IPC)方式
管道:有名管道和无名管道
消息队列
信号量
共享内存
套接字
(二)进程间通信的目的
1、数据传输:一个进程需要将它的数据发送给另一个进程。
2、共享数据:多个进程想要操作共享数据,一个进程对共享数据的修改,别的进程应该第一时间看到
3、通知事件:一个进程需要向另一个或另一组进程发送消息,通知它(们)发生了某种事件(如进程终止时通知父进程)
4、资源共享:多个进程之间共享同样的资源。为做到这一点,需要内核提供锁和同步机制
5、进程控制:
2. 管道通信:无名管道——一端写,一端读
管道是半双工的,数据只能向一个方向流动,需要双方通信时,需要建立两个管道。
注意点:无名管道不能在无父子关系的进程之间通信
因为子进程复制父进程资源(包括文件描述符)是发生在vfork()、fork()之后,所以vfork()、fork()一定要在pipe()之后发生。否则,子进程将不会继承父进程的文件描述符。
(1)如果管道的写端不存在,则认为已经读到数据末尾,该函数返回的读出字节数为0;
(2)如果管道写端存在时,请求的字节数目大于PIPE_BUF(这是个宏,大概4096),则返回现有的管道数据字节数;如果不大于,则返回管道现有数据字节数,或请求字节数。
用无名管道实现在子进程输入字符串,在父进程输出,代码如下:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
int main()
{
int fd[2];
int status;
pid_t pid;
if(-1 == pipe(fd))
{
perror("pipe ERROE!");
exit(1);
}
pid = vfork();
if(-1 == pid)
{
perror("fork error!");
exit(1);
}
if(0 == pid)
{
char buf[20] = {0};
printf("child\n");
printf("please input: \n");
scanf("%s",buf);
write(fd[1], buf, strlen(buf));
exit(0);
}
else
{
waitpid(pid, &status, 0);
char buffer[20] = {0};
read(fd[0], buffer, 20);
printf("%s\n",buffer);
}
return 0;
}
运行结果如下:
3. 管道通信:有名管道
int mkfifo(const char *pathname, mode_t mode);
可以在任意进程之间进行通信。
- 举例1:利用有名管道,实现在父子进程之间进行通信。
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#define FIFO "./text.txt"
int main()
{
int i = 0;
int fd;
int status;
pid_t pid;
int num = mkfifo(FIFO, 0666);
if(-1 == num)
{
perror("mkfifo error!\n");
exit(1);
}
pid = fork();
if(-1 == pid)
{
perror("fork error!\n");
exit(1);
}
if(0 == pid)
{
while(1)
{
fd = open(FIFO, O_WRONLY);
char str[21] = {0};
printf("Input:\n");
scanf("%s",str);
write(fd, str, strlen(str));
memset(str, 0, sizeof(str));
sleep(1);
fd = open(FIFO, O_RDONLY);
read(fd, str, 20);
printf("zh --> li:%s\n",str);
memset(str, 0, sizeof(str));
}
exit(0);
}
else
{
while(1)
{
fd = open(FIFO, O_RDONLY);
char buffer[1024] = {0};
read(fd, buffer, 15);
printf("li --> zh:%s\n",buffer);
memset(buffer, 0, sizeof(buffer));
fd = open(FIFO, O_WRONLY);
printf("Input:\n");
scanf("%s",buffer);
write(fd, buffer, strlen(buffer));
memset(buffer, 0, sizeof(buffer));
sleep(1);
}
}
return 0;
}
运行结果如下:
- 举例2:利用有名管道,实现在读进程和写进程之间进行通信。
write.c代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#define FIFO "./text.txt"
int main()
{
int fd;
pid_t pid;
int ret;
char buffer[100];
if((ret == mkfifo(FIFO, 0644)) < 0)
{
if(ret == -1 && errno == EEXIST)
{
}
else
{
perror("mkfifo error!");
exit(0);
}
}
fd = open(FIFO, O_WRONLY);
while(1)
{
memset(buffer, 0, sizeof(buffer));
printf("Please input:\n");
scanf("%s",buffer);
write(fd, buffer, strlen(buffer));
}
return 0;
}
read.c代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#define FIFO "./text.txt"
int main()
{
int fd;
int ret;
pid_t pid;
char buffer[100];
if((ret == mkfifo(FIFO, 0644)) < 0)
{
if(ret == -1 && errno == EEXIST)
{
}
else
{
perror("mkfifo error!");
exit(0);
}
}
fd = open(FIFO, O_RDONLY);
while(1)
{
memset(buffer, 0, sizeof(buffer));
read(fd, buffer, sizeof(buffer));
printf("recive:%s\n",buffer);
sleep(2);
}
return 0;
}
先在终端上运行可执行文件rd,用来读出文件中的字符串;然后另开一个终端,在该终端上运行可执行文件wr,用来将输入的字符串写入文件。运行结果如下:
总结:
1、传输数据量少,因此只适合小数据量的传输
2、管道数据被读出便会自动删除
3、管道数据是什么输出格式?二进制
4、管道的大小:4096
5、进程的信息存放在tach_struct中,包括所有进程ID(pid)、调度优先级、地址空间。