利用fork()函数实现类服务器连接的多进程处理程序及其应用场景

cpp 复制代码
#include<stdio.h>
#include <sys/types.h>
#include<unistd.h>
int main()
{
        pid_t pid;
        int data = 10;
        while(1)
        {
                printf("please input a data:");
                scanf("%d",&data);
                if (data == 1)
                {
                        pid = fork();
                        if (pid>0)
                        {

                        }
                        else if (pid == 0)
                        {
                                while(1)
                                {
                                        printf("\ndo net request,pid=%d\n",getpid());
                                        sleep(3);
                                }
                        }
                }
                else
                {
                        printf("wati,do nothing\n");
                }
        }
}
相关推荐
梦想很大很大2 天前
拒绝“盲猜式”调优:在 Go Gin 项目中落地 OpenTelemetry 链路追踪
运维·后端·go
Sinclair2 天前
内网服务器离线安装 Nginx+PHP+MySQL 的方法
运维
叶落阁主2 天前
Tailscale 完全指南:从入门到私有 DERP 部署
运维·安全·远程工作
茶杯梦轩2 天前
从零起步学习RabbitMQ || 第二章:RabbitMQ 深入理解概念 Producer、Consumer、Exchange、Queue 与企业实战案例
服务器·后端·消息队列
甲鱼9293 天前
MySQL 实战手记:日志管理与主从复制搭建全指南
运维
YuMiao4 天前
gstatic连接问题导致Google Gemini / Studio页面乱码或图标缺失问题
服务器·网络协议
碳基沙盒5 天前
OpenClaw 多 Agent 配置实战指南
运维
Sinclair7 天前
简单几步,安卓手机秒变服务器,安装 CMS 程序
android·服务器
Rockbean8 天前
用40行代码搭建自己的无服务器OCR
服务器·python·deepseek
蝎子莱莱爱打怪8 天前
Centos7中一键安装K8s集群以及Rancher安装记录
运维·后端·kubernetes