管道是多进程通信的工具
IPC进程间通信(位于同一台主机)
管道:
父子进程——》:无名管道(int fd【2】,两个整形数的数组,调用pipe(fd)fd才有意义)
父进程和子进程完全独立,无法直接访问
管道是单向的,
fd【0】读端
fd【1】写端
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int fd[2];
pipe(fd);
int pid = fork();
if (pid == 0)
{
close(fd[0]);
int count = 5;
char *msg = "hello world";
while (count--)
{
write(fd[1], msg, strlen(msg));
}
}
else
{
char c;
close(fd[1]);
while (read(fd[0], &c, sizeof(char)))
{
printf("%c", c);
}
}
return 0;
}
独立进程——》:有名管道