使用execvp传递参数

使用execvp传递参数,c,gcc,fork,execvp,C,Gcc,Fork,Execvp,我的parent.c文件中有这个 int main() { int n = 6; int pid; int status; char* command = "./child"; for (i=1; i<=n; i++){ if((pid = fork()) == 0) { execvp(command, NULL); } wait(&status); } 我基本上只是跑 gcc-o parent.c gcc-o child.c /家长

我的parent.c文件中有这个

int main()
{
int n = 6;
int pid;
int status;
char* command = "./child";
for (i=1; i<=n; i++){


    if((pid = fork()) == 0) {
        execvp(command, NULL);
    }
    wait(&status);
}
我基本上只是跑

gcc-o parent.c

gcc-o child.c

/家长

这会打印出这是6次的子空值,这是我所期望的。但我希望能够在运行child时传递一个参数,在本例中是进程号

所以我把我的父母改成这样

for (i=1; i<=n; i++){
    if(i == 1){
        char* args = "1";
    }
    if(i == 2){
        char* args = "2";
    }
    if(i == 3){
        char* args = "3";
    }
    if(i == 4){
        char* args = "4";
    }
    if(i == 5){
        char* args = "5";
    }
    if(i == 6){
        char* args = "6";
    }

    if((pid = fork()) == 0) {
        execvp(command, args);
    }
    wait(&status);
}
我以为会发生的是我的程序会打印这是孩子1,这是孩子2等等

然而,实际发生的情况是,程序似乎多次运行parent.c,我在parent.c的开头放了一个print语句,输出将该语句打印了20次,而不是child.c

有人能解释为什么会这样吗?有没有其他方法可以将参数传递给child.c

谢谢

有人能解释为什么会这样吗?有没有其他方法可以将参数传递给child.c

您应该将char*数组传递给execvp,而不是char*。如注释中所述,数组的最后一个元素应该是空指针。以下方面应起作用:

char* args[] = {"1\0", NULL};
execvp(command, args);
我猜execvp失败了,因为它不能将args作为char**取消引用,因此forked进程作为父进程继续循环。您应该检查execvp的返回值,以查看函数调用是否有效


此外,命令和参数应以null结尾。我建议使用函数itoa将int转换为c字符串。

目前的程序存在一些问题。首先,正如其他人所描述的,execvp的第二个参数是char**类型。。。它在执行程序中以argv结束

here is the critical excerpt from the man page for execvp()

   The execv(), execvp(), and execvpe()  functions  provide  an  array  of
   pointers  to  null-terminated  strings that represent the argument list
   available to the new  program.   The  first  argument,  by  convention,
   should  point  to the filename associated with the file being executed.
   The array of pointers must be terminated by a NULL pointer.
其次是 ifi==1{ char*args=1; } ... 代码设置一个变量args,其作用域在下一行结束。必须在要使用参数的作用域中声明参数

第三,将数字转换成这样的字符串是非常有限和乏味的!使用标准的C函数sprintf或者更好的snprintf会更好

以下是更新的父级。c:

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

int main()
{
        int n = 6;
        int pid;
        int status;
        char* command = "./child";
        char *child_args[2];
        char number_buff[32];   // more than big enough to hold any number!

        child_args[1] = NULL;
        for (int i=1; i<=n; i++){
                sprintf(number_buff, "%d", i);
                child_args[0] = number_buff;
                if((pid = fork()) == 0) {
                        execvp(command, child_args);
                }
                wait(&status);
        }
        return 0;
}

为什么期望argv[0]的值为0或1?execvp的手册页解释了如何传递参数。你说的不正确。它需要char*argv[]一个指向参数指针数组的指针。另一方面,if语句的字符串将作为switch语句来完成将存储6,我将能够在子级中使用它,但它不起作用如果您阅读execvp的文档,它应该会澄清一些事情。传递给execvp的参数是:1要执行的文件的路径/名称2传递的第三个参数中的条目数3指向C字符串的指针数组的P点,最后一个条目为空。
#include <sys/wait.h>
#include <stdio.h>
#include <unistd.h>

int main()
{
        int n = 6;
        int pid;
        int status;
        char* command = "./child";
        char *child_args[2];
        char number_buff[32];   // more than big enough to hold any number!

        child_args[1] = NULL;
        for (int i=1; i<=n; i++){
                sprintf(number_buff, "%d", i);
                child_args[0] = number_buff;
                if((pid = fork()) == 0) {
                        execvp(command, child_args);
                }
                wait(&status);
        }
        return 0;
}