Part 1 1. Write a program that executes the "cp -p -i filen...

Question
Part 1

1. Write a program that executes the "cp -p -i filename1 filename2" command. Call your executable myCp
Details:
a. Look in the man pages to see what the -p -i options do when using cp
b. Create a file with the name of filename1. Copy a random paragraph from the notes into this file
c. The call to your program will be made with the following command: % myCp filename1 filename2
d. Your code will fork()
e.The child will use the execl to call cp and use the filename1 and filename2 passed as arguments on the command line
f. The parent will wait for the child to finish
g. 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
h. Comment out the execl call and add instead a call to execv. Add any necessary variables to do that.

2. Answer the following questions:
a. If you try to print a message after the exec* call, does it print it? Why? Why not?
b. Who is the parent of your executable (myfork) program?
c. How would you change the code so that the child and parent "appear" to run concurrently (ie. at the same time)?
d. 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.
o Child is incrementing the ascii value of mynum
o Parent is not
o 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.
Solution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references.
Students may use these solutions for personal skill-building and practice.
Unethical use is strictly forbidden.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <time.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(int argc, char* argv[]) {

    if (argc < 3) {
       char message[] = "Usage: myCp filename1 filename2\n";
       write(2, message, strlen(message));
       exit(EXIT_SUCCESS);
    }
    else {
       pid_t fork_return;
       int status, ret;

       fork_return = fork();

       if (fork_return == 0){
            printf("In the CHILD process Trying to Copy\n");
            printf("Child Process ID: %d, Parent ID: %d, Process Group: %d\n", getpid(),getppid(),getpgid(getpid()));

//          execl("/bin/cp","cp","-p","-i",argv[1],argv[2],NULL);
            char *cmd[] = {"/bin/cp","-p","-i",argv[1],argv[2],NULL};
            execv ("/bin/cp", cmd);
       }
       else if(fork_return > 0){
This is only a preview of the solution.
Please use the purchase button to see the entire solution.
By purchasing this solution you'll be able to access the following files:
Solution.zip
Purchase Solution
$9.00
Google Pay
Amazon
Paypal
Mastercard
Visacard
Discover
Amex
View Available Computer Science Tutors 641 tutors matched
Ionut
(ionut)
Master of Computer Science
Hi! MSc Applied Informatics & Computer Science Engineer. Practical experience in many CS & IT branches.Research work & homework
5/5 (6,804+ sessions)
1 hour avg response
$15-$50 hourly rate
Pranay
(math1983)
Doctor of Philosophy (PhD)
Ph.D. in mathematics and working as an Assistant Professor in University. I can provide help in mathematics, statistics and allied areas.
4.6/5 (6,691+ sessions)
1 hour avg response
$40-$50 hourly rate
Leo
(Leo)
Doctor of Philosophy (PhD)
Hi! I have been a professor in New York and taught in a math department and in an applied math department.
4.9/5 (6,439+ sessions)
2 hours avg response

Similar Homework Solutions