多进程并发简易服务器代码实现

多进程实现并发服务器


多进程并发服务器.c

架构

int sockfd = socket()
bian()
listen()
while(1){

			acceptfd = accept()
			
			pid = fork()
		
			if(pid > 0){
			
			}else if(pid == 0){
						while(1){
								recv()/send()
						}
			}
}

注意:accept不能放在 PID > 0 中,因为fork之后,子进程和父进程一点关系都没有,而recv和send都需要,accept函数的返回值,父子进程都只执行自己私有空间里的内容,所以需要把,accent放在,fork之前让子进程在创建的时候拷贝返回值过去(泪的教训)。

父进程保证永远都在accept的位置堵塞,当有客户端请求同步链接的时候,就会创建一个子进程,而子进程会一直在while(1)中,判断当有一个客户端关闭的时候,就关闭子进程 就行(即通过信号,异步的去处理僵尸进程)


案例

/*
#    Multi-process concurrent server
#    https://www.cnblogs.com/kencszqh
#
#    File Name:  多进程并发.c
#    Created  :  2024/6/11
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/epoll.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/mman.h>
#include <sys/eventfd.h>
#include <sys/timerfd.h>
#include <sys/signalfd.h>
#include <sys/syscall.h>
#include <sys/prctl.h>
#include <sys/uio.h>

#define ERR_LOG(errmsg)                                     \
    do                                                      \
    {                                                       \
        perror(errmsg);                                     \
        exit(1);                                            \
    } while (0)

#define N 128

void handler(int sig)
{
    wait(NULL);
}

int main(int argc, char const *argv[])
{
    if (argc < 3)
    {
        fprintf(stderr, "Usage:%s [ip] [port]\n", argv[0]);
        exit(1);
    }

    int sockfd, acceptfd;

    struct sockaddr_in serveraddr, clientaddr;
    socklen_t addrlen = sizeof(serveraddr);

    // 1.创建套接字
    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        ERR_LOG("fail to socket");
    }

    // 2.填充服务器网络信息结构体
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = inet_addr(argv[1]);
    server_addr.sin_port = htons(atoi(argv[2]));

    // 3.绑定套接字
    if (bind(sockfd, (struct sockaddr *)&serveraddr, addrlen) < 0)
    {
        ERR_LOG("fail to bind");
    }

    // 4.监听
    if (listen(sockfd, 5) < 0)
    {
        ERR_LOG("fail to listen");
    }

    // 使用信号,异步的方式处理僵尸进程,并不会堵塞父进程
    signal(SIGCHLD, handler);

    while (1)
    {
        // 5.阻塞等待客户端连接请求
        if ((acceptfd = accept(socket, (struct sockaddr *)&clientaddr, &addrlen)) < 0)
        {
            ERR_LOG("fail to accept");
        }

        printf("cl ip:%s, port:%d\n", inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port));

        // 6.创建子进程
        pid_t pid;
        if ((pid = fork()) < 0)
        {
            ERR_LOG("fail to fork");
        }
        else if (pid > 0) // 父进程负责执行accept,所以if语句结束后继续在accept函数的位置阻塞
        {
        }
        else // 子进程负责执行与客户端通信的代码
        {
            char buf[N] = {0};
            ssize_t bytes;
            while (1)
            {
                if ((bytes = read(acceptfd, buf, N)) < 0)
                {
                    ERR_LOG("fail to read");
                }
                else if (bytes == 0)
                {
                    printf("fail to recv\n");
                    exit(0);
                }

                if (strncmp(buf, "qaq", 4) == 0)
                {
                    printf("qaq\n");
                    exit(0);
                }

                printf("recv:%s\n", buf);

                strcat(buf, " from server");
                if (send(acceptfd, buf, N, 0) < 0)
                {
                    ERR_LOG("fail to send");
                }
            }
        }
    }

    return 0;
}

posted @ 2024-06-11 21:43  晖_IL  阅读(6)  评论(0编辑  收藏  举报