Question

In: Computer Science

Compile and run the following code then answer the following questions: a.) Run this command from...

Compile and run the following code then answer the following questions:

a.) Run this command from the shell prompt:

./a.out ls -F

Explain, in your own words, why you see the screen output you do and how that output relates to both the content of the program AND the nature of the shell command used to invoke it. Be sure to comment on the pointer arithmetic done inside the line: execvp(*(argv+1), argv+1);

b.) Run this command from the shell prompt:

./a.out

Explain, in your own words, why you see the screen output you do and how that output relates to both the content of the program AND the nature of the shell command used to invoke it. Be sure to comment on the pointer arithmetic done inside the line: execvp(*(argv+1), argv+1);

c.) run this command from the shell prompt:

./a.out randomname

Explain, in your own words, why you see the screen output you do and how that output relates to both the content of the program AND the nature of the shell command used to invoke it. Of course, I’m assuming there is no executable file anywhere in your path called “randomname”. If you happen to have an executable named “randomname” –then substitute a name that does NOT exist in your command path J

#include <stdlib.h>
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main(int argc, char **argv)

{ pid_t fork_returned_pid;

fork_returned_pid = fork();

if (fork_returned_pid < 0)
{ printf("The fork() didn't work! Terminate\n");
exit (0); }

if (fork_returned_pid != 0)
{ printf("The parent will now wait\n");
wait(NULL);
printf("The parent is done\n");
}

else
{ printf("The child is about to rewrite itself to run new code\n");
execvp(*(argv+1), argv+1);
printf("this prints only if the previous call fails\n");
}
}

Solutions

Expert Solution

a.) Run this command from the shell prompt:

./a.out ls -F

Here ls -F is the argument passed to the program. argv is the array of commands given to main programs. args[0] is the name of the program , args[1] is the ls , argv[2] is the -f option .

We get below output

 ./main ls -F
The parent will now wait
The child is about to rewrite itself to run new code
main* main.cpp
The parent is done

In the given program, fork_returned_pid = fork();

fork is the system call which will create child process of current program(this is called parent process). Variable 'fork_returned_pid' will be zero or more than zero, Both the parent and child process will have same code after fork() system call. But only difference is using if statement we can distinguish between child and process and make the child and parent to execute their respective code. If 'fork_returned_pid' is greater zero than it's parent process.

parent process code has following line

if (fork_returned_pid != 0)

{ printf("The parent will now wait\n");

wait(NULL);

printf("The parent is done\n");

}

Suppose parent process is the first one to get execution time , then it prints the message 'The parent will now wait' and wait as program is using 'wait' system call to wait for it's child process to finish execution first.So control will be given to child process for execution.

In child process we have below code

else

{ printf("The child is about to rewrite itself to run new code\n");

execvp(*(argv+1), argv+1);

printf("this prints only if the previous call fails\n");

}

In above code , we use execvp system call , which will replace current code with the code which is given as command for execvp to execute. Here in this case *(argv+1) gives the name of the given command and argv+1, gives the address from the first element of 2-d array

In out case we have passes argv[1] as ls argv[2] as -F ,, *argv[1] will be ls command to get executed .

Once child finishes execution, parent process starts executing and it prints 'The parent is done' and exits.

==================================

b.) Run this command from the shell prompt:

./a.out

Here we are not passing any argument to the program ,, hence child process which is getting executed won't get any arguments *(args+1) and (args+1) will be NULL values , execvp executes with NULL values ,, Hence no program gets executed .we get output as below

The parent will now wait
The child is about to rewrite itself to run new code
The parent is done

=========================

c.) run this command from the shell prompt:

./a.out randomname

In this program name of the program passed is 'randomname' . let's say it's not present or implemented . Then in child process , execvp tries to search for the program named 'randomname' ,, it won't find one ,so it fails executing the command passed to it.  hence code after execvp is gets printed as below

 ./main randomname
The parent will now wait
The child is about to rewrite itself to run new code
this prints only if the previous call fails
The parent is done

Hope it helps!!


Related Solutions

Please look at the following code. When I run it from the command line, I am...
Please look at the following code. When I run it from the command line, I am supposed to get the following results: 1: I am 1: I am I need to fix it so that the functions 'print_i' and 'print_j' print all of their lines. What do I need to add? Thank you. C source code: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <pthread.h> #include <unistd.h> // These two functions will run concurrently void* print_i(void *ptr) { printf("1: I am...
Run the following code and answer the following questions: (a) How do accuracy change with changing...
Run the following code and answer the following questions: (a) How do accuracy change with changing the tree maximum depth? [Your answer] (b) What are the ways to reduce overfitting in a decision tree? [Your answer] from sklearn import datasets import numpy as np from sklearn.model_selection import train_test_split from sklearn.tree import plot_tree iris = datasets.load_iris() X = iris.data[:, [2, 3]] y = iris.target X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.3, random_state=1, stratify=y) tree = DecisionTreeClassifier(criterion='entropy', max_depth=10, random_state=1) tree.fit(X_train,...
Objectives To learn to code, compile, and run a program using file input and an output...
Objectives To learn to code, compile, and run a program using file input and an output file. Assignment Plan and code a program utilizing one file for input and one file for output to solve the following problem: Write a program to determine the highest number, the lowest number, their total, and the average of each line of numbers in a file. A file contains 7 numbers per line. How many lines a file contains is unknown. Note Label all...
Question 3. In the following code, answer these questions: Analyze the code and how it works?...
Question 3. In the following code, answer these questions: Analyze the code and how it works? How can we know if this code has been overwritten? Justify how? #include <stdlib.h> #include <unistd.h> #include <stdio.h> int main(int argc, char **argv) { int changed = 0; char buff[8]; while (changed == 0){ gets(buff); if (changed !=0){ break;} else{     printf("Enter again: ");     continue; } }      printf("the 'changed' variable is modified\n %d", changed); } END of the questions :::::::::::::::::::::::::: :::::::::::::::::::::::::: Submission...
  Answer the questions related to the following code:             Class B             {      &nbs
  Answer the questions related to the following code:             Class B             {                         Public:                                     Void b1();                         Protected:                                     Void b2();             };             Class A : public B             {                         Public:                                     Void  a1();                         Protected: Void  a2();             };             Class C: public A               {                         Public:                                     Void c1();             }; Void main () { B temp1; A temp2; C temp3; }    Name all member functions of all classes visible through temp1 in the main function? Name all member functions of all classes visible through temp2 in the main function? Name all...
in C++ Compile and run the program for the following values: r = 2 Cm, h...
in C++ Compile and run the program for the following values: r = 2 Cm, h = 10 Cm. The answer should be: The cross section area of the cylinder is 3.8955634 c The side area of the cylinder is 19.474819 inch-sqr Did you get the same answer? Explain the reason for such an error and fix the problem. Modify the previous program to include a new function called total_area, that computes the total suface area of a cylinder. The...
Compile the following C code to MIPS assembly. a. Assume that i and j are stored...
Compile the following C code to MIPS assembly. a. Assume that i and j are stored in register $s1 and $s2, respectively. i = i – 2 + j; b. Assume base address of Array B and the variable i are stored in registers $s3 and $s1, respectively. B[1] = (B[0] x 4) - I; Explain each step in detail! my professor has the following answer: 1) addi $t0, $s1, -2 add $s1, $$t0, $s2 2) lw $t0, 0 ($s3)...
Use the Compiler Explorer with the MIPS compiler to compile the following C code. Assuming this...
Use the Compiler Explorer with the MIPS compiler to compile the following C code. Assuming this function is called with the parameter n = 5… int summarize(int n) { int sum = 0; for (int i = 0; i < n; i++) { sum += i; } return sum; } c) Compile the code using the ARM gcc 8.2 compiler. Add the –O1 compiler option, which asks the compiler to optimize for speed (at least to one level). What is...
Use the Compiler Explorer with the MIPS compiler to compile the following C code. Assuming this...
Use the Compiler Explorer with the MIPS compiler to compile the following C code. Assuming this function is called with the parameter n = 5… int summarize(int n) { int sum = 0; for (int i = 0; i < n; i++) { sum += i; } return sum; } a) Accounting for delay slots, what is the dynamic instruction count of this routine? What is the dynamic instruction count if you add the –O1 compiler option, which asks the...
Convert the following code from awk to Perl: (please don't use a2p (awk to perl) command)...
Convert the following code from awk to Perl: (please don't use a2p (awk to perl) command) Here was the original problem: Calculate the grade for each student that appears in the data file. You may calculate the grade based on total earned points, divided by total possible points. Or, for 10 points extra credit; Use the weighted totals for this course; available in the syllabus and on the course home page. Output a list of students, their grade as a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT