Question

In: Computer Science

UNIX 1. Compile and run shell0.c file 2. Design and implement shell1.c program to handle "exit"...

UNIX

1. Compile and run shell0.c file

2. Design and implement shell1.c program to handle "exit" command to terminate the program.
That is, if the user input-string is "exit", then the program terminates.

3.Design and Implement shell2.c to implement a signal handler to handle ctrl+C (SIGINT) as listed below

            void sig_int(int signo) { printf("\nCaught SIGINT!\n"); }

Here is shell0.c file

shell0.c

/*
 *  shell0.c 
 *     running in loop to read input string (command) to be processed
 *     To exit, type EOF (ctlr+D) or ctlr+C 
 */
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>

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;

        while (getinput(buf, sizeof(buf))) {
                buf[strlen(buf) - 1] = '\0';

                if((pid=fork()) == -1) {
                        fprintf(stderr, "shell: can't fork: %s\n",
                                        strerror(errno));
                        continue;
                } else if (pid == 0) {
                        /* child */
                        execlp(buf, buf, (char *)0);
                        fprintf(stderr, "shell: couldn't exec %s: %s\n", buf,
                                        strerror(errno));
                        exit(EX_DATAERR);
                }

                if ((pid=waitpid(pid, &status, 0)) < 0)
                        fprintf(stderr, "shell: waitpid error: %s\n",
                                        strerror(errno));
        }
        exit(EX_OK);
}

Solutions

Expert Solution

Here is shell1.c

#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>
int main(int argc, char **argv)
{
char buf[1024];
  do {
    //write your code here
  }  
  while (strcasecmp(getinput(buf, sizeof(buf)),"exit"));
  return 0;   
}

This will exit the program in unix if user type string exit.

shell2.c

#include<stdio.h>
#include<signal.h>
#include<unistd.h>

void sig_int(int signo) { 
if (signo == SIGINT){
   printf("\nCaught SIGINT!\n"); 
}
}

int main(void)
{
  if (signal(SIGINT, sig_int) == SIG_ERR)
  while(1) 
    sleep(1);
  return 0;
}

Here it will stop ctrl+C (SIGINT) from exit and would indiake print Caught SIGINT each time when user presses  ctrl+C. We have also used a long wait to issue signal to process.


Related Solutions

Write a C program to run on unix to read a text file and print it...
Write a C program to run on unix to read a text file and print it to the display. It should count of the number of words in the file, find the number of occurrences of a substring, and take all the words in the string and sort them (ASCII order). You must use getopt to parse the command line. There is no user input while this program is running. Usage: mywords [-cs] [-f substring] filename • The -c flag...
UNIX/LINUX LAB (1) Review the sample shell program (tryShell.c), and compile/run the program (tryShell) with the...
UNIX/LINUX LAB (1) Review the sample shell program (tryShell.c), and compile/run the program (tryShell) with the following commands, and at the end, use CTRL+C to terminate the program: ls ls -l tryShell* date whoami hostname uname -a ctrl+C    (2) Run the program (tryShell) with "time -p" with a few commands: time -p ./tryShell (3) Edit the program (tryShell.c) so that it will exit (terminate the program) when the input command string is "exit" try shell.c code at bottom //////////// #include...
UNIX/LINUX LAB   (1) Compile hello.c program: gcc hello.c -o hello (2) Compile hello.c program with -static...
UNIX/LINUX LAB   (1) Compile hello.c program: gcc hello.c -o hello (2) Compile hello.c program with -static option: gcc -static hello.c -o hello1 (3) Use size command to see the memory layout of these two programs (hello and hello1). (4) Write a brief summary of what you have noted here. Submit the session history (log) of this activity and a brief summary. hello.c program bottom //////////// #include <stdio.h> #include <stdlib.h> int main() { printf("Hello World\n"); // waw hello Richard and ok...
in C++ Compile and run the program for the following values: r = 2 Cm, h...
in C++ Compile and run the program for the following values: r = 2 Cm, h = 10 Cm. The answer should be: The cross section area of the cylinder is 3.8955634 c The side area of the cylinder is 19.474819 inch-sqr Did you get the same answer? Explain the reason for such an error and fix the problem. Modify the previous program to include a new function called total_area, that computes the total suface area of a cylinder. The...
Implement RPC mechanism for a file transfer across a network in ‘C’. **********run the program and...
Implement RPC mechanism for a file transfer across a network in ‘C’. **********run the program and screenshot************
Task #1 void Methods Copy the file geometry.cpp. This program will compile, but when you run...
Task #1 void Methods Copy the file geometry.cpp. This program will compile, but when you run it , it doesn’t appear to do anything except wait. That is because it is waiting for user input, but the user doesn’t have the menu to choose from yet. We will need to create this. Above the main, write the prototype for a function called PrintMenu that has no parameter list and does not return a value.    Below the main, write the function...
Objectives To learn to code, compile, and run a program using file input and an output...
Objectives To learn to code, compile, and run a program using file input and an output file. Assignment Plan and code a program utilizing one file for input and one file for output to solve the following problem: Write a program to determine the highest number, the lowest number, their total, and the average of each line of numbers in a file. A file contains 7 numbers per line. How many lines a file contains is unknown. Note Label all...
6. Write a program in C programming (compile and run), a pseudocode, and draw a flowchart...
6. Write a program in C programming (compile and run), a pseudocode, and draw a flowchart for each of the following problems: a) Obtain three numbers from the keyboard, compute their product and display the result. b) Obtain two numbers from the keyboard, and determine and display which (if either) is the smaller of the two numbers. c) Obtain a series of positive numbers from the keyboard, and determine and display their average (with 4 decimal points). Assume that the...
Program in C Make both a SOURCE AND HEADER FILE WITH FUNCTIONS to run the program....
Program in C Make both a SOURCE AND HEADER FILE WITH FUNCTIONS to run the program. Input data from csv file. Two files. grades.c and grades.h Thank you data.csv file Mark Prest,37468,36,113,In Person Andy Joe,6785923,19,98,Online Caden Miller,237741,20,70.1,In Person Luke Jr,2347878,18,45.9,In Online Ally Rogers,8467483,30,89.99,Online Maya Jank,5674930,30,90,In Person Expected output Name: Mark Prest ID: 37468 Age: 36 Grade: 113.00 Attending: In Person Name: Andy Joe ID: 6785923 Age: 19 Grade: 98.00 Attending: Online Name: Caden Miller ID: 237741 Age: 20 Grade: 70.10...
Design and implement a C++ program that performs the following steps:Ask the user to enter a...
Design and implement a C++ program that performs the following steps:Ask the user to enter a positive integer number N; Your program may need to prompt the user to enter many times until it reads in a positive number;Let user to enter N (obtained in the previous step) floating point numbers, and count how many positive ones there are in the sequence and sum up these positive numbers; (Hint: negative numbers or 0 are ignored).Display result.You can and should use...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT