简体   繁体   English

流程,派生,管道程序...我在哪里出错?

[英]Processes, fork, pipes program…Where am I going wrong?

I'm working in Unix and I'm trying to write a .c file that will do the following: 我正在Unix上工作,并且试图编写一个.c文件来执行以下操作:

For each command line argument the main process will launch a subprocess (type A). 对于每个命令行参数,主进程将启动一个子进程(类型A)。

The A process will try to execute, in a new subprocess (type B), using one of the exec functions, the received argument. A进程将尝试使用exec函数之一在新的子进程(类型B)中执行接收到的参数。

In case of failure, the process of type A, will send to his parent, using a pipe channel, the error code (errno value) generated by the exec call. 如果失败,类型A的进程将使用管道通道将exec调用生成的错误代码(errno值)发送给其父级。

On success, the process A, will wait for process of type B and once finished it will transmit to his parent the zero code, using the same pipe channel. 成功后,进程A将等待类型B的进程,一旦完成,它将使用相同的管道通道将零代码发送给其父级。

The main process will print for each argument if the execution was successful or not. 如果执行成功与否,将为每个参数打印主进程。 Only for failed executions the error received code will be printed too. 仅对于失败的执行,也会打印收到的错误代码。

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

int main(int argc, char const *argv[]) {
        int c2p[2], g2c[2];
        int i;
        char n[100];
        int *stat;
        for (i = 0; i < argc; i++ ) {
                pipe(c2p);
                pipe(g2c);
                if (fork() == 0) {
                        //child code
                        if (fork() == 0) {
                                //grandchild code
                                if (execlp("argv[i]", "argv[i]", NULL) == -1) {
                                        write(g2c[1], errno, sizeof(int));
                                } else {
                                        write(g2c[1], 0, sizeof(int));
                                }
                        }
                        wait(0);
                        read(g2c[0], n, 10);
                        write(c2p[1], n, 10);
                        exit(0);
                }
                read(c2p[0], &stat, sizeof(int));
                if (*stat !=0) {
                        printf("Not succsesful: %s  %d\n", argv[i], *stat);
                } else {
                        printf("Succes! %s\n", argv[i]);
                }
        }

        return 0;
}

Changed your code as follow and this would worked for you: 如下更改您的代码,这将为您工作:

int main(int argc, char const *argv[])
{
    int c2p[2];
    int i;
    int stat;

    for (i = 1; i < argc; i++ ) {
            pipe(c2p);
            if (fork() == 0) {
                    //child code
                    if (fork() == 0) {
                            //grandchild code
                            if (execlp(argv[i], argv[i], NULL) == -1)
                                 exit(errno);
                    }
                    wait(&stat);
                    write(c2p[1], &stat, sizeof(stat));
                    exit(0);
             }
             read(c2p[0], &stat, sizeof(int));
             if (stat != 0)
                   printf("Not succsesful: %s %d\n", argv[i], stat);
             else
                   printf("Succes! %s\n", argv[i]);
    }
}

as you see in my code: 如您在我的代码中看到的:

  • stat must be integer not pointer stat必须是整数而不是指针
  • you pass argv[i] as argument not "argv[i]" into execlp 您将argv [i]作为参数而不是“ argv [i]”传递给execlp
  • you exit status code for passing status of grandchild (you can also you this technique in child) 您退出用于传递孙子状态的状态代码(您也可以在孩子中使用此技术)
  • start your execution loop from 1 because argv[0] is your program name. 从1开始执行循环,因为argv [0]是您的程序名称。
  • always remember after using exec family function your source code in that process replace with program you call so you cannot doing after it successful run. 永远记住,使用exec系列功能后,您在该过程中的源代码将替换为您调用的程序,因此在成功运行后便无法执行。

Two problems: 两个问题:

  • you can't write an integer that way, write expects a pointer , so int n; ...; write(fd, &n, sizeof n); 你不能那样写整数,写期望有一个指针 ,所以int n; ...; write(fd, &n, sizeof n); int n; ...; write(fd, &n, sizeof n);
  • execlp replaces the process that calls it, meaning if it works you'll never write zero to the pipe, because the grandchild has been replaced by the program it's execlp()ing execlp 替换了调用它的进程,这意味着如果它可以正常工作,则永远不会向管道写入零,因为孙子已被其execlp()ing的程序所替换

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

相关问题 我在哪里错这个推理? - Where am I going wrong about this reasoning? 编写了一个打印素数的程序,但没有得到所需的输出。我哪里出错了? - wrote a program for printing the prime numbers not getting the desired output .where i am going wrong? 在这个简单的 C 程序中分配内存时我哪里出错了? - Where am I going wrong when allocating memory in this simple C program? 我只是不确定我要去哪里。 编写程序以打印c中的素因式分解并练习与之链接 - I just am not sure where I am going wrong. Writing a program to print out prime factorization in c and practicing linking along with it 使用 fork() 和管道的进程环 - Ring of processes using fork() and pipes 它仍然崩溃,我要去哪里错了? - It's still crashing, where am I going wrong? 我的内存管理哪里出问题了? - Where am I going wrong with my memory management? 叉子和管道,我做错了什么? - forks and pipes, what am I doing wrong? 获取以下代码的细分错误。 我无法弄清楚我要去哪里 - Getting segmentation fault for the code below. I am not able to figure out where am I going wrong 这是用于评估后缀表达式的 C 代码。 我添加了评论以查看我哪里出错了 - This is C code for evaluation of a postfix expression. I have added comments to see where I am going wrong
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM