Skip to main content

Command Palette

Search for a command to run...

Exec() in Operating System

Published
3 min read
Exec() in Operating System
  • exec() -> executes a new program.

  • The memory space of the child process—its code, stack, and heap—is completely overwritten by the new program.

Example 1:

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

int main(int argc, char *argv[]) {
    printf("hello (pid:%d)\n", (int) getpid());
    int rc = fork();
    if (rc < 0) { // fork failed; exit
        fprintf(stderr, "fork failed\n");
        exit(1);
    } else if (rc == 0) { // child (new process)
        printf("child (pid:%d)\n", (int) getpid());
        char *myargs[3];
        myargs[0] = strdup("wc"); // program: "wc" ;this wc is for the program to name itself
        myargs[1] = strdup("exec.c"); // arg: input file    
        myargs[2] = NULL; // mark end of array
        execvp(myargs[0], myargs); // runs word count  
        printf("this shouldn’t print out\n");
    } 
    else { // parent goes down this path
            int rc_wait = wait(NULL);
            printf("parent of %d (rc_wait:%d) (pid:%d)\n",
            rc, rc_wait, (int) getpid());
        }
        return 0;
     }

  • The child process executes a new process i.e is “wc”.

  • “wc” : it a pre written binary executable file in bin which counts the number of lines, words and size of file.

  • On successful execution of “wc” program child process do not resumes with parent program.

  • If it wouldn’t have executed successfully then child process would have continued with parent process.

char *myargs[3];
//argument list which get passed to function in wc 
myargs[0] = strdup("wc"); 
//stores the name “wc” program will give it to itself
myargs[1] = strdup("exec.c"); 
//name of the file whose lines words and size need to be found
myargs[2] = NULL; 
//end of arguments
execvp(myargs[0], myargs); 
//parameter 1 -> “wc” helps OS to find the executable file in bin

Example 2:

int main(int argc, char *argv[]) {
    printf("hello (pid:%d)\n", (int) getpid());
    int rc = fork();
    if (rc < 0) { // fork failed; exit
        fprintf(stderr, "fork failed\n");
        exit(1);
    } else if (rc == 0) { // child (new process)
        printf("child (pid:%d)\n", (int) getpid());
        char *myargs[3];
        myargs[0] = strdup("w123c");
         myargs[1] = strdup("exec.c");    
        myargs[2] = NULL; 
        execvp(myargs[0], myargs);   
        printf("this shouldn’t print out\n");
    } else { // parent goes down this path
        int rc_wait = wait(NULL);
        printf("parent of %d (rc_wait:%d) (pid:%d)\n",
        rc, rc_wait, (int) getpid());
    }
    return 0;
 }

  • Executed a program which do not exist. Example: “w123c”.

  • Child process continues with parent program instructions.

  • execvp() returns -1 in case of failed execution.

Exec family functions

l(list)

Pass arguments as list of strings.

v(vector)

Pass arguments as vector of strings

Without e

New process inherits parent’s environment

With e

You explicitly pass a new environment (array of strings like "PATH=/user/bin")

Without p

You must specify full path 

With p

System will search PATH environment variable to find the program

Examples:

  • execl(“bin/ls”, “ls”, “-l”, “-a”, NULL)

  • char *args[] = {"ls", "-l", "-a", NULL};

    execl(“bin/ls”, args);

  • char *args[] = {"ls", NULL};

    char *env[] = {"PATH=/bin", "USER=yashit", NULL};

    execve("/bin/ls", args, env);

  • execvp("ls", args);

    works even without /bin/ls path because it searches /bin, /usr/bin, etc.