0

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

// 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);
        }
    }

我试图在两个进程之间来回发送动作,但是使用我拥有的代码,我从孩子那里得到了第一个动作,然后父母只是移动直到它获胜。我相信在父母搬家后我必须回信给孩子,但我在孩子进程的顶部关闭了这个能力。How 和我都相互沟通,直到找到获胜者以及何时退出。

这是一些示例输出:

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  -  - 
4

1 回答 1

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()功能简化了错误报告——通常在报告错误的地方只有一行。您可以在主题上使用无穷无尽的变化,但拥有这样的功能会大大简化错误检查。

于 2013-03-04T01:49:39.333 回答