Type the following C program to see how shell invocation of a command works: #in
ID: 3711513 • Letter: T
Question
Type the following C program to see how shell invocation of a command works:
#include <stdio.h>
main ( )
{
int i;
/* fork another process */
i=fork();
if (i < 0 ) { /* error occured */
fprintf(stderr, "Fork Failed");
exit(-1);
}
else if (i==0){ /* child process */
execlp("/bin/ls","ls", NULL);
}
else { /* parent process */
/* parent will wait for the child to complete */
wait(NULL);
printf(" Child Complete ");
exit(0);
}
}
Call the above program p4.c, and compile it as follows: gcc -o p4 p4.c <Enter>.
Then execute p4 and report the output.
Questions:
4.1 When does the wait system call returns in this program?
4.2 Explain the shell invocation of a command?
4.3 Compare this program with shell invocation of a command?
Explanation / Answer
4.1) Wait system call returns in this program when all the child process will complete. In this program wait system call is for parent process, after complition of child process only parent process will complete. so wait system call will wait for child process and after complition of child process it will return to this program.
4.2) Generally we can do loging in to invoke shell. An explicit command to invoke shell is sh. We can customize session with sh by using profile files. Profiles can do function and variable defination, aliases creation or option setting.
If sh command line consist of atleast one argument, the first argument will be taken as the shell script name to run. If the positional parameters are assigned with any additional arguments; usually, these is treated as shell script arguments.
4.3) Shell is a visible part of os which interprets command and receive . Each activity invoke a direct or indirect shell command. Command is basically a request of action given to the shell for execution. The action will have some input data and it and it will create output data.
This given program is a kind of System call. System call is a kind of function call used in c like programming language to check the the system functionality.
A shell invoking command is developed by shells in the shells.
This are the basic difference between program and shell invocation command.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.