Difference between fork() and exec() in C

In C, fork() and exec() are fundamental system calls for process management. The fork() function creates a new process by duplicating the calling process, while exec() replaces the current process image with a new program.

Syntax

#include <unistd.h>
pid_t fork(void);
int exec(const char *path, char *const argv[]);

Key Differences

Aspect fork() exec()
Purpose Creates new process (duplicate) Replaces current process image
Process Count Increases by one Remains same
Memory Image Copies parent's memory Loads new program
Return Value Child PID to parent, 0 to child Only returns on failure

Properties of fork()

  • The child process has its own unique process ID
  • The parent process ID of the child process is the same as the calling process ID
  • The child process does not inherit the parent's memory locks and semaphores
  • Returns child PID to parent (non−zero), 0 to child process

Example: fork() and exec() Demonstration

Note: This example demonstrates fork() and exec() concepts but may not execute properly in online compilers due to system call restrictions. It's designed for Unix/Linux systems.
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/wait.h>

int main() {
    pid_t process_id;
    int state;
    
    process_id = fork();
    
    if (process_id == -1) {
        printf("Can't fork, error occurred<br>");
        exit(EXIT_FAILURE);
    } else if (process_id == 0) {
        /* Child process */
        printf("The child process is (%u)<br>", getpid());
        char *argv_list[] = {"ls", "-lart", "/home", NULL};
        execv("/bin/ls", argv_list);
        /* execv only returns if error occurred */
        exit(0);
    } else {
        /* Parent process */
        printf("The parent process is (%u)<br>", getpid());
        
        if (waitpid(process_id, &state, 0) > 0) {
            if (WIFEXITED(state) && !WEXITSTATUS(state))
                printf("Program executed successfully<br>");
            else if (WIFEXITED(state) && WEXITSTATUS(state)) {
                if (WEXITSTATUS(state) == 127) {
                    printf("Execution failed<br>");
                } else
                    printf("Program terminated with non-zero status<br>");
            } else
                printf("Program didn't terminate normally<br>");
        } else {
            printf("waitpid() function failed<br>");
        }
        exit(0);
    }
    return 0;
}
The parent process is (8627)
The child process is (8756)
Program executed successfully

Simple fork() Example

Here's a simpler example showing basic fork() behavior −

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

int main() {
    pid_t pid = fork();
    
    if (pid == 0) {
        printf("This is child process<br>");
        printf("Child PID: %d<br>", getpid());
    } else if (pid > 0) {
        printf("This is parent process<br>");
        printf("Parent PID: %d, Child PID: %d<br>", getpid(), pid);
    } else {
        printf("Fork failed<br>");
    }
    
    return 0;
}
This is parent process
Parent PID: 1234, Child PID: 1235
This is child process
Child PID: 1235

Key Points

  • fork() creates a copy of the current process, while exec() replaces it entirely
  • After fork(), both parent and child processes execute simultaneously
  • exec() family functions only return to the calling program if an error occurs
  • Use wait() or waitpid() in parent to synchronize with child processes

Conclusion

The main difference is that fork() creates a new process as a copy of the current one, while exec() replaces the current process with a new program. Together, they form the foundation of process creation and program execution in Unix−like systems.

Updated on: 2026-03-15T10:39:09+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements