Linux下socketpair系统API调用使用说明

article/2025/9/22 12:01:17

目录

1.socketpair函数说明

 2.socketpair使用举例


在阅读nginx源码时,发现其调用socketpair来实现master和worker进程之间进行数据交互。其代码如下:

思考:master和worker进程是父子关系,有亲属关系的进程通过pipe/pipe2(匿名管道)和mkfifo(有名管道)也能实现数据传输,为什么要使用socketpair来进行数据交互?

原因:socketpair创建的全双工的一对套接字,而匿名管道和有名管道是单工的。

匿名管道和有名管道使用可以参考如下博客:

https://www.cnblogs.com/fortunely/p/14648146.html

1.socketpair函数说明

socketpair创建管道之后,fds[0]和fds[1]均可以读写,读写可发生在一个线程中,也可以发生在父子进程之间。关于socketpair使用,可参考如下说明:

SOCKETPAIR(2)                                                                          Linux Programmer's Manual                                                                         SOCKETPAIR(2)

NAME
       socketpair - create a pair of connected sockets

SYNOPSIS
       #include <sys/types.h>          /* See NOTES */
       #include <sys/socket.h>

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

DESCRIPTION
       The  socketpair()  call creates an unnamed pair of connected sockets in the specified domain, of the specified type, and using the optionally specified protocol.  For further details of these
       arguments, see socket(2).

       The file descriptors used in referencing the new sockets are returned in sv[0] and sv[1].  The two sockets are indistinguishable.

RETURN VALUE
       On success, zero is returned.  On error, -1 is returned, and errno is set appropriately.

       On Linux (and other systems), socketpair() does not modify sv on failure.  A requirement standardizing this behavior was added in POSIX.1-2016.

ERRORS
       EAFNOSUPPORT
              The specified address family is not supported on this machine.

       EFAULT The address sv does not specify a valid part of the process address space.

       EMFILE The per-process limit on the number of open file descriptors has been reached.

       ENFILE The system-wide limit on the total number of open files has been reached.

       EOPNOTSUPP
              The specified protocol does not support creation of socket pairs.

       EPROTONOSUPPORT
              The specified protocol is not supported on this machine.

CONFORMING TO
       POSIX.1-2001, POSIX.1-2008, 4.4BSD.  socketpair() first appeared in 4.2BSD.  It is generally portable to/from non-BSD systems supporting clones of the BSD  socket  layer  (including  System V
       variants).

NOTES
       On Linux, the only supported domain for this call is AF_UNIX (or synonymously, AF_LOCAL).  (Most implementations have the same restriction.)

       Since Linux 2.6.27, socketpair() supports the SOCK_NONBLOCK and SOCK_CLOEXEC flags in the type argument, as described in socket(2).

       POSIX.1 does not require the inclusion of <sys/types.h>, and this header file is not required on Linux.  However, some historical (BSD) implementations required this header file, and portable
       applications are probably wise to include it.
 

 2.socketpair使用举例

2.1 如下代码演示阻塞和非阻塞socketpair使用:

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>int parentProcess(int* fds, int num) {if (num < 2) {return -1;}char buf[128] {0};recv(fds[0], buf, 128, 0);printf("parent:: %s\n", buf);recv(fds[0], buf, 128, 0);printf("parent:: %s\n", buf);sleep(1);recv(fds[0], buf, 128, 0);printf("parent:: %s\n", buf);sleep(1);memset(buf, 0x00, sizeof(buf));strcpy(buf, "hello child, I am parent !");send(fds[1], buf, strlen(buf), 0);close(fds[0]);close(fds[1]);return 0;
}int childProcess(int* fds, int num) {if (num < 2) {return -1;}char buf[128] = "hello parent, I am child";send(fds[1], buf, strlen(buf), 0);sleep(1);send(fds[1], buf, strlen(buf), 0);sleep(1);char *pStr = (char*)"给父进程再发一次消息";send(fds[1], pStr, strlen(pStr), 0);memset(buf, 0x00, sizeof(buf));sleep(1);recv(fds[0], buf, 128, 0);printf("child:: %s\n", buf);close(fds[0]);close(fds[1]);return 0;
}//设置文件描述符非阻塞
int fd_nonblocking(int s)
{int  nb;nb = 1;//方法一/*int flag = fcntl(s, F_GETFL);flag |= O_NONBLOCK;return fcntl(s, F_SETFL, flag);*///方法二return ioctl(s, FIONBIO, &nb);
}int close_channel(int* fds) {if (close(fds[0]) == -1) {printf("close() channel fds[0] failed\n");}if (close(fds[1]) == -1) {printf("close() channel fds[1] failed\n");}return 0;
}void testNonblockingSocketFd(int* fds) {if (-1 == fd_nonblocking(fds[0])) {printf("fd_nonblocking fds[0] failed\n");close_channel(fds);}if (-1 == fd_nonblocking(fds[1])) {printf("fd_nonblocking fds[1] failed\n");close_channel(fds);}
}int main()
{int fds[2];// fds[0]: 读  fds[1]: 写 socketpair(PF_UNIX, SOCK_STREAM, 0, fds);
#ifdef NOBLOCKFDtestNonblockingSocketFd(fds);
#endifint pid = fork();printf("parent: %d, child: %d\n", getpid(), pid);switch (pid) {case -1: // errorreturn -1;case 0: // childchildProcess(fds, 2);printf("child exit \n");break;default: // parentparentProcess(fds, 2);printf("parent exit \n");break;}return 0;
}

阻塞应用:

非阻塞应用: 

通过非阻塞应用发现, 父进程退出后,子进程往管道发送数据后,接着自己读到了发送的数据。

2.2只使用一端进行读写

如2.1可以发现fds[0]和fds[1]均可以读写,那么自己进程读到的可能是别的进程发来的数据,也可能是自己进程发来的数据,编程逻辑很不清晰。

常用的方式是:

(1) 父进程 close(fd[0]),使用fd[1]读写,子进程close(fd[1]),使用fd[0]读写

(1) 父进程 close(fd[1]),使用fd[0]读写,子进程close(fd[0]),使用fd[1]读写

