Lab Assignment--Processes, Forks, & Exec
Lab Code
To get the sample and exercise code, please use the following commands in the directory that you have created for this lab:
wget www.labs.cs.uregina.ca/330/Fork/Lab4.zip
unzip Lab4.zip
(Please do this lab on os1 or os1)
Part 1- Build a Shell that can execute a Unix Command
Step 1- Modify the convertToC
You will be using execv that requires a char** as an argument.
The catch is that the char ** has to be NULL terminated
Please do the following
- allocate an extra "word" pointer for NULL (Hint: tokenCount+1)
- store the value of NULL into the extra "word"
Step 2- Modify the processCommand
You will be emulating a shell. First, the shell checks for specific commands ("shutdown", "lo", etc).
If it does not recognize those commands then, a fork occurs and the child will
execute the command as typed.
The algorithm to implement is as follows (Hint: add code inside the else):
- fork to create a child and parent
- the child will do the following:
- Call the converToC function and capture the return into a char** variable
- Call execvp sending it two arguments: the first element (or word) of the char** variable, and the entire array
- Print an error message using perror
- exit with value 1
- the parent will do the following:
- wait for the child
- Print the child's return value using WIFEXITED and WEXITSTATUS
- return 1
- don't forget to handle the error case of the fork
Step 3-Answer Questions
Answer the following questions as comments at the top of HALmod.cpp :
- You were asked to add a print message after the exec* call, does it print it in all cases?
Why? Why not?
- Who is the parent of your executable (./demo) program?
- How would you change the code so that the child and parent "appear" to run concurrently (ie. at the same time)?
Sample run:
os1[26]% ./demo
HALshell> pwd
/home/hercules/t/temp4/cs330/Lab4x
Child returned: 0
HALshell> ls
demo HALmod.cpp HALmod.h HALmod.o main.cpp main.o Makefile part2.cpp Samples
Child returned: 0
HALshell> cp main.cpp main.bak
Child returned: 0
HALshell> ls
demo HALmod.cpp HALmod.h HALmod.o main.bak main.cpp main.o Makefile part2.cpp Samples
Child returned: 0
HALshell> rm main.bak
Child returned: 0
HALshell> ls
demo HALmod.cpp HALmod.h HALmod.o main.cpp main.o Makefile part2.cpp Samples
Child returned: 0
HALshell> exit
Error running command: No such file or directory
Child returned: 1
HALshell> lo
HALshell: terminating ...
os1[27]%
Part 2
Experiment with Forks
Given the following code:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
char mynum='0';
int main(void)
{
int i;
pid_t fork_return;
static char buffer[10];
fork_return = fork();
if (fork_return == 0)
{
strcpy(buffer, "CHILD"); /*in the child process*/
for (i=0; i<5; ++i) /*both processes do this*/
{
mynum=i + '0';
sleep(1); /*5 times each*/
printf("%s%c\n",buffer, mynum);
fflush(stdout);
}
return 0;
}
else if (fork_return > 0)
{
strcpy(buffer, "PARENT"); /*in the parent process*/
for (i=0; i<5; ++i) /*both processes do this*/
{
sleep(1); /*5 times each*/
printf("%s%c\n",buffer, mynum);
fflush(stdout);
}
return 0;
}
else
{
printf("error\n");
fflush(stdout);
}
}
Run the above code and answer the following question:
- Notice that mynum is a global variable.
- Child is incrementing the ascii value of mynum
- Parent is not
- Child and Parent can take turns running
Why does child print CHILD0, CHILD1, CHILD2, etc whereas parent prints
PARENT0, PARENT0, PARENT0, etc? Remember mynum is a global
variable.
Deliverables:
Submit 2 files to URCourses
- HALmod.cpp (code)
Answers to questions as comments in your code
- Part1 has three questions: 2. a), b), and c)
- Part2 has one question
- Script of the run (saved as a .txt file). Please note that you will start your HALmod program (./demo) and type the following commands:
- pwd
- ls
- cp main.cpp main.bak
- ls
- exit (Please note, this will produce an error)
- lo
Notes
- Do not create a zip file
- Submission is on UR Courses
- Submit your own work
- Please properly indent your work. I will deduct marks for ziggy-zaggy code.
- Be mindful that your assignment is due two hours before the start of your next lab