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