Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

27. Program Execution > Implementing system()

Implementing system()

In this section, we explain how to implement system(). We begin with a simple implementation, explain what pieces are missing from that implementation, and then present a complete implementation.

A simple implementation of system()

The -c option of the sh command provides an easy way to execute a string containing arbitrary shell commands:

$ sh -c "ls | wc"
     38      38     444

Thus, to implement system(), we need to use fork() to create a child that then does an execl() with arguments corresponding to the above sh command:

execl("/bin/sh", "sh", "-c", command, (char *) NULL);

  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint