1Write a program that executes the cat b v t filename comman
1.Write a program that executes the “cat –b –v –t filename” command. Call your executable myfork
Details:
a.The call to your program will be made with the following command
myfork filename
b.The program will use the execl to call cat and use the filename passed as an argument on the command line./
c.You will use fork() and have the parent wait for the child to finish
d.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
e.Comment out the execl call and add instead the 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? Why? Why not?
b.Who is the parent of your executable (myfork) program?
c.How would you change the code so that a child and parent run concurrently?
Solution
1. Program code for the above scenario:
#include <stdio.h>
#include <unistd.h>
int main (int argc, char *argv[])
{
int pid, status,waitPid, childPid;
pid = fork (); /* Duplicate */
if (pid == 0 && pid != -1) /* Branch based on return value from fork () */
{
childPid = getpid();
printf (\"(The Child)\ Process ID: %d, Parent process ID: %d, Process Group ID: %d\ \",childPid,getppid (),getgid ());
execl(\"/bin/cat\",\"cat\",\"-b\",\"-t\",\"-v\",argv[1],(char*)NULL);
}
else
{
printf (\"(The Parent)\ Process ID: %d, The Parent Process ID: %d, Process Group ID: %d\ \",getpid (),getppid (),getgid ());
waitPid = wait(childPid,&status,1); /* Wait for PID 0 (child) to finish . */
}
return 1;
}

