簡單併發協作進程實現

題目

這裏寫圖片描述


以後有時間再補管道的圖……


C語言代碼

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int fib(int n)
{
    if(n==1||n==2)
        return 1;
    return fib(n-1)+fib(n-2);
}
int main(int argc, char *argv[])
{       
    int pid_x;
    int pid_y;

    int pipe_0x[2];
    int pipe_0y[2];
    int pipe_x0[2];
    int pipe_y0[2];

    int i;
    int x;
    int y;
    int fx;
    int fy;

    if(pipe(pipe_0x) < 0){
    perror("pipe not create");
    exit(EXIT_FAILURE);
    }
    if(pipe(pipe_0y) < 0){
    perror("pipe not create");
    exit(EXIT_FAILURE);
    }
    if(pipe(pipe_x0) < 0){
    perror("pipe not create");
    exit(EXIT_FAILURE);
    }
    if(pipe(pipe_y0) < 0){
    perror("pipe not create");
    exit(EXIT_FAILURE);
    }

    if((pid_x=fork())<0)
    {
        perror("process not create");
        exit(EXIT_FAILURE);
    }


    if(pid_x==0)//子進程f(x)
    {
        close(pipe_0x[1]);
        close(pipe_x0[0]);
        while(1)
        {
            fx=1;
            read(pipe_0x[0],&x,sizeof(int));
            if(x==0)
            break;
//          printf("child_x read: %d\n",x);
            for(i=1;i<=x;i++)
            {
                fx *= i;
            }
            write(pipe_x0[1],&fx,sizeof(int));  
//          printf("child_x write f(x)=%d\n",fx);                       
        }
        close(pipe_0x[0]);
        close(pipe_x0[1]);
        //子進程執行結束
        printf("child_x exit\n");
        exit(EXIT_SUCCESS);

    }
    else//主進程
    {

        if((pid_y=fork())<0)
        {
            perror("process not create");
            exit(EXIT_FAILURE);
        }

        if(pid_y == 0)//子進程f(y)
        {
            close(pipe_0y[1]);
            close(pipe_y0[0]);

            while(1)
            {   
                read(pipe_0y[0],&y,sizeof(int));
                if(y==0)
                break;
//              printf("child_y read: %d\n",y);
                fy=fib(y);
                write(pipe_y0[1],&fy,sizeof(int));  
//              printf("child_y write f(y)=%d\n",fy);           
            }

            close(pipe_0y[0]);
            close(pipe_y0[1]);
            //子進程執行結束
            printf("child_y exit\n");
            exit(EXIT_SUCCESS);
        }
        else//主進程
        {

            close(pipe_0x[0]);
            close(pipe_x0[1]);
            close(pipe_0y[0]);
            close(pipe_y0[1]);

            printf("parent %d\n",getpid());

            do{
                printf("Please enter the value:\nx="); 
                scanf("%d",&x);
                printf("y=");
                scanf("%d",&y); 
                if(y==0||x==0)
                {   
                    x=0;
                    y=0;            
                    write(pipe_0x[1],&x,sizeof(int));   
                    write(pipe_0y[1],&y,sizeof(int));
                    break;
                }

                write(pipe_0x[1],&x,sizeof(int));   
//          printf("parent write to x\n");

                write(pipe_0y[1],&y,sizeof(int));           
//              printf("parent write to y\n");


                read(pipe_x0[0],&fx,sizeof(int));   
//              printf("parent read from x\n");

                read(pipe_y0[0],&fy,sizeof(int));
//              printf("parent read from y\n");

                printf("f(x)=%d\nf(y)=%d\nf(x,y)=%d\n",fx,fy,fx+fy);

            }while(x!=0&&y!=0);

            printf("EXIT!\n");
        }
    }
    return EXIT_SUCCESS;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章