Write a C program that creates (or spawns) a child process to execute the Unix's command (echo) with a string as the command-line argument; while the program itself (the parent process) will execute another Unix command less with a filename as the command-line argument. If you aren't sure how the echo and less commands are meant to behave, experiment with them by running them directly from the command line first.
Hi,
Please go through code and output.
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char ** argv)
{
if(argc < 3) // check arguments
{
printf("Usage: ./a.out string
filename\n");
return 0;
}
if(fork() == 0) // create child process
{
// child
char str[64] = {0};
sprintf(str, "echo %s", argv[1]);
// copy command in str
system(str); // exicute
command
}
else
{
// parent
char str[64] = {0};
sprintf(str,"less %s",argv[2]); //
copy command in str
system(str); // exicute
command
}
}
OUTPUT:
Get Answers For Free
Most questions answered within 1 hours.