Exemplo n.º 1
0
// Consumes items
void consumer() {
    int item;
    int iempty;
    int ifull;
    int i = 0;

    while(i < 500) {
        sem_wait(full_sem);
        sem_wait(lock_sem);
        sem_getvalue(full_sem, &ifull);
        item = buffer[ifull];
        consume_item(item);
        sem_post(lock_sem);
        sem_post(empty_sem);

        // Print message if buffer is empty
        sem_getvalue(empty_sem, &iempty);
        if(iempty+1 == N) {
            printf("Buffer empty\n");
        }


        i++;
    }
}
Exemplo n.º 2
0
int main(int argc, const char *argv[])
{
    if(argc!=2)
    {
        perror("\nIncorrect Usage!\n\tUse: Consumer [delay]\n\n\t\tWhere [delay] is sleeping time in secs.");
        exit(EXIT_FAILURE);
    }
    int *shared_buffer = create_shared_mem_buffer();
    int semid = create_semaphore_set();
    
    char item;
    int delay=atoi(argv[1]);
    printf("\n--Configuration-------------------------------\n");
    printf("Delay: %d\n",delay);
    while(1) {
        semop(semid, &downFull, 1);
        semop(semid, &downMutex, 1);
        item = remove_item(semid, shared_buffer);
        //debug_buffer(shared_buffer);
        semop(semid, &upMutex, 1);
        semop(semid, &upEmpty, 1);
        consume_item(item);
        sleep(delay);
    } 
    
    return EXIT_SUCCESS;
}
Exemplo n.º 3
0
/********************************************************************************
 *  Description:
 *   Input Args:
 *  Output Args:
 * Return Value:
 ********************************************************************************/
int main (int argc, char **argv)
{
    int i, len;
    struct circ_buf        tx_ring; 
    char   data[LEN];
    char   buf[LEN];

    memset(&tx_ring, 0, sizeof(struct circ_buf)); 
    tx_ring.buf = malloc(CIRC_BUF_SIZE); 
    if( NULL == tx_ring.buf )
    {
        printf("Allocate Ring buffer failure.\n");
        return -1;
    }

    memset(data, 0, sizeof(data));
    /* Prepare for the data */
    for(i=0; i<sizeof(data); i++)
    {
        data[i] = 30+i;
    }

    printf("CIRC_SPACE: %d\n", CIRC_SPACE(tx_ring.head, tx_ring.tail, CIRC_BUF_SIZE));
    printf("CIRC_SPACE_TO_END: %d\n", CIRC_SPACE_TO_END(tx_ring.head, tx_ring.tail, CIRC_BUF_SIZE));
    printf("CIRC_CNT: %d\n", CIRC_CNT(tx_ring.head, tx_ring.tail, CIRC_BUF_SIZE));
    printf("CIRC_CNT_TO_END: %d\n", CIRC_CNT_TO_END(tx_ring.head, tx_ring.tail, CIRC_BUF_SIZE));
    while(1)
    {
       produce_item(&tx_ring, data, sizeof(data));
       len = consume_item(&tx_ring, buf, sizeof(buf) );
       sleep(1);
    }

    return 0;
} /* ----- End of main() ----- */
Exemplo n.º 4
0
int
v_sub_item(struct command *c)
{

  consume_item(c->who, c->a, c->b);
  return TRUE;
}
Exemplo n.º 5
0
void consumer() {
    int item;

    while(true) {
        item = buffer[--count];
        consume_item(item);
        if(count == 0){
            break;
        }
    }
}
void consumer(void)
{
	int item;

	while(TRUE)
	{
		if (count == 0)
			return;
		item = remove_item();
		count = count - 1;
		consume_item(item);
	}
}
Exemplo n.º 7
0
Arquivo: 2-14.c Projeto: pidodo/myHub
void consumer(void)
{
  int item;

  while (TRUE){             /* infinite loop */
    down(&full);            /* decrement full count */
    down(&mutex);           /* enter critical region */
    item = remove_item();   /* take item from buffer */
    up(&mutex);             /* leave critical region */
    up(&empty);             /* increment count of empty slots */
    consume_item(item);     /* do something with the item */
  }
}
void consumer(void){
    int item;

    while(TRUE){
        if(count == 0){
            sleep();
        }
        item = remove_item();
        count--;
        if(count == N-1){
            wakeup(producer);
        }
        consume_item(item);
    }
}
Exemplo n.º 9
0
void consumer(void* ptr) {
	int item;
	
	while (TRUE) {
		if (count == 0) {	// if buffer is empty, return to producer
			return;
		}
		item = remove_item();	// remove item from buffer
		count = count - 1;			// decrement count of items in buffer
		if (count == N - 1) {		// return to producer if buffer size == 99
			return;
		}
		consume_item(item);		// print item
	}
	return;
}
Exemplo n.º 10
0
static int __init consumer_init_module(void) {
	bool q_status=false;
  printk(KERN_ALERT "Consumer loaded with rate:%d\n", rate);
	consumer_wq=alloc_workqueue("cs-wq",WQ_UNBOUND,1);
	if (consumer_wq == NULL) {
		printk(KERN_ERR "Consumer Workqueue couldn't allocated\n");
		return -ENOMEM;
	}
	else{
//  	printk(KERN_ALERT "reading before starting workqueue\n");
		consume_item();
		q_status= queue_delayed_work(consumer_wq,&consumer,HZ/rate);
//	  printk(KERN_ALERT "Workqueue scheduled, status:\t%d\n",q_status);
	}
	printk(KERN_ALERT "Scheduler closed, we are exiting now.\n");
  return 0;
}
Exemplo n.º 11
0
int main()
{
        int fifo, item = 0;

        if ((fifo = open("myfifo", O_RDONLY)) < 0) {
               printf("open: pipe doesn't exist");
                exit(-1);
        }
        while(1) {
                        read(fifo, &item, sizeof(item));
                        printf("consumer: %d\n", item);
                        consume_item(item);
        }
        
        close(fifo);

        return 0;
}
Exemplo n.º 12
0
static 
PT_THREAD(consumer(struct pt *pt))
{
  static int consumed;
  
  PT_BEGIN(pt);
 
  for(consumed = 0; consumed < NUM_ITEMS; ++consumed) {
    
    PT_SEM_WAIT(pt, &empty);
    
    consume_item(get_from_buffer());    
    
    PT_SEM_SIGNAL(pt, &full);
  }
 
  PT_END(pt);
}
void Consumer::work() {
	if(m_mqd == (mqd_t)-1)
		return;

	WorkItem item;
	ssize_t receivedBytes = 0;
	timespec ts = get_expire_time();
	// The reason why we must +1 to the sizeof(WorkItem) is that the mq_receive requires the
	// buffer size must be greater than the msgsize_max
	receivedBytes = mq_timedreceive(m_mqd, (char*)&item, sizeof(WorkItem) + 1, NULL, &ts);
	if(receivedBytes > 0)
		consume_item(item);
	else if(receivedBytes == 0)
		std::cout << "0 length message received" << std::endl;
	else if(errno != ETIMEDOUT) {
		std::cout << "Error is " << strerror(errno) << std::endl;
		std::cout << "Oops, fail to receive message from Consumer " << m_id << ". Bytes = " << receivedBytes << std::endl;
	}
}
void* OUT(void* thread_number) { /*consumer function to write bytes from buffer into a file*/
    int x;
    x = *((int *) thread_number);
    while (TRUE) {
        get_sleep(ts);
        /*nothing to consume*/
        sem_wait(&full); //signals empty slots to fill wait
        sem_wait(&mutex); //sem_wait value is 0
        pthread_mutex_lock(&result);
        data_to_write = consume_item(buffer);
        if (fseek(outfile, buffer[out].offset, SEEK_SET) == -1) {
            fprintf(stderr, "error setting outfile file data:%c position to %u\n", buffer[out].data, (unsigned int) buffer[out].offset);
            exit(-1);
        }
        printf("CONSUMER DATA:%c\n", data_to_write);
        if (fputc(data_to_write , outfile) == EOF) {
            fprintf(stderr, "error writing byte %c at offset:%ld to output file\n", buffer[out].data, buffer[out].offset);
            exit(-2);
        }
        if (fseek(logfile, ftell(logfile), SEEK_SET) == -1) {
            fprintf(stderr, "error setting output file position to \n");
            exit(-3);
        }
        fprintf(logfile, "write_byte\tCT%d\tO%ld\tB%d\t%d\n", x, buffer[out].offset, data_to_write, out);
        printf("write_byte\tCT%d\tO%ld\tB%d\t%d\n", x, buffer[out].offset, data_to_write, out);
        fprintf(logfile, "consumer\tCT%d\tO%ld\tB%d\t%d\n", x, buffer[out].offset, data_to_write, out);
        printf("consumer\tCT%d\tO%ld\tB%d\t%d\n", x, buffer[out].offset, data_to_write, out);
        printf("Counter in Consumer thread %d \n", counter);
        pthread_mutex_unlock(&result);
        sem_post(&mutex);
        sem_post(&empty); //wakes a thread waiting to get in the critical session
        //sem_getvalue(&full, &exit_condtion); //ends sometimes
        printf("Waiting full consumer value:%d\n", data_to_write);
        if (data_to_write < 0) {
            fflush(stdout);
            pthread_exit(NULL);
        }
    }
    fflush(stdout);
    pthread_exit(NULL);
}
Exemplo n.º 15
0
/*
 *  Fri Oct  9 18:19:22 1998 -- Scott Turner
 *
 *  Destroying monster.
 *
 */
