Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is Unix/linux PART1 Sample 1. The following sample program implements a simple shell to run one command. /* * a3p1shell1.c * simplest shell with

This is Unix/linux

PART1

Sample 1. The following sample program implements a simple shell to run one command.

/*

* a3p1shell1.c

* simplest shell with a SIGINT signal handler. type EOF (^D) to exit.

*/

#include

#include

#include

#include

#include

#include

#include

#include

#include

static void sig_int(int);

char *getinput(char *buffer, size_t buflen)

{

printf("$$ ");

return fgets(buffer, buflen, stdin);

}

int main(int argc, char **argv)

{

char buf[1024];

pid_t pid;

int status;

if (signal(SIGINT, sig_int) == SIG_ERR) {

fprintf(stderr, "signal error: %s ", strerror(errno));

exit(1);

}

while (getinput(buf, sizeof(buf))) {

buf[strlen(buf) - 1] = '\0';

if((pid=fork()) == -1) {

fprintf(stderr, "shell: can't fork: %s ",

strerror(errno));

continue;

} else if (pid == 0) {

/* child */

execlp(buf, buf, (char *)0);

fprintf(stderr, "shell: couldn't exec %s: %s ", buf,

strerror(errno));

exit(EX_DATAERR);

}

if ((pid=waitpid(pid, &status, 0)) < 0)

fprintf(stderr, "shell: waitpid error: %s ",

strerror(errno));

}

exit(EX_OK);

}

void sig_int(int signo)

{

printf(" Caught SIGINT! ");

}

Sample 2. The following sample program is to run two commands in pipe (cmd1 | cmd2) where the two commands are hard-coded (cmd1 and cmd2).

/* 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);

}

}

Task1.

A simple shell to handle one command or two Commands in Pipe

You may use the sample code provided to handle one or two commands in pipe.

Your shell will loop to do the following work: (1) read input from the standard input (keyboard), (2) parse input, (3) print the parsed result to the standard output (console), (4) run the commands in pipe, and (4) back to the loop.

Each input is one command (e.g., ls) or two commands in pipe (e.g., ls | wc -l or ls | grep ".c").

For example, if there are two commands in pipe, the parent gets the command (for example, "ls | wc -l") to recognize that there are two commands piped together. Thus it will have a pipe to be shared, and then fork a child process (to do "ls") which will fork its child process (to do "wc -l") with the pipe shared (so that the output of one process doing "ls" will be input to the input of the other process doing "wc -l"). Meanwhile the parent process waits till all the child processes are terminated, and then back to the loop for the next command from the user.

Your Program Name: a3p1shell1.c

Task2.

A simple shell to process one command, two commands in pipe, or three Commands in Pipe

You will extend your shell program (in Task1) to handle one, two, or three commands in pipe.

Each input is one command (e.g., ls) or two commands in pipe (e.g., ls | wc -l or ls | grep ".c") or three commands (ls /etc | sort | head, or cat /etc/passwd | grep "sh" | sort).

Name your Program to be a3p1shell2.c

Task3.

Many Commands in Pipe

You will extend your shell program (in Task1 or Task2) to handle many commands in pipe.

Name the function to handle one or two commands to be runNcom.

This function is a generalization to process N commands in pipe. The program is in a loop (to handle many commands in pipe). That is, the function is in a for-loop to fork a process to handle the second, to create a pipe for each pair of commands in a pipe, and to fork a child process to handle the first command in pipe. It will repeat in for-loop for all the pairs of commands are processed in pipe.

For example, an example of four commands in pipe would be: ls /etc | grep .conf | sort | head

Your Program Name: a3p1shell3.c

Provide a Makefile file to compile your program.

-----------------------------------------------------------------------------

Part 2. Task1

A Simple Shell to run One Command with File Redirection (< input, > output, >> append).

Design and implement a simple shell program to handle "one command" with file redirection.

For example (and for test cases)

1. ls > part2out1.txt

2. wc < part2out1.txt

3. date > part2out2.txt

4. ls -1 >> part2out2.txt

5. wc < part2out2.txt >> part2out3.txt

6. whoami

7. date

Provide a Makefile file to compile your program.

Your Program Name: a3p2redir1.c

Part 2. Task2.

Two Commands in Pipe, with File Redirection (< input, > output, >> append).

Design and implement a simple shell program to handle "two commands in pipe" with file redirection.

For example (and for test cases),

1. ls | sort > part3out1.txt

2. cat < part3out1.txt | wc > part3out2.txt

3. wc < part3out2.txt | cat part3out2.txt

4. wc < out2.txt > out3.txt

5. whoami

6. date

Provide a Makefile file to compile your program.

Your Program Name: a3p3redir2.c

Part2. Solution

Place your answer here: (a) the program listing and (b) the result of the program run with a proper headings for each part.

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 "< filename"

then do the following:

fd = open(filename, O_CREAT | O_RDONLY);

close(STD_INPUT);

dup(fd);

close(fd);

Or with dup2:

if file redirection is specified for input "< filename"

then do the following:

fd = open(filename, O_CREAT | O_RDONLY);

dup2(fd, 0);

close(fd);

/* 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)) < 0) {

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);

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Working with other project stakeholders for support.

Answered: 1 week ago

Question

Explain the function and purpose of the Job Level Table.

Answered: 1 week ago