Putting it together – our simpsh project

So, now that we have all the bits and pieces in place—namely, the fork-exec semantic and the wait API—we can see how our simple shell should be designed.

In the C program, get into a loop, display the required prompt, accept user input (let's use the fgets(3) to do this—why? Please read the upcoming tip) into a cmd variable, and then fork. In the child code (use fork rule #2 to distinguish between the parent and child), use one of the many exec family APIs (a simple execlp(3) sounds promising here) to execute the user supplied command. In parallel (recall fork rule #3), have the parent process invoke the wait API; the parent now sleeps until the child dies. Now loop around again and repeat the whole ...

Get Hands-On System Programming with Linux now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.