For project 4, you’ll need to be able to start new processes. Error handling is also really important, and will prevent you from accidentally creating a forkbomb.

Getting started

Right click and download this link. This is the skeleton of your lab.

Please be sure to put your name at the top.

This program takes any number of command-line arguments and runs the program specified. I’ve already written the fork(), execvp() and waitpid() system calls for you, but you’ve got some work to do to make it output some useful error messages.

For example, if you compile it and run it like so:

$ ./lab7 ls -l
total 17
-rwxr-xr-x 1 abc123 UNKNOWN1 6875 Apr  6 02:04 lab7
-rw-r--r-- 1 abc123 UNKNOWN1 1593 Apr  6 02:02 lab7.c
$ _

Right now, all it does is run the program, and then print a line of --------.

This program can serve as the basis of your project 4, since the main responsibility of a shell is to start new processes and report errors from them!

What to do

Read the comments in the code I’ve given you to see what you have to do and where to do it.

This lab is also testing to see if you can follow those instructions by looking up documentation, so use the man pages.

man pages are… dense. They have a lot of information, and they go into a lot of detail. But the important parts to focus on are:

Here are some pages you will find useful:

// this is code that's already in the lab7.c file for you.
int status;
int childpid = waitpid(-1, &status, 0);

	// it exited normally; use WEXITSTATUS() to extract the exit code.

// et cetera...

Things to test

Try running some simple commands that you know will complete successfully. Your output might look like:

$ ./lab7 ls
lab7  lab7.c
Program exited successfully!

$ ./lab7 echo "hello"
Program exited successfully!

$ _

Then try running a valid command that will fail, like trying to ls a directory that doesn’t exist:

$ ./lab7 ls /bogus
ls: cannot access '/bogus': No such file or directory
Program exited with error code 2

$ _

Then try running some bogus commands that should cause the execvp to fail. In addition to printing the message about the invalid command, you should also see a report about the error code - that should be the error code that you pass to the exit() after execvp() failed.

$ ./lab7 aisfjajojojedge
Error running program: No such file or directory
Program exited with error code 1

$ _

Then, try killing a process with a signal. An easy one to use is SIGINT, which is sent when you use ctrl+C. That’s why you set up the SIGINT handler to be ignored. For example:

$ ./lab7 cat
Program was terminated by signal: Interrupt

$ _

This happens because cat without arguments simply copies stdin to stdout, so we got trapped in it. Pressing ctrl+C sends SIGINT to cat, which kills it, which your program can detect and print an error about.


Nope. This lab is optional.