歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 用socketpair()進行進程間的全雙工通訊

用socketpair()進行進程間的全雙工通訊

日期:2017/3/1 9:50:16   编辑:Linux編程

用socketpair()進行進程間的全雙工通訊

/*************************************************
* description: use socketpair() to implete IPC(全雙工的IPC)
* author: chengshuguang
**************************************************/
#include <stdio.h>
#include <sys/socket.h>
#include <unistd.h>

#define child "wo shi child fa lai de"
#define parent "wo shi parent fa lai de"

int main()
{
int fd[2];
int ret;
ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);

pid_t pid;
pid = fork();
printf("here\n");
if(pid == 0)
{
char buf[20];
close(fd[0]);
read(fd[1],buf,20);
printf("child: %s\n",buf);

write(fd[1],child,sizeof(child));
close(fd[1]);

}
else
{
char buf[20];
close(fd[1]);
write(fd[0],parent,sizeof(child));
read(fd[0],buf,20);
printf("parent: %s\n",buf);
close(fd[0]);
}

sleep(10);


return 0;
}

解決mini2440聲卡全雙工問題 實現同時錄音及播放 http://www.linuxidc.com/Linux/2011-05/36718.htm

Copyright © Linux教程網 All Rights Reserved