/**
 * Removes the TCB #pid from the queue #chid.
 * Hint: there are many cases in this function.
 */
void tqueue_remove(unsigned int chid, unsigned int pid)
{
  // TODO
  // case 1: not found
  	unsigned int head = tqueue_get_head(chid);
	unsigned int tail = tqueue_get_tail(chid);
  	if (head == NUM_IDS && tail == NUM_IDS)
	{
		return;	 
	}
  // case 2: only one element
    if (head == tail)
	{
		if (head == pid) 
		{
			// find pid
			tqueue_set_tail(chid, NUM_IDS);
			tqueue_set_head(chid, NUM_IDS);
		}
		tcb_set_next(pid, NUM_IDS);
		tcb_set_prev(pid, NUM_IDS);
		return;
	}
	while(head != NUM_IDS)
	{
		if (head == pid)
		{
			if (head == tqueue_get_head(chid))
			{
				unsigned new_head = tcb_get_next(head);
				tqueue_set_head(chid, new_head);
				tcb_set_prev(new_head, NUM_IDS);
				
			}
			else 
			{
				unsigned int pre = tcb_get_prev(head);
				unsigned int next = tcb_get_next(head);
				tcb_set_next(pre, next);
				if (next != NUM_IDS)
				{
					tcb_set_prev(next, pre);
				}
				else 
				{
					tqueue_set_tail(chid, pre);
				}
			}
			tcb_set_next(pid, NUM_IDS);
			tcb_set_prev(pid, NUM_IDS);
			return;
		}
		head = tcb_get_next(head);
	}
	
 // case 3: only 2 element
  // case 4: in the middle
}
Example #2
0
int PTCBInit_test1()
{
  unsigned int i;
  for (i = 1; i < NUM_IDS; i ++) {
    if (tcb_get_state(i) != TSTATE_DEAD || tcb_get_prev(i) != NUM_IDS || tcb_get_next(i) != NUM_IDS) {
      dprintf("test 1 failed.\n");
      return 1;
    }
  }
  dprintf("test 1 passed.\n");
  return 0;
}