Lab Assignment--Processes, Forks, & Exec

(Please do this lab in LINUX)

Part 1

  1. Write a program that executes the "cat -n filename1 - filename2" command. Call your executable myCat
    1. Look in the man pages to see what the -n and - options do when using cat
    2. The call to your program will be made with the following command (using sample files from the lab):
      % myCat hello.cpp orig.cpp
      Where hello.cpp is filename1 and orig.cpp is filename2.
    3. Your code will check for the correct number of arguments. If it is not correct, then an error message will be produced and the code will exit.
    4. Your code will fork()
    5. The child will use the execl to call cat and use the filename1 and filename2 passed as arguments on the command line
    6. The parent will wait for the child to finish
    7. Your program will also print from the child process:
      • The process id
      • The parent id
      • The process group id
      and print from the parent process:
      • the process id
      • the parent id
      • the process group id
    8. Comment out the execl call and add instead a call to execv. Add any necessary variables to do that.

  2. Answer the following questions as comments in the code you wrote above:
    1. If you try to print a message after the exec* call, does it print it? Why? Why not?
    2. Who is the parent of your executable (myCat) program?
    3. How would you change the code so that the child and parent "appear" to run concurrently (ie. at the same time)?
    4. What does -n and - mean for the cat

Sample run:

error myCat: not enough arguments   

[2]%myCat hello.cpp 
error myCat: not enough arguments

[3]%myCat hello.cpp orig.cpp 
In the CHILD process Trying to Meow
Child Process ID: 2014, Parent ID: 2013, Process Group: 2013
     1  #include 
     2  #include 
     3  #include 
     5  int main(void)
     6  {
     7     printf("Hello \n");
     8     fork();
     9     printf("bye\n");
    10     return 0;
    11  }
Sticking two files together <---Type this then hit enter and CTRL-D
    12  Sticking two files together
    13  #include 
    14  #include 
    15  #include 
    17  int main (void)
    18  {
    19          pid_t fork_return;
    21          fork_return = fork();
    22          if (fork_return == 0)
    23                  printf("In the CHILD process\n");
    24          else if (fork_return > 0)
    25                  printf("In the PARENT process\n");
    26          else
    27                  printf("Error Forking\n");
    28          return 0;
    29  }
In the PARENT process
Original Process ID: 2013, Parent Is: 1889, Process Group is: 2013 

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*/
         write(1, buffer, strlen(buffer));
         write(1, &mynum, 1);
         write(1, "\n", 1);
      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*/
         write(1, buffer, strlen(buffer));
         write(1, &mynum, 1);
         write(1, "\n", 1);
      return 0;
      write (1, "Error\n", strlen("Error\n"));

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.


Submit 2 files to URCourses

  1. myCat.cpp (code)
    Answers to questions as comments in your code
  2. Script containing the following commands:
    1. ls -l (Goal: show that hello.cpp and orig.cpp exist in your executable directory)
    2. myCat (Result: error message-not enough arguments)
    3. myCat hello.cpp (Result: error message-not enough arguments)
    4. myCat hello.cpp orig.cpp (Result: will display both files as well as accept keyboard input and print information about the child and parent processes)