<Linux> 实现匿名管道多进程任务派发

实现匿名管道多进程任务派发

mypipe

mypipe.cc

#include <iostream>
#include <string>
#include <unistd.h>
#include <cstring>
#include <cassert>
#include <sys/types.h>
#include <sys/wait.h>
#include <algorithm>
#include "Task.hpp"
#include "Log.hpp"
using namespace std;

#define PROCESS_NUM 10

int waitCommand(int fd, bool &quit)
{
    u_int32_t command = 0;
    ssize_t ret = read(fd, &command, sizeof command);
    Log("子进程读取任务", READ) << endl;
    if (0 == ret)
    {
        quit = true;
        return -1;
    }

    assert(ret == sizeof command);
    return command;
}

int main()
{
    // 装载任务
    load();

    // 父进程保留每个子进程的pid与对应管道文件写端描述符
    vector<pair<pid_t, int>> slots;

    for (size_t i = 0; i < PROCESS_NUM; i++)
    {

        int pipefd[2] = {0};
        int n = pipe(pipefd);

        Log("创建管道文件", CREATE) << endl;

        Log("打开管道文件", OPEN) << endl;

        assert(n != -1);
        // void(n);

        pid_t id = fork();
        assert(id != -1);

        if (0 == id)
        {
            // 子进程
            // 接收任务
            // 保留读,关闭1

            close(pipefd[1]);

            while (true)
            {
                bool quit = false;

                // 等待命令
                // 内部read阻塞等待
                int command = waitCommand(pipefd[0], quit);

                // 执行命令

                if (quit)
                {
                    break;
                }
                if (command > 0 && command < TASK_NUM)
                {

                    // 执行任务
                    callbacks[command]();
                }
                else
                {
                    cout << "非法命令: " << command + 1 << endl;
                }
            }

            exit(0);
            Log("关闭管道文件", CLOSE) << endl;
        }

        // 父进程
        // 保留写,关闭0
        // 保留每个管道对应子进程pid和写端

        close(pipefd[0]);
        slots.push_back(make_pair(id, pipefd[1]));
    }

    // 父进程
    // 分配任务

    srand((unsigned)time(nullptr));

    while (true)
    {

        printf("######################################\n");
        printf("#   1.show task      2.send task     #\n");
        printf("#   3.exit           4.....          #\n");
        printf("######################################\n");

        int command = -1;
        cin >> command;
        if (1 == command)
        {
            // auto it = desc.rbegin();

            // reverse(desc.cbegin(), desc.cend());
            for (auto e : desc)
            {
                cout << e.second << endl;
            }
        }
        else if (2 == command)
        {
            printf("Please chose task: ");

            int task = -1;
            cin >> task;

            // 派发任务给谁?
            int process = rand() % PROCESS_NUM;

            task--;

            write(slots[process].second, &task, sizeof task);

            Log("父进程发送任务", WRITE) << endl;
        }
        else if (3 == command)
        {
            break;
        }
        else
        {
            cout << "非法选项: " << command << endl;
        }

        sleep(2);
    }

    // 关闭fd
    for (auto e : slots)
    {
        close(e.second);
    }

    for (auto e : slots)
    {
        waitpid(e.first, nullptr, 0);
    }

    return 0;
}

Task

Task.hpp

#pragma once
#ifndef _TASK_H_
#define _TASK_H_
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

【 Stack_OverFlow 】

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值