Question: LABSET - UP Create a lab 9 directory under your / csc 6 0 directory. Create a makefile for building lab 9 executables, include

LABSET-UP
Create a "lab9" directory under your /csc60 directory.
Create a makefile for building lab9 executables, include a "clean" target for removing built files.
Take it one step/part at a time but make continual progress towards the complete solution.
PROBLEM Statement:
Part 1: exec()- Write a C program called "lab9" to meet the following requirements:
We will explore the use of the exec() library function.
- You will create the following file structure in Part 1:
- lab9.c \(\rightarrow \) contains the main()
- makefile \(\rightarrow \) for building the program
- Step 1: Create the makefile
- Your makefile will build the lab9 and myProg executables (2 different targets):
- lab9 is built from lab9.c
- myProg is build from myProg.c
- Include a clean target that "cleans-up" after a build
- Include a default target "all" which has dependencies on the targets lab9 and myProg. Note: target "all" will not need any makefile commands; the dependencies will take care of building everything ...
- Step 2: Create the lab9.c
- lab9.c will contain a main() which takes command line arguments.
- The first command line argument will be the name of a program to execvp() eg. "myProg" or "pwd" or ...
- The remaining arguments are to be passed to that program as arguments in the execvp() call.
- At the start of main(), use printf() to display the following:
In lab9[PID=; PPID=]
Exec-ing "" with argument ""
Where: Where is the process id of myProg, is its parent process id, and is the first parameter on the lab9 command line and is the second argument to lab9
Include the quote marks around and
Include a Call "fflush(stdout);" after the printf() call but before the execvp() call to ensure it displays right away to the screen
- Use the execvp() function to execute the program name () which was given as the first argument on the lab9 command line along with the argument to that program ().
E.g. to exec "Is -I" then you'll run "lab9 is -l" from the shell
Include a perror() with the message "Call to execvp() failed, only gets here on
error" in lab9.c immediately after the call to execvp()
Step 3: Create the myProg.c
myProg.c will contain a main() which receives one command line argument.
Include an error check. If no arg is provided, print the following, then exit:
Usage: myProg
myProg will display the following line after processing the command line
parameters:
In myProg [PID=; PPID=], received argument ""
Where is the process id of myProg, is its parent process id, and
is the first command line argument to myProg
Include the quote marks around
Have myProg() call exit with the value of EXIT_FAILURE if incorrect number of
arguments provided. Call exit with EXIT_SUCCESS otherwise.
Run the program:
Ensure that the current directory "." is in your PATH environment variable .
PATH=".:$PATH"
While in the lab9 directory, run make clean, then make to build the programs
From the shell, run: lab9 myProg
Did that work? If not, why did it not work? What did perror() output?
Why does the parent (lab9 and child (myProg) processes have the same
PID?
From the shell, run: lab9 is -I then run: lab9 abcdef - don't save output
What do you observe?
Now, run it again and save the output: lab9 myProg HiThere > run1.out 2> &1
Then, examine the contents of run1.out
Part 2: fork()/exec()- Modify the lab9.c program to use fork/exec
We will explore using fork/exec combo to start a new process running a different program.
Copy your lab9.c file to a new file named lab9_f.c (add lab9_f build to the makefile)
Modify lab9_f.c to perform a fork() to first create a separate child process, then execvp()
to run a new program in that child process.
Include a line to execute fork()- After the printf("In lab9[PID=; ...
Add conditional code for the program to determine if there was an error with the
fork() call, or it is the parent or child process:
If in Child Process:
Display the following 2 lines of output(indent the 2^("nd ") line with a tab): In Child [PID=; PPID=]
Exec-ing "" with argument ""
- Have the child process execvp() the executable given on the command line as we did in Part 1.
- If in Parent Process:
- logic following the fork, have it display the message:
In Parent []: waiting for child to finish...
Where is the pid of the running parent process
- After printing that, have the parent wait() for the child to finish ("terminate").
- Include the example code from the \(\mathbf{W k}\)-10 lecture which implements the child return status macros "W Macros" to check the exit status from a child process.
i.e. cut-and-paste the (parent) code in the "else" code block in the example code into the end of lab9_f.c
- From the shell, run: lab9_f myProg HiThereYou
- Did that work? If not, why did it not work? What did perror() output?
- What do you observe about the process IDs being presented
- From the shell, run:" lab9_f Is -1" then run: "lab9_f abc"-don't save output
- What do you observe? Is
LABSET - UP Create a "lab 9 " directory under

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!