1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
| #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/wait.h> #include <string.h> #include <signal.h> #include <sys/shm.h> #include <sys/stat.h> #include <sys/types.h> #include <wait.h> #define MAX_LINE 80
typedef struct { char history[10][MAX_LINE]; int po; int flag; } shared_data;
int segment_id; shared_data* shared_memory;
void setup(char inputBuffer[], char *args[],int *background) { int length, i, start, ct;
ct = 0;
length = read(STDIN_FILENO, inputBuffer, MAX_LINE);
start = -1; if (length == 0) exit(0); if (length < 0) { perror("error reading the command"); exit(-1); } for (i=0;i<length;i++) { switch (inputBuffer[i]) { case ' ': case '\t' : if (start != -1) { args[ct] = &inputBuffer[start]; ct++; } inputBuffer[i] = '\0'; start = -1; break;
case '\n': if (start != -1) { args[ct] = &inputBuffer[start]; ct++; } inputBuffer[i] = '\0'; args[ct] = NULL; break;
default : if (start == -1) start = i; if (inputBuffer[i] == '&') { *background = 1; inputBuffer[i] = '\0'; } } } args[ct] = NULL; }
void handle_SIGINT() { if (shared_memory->flag == 1) { exit(0); } shared_memory->flag = 1; printf("\nHistory:\n"); int i, j; for (i = 0; i < shared_memory->po; i++) { printf("%s\n", shared_memory->history[i]); } signal(SIGINT, SIG_IGN); exit(0); }
int main() { char inputBuffer[MAX_LINE]; int background; char *args[MAX_LINE/2 + 1]; for (int i = 0; i < MAX_LINE/2 + 1; i++) { args[i] = NULL; } pid_t pid; struct sigaction handler; handler.sa_handler = handle_SIGINT; sigaction(SIGINT, &handler, NULL);
segment_id = shmget(IPC_PRIVATE, sizeof(shared_data), S_IRUSR | S_IWUSR); if (segment_id == -1) { fprintf(stderr, "Allocate Shared Memory Fail!\n"); return 1; } printf("Create Shared Memory Successfully, segment_id = %d\n", segment_id); shared_memory = (shared_data *)shmat(segment_id, NULL, 0); if (shared_memory == (shared_data *)-1) { fprintf(stderr, "Attach Shared Memory Fail!\n"); return 0; } shared_memory->flag = 0; while (1) { background = 0; printf("COMMAND->"); fflush(stdout); setup(inputBuffer, args, &background); pid = fork(); if (pid == -1) { printf("Fork Error.\n"); } else if (pid == 0) { int j; int k = 0; for (j = 0; j < sizeof(args); j++) { if (args[j] != NULL) { for (int c = 0; c < strlen(args[j]); c++,k++) { shared_memory->history[shared_memory->po][k] = args[j][c]; } shared_memory->history[shared_memory->po][k++] = ' '; } else { break; } } shared_memory->history[shared_memory->po][k] = '\0'; shared_memory->po = (shared_memory->po + 1) % 10;
int error = execvp(args[0], args); if (error == -1) { shared_memory->po == 0 ? shared_memory->po = 0 : shared_memory->po--; } } if (background == 0) { wait(0); } else { setup(inputBuffer, args, &background); } } }
|