socketpair是进程间通信的一种方式。

API:

int socketpair(int domain, int type, int protocol, int sv[]);

DEMO:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <unistd.h> #define MAXLINE 4096 int main(int argc, char **argv) {
int fd[];
pid_t pid;
char line[MAXLINE];
int n; if (socketpair(AF_UNIX, SOCK_STREAM, , fd) < ) {
perror("socketpair error");
exit();
}
if ( (pid = fork()) < ) {
perror("fork error");
exit();
} else if (pid > ) {
close(fd[]);
write(fd[], "hello world\n", );
char tmp[MAXLINE];
read(fd[], tmp, MAXLINE);
printf("echo is: %s\n", tmp);
} else {
close(fd[]);
n = read(fd[], line, MAXLINE);
write(STDOUT_FILENO, line, n);
char tmp[] = "world says hi";
write(fd[], tmp, strlen(tmp));
}
exit();
}

管道命名管道相比,socketpair有以下特点:

1. 全双工

2. 可用于任意两个进程之间的通信

最新文章

  1. 在Oracle中使用rank()over()排名的问题
  2. ExtJs之Ext.isEmpty
  3. js sleep效果
  4. 【不积跬步,无以致千里】vim复制
  5. 两种查看eclipse或MyEclipse是64bit还是32bit的方法
  6. MongoDB简单操作
  7. 继承“HibernateDaoSupport”后,报“The hierarchy of the type AccoutDaoImpl is inconsistent”的解决方案
  8. 加固后,上传play store, 在 google play store 下载应用安装后,打开签名校验失败
  9. js查漏补缺
  10. 什么是pytorch(2Autograd:自动求导)(翻译)
  11. join、on、where、having的使用区别
  12. Python3基础 os listdir curdir pardir 查看工作目录及其上一级目录的所有文件名
  13. Endv 博客简介
  14. java List 学习
  15. Hive使用druid做连接池代码实现
  16. C 六度空间理论的实现
  17. MemoryStream类读写内存
  18. OpenGL入门学习(三)
  19. 使用 Azure 创建网络文件系统
  20. bzoj 1026: [SCOI2009]windy数【数位dp】

热门文章

  1. python3(二十三)classInstance
  2. 数据结构篇-数组(TypeScript版+Java版)
  3. 钩子函数 Function类
  4. L17 AlexNet VGG NiN GoogLeNet
  5. work of 1/5/2016
  6. Leetcode1353-最多可以参加的会议数目
  7. Spring5:事务管理【整合Mybatis】
  8. pytorch 孪生神经网络DNN
  9. Windows系统安装最新版本RabbitMQ3.8.3及报错解决
  10. 2019-2020-1 20199303 《Linux内核原理与分析》 第十一周作业