Question
C/C++ in unix/linux environment Hint . You may use dup or dup2 to redirect the file input or output for the given command (ls) which
C/C++ in unix/linux environment
Hint. You may use dup or dup2 to redirect the file input or output for the given command ("ls") which a child process will execute (with exec).
if file redirection is specified for input "
then for child process to do:
fid = open(filename, O_RDONLY);
close(STD_INPUT);
dup(fid);
close(fid);
/* a sample code for output redirection */
int main(int argc, char **argv)
{
int pid, status;
int fdout; /* new file descriptor for output */
char *command[] = { "ls", "-l", ">", "output.txt" };
if ((fdout = open(command[3], O_CREAT|O_TRUNC|O_WRONLY, 0644))
perror(command[3]); /* open failed */
exit(1);
}
printf("writing output of the command %s to \"%s\" ",
command[0], command[3]);
dup2(fdout, 1);
execlp(command[0], command[0], command[1], (char *)0);
perror(command[0]); /* execvp failed */
exit(1);
}
/* sample shell to handle two commands with pipe: ls | wc -l */
#include
#include
char *cmd1[] = { "/bin/ls", 0 };
char *cmd2[] = { "/bin/wc", "-l", 0 };
void run2com();
int main(int argc, char **argv)
{
int pid, status;
int fd[2];
pipe(fd);
pid = fork();
if (pid == 0) {
run2com(fd);
exit(0);
} else if (pid > 0) {
while ((pid = wait(&status)) != -1)
fprintf(stderr, "process %d exits with %d ", pid,
WEXITSTATUS(status));
} else {
perror("fork");
exit(1);
}
exit(0);
}
void run2com(int pfd[])
{
int pid;
pid = fork();
if (pid ==0) {
dup2(pfd[0], 0);
close(pfd[1]);
execvp(cmd2[0], cmd2);
perror(cmd2[0]);
} else if (pid > 0) {
dup2(pfd[1], 1);
close(pfd[0]);
execvp(cmd1[0], cmd1);
perror(cmd1[0]);
} else {
perror("fork");
exit(1);
}
}
Design and implement a C/C++ program (shel13.c or shell3.cpp) to implement a simple shell. Warning: you should not use any "system" call throughout this assignment (and thereafter) Taskl. One or Two Commands in Pipe You may use the sample code provided to handle two commands in pipe, to make a shell. Your shell starts to prompt ("$myShell" to the standard output and wait for user input (a command) via the standard input. Your input can be one command (for example, 1s -1) or two commands with pipe (for example, 1s -1 | sort) or three commands with pipe (for example, ls -1| sort | head). Once a command is entered, the shell check any presence of a pipe symbol in the input and break the input into each command (separated by pipe). Then the shell creates a process for each command where the input and/or output of process is connected to the other process corresponding to the pipe between these two processes Your shell handles one command or two commands in pipe ("ls w-1" or "ls sort"). Your shell should generated to handle which command, etc. For example, "ls-1 sort" should provide an output as shown below Shell pid-1000 for the input: Is -1| sort provide the result of this task to show the input, each command of the input, and each process (pid) to be Process1 pid 1001 for the command: 1s -1 Process2 Bid-1002 for the command: sort and so on. * If the input is "exit", then the shell displays a message "myShell terminates" and terminates the shell with return code 0 * If the input is "prompt Hello", then the shell's prompt wil be changed to: SHello> Note. If there are two commands in pipe, the parent gets the command (for example, "Is | Kg-1") to recognize that there are two commands piped together. Thu wll have a pipe to be shared, and then fork a child process (to do "ls") which will fork its child process (to do "w -1") with the pipe shared (so that the output of one process doing "ls" will be input to the input of the other process doing "c-1"). Meanwhile the parent waits till all the child processes are terminated, and then back to the loop for the next command from the user Extending the shell for two commands in pipe, your shell should handle up to "three commands in pipe" (foir example, "ls grep ".c" sort") Task2. A Command with File Redirection ( input, > output, > append) outl.txt") or two commands in pipe Continuing Task 1, your shell program should handle one command s with a file-redirection (for example, "ls sort> out2.txt"). Task3. Provide a Makefile file to compile your program. Design and implement a C/C++ program (shel13.c or shell3.cpp) to implement a simple shell. Warning: you should not use any "system" call throughout this assignment (and thereafter) Taskl. One or Two Commands in Pipe You may use the sample code provided to handle two commands in pipe, to make a shell. Your shell starts to prompt ("$myShell" to the standard output and wait for user input (a command) via the standard input. Your input can be one command (for example, 1s -1) or two commands with pipe (for example, 1s -1 | sort) or three commands with pipe (for example, ls -1| sort | head). Once a command is entered, the shell check any presence of a pipe symbol in the input and break the input into each command (separated by pipe). Then the shell creates a process for each command where the input and/or output of process is connected to the other process corresponding to the pipe between these two processes Your shell handles one command or two commands in pipe ("ls w-1" or "ls sort"). Your shell should generated to handle which command, etc. For example, "ls-1 sort" should provide an output as shown below Shell pid-1000 for the input: Is -1| sort provide the result of this task to show the input, each command of the input, and each process (pid) to be Process1 pid 1001 for the command: 1s -1 Process2 Bid-1002 for the command: sort and so on. * If the input is "exit", then the shell displays a message "myShell terminates" and terminates the shell with return code 0 * If the input is "prompt Hello", then the shell's prompt wil be changed to: SHello> Note. If there are two commands in pipe, the parent gets the command (for example, "Is | Kg-1") to recognize that there are two commands piped together. Thu wll have a pipe to be shared, and then fork a child process (to do "ls") which will fork its child process (to do "w -1") with the pipe shared (so that the output of one process doing "ls" will be input to the input of the other process doing "c-1"). Meanwhile the parent waits till all the child processes are terminated, and then back to the loop for the next command from the user Extending the shell for two commands in pipe, your shell should handle up to "three commands in pipe" (foir example, "ls grep ".c" sort") Task2. A Command with File Redirection ( input, > output, > append) outl.txt") or two commands in pipe Continuing Task 1, your shell program should handle one command s with a file-redirection (for example, "ls sort> out2.txt"). Task3. Provide a Makefile file to compile your programStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started