Multiple processes and Pipes

2019-08-21 17:18发布

问题:

I am making a connect four game and I would like to have Multiple processes and pipes, but I'm not sure where to start. I know you have to use fork and pipe, but when I fork just before the start of the game I get the same result for each game. I'm just confused where to go from here. Any suggestion would be greatly appreciated. Below is some of my code, I remover the parts for checking for wins, because I is not necessary to see.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>

int *moves;
//int **Board;
int w;

void display(int ** Board,int rows, int columns);
int** build_board(int N);
int makeMove(int** Board, int player);
int checkVictory(int** Board);
int checkHorr(int** Board);
void AI_move(int** Board,int player, int player2);
void play(int **Board);


int main(){

    srand((int) time(NULL));

    int width= 8;
    w=8-1;

    int** Board=build_board(width);
    int **Board2=build_board(width);

    //display(width, length);

    int i, check;

    if(fork()==0){
        play(Board);
    }else{
        puts("In Else");
        play(Board2);
    }

    return 0;
}

void play(int **Board){

    int i, check;

    for (i=0; i<((w+1)*(w+1)/2); i++) {

        AI_move(Board,1,2);

        // makeMove(Board, 1);
        // display(width, width);

        check=checkVictory(Board);

        if (check==1 || check==2) {
            puts("Winning Board");
            display(Board,w+1, w+1);
            break;
        }

        // AI_move(Board,2,1);
        makeMove(Board,2);

        check=checkVictory(Board);

        if (check==1 || check==2) {
            puts("Winning Board");
            display(Board, w+1, w+1);
            break;
        }

    }
}


int** build_board(int N){

    int i,j;

    int **Board = (int**) malloc(N*sizeof(int*));
    for (i = 0; i < N; i++)
        Board[i] = (int*) malloc(N*sizeof(int));
    for (i = 0; i < N; i++) {
        for (j = 0; j < N; j++) {
            Board[i][j] = 0;
        }
    }

    return Board;
}

void AI_move(int**Board,int player, int player2){

    int i,j;


    for (j=0; j<=w; j++) {
        for (i=w; i>=0; i--) {
            // printf("I: %d\n", i);
            if ( j < w && Board[j][i]==0 && Board[j+1][i]!=0) {
                Board[j][i]=player;
                if(checkVictory(Board)==1){
                    puts("Found Winning Move");
                    display(Board,w+1, w+1);
                    return;
                }
                else
                    Board[j][i]=0;
            }
        }
    }

    makeMove(Board,player);
}

int makeMove(int** Board,int player){

    int a;    
    start:a= rand()%(w+1);
    int i;
    for (i=w; i>=0; i--) {
        if ((Board[i][a])==0) {
            Board[i][a]=player;
            return 1;
        }
    }

    goto start;    
}

void display(int** Board,int rows, int columns){

    int i,j;

    for (i=0; i <= w;i++){
        for (j=0;j <=w;j++){
            if (Board[i][j]==1) {
                printf(" R ");
            }
            else if(Board[i][j]==2)
                printf(" B ");
                //printf(" %d ",Board[i][j]);
            else
                printf(" - ");
        }
        printf("\n");
    }

}

回答1:

You don't have any pipes here. If you want the parent process to write its moves to the child and the child to write its moves to the parent, you really need two pipes, one for each direction (though some systems have bidirectional pipes).

You have to create both pipes before forking. After forking, the parent would close the read end of the pipe it will write to, and the write end of the pipe it will read from. Similarly, the child would close the unused ends of the pipes (but it would close different file descriptors from the parent).

Then the two players (processes) can each be set to read from one pipe and write to the other. As long as they know who's writing first, there shouldn't be any problems. You do need to keep an eye out for zero-length reads; those indicate EOF (the other process is no longer there).

Remember that the two processes will no longer share the same data after the fork(); they are autonomous with their own private variables. The move information must identify where the move is from/to so that the receiving process can update its board correctly. Except in extremis, you won't send the whole board down the wire.

If you decide you must send the board down the wire, then you should probably create a text representation of the board in an array of characters and you send that to the partner process in a single write operation.



标签: c fork pipe