C Programming

Coding with fork and exec by example in c

The title of our article contains two different functions of the C language that occur while running any program in our system. Unfortunately, there is zero possibility of occurring more than one program at a time in C. Only a single task can happen at a particular time, which means it doesn’t allow concurrent programs to run. It causes you to wait for the completion of one process to execute the other one. To avoid this troubleshooting problem, you may need to develop your program in a good way, being a good developer. Linux fork () is used to overcome the waiting and to allow concurrency in your system. It helps in duplicating a new process and creates a new one by calling the duplicated process. The new process is the child process, and the previous one is called the parent process.

Whereas, exec () function is used to change the current existing program with the new one. This replacement is done by making amendments to the content of a program or a file. So the dissimilarity between fork and exec is that fork creates a new process from the existing process, and exec is used to replace the existing program by creating a new one.


To execute any c program on your Linux system, we need to install some prerequisites on it. Go to the terminal by using the shortcut method Ctrl+Alt+T. Now write the following commands to install the man pages.

$ sudo apt install manpages-dev

It will install all the corresponding pages.

Moving forward, to run a program on Linux, you need to install a code compiler. That is used to compile the code and execute it. For this purpose, we will install GCC repositories in our system.

$ sudo apt install GCC

Coding with exec in c

As we have installed the man pages in Linux, we will use the following command to see the description regarding exec. The primary sample syntax is as follow:


Exec (pathname/file, argv );

Here we have used the “unistd.h” header as it contains all information of families of exec function.

$ man exec

Now in the above-cited image, you can observe the types of exec. These are the family of exec functions. Each one is for a different function following the same base, “exec.”

Example: Now, moving further, we will describe the functionality of exec with the help of an example. We will take one function of exec to demonstrate its working, which is “execv.” Firstly, we will create two files with the extension of “.c.” After their creation, we will write respective codes in them and execute them to see the result.

Consider a file name “sample4.c”. Please open it and use the following code. In this code, we have used execv in a particular manner cited below.

Execv (“./sample4copy” , args);

The first part contains the new directory path, and the second part shows the arguments array as a parameter we have passed.


First of all, we have printed the current process’s id. Secondly, we have created a character array having NULL in the end for the termination. Thirdly we have called the sample4copy function.


When we call the exec function(), the process image is changed. Below cited image below shows the code of sample4copy.c.

Here we have used only print statements to get the id of the current process.

The output of the respective codes can be obtained by using the following commands.

$ GCC–o sample4 sample4.c
$ GCC –o sample4copy sample4copy.c
$ ./sample4

As we have described earlier, the “GCC” word is used to compile the code, and after compiling, the code is executed successfully.

According to the image, The PID of sample4.c file is shown first as it was declared before the exec call. Then after the exec() function is called, both the print statements of file sample4copy.c is executed where getpid() is used to obtain the process’s id.

Coding with a fork in c

The fork() function creates the child process from the parent process. It also contains two headers, including the fork information in it.


Pid_t fork(void);

We can use the man page for help in the usage

$ man fork

Example: Now consider an example by creating a file “sample3.c”. We will enter the code inside the file. According to the code, we have set the fork status as forkrank.


We have used the “if-else” statement to apply the condition. Simple print commands are declared here to help in the understanding of the fork() concept. Forkrank is first declared as 0 and then -1. With a fork(), there are now two processes that are working concurrently. Output can be obtained by using the same code, as used above in exec example.

$ GCC –o sample3.c

The output shows that the child process is executed earlier than the parent when the parent process was waiting. The wait function implies that it causes the parent function to wait unless one of all the child processes is terminated.

Fork and Exec system calls Collectively

Here we will take two files named “sample1.c” and “sample2.c”. First, open the file sampl1.c and write the code that is appended below in the image. We have used the fork() system-call here; when the child process is created, p will be assigned with 0. While using exec system-call, the sample1.c will be replaced with sample2.c.



Similar to the examples discussed above, the sample2 file will contain the printf statements in it. In sample1.c, the first print command is executed first, then the fork function is called, when p== 0, then child portion is executed and sample2.c file will be run. The output will contain GCC to compile both files. Here parent sample1.c id and sample2.c id is different because they are parent and child.


In this article, we have used both fork and exec separately and collectively to understand the usage and concept easily. I hope this tutorial contains sufficient content that will lead to access in escalating your knowledge.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.