Difference between fork() and exec() in C (2024)

Table of Contents
Example Output

Here we will see the effect of fork() and exec() system call in C. The fork is used to create a new process by duplicating the calling process. The new process is the child process. See the following property.

  • The child process has its own unique process id.
  • The parent process id of the child process is same as the process id of the calling process.
  • The child process does not inherit the parent’s memory lock and semaphores.

The fork() returns the PID of the child process. If the value is non-zero, then it is parent process’s id, and if this is 0, then this is child process’s id.

The exec() system call is used to replace the current process image with the new process image. It loads the program into the current space, and runs it from the entry point.

So the main difference between fork() and exec() is that fork starts new process which is a copy of the main process. the exec() replaces the current process image with new one, Both parent and child processes are executed simultaneously.

Example

#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 return_val = 1; int state; process_id = fork(); if (process_id == -1) { //when process id is negative, there is an error, unable to fork printf("can't fork, error occured
"); exit(EXIT_FAILURE); } else if (process_id == 0) { //the child process is created printf("The child process is (%u)
",getpid()); char * argv_list[] = {"ls","-lart","/home",NULL}; execv("ls",argv_list); // the execv() only return if error occured. exit(0); } else { //for the parent process printf("The parent process is (%u)
",getppid()); if (waitpid(process_id, &state, 0) > 0) { //wait untill the process change its state if (WIFEXITED(state) && !WEXITSTATUS(state)) printf("program is executed successfully
"); else if (WIFEXITED(state) && WEXITSTATUS(state)) { if (WEXITSTATUS(state) == 127) { printf("Execution failed
"); } else printf("program terminated with non-zero status
"); } else printf("program didn't terminate normally
"); } else { printf("waitpid() function failed
"); } exit(0); } return 0;}

Output

The parent process is (8627)The child process is (8756)program is executed successfully

Updated on: 30-Jul-2019

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started

Difference between fork() and exec() in C (2)

Advertisem*nts

Difference between fork() and exec() in C (2024)
Top Articles
how to add cardano to trust wallet 2024
The Ultimate Guide to Keycaps: Material, Profile, and Beyond
Ffxiv Act Plugin
Knoxville Tennessee White Pages
Moon Stone Pokemon Heart Gold
Wizard Build Season 28
Readyset Ochsner.org
Apex Rank Leaderboard
Elden Ring Dex/Int Build
Skip The Games Norfolk Virginia
My.doculivery.com/Crowncork
Elizabethtown Mesothelioma Legal Question
Missing 2023 Showtimes Near Landmark Cinemas Peoria
Gino Jennings Live Stream Today
Munich residents spend the most online for food
Tamilrockers Movies 2023 Download
Katherine Croan Ewald
Diamond Piers Menards
The Ultimate Style Guide To Casual Dress Code For Women
Site : Storagealamogordo.com Easy Call
Is Windbound Multiplayer
Filthy Rich Boys (Rich Boys Of Burberry Prep #1) - C.M. Stunich [PDF] | Online Book Share
Integer Division Matlab
Sandals Travel Agent Login
Horn Rank
Cognitive Science Cornell
Random Bibleizer
Craigslist Fort Smith Ar Personals
Jazz Total Detox Reviews 2022
The Clapping Song Lyrics by Belle Stars
Poe T4 Aisling
R/Sandiego
Kempsville Recreation Center Pool Schedule
Pfcu Chestnut Street
Beaver Saddle Ark
Log in or sign up to view
A Man Called Otto Showtimes Near Amc Muncie 12
Powerspec G512
The Minneapolis Journal from Minneapolis, Minnesota
Saybyebugs At Walmart
Gvod 6014
2007 Jaguar XK Low Miles for sale - Palm Desert, CA - craigslist
Tlc Africa Deaths 2021
Youravon Com Mi Cuenta
Nope 123Movies Full
Kushfly Promo Code
Diario Las Americas Rentas Hialeah
Game Akin To Bingo Nyt
Kidcheck Login
Marion City Wide Garage Sale 2023
Latest Posts
Article information

Author: Fr. Dewey Fisher

Last Updated:

Views: 6253

Rating: 4.1 / 5 (62 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Fr. Dewey Fisher

Birthday: 1993-03-26

Address: 917 Hyun Views, Rogahnmouth, KY 91013-8827

Phone: +5938540192553

Job: Administration Developer

Hobby: Embroidery, Horseback riding, Juggling, Urban exploration, Skiing, Cycling, Handball

Introduction: My name is Fr. Dewey Fisher, I am a powerful, open, faithful, combative, spotless, faithful, fair person who loves writing and wants to share my knowledge and understanding with you.