ソースを参照

完成实验7

x86-32
423A35C7 6ヶ月前
コミット
cd00bb2f50
3個のファイルの変更81行の追加2行の削除
  1. +3
    -0
      labcodes_answer/lab6_result/kern/process/proc.c
  2. +2
    -2
      labcodes_answer/lab7_result/kern/process/proc.c
  3. +76
    -0
      labcodes_answer/lab7_result/kern/sync/read_write_sync.c

+ 3
- 0
labcodes_answer/lab6_result/kern/process/proc.c ファイルの表示

@ -845,6 +845,9 @@ init_main(void *arg) {
int i = 0;
for (; i < 10; i++) {
int pid = kernel_thread(my_test_user_main, (void *)i, 0);
if (pid <= 0) {
panic("create my_test_user_main failed.\n");
}
struct proc_strucht *proc = find_proc(pid);
}

+ 2
- 2
labcodes_answer/lab7_result/kern/process/proc.c ファイルの表示

@ -844,8 +844,8 @@ init_main(void *arg) {
if (pid <= 0) {
panic("create user_main failed.\n");
}
extern void check_sync(void);
check_sync(); // check philosopher sync problem
extern void read_write_sync(void);
read_write_sync(); // check philosopher sync problem
while (do_wait(0, NULL) == 0) {
schedule();

+ 76
- 0
labcodes_answer/lab7_result/kern/sync/read_write_sync.c ファイルの表示

@ -0,0 +1,76 @@
#include <stdio.h>
#include <proc.h>
#include <sem.h>
#include <monitor.h>
#include <assert.h>
#define READER_NUM 100 // 读者数量,取值应大于1,小于int范围
#define WRITER_NUM 100 // 写者数量,取值应大于1,小于int范围
// #define BUFFER_NUM 10 // 1int范围
#define SLEEP_TIME 10
// down和upP和V acquire和release吧
#define acquire down
#define release up
semaphore_t mutex; /* 临界区互斥 */
semaphore_t synchronization; //
int read_pos_write_neg = 0; //
int buffer;
int reader(int index){
lab6_set_priority(rand() % 100);
int result;
acquire(&mutex);
read_pos_write_neg++;
if (read_pos_write_neg == 1)
acquire(&synchronization);
release(&mutex);
do_sleep(rand() % SLEEP_TIME); //
result = buffer;
acquire(&mutex);
read_pos_write_neg--;
assert(read_pos_write_neg >=0);
if (read_pos_write_neg == 0) {
release(&synchronization);
}
release(&mutex);
cprintf("reader %2d read %2d\n", index, result);
return result;
}
void writer(int index) {
lab6_set_priority(rand() % 100);
int write_data = rand() % 100;
acquire(&synchronization);
do_sleep(rand() % SLEEP_TIME); //
buffer = write_data;
release(&synchronization);
cprintf("writer %2d writed %2d\n", index, write_data);
}
void read_write_sync() {
sem_init(&mutex, 1);
sem_init(&synchronization, 1);
int reader_num = 0, writer_num = 0;
while (reader_num < READER_NUM || writer_num < WRITER_NUM) {
if (rand() % 2) {
if (reader_num < READER_NUM) {
int pid = kernel_thread(reader, (void *)reader_num, 0);
if (pid <= 0) {
panic("create No.%d reader failed.\n");
}
reader_num++;
}
} else {
if (writer_num < WRITER_NUM) {
int pid = kernel_thread(writer, (void *)writer_num, 0);
if (pid <= 0) {
panic("create No.%d writer failed.\n");
}
writer_num++;
}
}
do_sleep(rand() % SLEEP_TIME); //
}
}

読み込み中…
キャンセル
保存