Esempio n. 1
0
File: q.c Progetto: iamwjlee/My
int fifo_test(void)
{
	int size= 10;
	u8 buf;
	qb_t *p = malloc(sizeof(qb_t));
	p->buffer = (u8 *)malloc(size);
	
	p->wptr=p->rptr=p->buffer;	
	p->end = (msg_t *)p->buffer + size;

	print("fifo test");
	buf=1;
	q_write(p,&buf,1);
	buf=2;
	q_write(p,&buf,1);

	if(!q_read(p,&buf,1))
		print("read[%d] qsize[%d]",buf,q_size(p));
	if(!q_read(p,&buf,1))
		print("read[%d] qsize[%d]",buf,q_size(p));
	if(!q_read(p,&buf,1))
		print("read[%d] qsize[%d]",buf,q_size(p));

	//q_free(p->buffer);
	q_free(p);

	return 0;
}
Esempio n. 2
0
} END_TEST

START_TEST(check_queue_size) {
  Job job1, job2;
  Queue *q = new_queue();
  assert_equal(0, q_size(q));
  q_enqueue(q, &job1);
  assert_equal(1, q_size(q));
  q_enqueue(q, &job2);
  assert_equal(2, q_size(q));
  free_queue(q);
} END_TEST
Esempio n. 3
0
static void test_q_dequeue(void)
{
    Queue* q = q_new();
    int first;
    int status = q_enqueue(q, &first);
    int second;
    status = q_enqueue(q, &second);
    CU_ASSERT_PTR_EQUAL(q_dequeue(q), &first);
    CU_ASSERT_EQUAL(q_size(q), 1);
    CU_ASSERT_PTR_EQUAL(q_dequeue(q), &second);
    CU_ASSERT_EQUAL(q_size(q), 0);
    q_free(q);
}
Esempio n. 4
0
static char *test_q_size() {
  packet *q = q_create();
  packet p = pkt_create(ERROR, 0, 0, 0, 0);
  mu_assert("FAIL: test_q_size [1]", q_size(0, 0, q, N) == 0);
  for (int i = 0; i < 20; i++) {
    q_write(p, 0, q, N);
  }
  mu_assert("FAIL: test_q_size [2]", q_size(0, 0, q, N) == 16);
  q_read(&p, 0, q, N);
  mu_assert("FAIL: test_q_size [3]", q_size(0, 0, q, N) == 15);
  q_destroy(q);
  return NULL;
}
static bool clean_target_met(struct smq_policy *mq, bool critical)
{
	if (critical) {
		/*
		 * Cache entries may not be populated.  So we're cannot rely on the
		 * size of the clean queue.
		 */
		unsigned nr_clean = from_cblock(mq->cache_size) - q_size(&mq->dirty);
		unsigned target = from_cblock(mq->cache_size) * CLEAN_TARGET_CRITICAL / 100u;

		return nr_clean >= target;
	} else
		return !q_size(&mq->dirty);
}
Esempio n. 6
0
void *print_stats(void *arg) {
	while(running) {
		printf("Queue: %i\n",q_size());
		sleep(5);
	}
	return 0;
}
Esempio n. 7
0
static void test_q_new(void)
{
    Queue* q = q_new();
    CU_ASSERT_PTR_NOT_NULL_FATAL(q);
    CU_ASSERT_PTR_NULL(q_dequeue(q));
    CU_ASSERT_EQUAL(q_size(q), 0);
    q_free(q);
}
Esempio n. 8
0
static char *test_q_write() {
  packet *q = q_create();
  packet p = pkt_create(DATA, 1, 2, 3, 4);
  q_write(p, 0, q, N);
  mu_assert("FAIL: test_q_write [1]", !q_is_empty(0, 0, q, N));
  q_write(p, 0, q, N);
  mu_assert("FAIL: test_q_write [2]", q_size(0, 0, q, N) == 2);
  for (int i = 0; i < 14; i++) {
    q_write(p, 0, q, N);
  }
  mu_assert("FAIL: test_q_write [3]", !q_write(p, 0, q, N));
  q_destroy(q);
  return NULL;
}
/*-----------------------------------------------------------------------------
 * Receive Queue Functions
 *  - uint8_t q_pop()
 *  - void q_push(char)
 *  - uint8_t q_size()
 /----------------------------------------------------------------------------*/
static uint8_t q_pop() {
   uint8_t val;

   // Make sure there are elements in the queue
   if (q_size() <= 0)
      return 0;

   val = rqueue[rqueue_front];

   if (rqueue_front < (BS_QUEUE_MAX - 1))
      rqueue_front++;
   else
      rqueue_front = 0;

   return val;
}
Esempio n. 10
0
/* If the queue is full, we will wrap around and write over the data.
 *
 * @param item the item to insert into the queue
 */
static void q_push(uint8_t item) {

   rqueue[rqueue_back] = item;

   if (rqueue_back < (BS_QUEUE_MAX - 1))
      rqueue_back++;
   else
      rqueue_back = 0;

   // check if we overflowed
   if (q_size() == 0) {
      // discard the item in the front of the queue for now
      // TODO: Need to handle this by doubling our queue size
      if (rqueue_front < (BS_QUEUE_MAX - 1))
         rqueue_front++;
      else
         rqueue_front = 0;
   }
}
Esempio n. 11
0
File: q.c Progetto: iamwjlee/My
int q_read(qb_t *f, void *buf, int buf_size)
{
 //   return q_generic_read(f, buf_size, NULL, buf);
   int size = q_size(f);

    if (size < buf_size)
        return 1;
    do {
        int len = FFMIN(f->end - f->rptr, buf_size);
        memcpy(buf, f->rptr, len);
        buf = (u8*)buf + len;
        //q_drain(f, len);
	    f->rptr += len;
		if (f->rptr >= f->end)
			f->rptr -= f->end - f->buffer;



        buf_size -= len;
    } while (buf_size > 0);
    return 0;


}
Esempio n. 12
0
int main(int argc, char *argv[])
{
  size_t s = sizeof(int);
  int foo;
  {
    /*#Test_0*/
    queue q; q_init(&q, s);
    test(q_size(&q) == 0 && q_is_empty(&q));
  }
  {
    /*#Test_1*/
    queue q; q_init(&q, s);
    foo = 1; q_push(&q, &foo);
    test(q_size(&q) == 1 && !q_is_empty(&q) && *(int *)q_front(&q) == 1);
  }
  {
    /*#Test_2*/
    queue q; q_init(&q, s);
    foo = 2; q_push(&q, &foo);
    foo = 1; q_push(&q, &foo);
    test(
        q_size(&q) == 2 && 
        *(int *)q_front(&q) == 1 && 
        *(int *)q_back(&q) == 2);
  }
  {
    /*#Test_3*/
    queue q; q_init(&q, s);

    foo = 3; q_push(&q, &foo);
    foo = 2; q_push(&q, &foo);
    foo = 1; q_push(&q, &foo);

    q_pop(&q);
    BOOL bar = *(int *)q_front(&q) == 1 && *(int *)q_back(&q) == 2;

    q_pop(&q);
    bar &= *(int *)q_front(&q) == 1 && *(int *)q_back(&q) == 1;

    q_pop(&q);
    test(
        bar &&
        q_is_empty(&q));
  }
  {
    /*#Test_4*/
    queue q; q_init(&q, s);
    BOOL bar = TRUE;

    for (uint i = 0; i < N; ++i)
    {
      foo = i; q_push(&q, &foo);
    }

    for (uint i = 0; i < N; ++i)
    {
      bar &= *(int *)q_back(&q) == i;
      if (!bar) break;
      q_pop(&q);
    }

    test(
        bar&
        q_is_empty(&q));
  }

  return EXIT_SUCCESS;
}
int main(int argc, char *argv[]){
  //Init queues
  q_dispatch = (queue*)malloc(sizeof(queue*));
  q_dispatch->head=NULL;
  q_dispatch->tail=NULL;

  q_real = (queue*)malloc(sizeof(queue*));
  q_real->head=NULL;
  q_real->tail=NULL;

  q_1 = (queue*)malloc(sizeof(queue*));
  q_1->head=NULL;
  q_1->tail=NULL;

  q_2 = (queue*)malloc(sizeof(queue*));
  q_2->head=NULL;
  q_2->tail=NULL;

  q_3 = (queue*)malloc(sizeof(queue*));
  q_3->head=NULL;
  q_3->tail=NULL;


  //Init resources
  for(int i=0;i<PRINTERS;i++){res_avail.printers[i]=0;}
  for(int i=0;i<SCANNERS;i++){res_avail.scanners[i]=0;}
  for(int i=0;i<MODEMS;i++){res_avail.modems[i]=0;}
  for(int i=0;i<DRIVES;i++){res_avail.drives[i]=0;}
  for(int i=0;i<MEMORY;i++){res_avail.memory[i]=0;}

  // Load the dispatchlist adds to queue as well
  load_dispatch(argv[1]);

  int tick =0;
  //Start dispatching
  while(true){
    //check dispatch list for processes that have arrived
    node* dispatch_node = q_dispatch->head;
    while(dispatch_node){
      if(dispatch_node->process.arrival_time==tick){
        //Push process to appropriate queue
        switch(dispatch_node->process.priority){
          case 0:
          push(q_real,dispatch_node->process);
          break;
          case 1:
          push(q_1,dispatch_node->process);
          break;
          case 2:
          push(q_2,dispatch_node->process);
          break;
          case 3:
          push(q_3,dispatch_node->process);
          break;
        }
      }
      dispatch_node=dispatch_node->next;
    }

    //Run processes
    proc* get_proc;
    bool find_proc = true;
    int count;

    //Real Time Queue
    count = q_size(q_real);
    //loop through queue if there is something in queue and no job has been found
    while(find_proc&&count>0){
      get_proc = pop(q_real);
      //check if process can be allocated
      if(alloc_resources(&res_avail,get_proc)){
        //run
        run_proc(get_proc);
        find_proc=false;
      }else{
        push(q_real,*get_proc);
        count--;
      }
    }

    //1st Queue
    count = q_size(q_1);
    //loop through queue if there is something in queue and no job has been found
    while(find_proc&&count>1){
      get_proc = pop(q_1);
      //Check if process can be allocated
      if(alloc_resources(&res_avail,get_proc)){
        //run if not completed during run push to next queue
        if(run_proc(get_proc))
        push(q_2,*get_proc);
        find_proc=false;
      }else{
        //if resources not available push back onto queue
        push(q_1,*get_proc);
        count--;
      }
    }

    //2nd Queue
    count = q_size(q_2);
    //loop through queue if there is something in queue and no job has been found
    while(find_proc&&count>0){
      get_proc = pop(q_2);
      //if process is a new process
      if(!get_proc->suspended){
        //check if it can be allocated
        if(alloc_resources(&res_avail,get_proc)){
          //run if not completed during run push to next queue
          if(run_proc(get_proc))
          push(q_3,*get_proc);
          find_proc=false;
        }else{
          //if resources not available push back onto queue
          push(q_2,*get_proc);
          count--;
        }
      }else{
        //if processes has already been allocated just run it some more
        if(run_proc(get_proc))
        push(q_3,*get_proc);
        find_proc=false;
      }
    }

    //3rd Qeue
    count = q_size(q_3);
    while(find_proc&&count>0){
      get_proc = pop(q_3);
      if(!get_proc->suspended){
        if(alloc_resources(&res_avail,get_proc)){
          if(run_proc(get_proc))
          push(q_3,*get_proc);
          find_proc=false;
        }else{
          push(q_3,*get_proc);
          count--;
        }
      }else{
        if(run_proc(get_proc))
        push(q_3,*get_proc);
        find_proc=false;
      }
    }

    //if no process was able to be run either system is hung or there are no jobs left
    if(find_proc){
      printf("TERMINATING: Could not find anymore processes\n");
      break;
    }

    //increase tick count on dispatcher
    tick++;
  }

  return EXIT_SUCCESS;
}