Question
Looking to get an implementation for the spawnStage and setupCommandPipeline using the functions below, Thanks! mshell.h: #ifndef _MSHELL_H_ #define _MSHELL_H_ #define R_NONE 0 /* No
Looking to get an implementation for the spawnStage and setupCommandPipeline using the functions below, Thanks!
mshell.h:
#ifndef _MSHELL_H_
#define _MSHELL_H_
#define R_NONE 0 /* No redirections */
#define R_INPUT 1 /* input redirection bit */
#define R_OUTPUT 2 /* output redirection bit */
#define R_APPEND 4 /* append redirection bit */
#define TRUE 1
#define FALSE 0
#define ERROR_MSG(x) fprintf(stderr, "%s ", (x))
enum Kind {
noCMD = 0, exitCMD, cdCMD, basicCMD, pipelineCMD
};
typedef struct Stage {
int num_args;
char ** args;
int fdin;
int fdout;
pid_t child;
} Stage;
typedef struct Command {
char* cmd;
enum Kind kind;
int mode;
char* input;
char* output;
char** args;
int num_args;
Stage** stages;
int num_stages;
} Command;
int basicExecute(char* com,int mode,char* input,char* output,char** args);
int setupCommandPipeline(Command * c);
/* You may call this function to see the stage you are dealing with */
void printStage(Stage*);
#endif
pipeline.c:
#define _POSIX_C_SOURCE 200809L
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "mshell.h"
// like a method of Stage. set the input and output fds.
static Stage * setStageInput(Stage * s, int fd)
{
s->fdin = fd;
return s;
}
static Stage * setStageOutput(Stage * s, int fd)
{
s->fdout = fd;
return s;
}
// A pipeline "object" contains
// 1. An array of nbStages stages
// 2. A mode flag to determine whether
// - the pipeline input is redirected
// - the pipeline output is redirected
// Each stage is a structure that encapsulates
// - The number of arguments
// - Each argument (in string form)
// BY DEFAULT THE MAIN PROGRAM CALLS THE PRINT METHOD OF THE COMMAND
// OBJECT. So you can see how the pipeline description is represented/stored
// Your objective:
// * Implement the execute method to create and execute the entire pipeline.
// Hint: all the systems calls seen in class will be useful (fork/exec/open/close/pipe/....)
// Good Luck!
/*
* This is actually the core: fork() and set up pipe.
*
*/
static void spawnStage(Stage* s)
{
// TODO
}
// wait for the pipeline stage to complete.
static void waitStage(Stage* s)
{
int st;
waitpid(s->child, &st, 0);
}
/*
* list:
* 1. Setup 'global' redirections.
* 2. Create all pipes. It is a fatal error if a pipe cannot be created.
* 3. spawn all processes, using spawnStage().
* 4. wait for processes to finish, using waitStage().
*
* The function always returns 1 for now.
*/
int setupCommandPipeline(Command * c)
{
// TODO
return 1;
}
Exercise 2. Pipeline command (60 points) A pipeline is a sequence of external programs chained together to form a single construction. The chaining occurs when the standard output of stage i of the pipeline is fed to the standard input of stage i+1. Consider that each stage of the pipeline consists of an external command (as in Exercise 1) that can take a collection of arguments. Its input is coming from a pipe fed by the previous pipeline stage and its output feeds the next stage of the pipeline Your specific task is to implement the following function in pipeline.c. Note that it depends on some other function:s int setupCommandPipeline (Command* c) The command object is an abstract data type defined in the header file and reproduced in Figure 1. The Figure defines two structures. The Command structure specifies the entire pipeline. The arguments to each stage are embedded in the stages array and the array in the Command are not used for pipelines. Probably you have already noticed that the arguments past to the function in Exercise 1 are from the Command structure 1 typedef struct Stage f int num args char args; int int pidt child; fdin; fdout; 6 7 Stage; 9 typedef struct Command 10 cmd enum Kind kind int char* char ut put; char int num_args; Stage* stages; int num stages; mode input; 12 13 14 15 16 17 18 19 Command; args; Figure 1: The Command ADT Consider the following pipeline for counting the number of occurrences of each word in a text Exercise 2. Pipeline command (60 points) A pipeline is a sequence of external programs chained together to form a single construction. The chaining occurs when the standard output of stage i of the pipeline is fed to the standard input of stage i+1. Consider that each stage of the pipeline consists of an external command (as in Exercise 1) that can take a collection of arguments. Its input is coming from a pipe fed by the previous pipeline stage and its output feeds the next stage of the pipeline Your specific task is to implement the following function in pipeline.c. Note that it depends on some other function:s int setupCommandPipeline (Command* c) The command object is an abstract data type defined in the header file and reproduced in Figure 1. The Figure defines two structures. The Command structure specifies the entire pipeline. The arguments to each stage are embedded in the stages array and the array in the Command are not used for pipelines. Probably you have already noticed that the arguments past to the function in Exercise 1 are from the Command structure 1 typedef struct Stage f int num args char args; int int pidt child; fdin; fdout; 6 7 Stage; 9 typedef struct Command 10 cmd enum Kind kind int char* char ut put; char int num_args; Stage* stages; int num stages; mode input; 12 13 14 15 16 17 18 19 Command; args; Figure 1: The Command ADT Consider the following pipeline for counting the number of occurrences of each word in a textStep 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