通过命令行参数一个子进程,并指望他们 [英] passing command line arguments to a child process and count them

查看:136
本文介绍了通过命令行参数一个子进程,并指望他们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想父进程采取的参数的main(),并通过管道在其中一次发送的字符子进程以argv [1],并继续通过参数的休息。(一个调用写每个字符)。

欲子进程计数由父进程发送给它的字符,并打印出其从父接收的字符数。子进程不应该以任何方式使用该参数的main()。

我究竟做错了什么?我是否需要使用EXEC()?

这心不是正确的输出:

 〜$ GC A03
GCC -Wall -g -o a03.c A03
〜$ ./a03 ABCD EF GHI孩子:计12个字符
〜$

下面是节目..

 的#include< SYS / wait.h>
#包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&unistd.h中GT;
#包括LT&;&string.h中GT;INT主(INT ARGC,CHAR *的argv [])
{    INT长度= 0;
    INT I,计数;    INT fdest [2]; //管道
    将为pid_t PID; //进程ID
    字符缓冲区[BUFSIZ];    如果(管道(fdest)小于0)/ *尝试建立管* /
        的printf(管子错误);    如果((PID =叉())小于0)/ *试图创建子/父进程* /    {
        的printf(叉错误);
    }
    / *父进程* /
    否则如果(PID大于0){
        关闭(fdest [0]);        对于(i = 1; I< ARGC,我++)/ *写*管材/
        {
            写(fdest [1],ARGV [I],strlen的(ARGV [1]));
        }        (0)等;    }其他{        / *子进程* /
        关闭(fdest [1]);        对于(i = 0; I< ARGC,我++)
        {
            长度+ =(strlen的(的argv [I])); / *参数获取长度* /
        }        计数=读(fdest [0],缓冲液,长度);
        的printf(\\ nchild:计%d个字符\\ n,计数);    }    出口(0);}


解决方案

您说:以任何方式,子进程不应该使用参数的main()。但是,我看到你的孩子的过程中使用 ARGC 。这难道不是击败你的限制呢?

您也说要一次通话写的每个字符。您当前实现使用一个调用写的每一个参数,而不是每个字符。这是一个错字?如果没有,你将要使用这样的东西更多:

 字符NUL ='\\ 0',ENDL ='\\ n';
用于:(a = 1;&下;的argc ++一个){
    为(C = 0;℃下的strlen(的argv [A]); ++ C){
        写(fdest [1],和放大器;的argv [A] [C],1);
    }
    写(fdest [1],&安培; NUL,1);
}
写(fdest [1],&安培; ENDL,1);

这一次会写一个字,每一个参数为空值终止字符串,并在最后一个换行符。换行符是只有在那里,作为一个标记,以表明有没有更多的数据发送(并且是安全的使用,因为你不会换行来传递一个命令行参数)。

子进程将只需要一个循环,一个读取输入的数据之一,并增加计数器如果字节是不是'\\ 0'的'\\ n'。当它读取换行符,它突破了输入处理循环,并报告该计数器的值。

I want the parent process to take the arguments to main() and send the characters in them one at a time to the child process through a pipe starting with argv[1] and continue through the rest of the arguments.(one call to write for each character).

I want the child process to count the characters sent to it by the parent process and print out the number of characters it received from the parent. The child process should not use the arguments to main() in any way whatsoever.

What am i doing wrong? do i need to use exec()?

output that isnt correct:

    ~ $ gc a03
gcc -Wall -g a03.c -o a03
~ $ ./a03 abcd ef ghi

child: counted 12 characters
~ $

here is the program..

#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

int main(int argc, char *argv[])
{

    int length = 0;
    int i, count;

    int     fdest[2];          // for pipe
    pid_t   pid;              //process IDs
    char    buffer[BUFSIZ];



    if (pipe(fdest) < 0)          /* attempt to create pipe */
        printf("pipe error");

    if ((pid = fork()) < 0)  /* attempt to create child / parent process */

    {
        printf("fork error");
    } 


    /* parent process */
    else if (pid > 0) {      
        close(fdest[0]);

        for(i = 1; i < argc; i++)    /* write to pipe */
        {
            write(fdest[1], argv[i], strlen(argv[1]));
        }

        wait(0);

    } else {   

        /* child Process */
        close(fdest[1]);

        for(i = 0; i < argc; i++)
        {
            length +=( strlen(argv[i])); /* get length of arguments */
        }

        count = read(fdest[0], buffer, length);
        printf("\nchild: counted %d characters\n", count);

    }

    exit(0);

}

解决方案

You said that "the child process should not use the arguments to main() in any way whatsoever". However, I see that your child process is using argc. Doesn't this defeat your restriction?

You also say that you want "one call to write for each character". Your current implementation uses one call to write for each argument, not each character. Was this a typo? If not, you will want to use something more like this:

char nul='\0', endl='\n';
for (a=1; a < argc; ++a) {
    for (c=0; c < strlen(argv[a]); ++c) {
        write(fdest[1], &argv[a][c], 1);
    }
    write(fdest[1], &nul, 1);
}
write(fdest[1], &endl, 1);

This will write one character at a time, with each argument as a NULL-terminated string and a newline character at the end. The newline is only there to serve as a marker to indicate that there is no more data to send (and is safe to use since you won't be passing in a newline in a CLI argument).

The child process will just need to be a loop that reads incoming bytes one by one and increments a counter if the byte is not '\0' or '\n'. When it reads the newline character, it breaks out of the input processing loop and reports the value of the counter.

这篇关于通过命令行参数一个子进程,并指望他们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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