In C code, please whole code In C code ( UNIX C CODE), please Write a program ca
ID: 3857554 • Letter: I
Question
In C code, please whole code In C code ( UNIX C CODE), please
Write a program called minishell that creates two child processes: one to execute 'ls' and the other
to execute 'sort'. After the forks, the original parent process waits for both child processes to finish
before it terminates. The standard output of 'ls' process should be piped to the input to the 'sort'
process. Make sure you close the unnecessary open files for the three processes.
---
Carefully study the examples provided: fork.c, myls.c, myshell.c, etc. You need to know how they
work in order for you to finish this assignment; the examples
fork.c (link where to find the example) https://drive.google.com/file/d/0B9b80md_Miu5bTN0cV9ndkZ5T1k/view?usp=sharing
myls.c (link where to find the example) https://drive.google.com/file/d/0B9b80md_Miu5N0ZvaGlRZDdRWjA/view?usp=sharing
myshell.c (link where to find the example) https://drive.google.com/file/d/0B9b80md_Miu5N0xyUFk0bGdDXzQ/view?usp=sharing
Explanation / Answer
#include <stdio.h>
int main()
{
int pipe1[2],pid1,pid2;
pipe(pipe1);
pid1 = fork();
if (pid1 == 0)
{
close(1);
dup(pipe1[1]);
close(pipe1[0]);
close(pipe1[1]);
printf("child1 executing ls ");
execl("/bin/ls", "ls", 0, 0);
perror("execl error");
}
else
{
wait(&pid1); //wait for children
pid2 = fork();
if (pid2 == 0)
{
close(0);
dup(pipe1[0]);
close(pipe1[0]);
close(pipe1[1]);
printf("child2 executing sort ");
execl("/bin/sort", "sort", 0, 0);
perror("execl error");
}
else
{
/* Parent Code */
printf("Parent waiting for children to finish ");
wait(0); //wait for children
}
}
}