1.进程间通信
进程间通信 进程是孤立的故此需要去访问其他进程 IPC 进程间通讯
进程通信一般用于 数据传输 通知数据 资源共享 进程控制
进程通信的方法
2.无名管道
1.特点
2.管道创建 pipe函数
3.利用无名管道进行父子间通信
int main()
{
int fds[2];
int ret=-1;
pid_t pid;
ret=pipe(fds);
pid=fork();
if(pid==0)
{
char buf[]=" i am cxy";
write(fds[1],buf,strlen(buf));///管道写
_exit(0);
}
else if(pid>0)
{
wait(NULL);
char str[80]={0};
read(fds[0],str,sizeof(str));//管道读
printf("str=[%s]\n", str);
}
管道阻塞属性 当父进程读管道内无数据就会阻塞 只有写了才会有数据
4.管道读写特点
5.查看缓冲区大小
6.设置非阻塞
设置方法:
//获取原来的flags
int flags = fcntl(fd[0], F_GETFL);
// 设置新的flags
flag |= O_NONBLOCK;
// flags = flags | O_NONBLOCK;
fcntl(fd[0], F_SETFL, flags);