Question

In: Computer Science

In C language, Write a program called minishell that creates two child processes: one to execute...

In C language,

Write a program called minishell that creates two child processes:
one to execute 'ls -al' and the other to execute ‘grep minishell.c’.

After the forks, the original parent process waits for both child processes to finish before it terminates. The parent should print out the pid of each child after it finishes. The standard output of 'ls -al' process should be piped to the input to the 'grep minishell.c' process. Make sure you close the unnecessary open files for the three processes. The output should be the one line that includes the directory entry for minishell.c. You will need to call the source file minishell.c for this to work properly.

Please comment thoroughly so i can follow the logic.

Solutions

Expert Solution

ANSWER:--

GIVEN THAT:--

Answering the fist part of the question where the parent should wait for the two child process to finish

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
pid_t pid1, pid2;
int status;
// create the first child process

if ((pid1 = fork()) < 0) {
perror("error forking first child");
return -1;

}

if (pid1 == 0) {
// first child process - execute 'ls -al'
char *args[]={"/bin/ls","-al", NULL};
execvp(args[0], args);
}
//control does not get here since execvp has replaced the program image
// again fork
if ((pid2 = fork()) < 0) {
perror("error forking second child");
return -1;
}
if (pid2 == 0) {
// second child process execute 'grep main minishell.c'
char *args[]={"/bin/grep","main", "minishell.c", NULL};
execvp(args[0], args);
}
wait(&status);
fflush(stdout);
if (pid2 > 0 && pid1 > 0) {
printf("PID of the first child: %d\n", pid1);
printf("PID of the second child: %d\n", pid2);
}
return 0;
}

Now coming to the second part of the question the question seems little incorrect. Ideally to expect the desired output the grep pattern should be "grep minishell". Considering that the following code does the trick -

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
pid_t pid1, pid2;
int status;
int pipefds[2];
int ret;

// create a pipe where the child processes could communicate
ret = pipe(pipefds);

if (ret < 0) {
perror("error creating pipe");
return -1;
}


// create the first child process
if ((pid1 = fork()) < 0) {
perror("error forking first child");
return -1;
}
if (pid1 == 0) {
// first child process - execute 'ls -al'
char *args[]={"/bin/ls","-al", NULL};
// close the stdout of the process; redirect to the pipe write end
close(1);
dup2(pipefds[1], 1);
//close read end of pipe
close(pipefds[0]);
execvp(args[0], args);
}
//control does not get here since execvp has replaced the program image
// again fork
if ((pid2 = fork()) < 0) {
perror("error forking second child");
return -1;
}
if (pid2 == 0) {
// second child process execute 'grep main minishell.c'
char *args[]={"/bin/grep","minishell",NULL};
// redirect the stdin to the read end of pipe
close(0);
dup2(pipefds[0], 0);
//close write end of pipe
close(pipefds[1]);
execvp(args[0], args);
}
// close both pipefds in the parent since it never uses it
close (pipefds [0]);
close (pipefds [1]);
wait(&status);
fflush(stdout);
return 0;
}


Related Solutions

C programming language The program first creates a child process CP. So, there are two processes:...
C programming language The program first creates a child process CP. So, there are two processes: The parent process does the following: a. compute the summary of all the even number from 1, 2, .. 1000, and output this summary; b. wait for the termination of the child process CP, then terminate; The child process does the following: a. compute the summary of all the odd number from 1, 2, .. 1000, and output this summary; b. terminates;
Write a C program that creates a toy scheduler for the child processes in part A....
Write a C program that creates a toy scheduler for the child processes in part A. This program takes as input the number of processes, and all of the PIDs that are being echoed. HINT: Look up redirecting echo output. The program will schedule the ”processes” (note that these are not true processes, this is a toy system. You are effectively only scheduling echo statements). The result of the scheduler will be to echo the PID and current system time...
Write a program in C or C++ that spawns three child processes. The first child sends...
Write a program in C or C++ that spawns three child processes. The first child sends a signal to the parent (you'll need the parent's pid!), which the parent shall catch. The second child waits for 10 seconds and then terminates. Once the parent detects that both of these has happened, it should signal the third child to terminate.
Write a C++ program that creates a base class called Vehicle that includes two pieces of...
Write a C++ program that creates a base class called Vehicle that includes two pieces of information as data members, namely: wheels (type int) weight (type float) Program requirements (Vehicle class): Provide set and a get member functions for each data member. Your class should have a constructor with two parameters (one for each data member) and it must use the set member functions to initialize the two data members. Provide a pure virtual member function by the name displayData()...
In programming C language, write a program that creates a binary tree of words to be...
In programming C language, write a program that creates a binary tree of words to be used as a spell checking device for various text files. The list of words will come from a file “words.txt”. Your program is to read through the “words.txt” file and insert the word on that line into the tree in lexicographic order (also known as Dictionary order). Words in the file will be separated by spaces. Once this is done, your program should then...
In programming C language, write a program that creates a binary tree of words to be...
In programming C language, write a program that creates a binary tree of words to be used as a spell checking device for various text files. The list of words will come from a file “words.txt”. Your program is to read through the “words.txt” file and insert the word on that line into the tree in lexicographic order (also known as Dictionary order). Words in the file will be separated by spaces. Once this is done, your program should then...
Write a C program which fork the process and have the child execute a process.You have...
Write a C program which fork the process and have the child execute a process.You have to implement void forkexec(char** argv).This takes in an array of strings representing arguments.The first argument is the filename of an executable (which will be given as a "./a").The remaining terms would be arguments for said executable.The array is null terminated and you need to fork your process.The child needs to call exec/execvp to execute the specified file with the specified arguments. Also have the...
in C++ Requirements: Write a program that creates a new class called Customer. The Customer class...
in C++ Requirements: Write a program that creates a new class called Customer. The Customer class should include the following private data: name - the customer's name, a string. phone - the customer's phone number, a string. email - the customer's email address, a string. In addition, the class should include appropriate accessor and mutator functions to set and get each of these values. Have your program create one Customer objects and assign a name, phone number, and email address...
Write a C program that outputs a formatted header line and creates 10 children processes each...
Write a C program that outputs a formatted header line and creates 10 children processes each of which displays a line of the output as shown below. Notice the values of Child no and x, they have to be the same as shown here while the processes PIDs values are based on the what your system assigns to these processes. Child    PID        PPID      X 0           13654    13653    5 1           13655    13653    10 2           13656    13653    15 3           13657    13653    20...
C++ Write a program that creates two rectangular shapes and then animates them. The two shapes...
C++ Write a program that creates two rectangular shapes and then animates them. The two shapes should start on opposite ends of the screen and then move toward each other. When they meet in the middle of the screen, each shape reverses course and moves toward the edge of the screen. The two shapes keep oscillating and bouncing off of each other in the middle of the screen. The program terminates when the shapes meet each other in the middle...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT