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

  1. allocate an extra "word" pointer for NULL (Hint: tokenCount+1)
  2. 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):

  1. fork to create a child and parent
     
  2. the child will do the following:
  3. the parent will do the following:
  4. 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 :

  1. You were asked to add a print message after the exec* call, does it print it in all cases? Why? Why not?
  2. Who is the parent of your executable (./demo) program?
  3. 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:

  1. Notice that mynum is a global variable. 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

  1. HALmod.cpp (code)
    Answers to questions as comments in your code

  2. Script of the run (saved as a .txt file). Please note that you will start your HALmod program (./demo) and type the following commands:
    1. pwd
    2. ls
    3. cp main.cpp main.bak
    4. ls
    5. exit (Please note, this will produce an error)
    6. lo

Notes