├── README.md ├── banker_algorithm └── banker.cpp ├── project1 ├── project1 report.pdf └── test.c ├── project2 ├── project2 report.pdf └── shell.c ├── project3 ├── matrix-pthread.c ├── matrix-win32.c └── project3 report.pdf └── project4 ├── producer_and_consumer.c └── project4 report.pdf /README.md: -------------------------------------------------------------------------------- 1 | # SJTU_OS-project 2 | 3 | The projects are based on the ubuntu 15.04 4 | 5 | 6 | - project 1: add a system call to the linux kernel 7 | 8 | - project 2: a unix shell and history feature 9 | 10 | - project 3: matrix calculation 11 | 12 | - project 4: producer and consumer problem 13 | -------------------------------------------------------------------------------- /banker_algorithm/banker.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define MAXPROCESS 50 5 | #define MAXRESOURCE 100 6 | int AVAILABLE[MAXRESOURCE]; 7 | int MAX[MAXPROCESS][MAXRESOURCE]; 8 | int ALLOCATION[MAXPROCESS][MAXRESOURCE]; 9 | int NEED[MAXPROCESS][MAXRESOURCE]; 10 | int REQUEST[MAXPROCESS][MAXRESOURCE]; 11 | bool FINISH[MAXPROCESS]; 12 | int p[MAXPROCESS]; 13 | int m, n; 14 | void Init(); 15 | bool Safe(); 16 | void Bank(); 17 | void showdata(int, int); 18 | int main() 19 | { 20 | Init(); 21 | Safe(); 22 | Bank(); 23 | } 24 | void Init() 25 | { 26 | int i, j; 27 | cout << "please input the number of process:"; 28 | cin >> m; 29 | cout << "Please input the number of resource:"; 30 | cin >> n; 31 | cout << "please input max[i][j],follow the form" << m << "x" << n << endl; 32 | for (i = 0; i> MAX[i][j]; 35 | cout << "please input allocation[i][j],follow the form" << m << "x" << n << endl; 36 | for (i = 0; i> ALLOCATION[i][j]; 41 | NEED[i][j] = MAX[i][j] - ALLOCATION[i][j]; 42 | if (NEED[i][j]<0) 43 | { 44 | cout << "allocation[" << i + 1 << "][" << j + 1 << "] is wrong, input again:" << endl; 45 | j--; 46 | continue; 47 | } 48 | } 49 | } 50 | cout << "please input available[i]:" << endl; 51 | for (i = 0; i> AVAILABLE[i]; 54 | } 55 | } 56 | void Bank() 57 | { 58 | int i, cusneed, flag = 0; 59 | char again; 60 | while (1) 61 | { 62 | showdata(n, m); 63 | cout << endl; 64 | input: 65 | cout << "please input the process number need to request" << endl; 66 | cin >> cusneed; 67 | if (cusneed > m) 68 | { 69 | cout << "no such process, again" << endl; 70 | goto input; 71 | } 72 | cout << "please input request[i]" << endl; 73 | for (i = 0; i> REQUEST[cusneed][i]; 76 | } 77 | for (i = 0; iNEED[cusneed][i]) 80 | { 81 | cout << "please do not bigger than need, again!" << endl; 82 | goto input; 83 | } 84 | if (REQUEST[cusneed][i]>AVAILABLE[i]) 85 | { 86 | cout << "please do not bigger than available, again!" << endl; 87 | goto input; 88 | } 89 | } 90 | for (i = 0; i> again; 134 | if (again == 'y' || again == 'Y') 135 | { 136 | continue; 137 | } 138 | break; 139 | } 140 | } 141 | bool Safe() 142 | { 143 | int i, j, k, l = 0; 144 | int Work[MAXRESOURCE]; 145 | for (i = 0; iWork[j]) 156 | { 157 | break; 158 | } 159 | } 160 | if (j == n && FINISH[i]==false) 161 | { 162 | FINISH[i] = true; 163 | for (k = 0; k"; 184 | } 185 | } 186 | cout << "" << endl; 187 | return true; 188 | } 189 | } 190 | cout << "unsafe" << endl; 191 | return false; 192 | } 193 | 194 | void showdata(int n, int m) 195 | { 196 | int i, j; 197 | cout << endl; 198 | cout << "-------------------------------------------------------------" << endl; 199 | cout << "available: "; 200 | for (j = 0; j 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | int temp; 8 | temp = syscall(314, 1); 9 | if(temp == 1) 10 | printf("success!\n"); 11 | else 12 | printf("failed!\n"); 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /project2/project2 report.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chasingegg/SJTU_OS-project/8c91d99ba80a4da759e48caed767203efab655a1/project2/project2 report.pdf -------------------------------------------------------------------------------- /project2/shell.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define MAX_LINE 80 10 | #define BUFFER_SIZE 50 11 | 12 | char buffer[BUFFER_SIZE]; 13 | char history[10][BUFFER_SIZE]; //store the history command 14 | int NumOfCommand = 0; //the num of history command, the maximum value is 10 15 | int start_index = 0; 16 | 17 | void setup(char inputBuffer[], char *args[], int *background) 18 | { 19 | int len; 20 | int start = -1; 21 | int j = 0; 22 | int i; 23 | int flag = 0; 24 | 25 | len = read(STDIN_FILENO, inputBuffer, MAX_LINE); //read the command 26 | inputBuffer[len] = '\0'; 27 | 28 | if(strcmp(inputBuffer, "quit\n") == 0 || strcmp(inputBuffer, "exit\n") == 0) 29 | exit(0); 30 | 31 | //first deal with the r x, find the corresponding command from the history, and put it to the inputBuffer 32 | if(inputBuffer[0] == 'r' && NumOfCommand > 0) 33 | { 34 | if(inputBuffer[1] == '\n') 35 | { 36 | strcpy(inputBuffer, history[NumOfCommand - 1]); 37 | } 38 | else if(inputBuffer[1] == ' ' && inputBuffer[2] != '\0' && inputBuffer[3] == '\n') 39 | { 40 | for(i = NumOfCommand - 1; i >= 0; --i) 41 | { 42 | if(inputBuffer[2] == history[i][0]) 43 | { 44 | strcpy(inputBuffer, history[i]); 45 | flag = 1; 46 | break; 47 | } 48 | } 49 | if(!flag) //if there is no command match to the r x, then return and no action to the history 50 | { 51 | args[0] = NULL; 52 | return; 53 | } 54 | 55 | } 56 | len = strlen(inputBuffer); 57 | } 58 | 59 | if(len < 0) //ctrl c 60 | { 61 | args[0] = NULL; 62 | return; 63 | } 64 | 65 | if(len == 0) //ctrl z 66 | { 67 | exit(0); 68 | } 69 | 70 | strcpy(history[(start_index + NumOfCommand) % 10], inputBuffer); 71 | if(NumOfCommand == 10) //if the num of history command is 10,move the olddest command out 72 | { 73 | start_index = (start_index + 1 + 10) % 10; 74 | } 75 | else 76 | { 77 | ++NumOfCommand; 78 | } 79 | 80 | //seperating the command to the distinct tokens 81 | for(i = 0; i < len; ++i) 82 | { 83 | if(inputBuffer[i] == ' ' || inputBuffer[i] == '\t') //find the delimiters 84 | { 85 | if(start != -1) 86 | { 87 | args[j++] = &inputBuffer[start]; 88 | start = -1; 89 | } 90 | inputBuffer[i] = '\0'; 91 | } 92 | else if(inputBuffer[i] == '\n') 93 | { 94 | if(start != -1) 95 | { 96 | //inputBuffer[i] = '\0'; 97 | args[j++] = &inputBuffer[start]; 98 | } 99 | args[j] = NULL; 100 | inputBuffer[i] = '\0'; 101 | } 102 | else 103 | { 104 | if(inputBuffer[i] == '&') 105 | { 106 | *background = 1; 107 | inputBuffer[i] = '\0'; 108 | } 109 | if(start == -1) 110 | start = i; 111 | } 112 | } 113 | 114 | args[j] = NULL; 115 | 116 | } 117 | 118 | void handle_SIGINT() //get the Ctrl-c signal, to display the history command 119 | { 120 | //printf("\nCaught Ctrl C\n"); 121 | write(STDOUT_FILENO, buffer, strlen(buffer)); 122 | printf("The Command History:\n"); 123 | int i; 124 | i = start_index; 125 | int j = 0; 126 | fflush(stdout); 127 | for(; i < NumOfCommand + start_index; ++i) 128 | { 129 | printf("%d. %s", ++j, history[i % 10]); 130 | //fflush(stdout); 131 | } 132 | //printf("COMMAND-> "); 133 | //fflush(stdout); 134 | return; 135 | //exit(0); 136 | } 137 | 138 | 139 | int main() 140 | { 141 | char input[MAX_LINE]; 142 | int background; 143 | char *args[MAX_LINE / 2 + 1]; 144 | 145 | //set up the signal handler 146 | struct sigaction handler; 147 | handler.sa_handler = handle_SIGINT; 148 | sigaction(SIGINT, &handler, NULL); 149 | 150 | strcpy(buffer, "\nCaught Control C\n"); 151 | 152 | while(1) 153 | { 154 | background = 0; 155 | fflush(stdin); 156 | printf("COMMAND-> "); 157 | fflush(stdout); //print the stdout buffer 158 | background = 0; 159 | setup(input, args, &background); //read the command 160 | 161 | //deal with cd command 162 | if(strcmp(input, "cd") == 0) 163 | { 164 | if(*args[1] == '~') 165 | { 166 | strcpy(args[1], "/home/gao"); 167 | } 168 | if(chdir(args[1]) == 0) 169 | { 170 | continue; 171 | } 172 | } 173 | if(args[0] != NULL) 174 | { 175 | 176 | pid_t pid; 177 | 178 | pid = fork(); 179 | 180 | if(pid < 0) 181 | { 182 | printf("fork failed\n"); 183 | exit(1); 184 | } 185 | else if(pid == 0) 186 | { 187 | execvp(args[0], args); 188 | background = 0; 189 | printf("command error\n"); 190 | exit(0); 191 | } 192 | 193 | else 194 | { 195 | if(background == 0) 196 | { 197 | waitpid(pid, NULL, 0); 198 | } 199 | } 200 | } 201 | } 202 | return 0; 203 | } 204 | 205 | 206 | 207 | 208 | 209 | 210 | -------------------------------------------------------------------------------- /project3/matrix-pthread.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int M; 6 | int K; 7 | int N; 8 | 9 | #define MAX 50 10 | int A[MAX][MAX]; 11 | int B[MAX][MAX]; 12 | int C[MAX][MAX]; 13 | 14 | struct v 15 | { 16 | int vi; 17 | int vj; 18 | }; 19 | 20 | void *runner(void *param) 21 | { 22 | struct v *args; 23 | args = (struct v*)param; 24 | int i = args -> vi, j = args -> vj; 25 | int m, res = 0; 26 | for(m = 0; m < K; ++m) 27 | { 28 | res += A[i][m] * B[m][j]; 29 | } 30 | C[i][j] = res; 31 | free(param); 32 | return NULL; 33 | 34 | } 35 | 36 | int main() 37 | { 38 | pthread_t thread[MAX][MAX]; 39 | 40 | int i, j; 41 | 42 | int rc; 43 | struct v *data; 44 | printf("please input the M K N\n"); 45 | scanf("%d%d%d", &M, &K, &N); 46 | printf("input the matrix1:\n"); 47 | for(i = 0; i < M; ++i) 48 | for(j = 0; j < K;++j) 49 | { 50 | scanf("%d", &A[i][j]); 51 | } 52 | printf("\ninput the matrix\n"); 53 | for(i = 0; i < K; ++i) 54 | for(j = 0; j < N; ++j) 55 | { 56 | scanf("%d", &B[i][j]); 57 | } 58 | for(i = 0; i < M; ++i) 59 | for(j = 0; j < N; ++j) 60 | { 61 | data = (struct v*) malloc(sizeof(struct v)); 62 | data -> vi = i; 63 | data -> vj = j; 64 | 65 | 66 | rc = pthread_create(&thread[i][j], NULL, runner, data); 67 | 68 | if(rc) 69 | { 70 | printf("create error!\n"); 71 | return 0; 72 | } 73 | 74 | 75 | } 76 | 77 | for(i = 0; i < M; ++i) 78 | for(j = 0 ; j < N; ++j) 79 | { 80 | pthread_join(thread[i][j], NULL); 81 | } 82 | printf("\nthe result: \n"); 83 | for(i = 0; i < M; ++i) 84 | { 85 | for(j = 0; j < N; ++j) 86 | { 87 | printf("%d ", C[i][j]); 88 | } 89 | printf("\n"); 90 | } 91 | 92 | 93 | return 0; 94 | } 95 | -------------------------------------------------------------------------------- /project3/matrix-win32.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int A[30][30]; 6 | int B[30][30]; 7 | int C[30][30]; 8 | 9 | HANDLE hthread[300]; 10 | 11 | int M; 12 | int K; 13 | int N; 14 | 15 | struct v 16 | { 17 | int vi; 18 | int vj; 19 | }; 20 | 21 | DWORD WINAPI ThreadProc(LPVOID lpParameter)//创建线程时传递给新线程一个指针参数 22 | { 23 | struct v *hCur = (struct v*)lpParameter; 24 | int x = hCur -> vi; 25 | int y = hCur -> vj; 26 | 27 | 28 | int res = 0, i; 29 | for(i = 0; i < K; ++i) 30 | res += A[x][i] * B[i][y]; 31 | C[x][y] = res; 32 | 33 | return 0; 34 | } 35 | 36 | int main() 37 | { 38 | int i, j; 39 | printf("input M K N:\n"); 40 | scanf("%d %d %d", &M, &K, &N); 41 | printf("input matrix1:\n"); 42 | for(i = 0; i < M; ++i) 43 | { 44 | for(j = 0; j < K; ++j) 45 | scanf("%d", &A[i][j]); 46 | } 47 | 48 | printf("\ninput matrix2:\n"); 49 | for(i = 0; i < K; ++i) 50 | { 51 | for(j = 0 ; j < N; ++j) 52 | scanf("%d", &B[i][j]); 53 | } 54 | 55 | for(i = 0; i < M; ++i) 56 | { 57 | for(j = 0; j < N; ++j) 58 | { 59 | struct v*data = (struct v*)malloc(sizeof(struct v)); 60 | data -> vi = i; 61 | data -> vj = j; 62 | hthread[i * N + j] = CreateThread(NULL, 0, ThreadProc, data, 0, NULL); 63 | } 64 | } 65 | 66 | WaitForMultipleObjects(M*N, hthread, TRUE, INFINITE);//等待线程结束 67 | 68 | for(i = 0; i < M * N; ++i) 69 | { 70 | CloseHandle(hthread[i]); 71 | } 72 | printf("\nthe result:\n"); 73 | 74 | for(i = 0; i < M; ++i) 75 | { 76 | for(j = 0; j < N; ++j) 77 | printf("%d ", C[i][j]); 78 | printf("\n"); 79 | } 80 | return 0; 81 | } 82 | -------------------------------------------------------------------------------- /project3/project3 report.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chasingegg/SJTU_OS-project/8c91d99ba80a4da759e48caed767203efab655a1/project3/project3 report.pdf -------------------------------------------------------------------------------- /project4/producer_and_consumer.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | typedef int buffer_item; 8 | #define BUFFER_SIZE 5 9 | #define NUM 100 //maximum num of producer_threads or consumer_threads 10 | buffer_item buffer[BUFFER_SIZE]; //the shared buffer 11 | 12 | sem_t empty, full; 13 | sem_t mutex; 14 | int in = 0, out = 0; //in = first empty buffer, out = first full buffer 15 | int pOrder[NUM], cOrder[NUM]; //every producer and consumer has an id(from 1 to NUM) 16 | 17 | int counter1 =0; 18 | int counter2 = 0; 19 | 20 | void *producer(void *param) 21 | { 22 | buffer_item ran; 23 | int id = *(int *) param; 24 | 25 | while(1) 26 | { 27 | sleep(rand() % 5); 28 | ran = rand() % 1000; 29 | sem_wait(&empty); 30 | sem_wait(&mutex); 31 | 32 | buffer[in] = ran; 33 | ++counter1; 34 | 35 | printf("producer %d produced %d\tto buffer[%d] %d\n", id, ran, in, counter1); 36 | 37 | in = (in + 1) % BUFFER_SIZE; 38 | sem_post(&mutex); 39 | sem_post(&full); 40 | 41 | } 42 | return NULL; 43 | 44 | } 45 | 46 | void *consumer(void *param) 47 | { 48 | buffer_item ran; 49 | int id = *(int *) param; 50 | 51 | while(1) 52 | { 53 | sleep(rand() % 5); 54 | 55 | sem_wait(&full); 56 | sem_wait(&mutex); 57 | ran = buffer[out]; 58 | 59 | ++counter2; 60 | 61 | printf("consumer %d consumed %d\tin buffer[%d] %d\n", id, ran, out, counter2); 62 | 63 | out = (out + 1) % BUFFER_SIZE; 64 | sem_post(&mutex); 65 | sem_post(&empty); 66 | 67 | } 68 | return NULL; 69 | 70 | } 71 | 72 | void callError() 73 | { 74 | perror("create thread error!\n"); 75 | exit(0); 76 | } 77 | 78 | int main(int argc, char *argv[]) 79 | { 80 | int pNum, cNum, sleepTime, i, ret; 81 | pthread_t p_thread[NUM]; 82 | pthread_t c_thread[NUM]; 83 | 84 | //input error 85 | if(argc != 4) 86 | { 87 | printf("error input!\n"); 88 | exit(-1); 89 | } 90 | 91 | //read from the command line 92 | sleepTime = atoi(argv[1]); 93 | pNum = atoi(argv[2]); 94 | cNum = atoi(argv[3]); 95 | 96 | //semaphore initialization 97 | sem_init(&empty, 0, BUFFER_SIZE); 98 | sem_init(&full, 0, 0); 99 | sem_init(&mutex, 0, 1); 100 | 101 | 102 | //create pthreads 103 | for(i = 0; i < pNum; ++i) 104 | { 105 | pOrder[i] = 1 + i; 106 | ret = pthread_create(&p_thread[i], NULL, producer, &pOrder[i]); 107 | if(ret != 0) 108 | callError(); 109 | } 110 | for(i = 0; i < cNum; ++i) 111 | { 112 | cOrder[i] = 1 + i; 113 | ret = pthread_create(&c_thread[i], NULL, consumer, &cOrder[i]); 114 | if(ret != 0) 115 | callError(); 116 | } 117 | 118 | sleep(sleepTime); 119 | return 0; 120 | 121 | } 122 | -------------------------------------------------------------------------------- /project4/project4 report.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chasingegg/SJTU_OS-project/8c91d99ba80a4da759e48caed767203efab655a1/project4/project4 report.pdf --------------------------------------------------------------------------------