简体   繁体   中英

How can I exec() my program in c?

I am trying to run my program in c using the execl () function. Currently, the piece of code that should run the program looks like this, and info_proc is in a info_proc.c file with a main containing a printf.

`switch(fork()) {
   case -1:
      perror("fork");
      exit(EXIT_FAILURE);
   case 0:
      getcwd(dir, 256);
      strcat(dir, "/info_proc");
      printf("dir %s", dir);
      execl(dir, "info_proc", req->data, NULL);
      perror("execl");
      exit(EXIT_FAILURE);
    default:
      wait(NULL);
      break;
}

`

My program returns "execl: bad address" to the execution ... I do not really see where the error can come from

So, my problem was that req->data was a pid_t. Here is how I solved my problem (thanks to cdark, usr2564301 and Fred Larson !!)

char *dir = malloc(sizeof(char) * 256);
    switch(fork()) {
        case -1:
            perror("fork");
            exit(EXIT_FAILURE);
        case 0:
            getcwd(dir, 256);
            strcat(dir, "/info_proc");
            printf("dir %s", dir);
            char *pid = malloc(10 * sizeof(char));
            sprintf(pid, "%d", req->data);
            execl(dir, "info_proc", pid, NULL);
            perror("execl");
            exit(EXIT_FAILURE);
        default:
            wait(NULL);
            break;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM