Ejemplo n.º 1
0
Archivo: main.c Proyecto: Madsen90/BOSC
int main(int argc, char* argv[])
{ 
  fifo = list_new();

  list_add(fifo, node_new_str("s1"));
  list_add(fifo, node_new_str("s2"));

  Node *n1 = list_remove(fifo);
  if (n1 == NULL) { printf("Error no elements in list\n"); exit(-1);}
  Node *n2 = list_remove(fifo);
  if (n2 == NULL) { printf("Error no elements in list\n"); exit(-1);}
  printf("%s\n%s\n", n1->elm, n2->elm);

  return 0;
}
Ejemplo n.º 2
0
Archivo: oo2_3.c Proyecto: Jelb/BOSC
/*Producer function*/
void *producer(void *param) {
  
  int id = *(int *)param;
  Node *node;
  char elm[10];

  while(do_produce()) {
    Sleep(1000);
    
    sem_wait(&sem_count);
    sprintf(elm,"Item_%i",product_count);
    product_count++;
    sem_post(&sem_count);
    
    node = node_new_str(elm);

    sem_wait(&empty);
    sem_wait(&_mutex);

    list_add(buf, node);
    printf("Producer %4i produced %9s. Items in buffer: %4i (out of %4i)\n", id, elm, buf->len, b);
    
    sem_post(&_mutex);
    sem_post(&full);    
  }

  pthread_exit(0);
}
Ejemplo n.º 3
0
Archivo: main.c Proyecto: swungify/BOSC
void *producer(void* producerNumber) {
	while(1){	//Don't worry, we will break it
		sem_wait(&mutex);

		int tempProducts;	
		sem_getvalue(&products, &tempProducts);	//Get amount of products left to be produced and consumed

		if (tempProducts == 0){	//Break, we're done!
			sem_post(&mutex);
			return 0;
		}
		int emptyTemp;
		sem_getvalue(&empty, &emptyTemp);	//Get if there's any empty spaces left

		if (emptyTemp > 0){	//Yes, there's room!
			char nodenr[8];
			sprintf(nodenr, "%d", tempProducts);
			list_add(buffer, node_new_str(nodenr));
			sem_wait(&products); //-1
			sem_post(&full); //+1
			sem_wait(&empty); //-1
			int tempFull;
			sem_getvalue(&full, &tempFull);
			printf("Producer %d produced item no. %d. Items in buffer: %d.\n",(int) producerNumber, tempProducts, (tempFull));
			
		}
		sem_post(&mutex);

		Sleep();
	}
}
Ejemplo n.º 4
0
// The code for the threads.
void *TaskCode(void *argument)
{
    // Converts the parameter to an integer that holds the number
    // of the thread.
    int *threadNo = (int *) argument;

    int i;
    // Adds elements to the list.
    for (i = 0; i < number_of_elements_per_thread; ++i)
    {
        char x[250];
        char *s = "Thread";
        sprintf(x, "%s #%d, element %d", s, *threadNo + 1, i);
        list_add(fifo, node_new_str(x));
    }

    // Removes elements from the list.
    for (i = 0; i < number_of_elements_per_thread; ++i)
    {
        Node *node = list_remove(fifo);
        char *nodeElem = (char *)node->elm;
        printf("%s\n", nodeElem);
    }
	free(argument);
    pthread_exit(0);
}
Ejemplo n.º 5
0
//Add nodes to the list
void *addnodes(void *data){
  struct add_nodes_data *dat = data;
  int i;
  for(i=0; i<dat->count; i++){
    char label[50];
    sprintf(label,"%s%d",dat->mark,i); //Construct a label to identify the data and where it's coming from
    printf("Added element %s\n",label);
    list_add(dat->l, node_new_str(label)); //Add node
    sleep(1);
  }
}
Ejemplo n.º 6
0
void *producer(void *param) {
	prod_t work = *((prod_t*) param);
	char itemstr[100];
	int i;
	for (i = work.from; i < work.to; i++) {
		sprintf(itemstr, "item %d", i);
		Node *n = node_new_str(itemstr);
		sem_wait(&empty);
		list_add(buf, n);

		printf("Producer %d produced %s. Items in buffer: %d (Max: %d)\n", work.id, itemstr, buf->len, buffer_size);

		sem_post(&full);
		/* sleep for a random period of time */
		Sleep(200);
	}
	pthread_exit(NULL);
}
Ejemplo n.º 7
0
// Produces a new node.
Node *produceProduct()
{
	Node* node;
	
	// Lock produced_products
	pthread_mutex_lock(&produce_lock);

	// Create node
	char tmp[250];
	sprintf(tmp, "ITEM_%d", produced_products);
	
	produced_products++; // Increased produced products.

	pthread_mutex_unlock(&produce_lock); // Unlocked produced_products.
	
	node = node_new_str(tmp); // Initialisation of the new node.
	
	return node;
}