简体   繁体   English

多个过程和管道

[英]Multiple processes and Pipes

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

}

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). 那些表示EOF(不再存在其他进程)。

Remember that the two processes will no longer share the same data after the fork() ; 请记住,在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. 如果您决定必须通过线路发送电路板,则可能应该以字符数组创建电路板的文本表示形式,然后通过一次写入操作将其发送给合作伙伴进程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM