Linux的系统调用使用fork()的传球整数到子和父进程的问题 [英] Linux System Calls problems using Fork() passing ints to child and parent processes

查看:159
本文介绍了Linux的系统调用使用fork()的传球整数到子和父进程的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个程序,将采取一个整数,并创建两个过程,一个家长和一个孩子。家长会减去5从整数,它传递给谁将会由5划分它的孩子,那么他们将重复此过程5次,每次打印整数的当前值。

整数可以通过文本文件传递和被书面和读出的,或者管道可以使用这曾经是简单。

我一直在寻找了所需要的系统调用,并有一个半工作程序。我一直坚持了几个小时然而,我认为我的问题是,我不能让他们等待对方来完成,因为我的输出不正确。

下面是我得到那么远。

 的#include<&stdio.h中GT;
#包括LT&;&unistd.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&; SYS / types.h中>
#包括LT&; SYS / wait.h>
#包括LT&;&文件ctype.h GT;
#包括LT&;&fcntl.h GT;INT主要(无效)
{
    INT X = 19530;
    INT W = 1;
    INT FD [2];
    管(FD);
    INT PID =叉();    时int k;
    为(K = 0; K&小于5; k ++){
        如果(PID大于0){
            // INT X = 19530;            如果((接近(FD [0]))== - 1){
                PERROR(关闭);
            }            读(FD [0],&放大器; X,的sizeof(int)的);
            X = X-5;
            写(FD [1],&放大器; X,的sizeof(int)的);
            的printf(X父%d个\\ N,X);            关闭(FD [1]);
            关闭(FD [0]);
        }如果别人(PID == 0){            如果((接近(FD [1]))== - 1){
                PERROR(关闭);
            }            读(FD [0],&放大器; X,的sizeof(int)的);
            X = X / 5;
            的printf(X儿童%d个\\ N,X);
            写(FD [1],&放大器; X,的sizeof(int)的);            关闭(FD [1]);
            关闭(FD [0]);
        }
    }
    返回0;
}

不过我的输出是一个问题,我得到:

  X在父母19525
中X的孩子3905
关闭::错误的文件描述符
中X父19520
关闭::错误的文件描述符
中X父19515
关闭::错误的文件描述符
中X父19510
关闭::错误的文件描述符
中X父19505
关闭::错误的文件描述符
中X子781
关闭::错误的文件描述符
中X子156
关闭::错误的文件描述符
中X 31儿童
关闭::错误的文件描述符
中X子6

这似乎开始很好,但孩子赶上前,孩子不连续传回正确则父运行次数太多。我也一直试图解决坏的文件描述符,但无济于事。

任何帮助将大大AP preciated。

感谢。


解决方案

 的#include<&stdio.h中GT;
#包括LT&;&unistd.h中GT;
#包括LT&;&err.h GT;#定义OK(X)({INT I_ =(X);如果(I_ == -1)错误(1,#X); I_;})枚举{P_,C_}; //父母,子女
枚举{R_,W_}; //读,写INT主要(无效)
{
        INT X = 19530;
        INT FD [2] [2];
        INT PID;        OK(管(FD [0]));
        确定(管道(FD [1]));        确定(PID =叉());        关闭(FD [P _] [PID R_:W_?]);
        关闭(FD [C _] [PID W_:R_?]);        的for(int i = 0;我小于5;我++){
                如果(PID){
                        点¯x - = 5;
                        的printf(X父%d个\\ N,X);
                        确定(写(FD [P _] [W_],&放大器; X,的sizeof(X)));
                        OK(读(FD [C _] [R_],和放大器; X,的sizeof(X)));
                }
                其他{
                        确定(读(FD [P _] [R_],&放大器; X,的sizeof(X)));
                        X / = 5;
                        的printf(X儿童%d个\\ N,X);
                        OK(写(FD [C _] [W_],和放大器; X,的sizeof(X)));
                }
        }
        返回0;
}

管道是单向的,所以你需要两个。我使用了一些枚举,试图使事情更容易阅读。

I am working on a program that will take an integer and create two processes, a parent and a child. The parent will subtract 5 from the integer, pass it to the child who will divide it by 5, and then they will repeat this process 5 times, each time printing the current value of the integer.

The integer can be passed through a text file and be both written and read off of, or a pipeline can be used which ever is simpler.

I have been looking up the systems calls needed, and have a semi working program. I have been stuck for hours however, and I think my issue is that I can't get them to wait for each other to finish because my output is incorrect.

Here is what I got so far.

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <ctype.h>
#include <fcntl.h>

int main(void)
{    
    int x=19530;
    int w=1;
    int fd[2];
    pipe(fd);
    int pid = fork();

    int k;
    for (k=0; k<5; k++) {
        if (pid>0) {
            //int x = 19530;

            if ((close(fd[0]))==-1) {
                perror("Close:");
            }

            read(fd[0], &x, sizeof(int));
            x=x-5;
            write (fd[1], &x, sizeof(int));
            printf("X in parent %d\n", x);

            close(fd[1]);
            close(fd[0]);
        } else if (pid==0) {

            if ((close(fd[1]))==-1) {
                perror("Close:");
            }

            read(fd[0], &x, sizeof(int));
            x=x/5;
            printf("X in child %d\n", x);
            write (fd[1], &x, sizeof(int));

            close(fd[1]);
            close(fd[0]);
        }
    }
    return 0;
}

However my output is an issue, I am getting:

X in parent 19525
X in child 3905
Close:: Bad file descriptor
X in parent 19520
Close:: Bad file descriptor
X in parent 19515
Close:: Bad file descriptor
X in parent 19510
Close:: Bad file descriptor
X in parent 19505
Close:: Bad file descriptor
X in child 781
Close:: Bad file descriptor
X in child 156
Close:: Bad file descriptor
X in child 31
Close:: Bad file descriptor
X in child 6

It seems to start off well, but then the child doesn't pass back properly then the parent runs too many times in a row before the child catches up. I also been trying to fix that bad file descriptor but to no avail.

Any help would be greatly appreciated.

Thanks.

解决方案

#include <stdio.h>
#include <unistd.h>
#include <err.h>

#define ok(x) ({ int i_ = (x); if (i_ == -1) err(1, #x); i_; })

enum { p_, c_ }; // parent, child
enum { r_, w_ }; // read, write

int main(void)
{
        int x = 19530;
        int fd[2][2];
        int pid;

        ok(pipe(fd[0]));
        ok(pipe(fd[1]));

        ok(pid = fork());

        close(fd[p_][pid ? r_ : w_]);
        close(fd[c_][pid ? w_ : r_]);

        for (int i = 0; i < 5; i++) {
                if (pid) {
                        x -= 5;
                        printf("X in parent %d\n", x);
                        ok(write(fd[p_][w_], &x, sizeof(x)));
                        ok(read(fd[c_][r_], &x, sizeof(x)));
                }
                else {
                        ok(read(fd[p_][r_], &x, sizeof(x)));
                        x /= 5;
                        printf("X in child %d\n", x);
                        ok(write(fd[c_][w_], &x, sizeof(x)));
                }
        }
        return 0;
}

Pipes are unidirectional, so you need two. I used some enums to try and make things easier to read.

这篇关于Linux的系统调用使用fork()的传球整数到子和父进程的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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