Lab Assignment--Processes, Forks, & Exec


(Please do this lab in LINUX)

Part 1

  1. Write a program that executes the "cp -p -i filename1 filename2" command. Call your executable myCp
    Details:
    1. Look in the man pages to see what the -p -i options do when using cp
    2. Create a file with the name of filename1. The contents of the file can be this this.
    3. The call to your program will be made with the following command:
      % myCp filename1 filename2
    4. 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.
    5. Your code will fork()
    6. The child will use the execl to call cp and use the filename1 and filename2 passed as arguments on the command line
    7. The parent will wait for the child to finish
    8. 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
    9. Comment out the execl call and add instead a call to execv. Add any necessary variables to do that.

  2. Answer the following questions:
    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 (myCp) 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 -p -i mean for the cp

Sample run:

[1]%myCp 
error myCp: not enough arguments   

[2]%myCp filename1
error myCp: not enough arguments

[3]%myCp filename1 filename2
In the CHILD process Trying to Copy
Child Process ID: 2014, Parent ID: 2013, Process Group: 2013
In the PARENT process
Original Process ID: 2013, Parent Is: 1889, Process Group is: 2013

[4]%myCp filename1 filename2
In the CHILD process Trying to Copy
Child Process ID: 2014, Parent ID: 2013, Process Group: 2013

cp: overwrite `backup'? y

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, sizeof(buffer));
         write(1, &mynum, 1);
         write(1, "\n", 1);
      }
      return 0;
   }
   else
   {
      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, sizeof(buffer));
         write(1, &mynum, 1);
         write(1, "\n", 1);
      }
      return 0;
   }
} 

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 3 files to URCourses

  1. myCp.cpp (code)
  2. Answers to questions in a plain text file
  3. Script containing the following commands:
    1. ls -l (Goal: show that filename1 exists, but filename2 does not)
    2. cat filename1 (Goal: display the contents of filename1)
    3. myCp (Result: error message-not enough arguments)
    4. myCp filename1 (Result: error message-not enough arguments)
    5. myCp filename1 filename2 (Result: will produce a copy file and print information about the child and parent processes)
    6. ls -l (Goal: show that filename2 has been created)
    7. cat filename2 (Goal: display the contents of the copied file)
    8. myCp /net/share/aliases/CS330-001.201730 filename2 (Result: cp will prompt if you want to overwrite filename2- type 'y')
    9. ls -l (Goal: show the timestamps and permissions on filename2)

Notes