Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/types.h>
- // Assume that each command line has at most 256 characters (including NULL)
- #define MAX_CMDLINE_LEN 256
- // Assume that we have at most 16 pipe segments
- #define MAX_PIPE_SEGMENTS 16
- // Assume that each segment has at most 256 characters (including NULL)
- #define MAX_SEGMENT_LENGTH 256
- // This function will be invoked by main()
- // TODO: Implement the multi-level pipes below
- void process_cmd(char *cmdline);
- // tokenize function is given
- // This function helps you parse the command line
- //
- // Suppose the following variables are defined:
- //
- // char *pipe_segments[MAX_PIPE_SEGMENTS]; // character array buffer to store the pipe segements
- // int num_pipe_segments; // an output integer to store the number of pipe segment parsed by this function
- // char cmdline[MAX_CMDLINE_LEN]; // The input command line
- //
- // Sample usage of this tokenize function:
- //
- // tokenize(pipe_segments, cmdline, &num_pipe_segments, "|");
- //
- void tokenize(char **argv, char *line, int *numTokens, char *token);
- /* The main function implementation */
- int main()
- {
- char cmdline[MAX_CMDLINE_LEN];
- fgets(cmdline, MAX_CMDLINE_LEN, stdin);
- process_cmd(cmdline);
- return 0;
- }
- /*
- Implementation of process_cmd
- TODO: Clearly explain how you implement process_cmd in point form. For example:
- Step 1: ....
- Step 2: ....
- Step 2.1: .....
- Step 2.2: .....
- Step 2.2.1: .....
- Step 3: ....
- */
- void process_cmd(char *cmdline)
- {
- // Delete this line to start your work
- printf("Debug: %s\n", cmdline);
- char *pipe_segments[MAX_PIPE_SEGMENTS]; // character array buffer to store the pipe segements
- int num_pipe_segments; // an output integer to store the number of pipe segment parsed by this function
- // char cmdline[MAX_CMDLINE_LEN];
- // int num_pipes;
- // int num_pipe_segments;
- tokenize(pipe_segments, cmdline, &num_pipe_segments, "|");
- int num_pipes = num_pipe_segments;
- for (int i = 0; i < num_pipes; i++)
- {
- int pfds[2];
- if (i < num_pipes - 1) {pipe(pfds);}
- pid_t pid = fork();
- if (pid == 0)
- {
- close(1);
- if (i< num_pipes -1) {dup2(pfds[1], 1); close(pfds[0]);}
- char* piper[MAX_SEGMENT_LENGTH];
- // int y = 0;
- int numpipes = 0;
- tokenize(piper, pipe_segments[i], &numpipes, " ");
- // close(1);
- // close(pfds[0]);
- execvp(piper[0], piper);
- exit(1);
- }
- else
- {
- wait(0);
- /* code */
- close(0);
- // dup2(pfds[0], 0);
- // close(pfds[1]);
- }
- }
- }
- // Implementation of tokenize function
- void tokenize(char **argv, char *line, int *numTokens, char *delimiter)
- {
- int argc = 0;
- char *token = strtok(line, delimiter);
- while (token != NULL)
- {
- argv[argc++] = token;
- token = strtok(NULL, delimiter);
- }
- argv[argc++] = NULL;
- *numTokens = argc - 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement