gpt4 book ai didi

c - Execve 给出了错误的地址

转载 作者:太空宇宙 更新时间:2023-11-04 00:45:35 25 4
gpt4 key购买 nike

我在使用 execve 命令时遇到了一点问题。程序 Test 应该创建两个 child ,每个 child 都应该运行一个 execve 来加载和运行另一个程序。但是我在两个 execve 上的地址都不好。代码如下:

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <errno.h>
#include <time.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(){
int child_1, child_2;

child_1=fork();
if(child_1==0){
char* argv[]={"Test", "Test_1", "NULL"};
if((execve("Test_1", argv, NULL))<0) perror("execve 1 fail");
exit(0);
}else if(child_1<0)perror("error fork");
else wait(NULL);

child_2=fork();
if(child_2==0){
char* argv1[]={"Test", "Test_2", "NULL"};
if((execve("Test_2", argv1, NULL))<0) perror("execve 2 fail");
exit(0);
}else if(child_2<0)perror("error fork");
else wait(NULL);
return 0;
}

最佳答案

您没有正确终止参数数组:

    char* argv[]={"Test", "Test_1", "NULL"};

"NULL" 是字符串字面量,与NULL 不同。该数组需要以空指针终止。而是这样做:

    char* argv[]={"Test", "Test_1", (char*)0};

同样,修复另一个参数数组。

关于c - Execve 给出了错误的地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41364112/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com