Search

Using fork and exec

We have seen the introducition to using fork as well as using the exec family of calls in the following posts.
Now we can combine these two to create a child using fork, which executes a differnt program than the parent using exec.
The following program is an example for doing the same.
Let us create a simple program that we will execute as the child process.

hello.c



Compile it and create an executable named "hello".



Now we will use execl to execute the "hello" executable from the child.
fork_exec.c



Save the program and compile it



Execute it



Output:



We can see from the output that the child has executed the "hello" program successfully and the parent has contiued with its normal operations.
Thus we can create a child and make it to launch any new process using the combination of fork and exec.

No comments:

Post a Comment