Skip to content

Instantly share code, notes, and snippets.

@hotpxl
Created April 23, 2014 14:30
Show Gist options
  • Save hotpxl/11217511 to your computer and use it in GitHub Desktop.
Save hotpxl/11217511 to your computer and use it in GitHub Desktop.
Monitor solution for Chapter 2 Exercise 1 Extension 1 from 操作系统之PV金典
#include <stdio.h>
#include <proc.h>
#include <sem.h>
#include <monitor.h>
#define N 3
#define M 4
#define TIMES 10
int numberA;
int numberB;
// Using monitor
struct proc_struct* procMonitor[2];
monitor_t mt;
int usingMonitor(void* arg) {
int i = (int) arg;
int iter = 0;
cprintf("I am No.%d usingMonitor\n",i);
while (iter++ < TIMES) {
if (!i) {
down(&(mt.mutex));
if (N <= numberA - numberB) {
cond_wait(&(mt.cv[0]));
}
++numberA;
cprintf("Put A in depot. Now A = %d, B = %d, A - B = %d\n", numberA, numberB, numberA - numberB);
cond_signal(&(mt.cv[1]));
if(0 < mt.next_count) {
up(&(mt.next));
} else {
up(&(mt.mutex));
}
do_sleep(200);
} else {
down(&(mt.mutex));
if (numberA - numberB <= -M) {
cond_wait(&(mt.cv[1]));
}
++numberB;
cprintf("Put B in depot. Now A = %d, B = %d, A - B = %d\n", numberA, numberB, numberA - numberB);
cond_signal(&(mt.cv[0]));
if(0 < mt.next_count) {
up(&(mt.next));
} else {
up(&(mt.mutex));
}
do_sleep(10);
}
}
cprintf("No.%d usingMonitor quit\n",i);
return 0;
}
void check_sync() {
int i;
// Use condition variable
monitor_init(&mt, 2);
numberA = 0;
numberB = 0;
for (i = 0; i < 2; ++i) {
int pid = kernel_thread(usingMonitor, (void* )i, 0);
if (pid <= 0) {
panic("Create No.%d usingMonitor failed.\n");
}
procMonitor[i] = find_proc(pid);
set_proc_name(procMonitor[i], "usingMonitor");
}
}
/*
* I am No.1 usingMonitor
* Put B in depot. Now A = 0, B = 1, A - B = -1
* cond_signal begin: cvp c03a7278, cvp->count 0, cvp->owner->next_count 0
* cond_signal end: cvp c03a7278, cvp->count 0, cvp->owner->next_count 0
* I am No.0 usingMonitor
* Put A in depot. Now A = 1, B = 1, A - B = 0
* cond_signal begin: cvp c03a728c, cvp->count 0, cvp->owner->next_count 0
* cond_signal end: cvp c03a728c, cvp->count 0, cvp->owner->next_count 0
* Put B in depot. Now A = 1, B = 2, A - B = -1
* cond_signal begin: cvp c03a7278, cvp->count 0, cvp->owner->next_count 0
* cond_signal end: cvp c03a7278, cvp->count 0, cvp->owner->next_count 0
* Put B in depot. Now A = 1, B = 3, A - B = -2
* cond_signal begin: cvp c03a7278, cvp->count 0, cvp->owner->next_count 0
* cond_signal end: cvp c03a7278, cvp->count 0, cvp->owner->next_count 0
* Put B in depot. Now A = 1, B = 4, A - B = -3
* cond_signal begin: cvp c03a7278, cvp->count 0, cvp->owner->next_count 0
* cond_signal end: cvp c03a7278, cvp->count 0, cvp->owner->next_count 0
* Put B in depot. Now A = 1, B = 5, A - B = -4
* cond_signal begin: cvp c03a7278, cvp->count 0, cvp->owner->next_count 0
* cond_signal end: cvp c03a7278, cvp->count 0, cvp->owner->next_count 0
* cond_wait begin: cvp c03a728c, cvp->count 0, cvp->owner->next_count 0
* Put A in depot. Now A = 2, B = 5, A - B = -3
* cond_signal begin: cvp c03a728c, cvp->count 1, cvp->owner->next_count 0
* cond_wait end: cvp c03a728c, cvp->count 0, cvp->owner->next_count 1
* Put B in depot. Now A = 2, B = 6, A - B = -4
* cond_signal begin: cvp c03a7278, cvp->count 0, cvp->owner->next_count 1
* cond_signal end: cvp c03a7278, cvp->count 0, cvp->owner->next_count 1
* cond_signal end: cvp c03a728c, cvp->count 0, cvp->owner->next_count 0
* cond_wait begin: cvp c03a728c, cvp->count 0, cvp->owner->next_count 0
* Put A in depot. Now A = 3, B = 6, A - B = -3
* cond_signal begin: cvp c03a728c, cvp->count 1, cvp->owner->next_count 0
* cond_wait end: cvp c03a728c, cvp->count 0, cvp->owner->next_count 1
* Put B in depot. Now A = 3, B = 7, A - B = -4
* cond_signal begin: cvp c03a7278, cvp->count 0, cvp->owner->next_count 1
* cond_signal end: cvp c03a7278, cvp->count 0, cvp->owner->next_count 1
* cond_signal end: cvp c03a728c, cvp->count 0, cvp->owner->next_count 0
* cond_wait begin: cvp c03a728c, cvp->count 0, cvp->owner->next_count 0
* Put A in depot. Now A = 4, B = 7, A - B = -3
* cond_signal begin: cvp c03a728c, cvp->count 1, cvp->owner->next_count 0
* cond_wait end: cvp c03a728c, cvp->count 0, cvp->owner->next_count 1
* Put B in depot. Now A = 4, B = 8, A - B = -4
* cond_signal begin: cvp c03a7278, cvp->count 0, cvp->owner->next_count 1
* cond_signal end: cvp c03a7278, cvp->count 0, cvp->owner->next_count 1
* cond_signal end: cvp c03a728c, cvp->count 0, cvp->owner->next_count 0
* cond_wait begin: cvp c03a728c, cvp->count 0, cvp->owner->next_count 0
* Put A in depot. Now A = 5, B = 8, A - B = -3
* cond_signal begin: cvp c03a728c, cvp->count 1, cvp->owner->next_count 0
* cond_wait end: cvp c03a728c, cvp->count 0, cvp->owner->next_count 1
* Put B in depot. Now A = 5, B = 9, A - B = -4
* cond_signal begin: cvp c03a7278, cvp->count 0, cvp->owner->next_count 1
* cond_signal end: cvp c03a7278, cvp->count 0, cvp->owner->next_count 1
* cond_signal end: cvp c03a728c, cvp->count 0, cvp->owner->next_count 0
* cond_wait begin: cvp c03a728c, cvp->count 0, cvp->owner->next_count 0
* Put A in depot. Now A = 6, B = 9, A - B = -3
* cond_signal begin: cvp c03a728c, cvp->count 1, cvp->owner->next_count 0
* cond_wait end: cvp c03a728c, cvp->count 0, cvp->owner->next_count 1
* Put B in depot. Now A = 6, B = 10, A - B = -4
* cond_signal begin: cvp c03a7278, cvp->count 0, cvp->owner->next_count 1
* cond_signal end: cvp c03a7278, cvp->count 0, cvp->owner->next_count 1
* cond_signal end: cvp c03a728c, cvp->count 0, cvp->owner->next_count 0
* No.1 usingMonitor quit
* Put A in depot. Now A = 7, B = 10, A - B = -3
* cond_signal begin: cvp c03a728c, cvp->count 0, cvp->owner->next_count 0
* cond_signal end: cvp c03a728c, cvp->count 0, cvp->owner->next_count 0
* Put A in depot. Now A = 8, B = 10, A - B = -2
* cond_signal begin: cvp c03a728c, cvp->count 0, cvp->owner->next_count 0
* cond_signal end: cvp c03a728c, cvp->count 0, cvp->owner->next_count 0
* Put A in depot. Now A = 9, B = 10, A - B = -1
* cond_signal begin: cvp c03a728c, cvp->count 0, cvp->owner->next_count 0
* cond_signal end: cvp c03a728c, cvp->count 0, cvp->owner->next_count 0
* Put A in depot. Now A = 10, B = 10, A - B = 0
* cond_signal begin: cvp c03a728c, cvp->count 0, cvp->owner->next_count 0
* cond_signal end: cvp c03a728c, cvp->count 0, cvp->owner->next_count 0
* No.0 usingMonitor quit
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment