QuestionQuestion

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 PreviewSolution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse 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){...
$21.00 for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Linux Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats