I'm having a little problem with the execve command. The program Test should create two children and each one should run an execve to load and run another program. But I'm getting a bad address on both the execve. The code is as follows:
#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;
}
                You are not terminating the argument array correctly:
    char* argv[]={"Test", "Test_1", "NULL"};
"NULL" is a string literal, it's not same as NULL. The array needs to be terminated with a null pointer. Instead do:
    char* argv[]={"Test", "Test_1", (char*)0};
Similarly, fix the other argument array.
or you have to change "NULL" to simply NULL. In the case "NULL" you give NULL as a commandline parameter. The case NULL without " means end of argument list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With