如下代码显示情形(1)

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>int parentProcess(int* fds, int num) {if (num < 2) {return -1;}close(fds[0]); // 关闭fds[0] 使用fds[1]读写,子进程中关闭fds[1] 使用fds[0]读写char buf[128] {0};char *pStr = (char*)"hello child, I am parent";int inum = 0;while (inum++ < 10) {memset(buf, 0x00, sizeof(buf));//写write(fds[1], pStr, strlen(pStr));//读read(fds[1], buf, 128);printf("parent收到child的招呼 %d :: %s\n", inum, buf);sleep(1);}close(fds[1]);return 0;
}int childProcess(int* fds, int num) {if (num < 2) {return -1;}close(fds[1]);char buf[128] {0};char *pStr = (char*)"hello parent, I am child";int inum = 0;while (inum++ < 10) {memset(buf, 0x00, sizeof(buf));//读read(fds[0], buf, 128);printf("child收到paren的招呼 %d :: %s\n", inum, buf);//写write(fds[0], pStr, strlen(pStr));sleep(1);}close(fds[0]);return 0;
}//设置文件描述符非阻塞
int fd_nonblocking(int s)
{int  nb;nb = 1;//方法一/*int flag = fcntl(s, F_GETFL);flag |= O_NONBLOCK;return fcntl(s, F_SETFL, flag);*///方法二return ioctl(s, FIONBIO, &nb);
}int close_channel(int* fds) {if (close(fds[0]) == -1) {printf("close() channel fds[0] failed\n");}if (close(fds[1]) == -1) {printf("close() channel fds[1] failed\n");}return 0;
}void testNonblockingSocketFd(int* fds) {if (-1 == fd_nonblocking(fds[0])) {printf("fd_nonblocking fds[0] failed\n");close_channel(fds);}if (-1 == fd_nonblocking(fds[1])) {printf("fd_nonblocking fds[1] failed\n");close_channel(fds);}
}int main()
{int fds[2];socketpair(PF_UNIX, SOCK_STREAM, 0, fds);
#ifdef NOBLOCKFDtestNonblockingSocketFd(fds);
#endifint pid = fork();printf("parent: %d, child: %d\n", getpid(), pid);switch (pid) {case -1: // errorreturn -1;case 0: // childchildProcess(fds, 2);printf("child exit \n");break;default: // parentparentProcess(fds, 2);printf("parent exit \n");break;}return 0;
}

运行结果如下:

2.3一个master多个worker数据收发

如下代码模拟nginx中一个 master进程,多个worker进程进行数据交互

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>int parentProcess(int* fds, int num) {if (num < 2) {return -1;}close(fds[0]); // 关闭fds[0] 使用fds[1]读写,子进程中关闭fds[1] 使用fds[0]读写char buf[128] {0};char *pStr = (char*)"hello child, I am parent";int inum = 0;while (inum++ < 3) {memset(buf, 0x00, sizeof(buf));//写write(fds[1], pStr, strlen(pStr));//读read(fds[1], buf, 128);printf("parent [%d] %d :: %s\n", getpid(), inum, buf);sleep(1);}close(fds[1]);return 0;
}int childProcess(int* fds, int num) {if (num < 2) {return -1;}close(fds[1]);char buf[128] {0};char *pStr = (char*)"hello parent, I am child";char sendBuf[128] = {0};sprintf(sendBuf, "hello parent, I am child %d", getpid());int inum = 0;while (inum++ < 3) {memset(buf, 0x00, sizeof(buf));//读read(fds[0], buf, 128);printf("child [%d] %d :: %s\n", getpid(), inum, buf);//写write(fds[0], sendBuf, strlen(sendBuf));sleep(1);}close(fds[0]);return 0;
}//设置文件描述符非阻塞
int fd_nonblocking(int s)
{int  nb;nb = 1;//方法一/*int flag = fcntl(s, F_GETFL);flag |= O_NONBLOCK;return fcntl(s, F_SETFL, flag);*///方法二return ioctl(s, FIONBIO, &nb);
}int close_channel(int* fds) {if (close(fds[0]) == -1) {printf("close() channel fds[0] failed\n");}if (close(fds[1]) == -1) {printf("close() channel fds[1] failed\n");}return 0;
}void testNonblockingSocketFd(int* fds) {if (-1 == fd_nonblocking(fds[0])) {printf("fd_nonblocking fds[0] failed\n");close_channel(fds);}if (-1 == fd_nonblocking(fds[1])) {printf("fd_nonblocking fds[1] failed\n");close_channel(fds);}
}struct TDataExchangeChannel {int fds[2];
};#define GROUP_NUM 2int main()
{
#ifdef NOBLOCKFDtestNonblockingSocketFd(fds);
#endifTDataExchangeChannel channels[GROUP_NUM];for (int i = 0; i < GROUP_NUM; i++) {socketpair(PF_UNIX, SOCK_STREAM, 0, channels[i].fds);int pid = fork();switch (pid) {case -1: // errorreturn -1;case 0: // childchildProcess(channels[i].fds, 2);printf("child exit \n");exit(0);default: // parentbreak;}}//父进程给子进程发送消息,并接收子进程发来的消息for (int i = 0; i < GROUP_NUM; i++) {parentProcess(channels[i].fds, 2);}return 0;
}

运行效果如下:

 根据运行结果看,master进程为31558,两个子进程为31559,31560,master进程可以分别与两个worker进行数据收发。


http://chatgpt.dhexx.cn/article/LAsWsSTi.shtml

相关文章

socketpair函数介绍及使用

摘要 在linux下&#xff0c;使用socketpair函数能够创建一对套节字进行进程间通信&#xff08;IPC&#xff09;。 函数原形&#xff1a; #include <sys/types.h> #include <sys/socket.h>int socketpair(int domain, int type, int protocol, int sv[2]); 参数…

socketpair机制

描述 先看下传统的CS模型&#xff0c;如下: 总是一方发起请求&#xff0c;等待另一方回应。当一次传输完成之后&#xff0c;client端发起新的请求之后&#xff0c;server端才作出回应。 那如何才能做到双向通信&#xff1f; 一种解决办法就是client端即使client&#xff0c;又…

【socketpair函数介绍及使用】

socketpair函数介绍及使用 函数原型进程间通信线程间通信 Linux环境下使用socketpair函数创造一对未命名的、相互连接的UNIX域套接字。   管道历史上&#xff0c;它们是半双工的&#xff08;数据只能在一个方向上流动&#xff09;&#xff0c;但是现在也有全双工管道。管道只…

LAMP架构超详细搭建步骤

LAMP介绍&#xff1a; LinuxApacheMysql/MariaDBPerl/PHP/Python一组常用来搭建或者服务器的开源软件&#xff0c;本身都是各自独立的程序&#xff0c;但是因为常被放在一起使用&#xff0c;拥有了越来越高的兼容度&#xff0c;共同组成了一个强大的Web应用程序平台。 目前LA…

LAMP架构介绍

转载自&#xff1a; https://blog.csdn.net/aoli_shuai/article/details/78745984 https://blog.csdn.net/sj349781478/article/details/84224440 一、LAMP介绍&#xff1a; LAMP是 linux Apache MySQL PHP的简写&#xff0c;即把Apache MySQL PHP 安装在linux系统上&#xff…

Lamp架构部署phpmadmin项目

Lamp架构部署phpmadmin项目 文章目录 Lamp架构部署phpmadmin项目官网下载phpmadmin的包测试登陆 官网下载phpmadmin的包 [phpMyAdmin]: 将下载好的包拖入目录里&#xff0c;并解压 [rootlocalhost ~]# cd /usr/src/ [rootlocalhost src]# lsapr-1.6.5 apr-1.6.5.tar.gz a…

【笔记】lamp架构框图

一、lamp架构 (1)、lamp基础结构 (2)、分布式lamp架构 (3)、实际运用 二、OSI七层和TCP/IP五层关系 这部分具体可以参考网址 (1)、OSI七层 OSI&#xff08;Open System Interconnect&#xff09;&#xff0c;即开放式系统互联。 一般都叫OSI参考模型&#xff0c;是ISO&…

LAMP服务架构

LAMP服务架构 文章目录 LAMP服务架构1. LAMP简介2. LAMP工作原理3. 部署LAMP3.1 源码安装http服务3.2 源码安装mysql数据库3.3 源码安装php3.2 apache和php配置 1. LAMP简介 LAMP是指一组通常一起使用来运行动态网站或者服务器的自由软件名称首字母缩写 L代表服务器操作系统使用…

LAMP架构部署论坛

搭建论坛&#xff0c;要求为8080端口 安装LAMP 架构所需的服务&#xff0c;部分及插件 yum -y install mariadb mariadb-server mariadb-libs php php-mysql php-gd php-fpm php-cli gd httpd 启动服务&#xff1a;systemctl start httpd mariadb 设置数据库密码&#xff1a…

快速搭建LAMP架构

快速搭建LAMP架构 1.安装apache2.部署mysql3.部署php结合apache4.验证5.部署一个论坛disuz 1.安装apache [rootlocalhost ~]# yum install httpd -y 已加载插件&#xff1a;fastestmirror, langpacks Loading mirror speeds from cached hostfile* base: mirrors.aliyun.com* …

LAMP架构简介与配置

文章目录 前言LAMP 简介与概述LAMP平台概述 构建LAMP平台顺序编译安装的优点各组件的主要作用 编译安装 Apache编译安装mysql安装PHP 前言 LAMP 简介与概述 LAMP平台概述 LAMP架构是目前成熟的企业网站应用模式之一&#xff0c;指的是协同工作的一整台系统和相关软件&#x…

搭建LAMP架构

搭建LAMP架构 一、LAMP架构概述 1、LAMP简介 LAMP架构是目前成熟的企业网站应用模式之一&#xff0c;指的是协同工作的一整台系统和相关软件&#xff0c;能够提供动态web站点服务及其应用开发环境。 LAMP是一个缩写词&#xff0c;具体包括Linux操作系统&#xff0c;Apache网…

LAMP架构简介与概述 及服务安装

目录 1、LAMP平台概述 &#xff08;1&#xff09;LAMP平台概述 &#xff08;2&#xff09;构建LAMP平台顺序 &#xff08;3&#xff09;编译安装的优点 &#xff08;4&#xff09;各组件的主要作用 2、Apache概述 &#xff08;1&#xff09; 安装Apache服务&#xff1a;…

什么是LAMP架构?

LAMP架构介绍 LAMP动态网站架构 LAMP是指一组通常一起使用来运行动态网站或者服务器的自由软件名称首字母缩写。 1、LAMP分别代表什么&#xff1f; L代表服务器操作系统使用Linux A代表网站服务使用的是Apache软件基金会中的httpd软件 M代表网站后台使用的数据库是MySQL数…

【ubuntu】搭建lamp架构

文章目录 一、准备工作1、更新源2、更新软件3、防火墙UFW 二、安装apache21、安装apache包2、网页浏览查看 三、安装mysql1、安装mysql2、查看mysql3、设置mysql数据库的密码 四、安装PHP1、安装php包以及插件2、测试php包 五、安装wordpress1、获取wordpress的压缩包2、上传压…

腾讯云— LAMP 架构个人实践分享

LAMP 环境通常指Linux 环境下&#xff0c;由ApacheMySQL/MariaDBPHP 以及其它相关组件组成的网站服务器架构。目前以LAMP组成的Web 应用程序平台广泛被应用&#xff0c;70%以上的访问流量由LAMP提供&#xff0c;所以我们也认同LAMP是最强大的网站解决方案。 关于LAMP的环境部署…

一:LAMP 架构简介

一&#xff1a;LAMP 架构简介 LAMP 是目前成熟的一种企业网站应用模式之一&#xff0c;指的是协同工作的一套系统和相关软件的整合&#xff0c;可提供PHP动态web站点应用及开发环境&#xff0c;LAMP 经过十年的完善各个组件间的兼容性&#xff0c;协作能力&#xff0c;稳定等方…

php lamp架构,lamp架构搭建

lamp架构搭建(yum) 环境&#xff1a;一台主机&#xff0c;centos7.5 lamp安装 yum -y install httpd php-fpm php-mysql mariadb-server 启动php-fpm服务 systemctl start php-fpm 配置httpd生成配置文件&#xff0c;提示http&#xff0c;php-fpm服务在本地 vim /etc/httpd/con…

LAMP架构介绍及配置

1.LAMP简介与概述 1.1 LAMP平台概述 LAMP架构是目前成熟的企业网站应用模式之一&#xff0c;指的是协同工作的一整台系统和相关软件&#xff0c;能够提供动态web站点服务及其应用开发环境 LAMP是一个缩写词&#xff0c;具体包括Linux操作系统&#xff0c;Apache网站服务器&a…

lamp

文章目录 1. LAMP架构介绍2. web服务器工作流程2.1 cgi与fastcgi2.2 httpd与php结合的方式2.3 web工作流程 3. lamp平台构建3.1 安装httpd3.2 安装mysql3.3 安装php3.4 配置apache3.4.1 启用代理模块3.4.2 配置虚拟主机 3.5 验证 1. LAMP架构介绍 lamp&#xff0c;其实就是由L…