Question 3 -- 10 points

Behold the following monitor/condition variable calls: Implement the following general semaphore calls (also provide a typedef for Gsem_t: Don't worry about your #includes.

Answer

This is so straightforward it needs no explanation:
typedef struct {
  Mon_t *m;
  Cv_t *c;
  int val;
} Gsem_t;

Gsem_t *gsem_create(int i)
{
  Gsem_t *g;
  if (i < 0) return NULL;
  g = (Gsem_t *) malloc(sizeof(Gsem_t));

  g->m = mon_create();
  g->c = cv_create();
  g->val = i;
  return g;
}

void gsem_P(Gsem_t *g)
{
  mon_enter(g->m);
  g->val--;
  if (g->val < 0) cv_wait(g->c, g->m);
  mon_exit(g->m);
}


void gsem_V(Gsem_t *g)
{
  mon_enter(g->m);
  g->val++;
  if (g->val <= 0) cv_signal(g->c);
  mon_exit(g->m);
}

Grading

This was a very simple problem. Basically, you started at 10 points, and lost points for the following:

If your answer didn't really make any sense, but you had the struct set up right, I gave you two points.

Histogram