Fork and Execute
Problem / Exercise
For this lab, you will implement a program that continually prompts the user to enter a shell command with its options
and arguments (e.g., cat file1.txt, wc -cl file2.txt, etc.). The parent process should then fork a new child process to
execute the command. The expectation is that the output generated by the child process should be the same as when the
command is executed by the bash shell directly. The parent process should wait for the child process to terminate before
prompting the user the enter the next command. When the command is exit, then program should terminate.
Regarding the exec(3) family of functions:
“p” execs “non-p” execs
in/ls X X
ls X
As seen above, the exec functions which search the path (i.e. those which take a “filename”) also work with absolute path-
names. My suggestion is to use those (e.g., execvp(3)).
When reading in a command via the read(2) system call, the user inputs are stored in an a
ay of characters, also known
as the bu↵er. There are string-related utility functions that can tokenize a single string into an a
ay of strings delimited by
space. Such an a
ay of strings may be needed to invoke one of the exec(3) family of functions. It is important to note that
the bu↵er is not a valid C string until a null character (the \0 character) is explicitly placed after the last valid character in
your bu↵er. Please make sure to do this before calling any string utility function that expects a valid C string as its input
parameter.
Please also note that as you prepare that a
ay of strings for exec(3), the a
ay must be NULL-terminated. You may need
to explicitly assign the value NULL right after the last valid string in your a
ay.
1 Some Nonfunctional Requirements
Your submission needs to satisfy the following nonfunctional requirements:
• Fork Bomb WARNING: You must not “fork bomb” the server. Doing so will hurt your grade. A 25-point
deduction will be applied for these o↵enses. You must ensure that you are working on one of the vcf cluster nodes
when developing and testing your code.
• Directory Setup:
For this lab, you should
place your source code into a file called lab06.c. The executable should be named lab06.
• Li
aries: You are allowed to use any of the C standard li
aries, but with the following restrictions: When reading
or writing to a file (including standard input and output) are concerned, you need to use low-level calls to read(2),
write(2), and related functions. Functions like scanf(3), gets(3), puts(3), fopen(3), fgets(3), fscanf(3), and
other related f-functions are not low-level I/O functions and therefore cannot be used. The printf(3) function is
acceptable when you need to display simple output messages to the screen. Whenever possible, program output should
e unbu↵ered. You are NOT allowed to use popen(3) and system(3).
• Documentation: You code must be documented using Javadoc style comments. Use inline documentation, as needed,
to explain ambiguous or tricky parts of your code.
1
Nikita Kondla
• makefile: You need to include a makefile. The expectation is that the grader should be able to type make clean
and make to clean and compile/link your submission, respectively.
• Standards & Flags: Make sure that when you compile, you pass the following options to gcc:
-Wall -pedantic-e
ors
Other compile
linker options may be needed in addition to the ones mentioned above.
•
• Compiler Warnings: Since you should be compiling with both the -Wall and -pedantic-e
ors options, your code
is expected to compile without gcc issuing any warnings.
2 Sample Input
This section provides a sample run of the program. The user input typed on the keyboard is highlighted in green. The output
of the commands are not explicitly given but they should be the same as when the commands are executed by the bash shell
directly.
$ ./lab06
Please enter command: ls -l
...(output of ls -l displayed here) ...
Please enter command: wc file1.txt
...(output of wc file1.txt displayed here) ...
Please enter command: exit
Note: The cd command may not work with this program and this is normal. You do not have to support the cd command
for this lab. For those interested to get it working, please look up the chdir(2) system call. When the command is cd, the
parent process should call chdir(2) instead of fork and execute.
Note: the “submit” command may not work on the vcf cluster nodes. You may need to exit back to the
main server before you can execute the “submit” command.
2
Some Nonfunctional Requirements
Sample Input
Submission