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...
Write a “C” program(Linux) that creates a pipe and forks a child process. The parent then...
Write a “C” program(Linux) that creates a pipe and forks a child process. The parent then sends the following message on his side of the pipe “I am your daddy! and my name is <pid-of the-parent-process>\n”, the child receives this message and prints it to its stdout verbatim. The parent then blocks reading on the pipe from the child. The child writes back to its parent through its side ofthe pipe stating “Daddy, my name is <pid-of-the-child>”. The parent then...
Write a C++ program that creates a file called Readings.txt. Inside the file, your program must...
Write a C++ program that creates a file called Readings.txt. Inside the file, your program must create a list. The list is composed of integer double pairs. There is one pair per line. The integers are in sequence (0, 1, 2, 3, ...) beginning with zero and ending with some random value between 512 and 1024. The doubles should be random values between 50.000 and 90.000. The doubles only have 3 decimal places. The file should look like this (of...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT