Question

Round 1: sequence.c This program should read and execute a list of commands from stdin. Each command and its arguments (if an1 while there are remaining commands read line from file parse line into command and arguments 4 5 for cmd in commands 6 7 fo

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/wait.h>

#define MAX 100
/*Function definition to parse the command */
void parse(char *line,char **argv){
   while(*line !='\0'){
       while(*line ==' ' || *line == '\t' || *line == '\n')
           *line++ = '\0';
       *argv++ = line;
       while(*line !='\0' && *line != ' ' && *line != '\t' && *line != '\n')
           line++;
   }
   *argv = '\0';
}
/*Function definition of main()*/
int main(int argc, char *argv[]){
   char *arg[MAX],line[256];
   int status;
   pid_t pid;
   FILE *fp;
   /*Error checking for number of command line arguments */
   if(argc < 2){
       printf("Input file is missing!!\n");
       exit(0);
   }
   fp = fopen( argv[1],"r");
   /* Error checking for file opening */
   if(fp == NULL){
       printf("Error in opening file!!\n");
       return 0;
   }

/* Reading file line by line */
   while(fgets(line,256,fp)!= NULL){
       strtok(line, "\n");
       pid = fork();       //creating child process using fork() system call.
       /*Errro checking for creation of child */
       if(pid < 0){
           printf("Forking Failed!!\n");
           exit(1);
       }
       else if(pid == 0){
           parse(line,arg);       //Function call to parse command
           if(execvp(*arg,arg) < 0){
               printf("Execvp failed!!\n");
               exit(1);
           }

       }else{
           wait(&status);       //Waiting for child process to finish.
       }
   }
   return 0;
}

SCREEN SHOTS:

incluce string.h 11 ,Function defrition to parse the command- Emor checking for number of command ine arguments pnmtt inp.telse wai&sta:iting for chid procoss t o finsh. relurn 0;Programs$ gcc shell.c :-/Programs$ -la.out :/Programs /a.out cndfile i/Progranss /a.out test Error in opentng file!! Input fi

Add a comment
Know the answer?
Add Answer to:
Round 1: sequence.c This program should read and execute a list of commands from stdin. Each comm...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using Unix processes Submit a README file that lists the files you have submitted along with...

    Using Unix processes Submit a README file that lists the files you have submitted along with a one sentence explanation. Call it Prj1README. MakeCopy.c : Write a C program that makes a new copy of an existing file using system calls for file manipulation. The names of the two files and copy block sizes are to be specified as command line arguments. Open the source file in read only mode and destination file in read/write mode. ForkCopy.c : Write a...

  • What are the exact commands I should use to complete the task. 1. Using the ps...

    What are the exact commands I should use to complete the task. 1. Using the ps –ef and grep command list all processes started by you (i.e. owned by your userid). (Hint: ‘^userid’ with match with a line that start with userid.) 2. Run command group (echo “Snoozing…”; date; sleep 50; echo “Awake.”;date ) > sleep.out & Note: Do not omit the parenthesis!!! 3. Repeat 1, identify process number of the process sleep 4. Display the content of file sleep.out...

  • I am having trouble on what exactly to write so I can make the script work,...

    I am having trouble on what exactly to write so I can make the script work, can someone show how to properly do it? Use the vi editor to enter the following script (name this file testscript): #!/bin/bash #script to demonstrate : reading arguments from a command line & # interactively from within script # selection (if… then… else…. fi) # iteration (for loop) # USE FILES ENTERED ON THE COMMAND LINE echo The script you are running is $0...

  • C homework help pipes

    Round 2: pipeline.cThis program takes the same input as sequence.c, but executes the commands as a pipeline, where the output of each command is piped to the input of the next command in line. The input of the first command, and the output of the final command, should not be altered. For example, if the file "cmdpipe" contains the linesls -s1sort -ntail -n 5then running./pipeline < cmdpipeshould output the 5 largest files in the current directory, in order of size.Suggested approach: set...

  • INFO 1211

    Lab 0: Essential UNIX OperationsObjectivesAt the end of this of lab, you should be able to:securely      log in to a remote computer running a UNIX-like operating systemread the      manual page for any commanduse the      UNIX file systemedit a      text fileNotes:·         In order to get familiar with UNIX, do all your work on UNIX.·         A command line interface may be harder to learn, but can be more powerful for scripting and automating tasks.·        ...

  • Could you help me do the commands from nmber 16 to 21 For this assignment, you...

    Could you help me do the commands from nmber 16 to 21 For this assignment, you will: Unpack a tar archive. Change the unpacked files. Repack the files into a new tar archive. Turn in your new tar archive. Details Do not use an editor unless it specifically states to edit the file. By "editor" I mean vim, gedit, pico, etc. Despite its name, we don't consider sed to be an editor in this assignment, so you may use it...

  • 1. Write 4 simple shell scripts. Each question will ask you to write one or more...

    1. Write 4 simple shell scripts. Each question will ask you to write one or more command lines to solve a problem. Put the command(s) from each problem into a file named problem1.sh, problem2.sh Note that if you want your script to be interpreted by bash, each file should start with the line: #!/bin/bash and will have one (or more, as needed) commands following it. To execute each script in bash, so you can determine if it is working or...

  • I need this done ASAP. Please write a bash shell script that does the following: Prints...

    I need this done ASAP. Please write a bash shell script that does the following: Prints out how many users are logged on. This can be accomplished using who and wc. Prints out a list of currently logged on users. This can be accomplished using who, grep, regular expressions, and echo. Prints out how many processes you have running from past days, and a list of those jobs. Use `whoami` to get your username. Run ps -ef and see how...

  • For part one of this assignment, write a program (parse.c) that contains a function to parse...

    For part one of this assignment, write a program (parse.c) that contains a function to parse a single line of input and and prints out the individual tokens. Part 1 - Single Line Parser For part one of this assignment, you will need to learn how to do some parsing (or more accurately--lexing a line, you do not have to check for correctness). You have previously done some parsing with the cycle count tool perhaps using functions such as strcmp....

  • CSIT 345 Lab 2 Process Programming Your lab requirement is to write program codes for 3.21 and sh...

    *Help Please with the code** CSIT 345 Lab 2 Process Programming Your lab requirement is to write program codes for 3.21 and shared memory program for producer and consumer as shown in the following. You can start with the code provided in the virtual machine in the virtual box you installed. The code can be found in /home/oscreader/osc9e-src/ch3 a. For 3.21, you can start with the newprocposix.c and modify the code to meet your requirement. Then type: gcc neypCOCROSİS.c to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT