利用管道和fork实现进程间通信
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#define BUFES 256
int main()
{
pid_t pid;
int fd[2];
char str[BUFES];
int len;
if((pipe(fd)) < 0)
{
perror("failed to create pipe!/n");
exit(1);
}
if((pid=fork())<0)
{
perror("fail to fork!/n");
exit(1);
}
else if(pid == 0)
{
close(fd[1]);
len = read(fd[0], str, BUFES);
if(len < 0)
{
perror("process failed when read a pipe!/n");
exit(1);
}
else
{
write(STDOUT_FILENO,str,len);
}
exit(0);
}
else
{
close(fd[0]);
write(fd[1],"this is a message from you father!/n",36);
exit(0);
}
return 0;
}