Hi people. I'm trying to run whoami using execv but I'm getting a problem:

child PID: 3146
parent PID: 3145
execv: Permission denied
child exit code: 1

I already have certified myself that I have run permission for whoami for all users. I don't know what I can do to solve this problem.

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

int main()
    pid_t cpid; 
    int status, err;
    char* cmd[] = {"whoami", NULL};

    cpid = fork();

    if(cpid == 0) 
       printf("--- CHILD PROCESS ---\n");
       printf("child PID: %ld\n", (long)getpid());
       printf("parent PID: %ld\n", (long)getppid());

       if((err = execv("/usr/bin/", cmd)) == -1)

    else if(cpid < 0)

    else { /* parent process */
        printf("child exit code: %d\n", WEXITSTATUS(status));
    return EXIT_SUCCESS;

Your problem is that your first argument should be then entire command path, and the second an array of argument to pass to the command. IE,

const char* arglist[5] = {"arg1", "arg2", "arg3", "arg4", 0};
if ((err = execv("/usr/bin/cmd", arglist)) == -1)
This question has already been answered. Start a new discussion instead.