wget www.labs.cs.uregina.ca/330/Fork/Lab4.zip
unzip Lab4.zip
(Please do this lab on os1 or os1)
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
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):
Answer the following questions as comments at the top of HALmod.cpp :
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]%
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:
Submit 2 files to URCourses