Starting a new process with system()
What we just covered regarding using fork()
, waitpid()
, and execl()
to start a new program in a forked process is the key to understanding Linux and processes at a deeper level. This understanding is key to becoming an excellent system developer. However, there is a shortcut. Instead of manually dealing with forking, waiting, and executing, we can use system()
. The system()
function does all these steps for us.
Getting ready
For this recipe, you only need what's listed in the Technical requirements section of this chapter.
How to do it…
In this recipe, we'll rewrite the previous program—my-fork
—using the system()
function instead. You'll notice how much shorter this program is compared to the previous one. Let's get started:
- Write the following code in a file and save it as
sysdemo.c
. Notice how much smaller (and easier) this program is. Thesystem()
function does all the complex stuff...