Exec( ) System Call in C Program with Examples

exec() system call:

  • The exec family of functions replaces the currently running process with a new process.
  • It can be used to run a C program by using another C program.
  • It comes under the header file unistd.h.

Program:

// parent.c: the parent program

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

int main (int argc, char **argv)
{
    int i = 0;
    long sum;
    int pid;
    int status, ret;

    printf ("Parent: Hello, World!\n");

    pid = fork ();

    if (pid == 0) {

        // I am the child

        execvp ("./child", NULL);
    }

    // I am the parent

    printf ("Parent: Waiting for Child to complete.\n");

    if ((ret = waitpid (pid, &status, 0)) == -1)
         printf ("parent:error\n");

    if (ret == pid)
        printf ("Parent: Child process waited for.\n");
}

Output:

gcc parent.c -o parent   //compiling parent.c
./parent                 // running parent.c

Leave a Comment