int
v_art_destroy(struct command *c)
{
  int item = c->use_skill;
  int where = province(subloc(c->who));
  int num;
  struct item_ent *t;
  int kind;

  assert(rp_item_artifact(item));
  kind = rp_item_artifact(item)->param1;

  if (rp_item_artifact(item)->uses < 1) {
    wout(c->who, "Nothing happens.");
    wout(c->who, "%s vanishes!", box_name(item));
    destroy_unique_item(c->who, item);
    return TRUE;
  };

  log_write(LOG_SPECIAL, "Destroy monster artifact %s used by %s",
            box_code_less(item), box_code_less(player(c->who)));

  wout(c->who, "A golden glow suffuses the province.");
  wout(where, "A golden glow suffuses the province.");

  loop_all_here(where, num) {
    wout(num, "A golden glow suffuses the province.");

    loop_inv(num, t) {
      if (t->item == kind) {
        wout(num, "%s vanished!", box_name_qty(t->item, t->qty));
        consume_item(num, t->item, t->qty);
      }
    }
    next_inv;

    if (subkind(num) == sub_ni && noble_item(num) == kind) {
      kill_char(num, MATES, S_body);
    }
  }
Exemplo n.º 16
0
int
d_breed(struct command *c)
{
  int i1 = c->a;
  int i2 = c->b;
  int offspring;
  int breed_accident = BREED_ACCIDENT;
  int killed = FALSE;

  if (is_real_npc(c->who)) return d_npc_breed(c);

  if (kind(i1) != T_item)
    {
      wout(c->who, "%s is not an item.", c->parse[1]);
      return FALSE;
    }

  if (kind(i2) != T_item)
    {
      wout(c->who, "%s is not an item.", c->parse[2]);
      return FALSE;
    }

  if (has_item(c->who, i1) < 1)
    {
      wout(c->who, "Don't have any %s.", box_code(i1));
      return FALSE;
    }

  if (has_item(c->who, i2) < 1)
    {
      wout(c->who, "Don't have any %s.", box_code(i2));
      return FALSE;
    }

  if (i1 == i2 && has_item(c->who, i1) < 2)
    {
      wout(c->who, "Don't have two %s.", box_code(i1));
      return FALSE;
    }

  /*
   *  A normal union just succeeds.
   *
   */
  if (normal_union(i1, i2)) {
    offspring = find_breed(i1, i2);
    wout(c->who, "Produced %s.", box_name_qty(offspring, 1));
    gen_item(c->who, offspring, 1);
    add_skill_experience(c->who, sk_breed_beasts);
    p_skill(sk_breed_beasts)->use_count++;
    return TRUE;
  };

  /*
   *  A non-normal union is more problematic.
   *
   */
  if (!has_holy_symbol(c->who)) {
    wout(c->who, "A holy symbol is required for that breeding.");
    return FALSE;
  };

  /*
   *  Wed Feb 23 12:01:17 2000 -- Scott Turner
   *
   *  Have to directly encode the piety required here so it won't
   *  be charged automatically in use.c
   *
   */
  if (!use_piety(c->who, 3)) {
    wout(c->who, "You don't have the piety required to use that prayer.");
    return FALSE;
  };

  p_skill(sk_breed_beasts)->use_count++;

  /*
   *  The following isn't quite right -- there is no chance of
   *  killing both the breeders if they are of the same type.
   */

  offspring = find_breed(i1, i2);

  if (offspring == item_dragon)
    breed_accident = 13;

  if (i1 == i2)
    breed_accident *= 2;

  if (i1 && rnd(1,100) <= breed_accident)
    {
      wout(c->who, "%s was killed in the breeding attempt.",
	   cap(box_name_qty(i1, 1)));
      consume_item(c->who, i1, 1);
      killed = TRUE;
    }

  if (i2 && rnd(1,100) <= breed_accident && i1 != i2)
    {
      wout(c->who, "%s was killed in the breeding attempt.",
	   cap(box_name_qty(i2, 1)));
      consume_item(c->who, i2, 1);
      killed = TRUE;
    }

  if (offspring == 0 || rnd(1,4) == 1)
    {
      wout(c->who, "No offspring was produced.");
      return FALSE;
    }

  wout(c->who, "Produced %s.", box_name_qty(offspring, 1));

  gen_item(c->who, offspring, 1);
  add_skill_experience(c->who, sk_breed_beasts);

  return TRUE;
};
Exemplo n.º 17
0
 int svc (void)
 {
   while (!is_closed ())
     consume_item ();
   return 0;
 }
Exemplo n.º 18
0
void
location_production()
{
  int where;
  int i, enclosed;
  int terr, encl_terr;
  int has_city;
  float pop_grow = 0.0;
  int pop_limit = 200, pop, dpop = 0;

  loop_loc(where) {
    terr = subkind(where);
    has_city = 0;

    for (i = 0; terr_prod[i].terr; i++)
      if (terr_prod[i].terr == terr) {
        replenish(where, terr_prod[i].item, terr_prod[i].qty,
                  terr_prod[i].max);
      }

    /*
     *  Mon Sep 16 11:42:22 1996 -- Scott Turner
     *
     *  Now check for production from enclosed locations...
     *
     */
    loop_here(where, enclosed) {
      encl_terr = subkind(enclosed);
      if (encl_terr == sub_city)
        has_city = 1;
      for (i = 0; terr_prod2[i].terr; i++)
        if (terr_prod2[i].terr == encl_terr) {
          replenish(where, terr_prod2[i].item, terr_prod2[i].qty,
                    terr_prod2[i].max);
        };
    }
    next_here;

    /*
     *  First limit poppy fields to normal production level.
     *  Then double opium if poppy field was specially tended.
     */
    if (terr == sub_poppy_field) {
      int n;

      n = has_item(where, item_opium);
      if (n > POPPY_OPIUM)
        consume_item(where, item_opium, n - POPPY_OPIUM);

      if (rp_misc(where) && rp_misc(where)->opium_double) {
        rp_misc(where)->opium_double = FALSE;
        gen_item(where, item_opium, has_item(where, item_opium));
      }
    }

    if (terr == sub_island ||
        (loc_depth(where) == LOC_province && has_ocean_access(where)))
      replenish(where, item_flotsam, 30, 30);

    /*
     *  Sun Dec  1 10:34:41 1996 -- Scott Turner
     *
     *  Peasant production.  Depends upon the location (and
     *  whether it contains a city).
     *
     *  Has_city is set up above...
     *
     *  Tue Sep 22 13:20:18 1998 -- Scott Turner
     *
     *  Faery ought not have peasants.  It should have (I guess)
     *  elf peasants, although what you can do with those is
     *  open to conjecture :-)
     *
     */
    if (pop = has_item(where, item_peasant)) {
      if (has_city) {
        pop_grow = 0.03;
        pop_limit = 10000;
      }
      else {
        switch (terr) {
        case sub_plain:
        case sub_forest:
          pop_grow = 0.01;
          pop_limit = 1000;
          break;
        case sub_mountain:
        case sub_swamp:
          pop_grow = 0.005;
          pop_limit = 1000;
          break;
        default:
          pop_grow = 0.000;
          pop_limit = 500;
          break;
        };
      };

      /*
       *  Might be an effect here.
       *
       */
      if (get_effect(where, ef_grow, 0, 0)) {
        wout(where, "The peasants seem particularly happy this month.");
        pop_grow += 0.02;
      };

      dpop = pop * pop_grow;
      if (pop_grow > 0.0 && dpop < 1)
        dpop = 1;
      /*
       *  Lose population at a reasonable rate.
       *
       */
      if (pop > pop_limit)
        dpop = -(pop - pop_limit) / 10;

      if (p_subloc(province(where))->loot && dpop > 0) {
        wout(where,
             "Pillaging traumatizes the population and no growth occurs.");
      }
      else if (dpop > 0) {
        if (pop > 100)
          wout(where, "The population grows by %s peasant%s.",
               nice_num(dpop), add_s(dpop));
        gen_item(where, item_peasant, dpop);
      }
      else {
        if (pop > 100)
          wout(where, "Overcrowding causes %s peasant death%s.",
               nice_num(-dpop), add_s(-dpop));
        consume_item(where, item_peasant, -dpop);
      };
    };

    /*
     *  Sat Apr 18 16:57:53 1998 -- Scott Turner
     *
     *  Special case for gold production from peasants.
     *  They generate 1 gold per 20 peasants (1/10 in cities)
     *  which accumulates to be removed by various means.
     *
     *  Only in civilized (> 100) provinces
     */
    if ((pop = has_item(where, item_peasant)) > 100) {
      if (has_city) {
        dpop = pop * 0.10;
      }
      else {
        dpop = pop * 0.05;
      };
      gen_item(where, item_gold, dpop);
    };
  }