多个进程和管道 [英] Multiple processes and Pipes

查看:206
本文介绍了多个进程和管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个连接四个游戏,我想有多个进程和管道,但我不知道从哪里开始。我知道你要使用叉子和管道,但是当我刚比赛开始前叉,我得到每场比赛相同的结果。我只是困惑在哪里可以从这里走。任何建议将不胜AP preciated。下面是我的一些code,我卸妆的部位进行检查获胜,因为我没有必要看。

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&unistd.h中GT;
#包括LT&;&time.h中GT;INT *移动;
// ** INT委员会;
INT瓦;无效显示(INT **局,诠释行,诠释列);
INT ** build_board(INT N);
INT makeMove(INT **局,INT播放器);
INT checkVictory(INT **局);
INT checkHorr(INT **局);
无效AI_move(INT **局,INT球员,诠释player2);
无效播放(INT **局);
诠释主(){    函数srand((INT)时间(NULL));    INT宽度= 8;
    W = 8-1;    INT **局= ​​build_board(宽);
    INT ** Board2 = build_board(宽);    //显示(宽度,长度);    INT I,检查;    如果(叉()== 0){
        玩(局);
    }其他{
        看跌期权(中的其他);
        玩(Board2);
    }    返回0;
}无效播放(INT **局){    INT I,检查;    对于(I = 0; I&≤((W + 1)*(W + 1)/ 2);我++){        AI_move(局,1,2);        // makeMove(局,1);
        //显示(宽度,宽度);        检查= checkVictory(局);        如果(检查== 1 ||检查== 2){
            看跌期权(胜利董事会」);
            显示器(局,W + 1,W + 1);
            打破;
        }        // AI_move(局,2,1);
        makeMove(局,2);        检查= checkVictory(局);        如果(检查== 1 ||检查== 2){
            看跌期权(胜利董事会」);
            显示器(局,W + 1,W + 1);
            打破;
        }    }
}
INT ** build_board(INT N){    INT I,J;    INT **局=(INT **)的malloc(N * sizeof的为(int *));
    对于(i = 0; I< N;我++)
        董事会由[i] =(INT *)malloc的(N * sizeof的(INT));
    对于(i = 0; I< N;我++){
        为(J = 0; J< N; J ++){
            董事会由[i] [J] = 0;
        }
    }    返回委员会;
}无效AI_move(INT **局,INT球员,诠释player2){    INT I,J;
    为(J = 0; J&下; = W; J ++){
        为(ⅰ= W; I> = 0;我 - ){
            //输出(I:%d个\\ N,I);
            如果(J< W&放大器;&安培;董事会研究[J] [I] == 0安培;&安培;!板[J + 1] [I] = 0){
                董事会研究[J] [I] =玩家;
                如果(checkVictory(局)== 1){
                    看跌期权(找到制胜移动);
                    显示器(局,W + 1,W + 1);
                    返回;
                }
                其他
                    董事会研究[J] [I] = 0;
            }
        }
    }    makeMove(局,播放器);
}INT makeMove(INT **局,诠释播放器){    int类型的;
    开始:A =兰特()%(W + 1);
    INT I;
    为(ⅰ= W; I> = 0;我 - ){
        如果((板由[i] [A])== 0){
            板[I] [A] =玩家;
            返回1;
        }
    }    转到启动;
}无效显示(INT **局,诠释行,诠释列){    INT I,J;    对于(i = 0; I< = W;我++){
        为(J = 0; J&下; = W; J ++){
            如果(董事会由[i] [j]的== 1){
                的printf(R);
            }
            否则,如果(董事会由[i] [j]的== 2)
                的printf(B);
                //的printf(%d个,董事会由[i] [j]的);
            其他
                的printf( - );
        }
        的printf(\\ n);
    }}


解决方案

您没有任何管道在这里。如果你想父进程写其移动到孩子,孩子的举动写信给家长,你真的需要两个管道,每个方向(尽管有些系统有双向的管道)。

您有分叉之前创建两个管道。分叉后,父将关闭将会写入管道的读取结束时,将它从读出的管的写入端。同样的,儿童将关闭管的未端(但它会从父关闭不同文件描述符)

然后,两个玩家(进程)可以各自被设置为从一个管道到读写到其他。只要他们知道是谁写的第一次,不应该有任何问题。你需要留意的零长度读取;这些指示EOF(其他进程就不再出现)。

请记住,这两个过程将在 fork之后不再共享相同的数据();它们是自主用自己的私有变量。此举信息必须找出此举是自/至使接收过程能够正确地更新其董事会。除非在极端情况下,你会不会送全板沿着电线。

如果你决定,你必须向董事会沿着电线,那么你或许应该在一个字符数组建立董事会的文本再presentation和您发送到一个写操作合作伙伴的过程。

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).

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.

这篇关于多个进程和管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