A process creates a child process and may want to change the program code the ch
ID: 3668276 • Letter: A
Question
A process creates a child process and may want to change the program code the child is executing. There are 6 system calls under the generalized heading exec. When the process issues any exec call, if the call is successful, the existing process is overlaid with a new set of program code. # include # include # include # include void main(int argc, char *argv[]) { if (argc > 1) { execlp ("/bin/cat", "cat", argv[l], (char *) NULL); perror ("exec failure'' ); exit(l); } printf(" Usage: %s text_file ", *argv); exit(l); } Check for program error, compile, and run. Observe the results. Replace "cat'' with other commands. Replace contents of main function with execvp(argv[l], &argv;[l]); perror("exec failure''); exit(l); Compile, run, and observe results. Use other commands. Replace contents of main function with the following (replace "yourfile.txt" with any existing text file in your directory) static char *new_argv[] = {''cat'',yourfile.txt", (char *) 0}; execvp("/bin/cat", new_argv); perror("exec failure'); exit(l); Compile, run, and observe results. Use commands other than "cat".Explanation / Answer
1.
// test.c
save this program in , lest say test.c file
compile using: gcc test.c
run: ./a.out file_name // here lets say file_name = adc.txt , this should be in same directory, where test.c file is present.
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
if(argc > 1){
execlp("/bin/cat","cat",argv[1],(char*)NULL);
perror("exec failure");
exit(1);
}
printf(" Usage:%s text_file ",*argv);
return 0;
}
2.
//test.c
Compile: gcc test.c
Run: ./a.out ls // you can use any valid linux command
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
if(argc > 1){
//execlp("/bin/cat","cat",argv[1],(char*)NULL);
execvp(argv[1],&argv[1]);
perror("exec failure");
exit(1);
}
printf(" Usage:%s text_file ",*argv);
return 0;
}
3.
//test.c
Compile: gcc test.c
Run: ./a.out // please replace 'insertion.cpp' with any text file name that is present in same directory where test.c is present
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
static char *new_argv[] = {"cat","insertion.cpp",(char*)0};
execvp("/bin/cat",new_argv);
//execlp("/bin/cat","cat",argv[1],(char*)NULL);
//execvp(argv[1],&argv[1]);
perror("exec failure");
exit(1);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.