通过功能创建管道

时间:2018-09-24 15:58:57

标签: c pipe fork

我想知道是否有人可以帮助我修改当前代码。...

当前,它使用fork()创建我的进程,并获取指向执行该子代码块的函数的指针。

我想玩弄管道,现在尝试让Process Y将其pid发送给Process X,然后我想将其发送回Main ...

这是我目前拥有的

#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <stdlib.h> // exit
#include <fcntl.h>
#include <sys/wait.h>
void processX();
void processY();

pid_t addChild(void (*childPtr) (), int fileDes[2]) {
    pid_t cpid;
    if((cpid=fork()) == 0) {
        pipe(fileDes);
        childPtr(fileDes);
        wait(NULL);
        exit(0);
    } else if (cpid < 0) {
        printf("failed to fork");
        exit(1);
    } else {

    }
    return cpid;
}

void processY(int fileDes[2]) {
    printf("Child Y[%d] Created of Parent X[%d]\n", getpid(), getppid());
    printf("We are now going to write Y PID to process X\n");
    pid_t a = getpid();
    char buf[1024]; // child reads from pipe() to buffer
    close(fileDes[0]); // close reading end of the pipe
    write(fileDes[1], &a, sizeof(buf) / sizeof(int));

}

void processX(int fileDes[2]) {
    printf("Child X[%d] Created of parent Main[%d]\n", getpid(), getppid());
    int status;

    pid_t Y = addChild(processY, fileDes);
    wait(&status);

    pid_t new_val = 5;
    close(fileDes[1]); // closing the writing end of the pipe.
    read(fileDes[0], &new_val, sizeof(new_val));
    printf("Message read with number %d: \n", new_val);

}


int main() {
    int status;
    int fd[2];
    printf("Main process[%d]\n", getpid());
    pid_t root = addChild(processX, fd);
    wait(&status);
    printf("We are going to read from X to Main and then return the Value we got from Y\n");
    return 0;
}

我不知道要从Y-X然后是X-Main ....来创建管道。

Y ---->发送pid ----> X收到Y pid -----将新信息发送到主---> Main打印收到的数据...

1 个答案:

答案 0 :(得分:0)

我想出了我的答案

#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <stdlib.h> // exit
#include <fcntl.h>
#include <sys/wait.h>
void processX();
void processY();

pid_t addChild(void (*childPtr) (), int fileDes[2], int backToMainFd[2]) {
    pid_t cpid;
    if(childPtr != *processX //prevents the the pipe from main to x from recreating
        pipe(fileDes);

    if((cpid=fork()) == 0) {
        if(childPtr == *processX) {
            childPtr(fileDes, backToMainFd);
        } else {
            childPtr(fileDes);
        }
        wait(NULL);
        exit(0);
    } else if (cpid < 0) {
        printf("failed to fork");
        exit(1);
    } else {

    }
    return cpid;
}

void processY(int fileDes[2]) {
    printf("[PROCESS Y]: Child Y[%d] Created of Parent X[%d]\n", getpid(), getppid());
    pid_t a = getpid();
    char buf[1024]; // child reads from pipe() to buffer
    close(fileDes[0]); // close reading end of the pipe
    write(fileDes[1], &a, sizeof(buf) / sizeof(int));

}

void processX(int fileDes[2], int BackToMainFd[2]) {
    printf("[PROCESS X]: Child X[%d] Created of parent Main[%d]\n", getpid(), getppid());
    int status;

    pid_t Y = addChild(processY, fileDes, NULL);
    wait(&status);

    pid_t new_val = 5;
    close(fileDes[1]); // closing the writing end of the pipe.
    read(fileDes[0], &new_val, sizeof(new_val));
    printf("[PROCESS X]: We got Ys' PID as:%d from [PROCESS Y]\n", new_val);

    close(BackToMainFd[0]); // close reading end of the pipe
    char buf[1024]; // child reads from pipe() to buffer
    write(BackToMainFd[1], &new_val, sizeof(buf) / sizeof(pid_t));

}


int main() {
    int status;
    int fd[2];
    int backToMainFD[2];
    printf("Main process[%d]\n", getpid());
    pipe(backToMainFD);
    pid_t root = addChild(processX, fd, backToMainFD);
    wait(&status);

    pid_t new_val = 5;
    close(backToMainFD[1]); // closing the writing end of the pipe.
    read(backToMainFD[0], &new_val, sizeof(new_val));
    printf("[MAIN]: We got Ys' PID as:%d from [PROCESS X]\n", new_val);
    printf("Send sig kills too Y and root\n");
    kill(new_val, SIGKILL);
    kill(root, SIGKILL);
    printf("Terminate program.\n");
    return 0;
}