socketpair学习
来源:互联网 发布:java 动态脚本语言 编辑:程序博客网 时间:2024/06/07 05:03
读Nginx源码,看到了socketpair函数,以前没有见过,网上找到一篇博客介绍的很详细,这里转载过来。
http://liulixiaoyao.blog.51cto.com/1361095/533469/
今天跟人谈到socketpair的问题,晚上回来写了个程序验证下自己的猜测!
先说说我的理解:socketpair创建了一对无名的套接字描述符(只能在AF_UNIX域中使用),描述符存储于一个二元数组,eg. s[2] .这对套接字可以进行双工通信,每一个描述符既可以读也可以写。这个在同一个进程中也可以进行通信,向s[0]中写入,就可以从s[1]中读取(只能从s[1]中读取),也可以在s[1]中写入,然后从s[0]中读取;但是,若没有在0端写入,而从1端读取,则1端的读取操作会阻塞,即使在1端写入,也不能从1读取,仍然阻塞;反之亦然......
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <error.h>
- #include <errno.h>
- #include <sys/socket.h>
- #include <stdlib.h>
- #define BUF_SIZE 30
- int main(){
- int s[2];
- int w,r;
- char * string = "This is a test string";
- char * buf = (char*)calloc(1 , BUF_SIZE);
- if( socketpair(AF_UNIX,SOCK_STREAM,0,s) == -1 ){
- printf("create unnamed socket pair failed:%s\n",strerror(errno) );
- exit(-1);
- }
- /*******test in a single process ********/
- if( ( w = write(s[0] , string , strlen(string) ) ) == -1 ){
- printf("Write socket error:%s\n",strerror(errno));
- exit(-1);
- }
- /*****read*******/
- if( (r = read(s[1], buf , BUF_SIZE )) == -1){
- printf("Read from socket error:%s\n",strerror(errno) );
- exit(-1);
- }
- printf("Read string in same process : %s \n",buf);
- if( (r = read(s[0], buf , BUF_SIZE )) == -1){
- printf("Read from socket s0 error:%s\n",strerror(errno) );
- exit(-1);
- }
- printf("Read from s0 :%s\n",buf);
- printf("Test successed\n");
- exit(0);
- }
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <error.h>
- #include <errno.h>
- #include <sys/socket.h>
- #include <stdlib.h>
- #define BUF_SIZE 30
- int main(){
- int s[2];
- int w,r;
- char * string = "This is a test string";
- char * buf = (char*)calloc(1 , BUF_SIZE);
- pid_t pid;
- if( socketpair(AF_UNIX,SOCK_STREAM,0,s) == -1 ){
- printf("create unnamed socket pair failed:%s\n",strerror(errno) );
- exit(-1);
- }
- /***********Test : fork but don't close any fd in neither parent nor child process***********/
- if( ( pid = fork() ) > 0 ){
- printf("Parent process's pid is %d\n",getpid());
- close(s[1]);
- if( ( w = write(s[0] , string , strlen(string) ) ) == -1 ){
- printf("Write socket error:%s\n",strerror(errno));
- exit(-1);
- }
- }else if(pid == 0){
- printf("Fork child process successed\n");
- printf("Child process's pid is :%d\n",getpid());
- close(s[0]);
- }else{
- printf("Fork failed:%s\n",strerror(errno));
- exit(-1);
- }
- /*****read***In parent and child****/
- if( (r = read(s[1], buf , BUF_SIZE )) == -1){
- printf("Pid %d read from socket error:%s\n",getpid() , strerror(errno) );
- exit(-1);
- }
- printf("Pid %d read string in same process : %s \n",getpid(),buf);
- printf("Test successed , %d\n",getpid());
- exit(0);
- }
原因是:如果子进程先sleep,父进程读取完数据之后,父进程退出,此时写端s[0]的引用计数变为0(之前子进程已主动close了一次),被系统释放,根据read的语义,当子进程被唤醒后会读取到EOF;但是当我们先让父进程sleep的时候,子进程读取完后退出,由于写端在父进程,没有被释放,所以父进程此时阻塞在读操作上.
0 0
- socketpair学习
- socketpair学习笔记
- socketpair
- socketpair
- socketpair
- socketpair
- socketpair
- socketpair
- socketpair
- socketpair
- socketpair
- socketpair
- socketpair
- 网络编程学习笔记(socketpair函数)
- socketpair用法
- Socketpair 简介
- socketpair理解
- socketpair()函数
- OAF—Error Handling
- POJ 3069 Saruman's Army(水题,简单的贪心)
- 顺序表合并
- 【Python】【Dict】dict 基本用法
- dedecms的title优化?
- socketpair学习
- ctype.h 参考
- char与byte的区别
- 速来膜拜!20位活跃在Github上的国内技术大牛
- Andriod PackageManager使用
- hdu 4937 Lucky Number(数学题 进制转换)2014多校训练第7场
- next_permutation与prev_permutation
- asp中URL传参遇空格字符串被截断
- FFmepg 多线程解码历程