C中的管道通信

时间:2013-03-04 01:03:53

标签: c process pipe

我一直在更多地研究管道,我想知道是否有一种方法可以使用管道在同一个进程中进行读写。这是我的代码:

// Creates 2 pipes per child
    for (i=0; i<atoi(argv[1]); i++) {
        pipe(fd[i]);
    }

    for(i = 0; i < atoi(argv[1]); i++) {
        pid = fork();
        //pipe(fd);
        int r[2];
        int check=0;
        srand(getpid());
        int** Board=build_board(width);
        if(pid < 0) {
            printf("Error");
            exit(1);
        } else if (pid == 0) {
            printf("Child (%d): %d\n", i + 1, getpid());

           // srand(getpid());

            makeMove(Board, 2,r);
            printf("Child R: %d:%d\n", r[0],r[1]);
            display(Board, width, width);

            close(fd[i][0]);

            write(fd[i][1], r, sizeof(r));


         //   play(Board);
            exit(0);
        } else  {
            printf("Parent (%d): %d\n", i + 1, getpid());

            while (1) {

                if (check==0) {

                close(fd[i][1]);
                read(fd[i][0], readbuffer, sizeof(readbuffer));
                printf("Received string: %d:%d\n", readbuffer[0], readbuffer[1]);

                Board[readbuffer[0]][readbuffer[1]]=2;
                puts("Fixed Board");
                display(Board, width, width);
                check=checkVictory(Board);

                if (check!=0) {
                    puts("Winner");
                    display(Board, width, width);
                    break;
                }

                //srand(getpid());
                AI_move(Board,1,2);
                display(Board, width, width);

                }   
            }
            wait(NULL);
        }
    }

我正在尝试在两个进程之间来回发送移动,但是根据我的代码,我从孩子那里得到第一个移动,然后父移动直到它获胜。我相信在父母移动之后我必须回信孩子,但我在子过程的顶部关闭了这个能力。在找到胜利者和退出胜利之前,我和他们之间是如何相互沟通的。

以下是一些示例输出:

Parent (1): 17291
Child (1): 17293
Child laced peice at: 7,3
Child R: 7:3
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  B  -  -  -  - 
Received string: 7:3
Fixed Board
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  B  -  -  -  - 
Parent placed peice at: 7,5
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  B  -  R  -  - 
Received string: 7:3
Fixed Board
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  B  -  R  -  - 
Parent placed peice at: 6,5
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  R  -  - 
 -  -  -  B  -  R  -  - 
Received string: 7:3
Fixed Board
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  R  -  - 
 -  -  -  B  -  R  -  - 
Parent placed peice at: 7,4
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  R  -  - 
 -  -  -  B  R  R  -  - 
Received string: 7:3
Fixed Board
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  R  -  - 
 -  -  -  B  R  R  -  - 
Parent placed peice at: 7,0
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  R  -  - 
 R  -  -  B  R  R  -  - 
Received string: 7:3
Fixed Board
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  R  -  - 
 R  -  -  B  R  R  -  - 
Parent placed peice at: 7,2
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  R  -  - 
 R  -  R  B  R  R  -  - 
Received string: 7:3
Fixed Board
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  R  -  - 
 R  -  R  B  R  R  -  - 
Parent placed peice at: 6,2
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  R  -  -  R  -  - 
 R  -  R  B  R  R  -  - 
Received string: 7:3
Fixed Board
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  R  -  -  R  -  - 
 R  -  R  B  R  R  -  - 
Parent placed peice at: 5,5
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  R  -  - 
 -  -  R  -  -  R  -  - 
 R  -  R  B  R  R  -  - 
Received string: 7:3
Fixed Board
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  R  -  - 
 -  -  R  -  -  R  -  - 
 R  -  R  B  R  R  -  - 
Parent placed peice at: 4,5
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  R  -  - 
 -  -  -  -  -  R  -  - 
 -  -  R  -  -  R  -  - 
 R  -  R  B  R  R  -  - 
Received string: 7:3
Fixed Board
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  R  -  - 
 -  -  -  -  -  R  -  - 
 -  -  R  -  -  R  -  - 
 R  -  R  B  R  R  -  - 
Winner
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  -  -  - 
 -  -  -  -  -  R  -  - 
 -  -  -  -  -  R  -  - 
 -  -  R  -  -  R  -  - 
 R  -  R  B  R  R  -  - 

1 个答案:

答案 0 :(得分:0)

以下是一些代码的概述,大致是我如何实现它。我假设这两个球员将是父母和子女的过程;我不确定两个孩子如何与父母一起工作。

#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

static void err_exit(const char *, ...);
static void be_childish(int fd_in, int fd_out);
static void be_parental(int fd_in, int fd_out);

int main(void)
{
    pid_t pid;
    int to_child[2];
    int to_parent[2];

    if (pipe(to_child) != 0 || pipe(to_parent) != 0)
        err_exit("Failed to open pipe");
    if ((pid = fork()) < 0)
        err_exit("Failed to fork");

    if (pid == 0)
    {
        close(to_child[1]);
        close(to_parent[0]);
        be_childish(to_child[0], to_parent[1]);
    }
    else
    {  
        close(to_child[0]);
        close(to_parent[1]);
        be_parental(to_parent[0], to_child[1]);
    }
    return(0);
}

static void be_childish(int fd_in, int fd_out)
{
    srand(getpid());
    char buffer[32];
    ssize_t nbytes;

    while ((nbytes = read(fd_in, buffer, sizeof(buffer))) > 0)
    {
        /* Process parent's move from buffer */
        /* Write child's move to buffer */
        nbytes = strlen(buffer);
        if (write(fd_out, buffer, nbytes) != nbytes)
            err_exit("Short write from child to parent");
    }
}

static void be_parental(int fd_in, int fd_out)
{
    srand(getpid());
    char buffer[32];
    ssize_t nbytes;

    /* Determine first move */
    nbytes = strlen(buffer);

    if (write(fd_out, buffer, nbytes) != nbytes)
        err_exit("Short write from parent to child");
    while ((nbytes = read(fd_in, buffer, sizeof(buffer))) > 0)
    {
        /* Process child's move from buffer */
        /* Write parent's move to buffer */
        nbytes = strlen(buffer);
        if (write(fd_out, buffer, nbytes) != nbytes)
            err_exit("Short write from parent to child");
    }
}

static void err_exit(const char *fmt, ...)
{
    int errnum = errno;
    va_list args;
    va_start(args, fmt);
    vfprintf(stderr, fmt, args);
    va_end(args);
    if (errnum != 0)
        fprintf(stderr, " (%d: %s)", errnum, strerror(errnum));
    putc('\n', stderr);
    exit(1);
}

err_exit()函数简化了错误报告 - 通常在报告错误的位置只有一行。您可以在主题上使用无穷无尽的变体,但具有此类功能可以极大地简化对错误的检查。