Synchronization primitives: Mutex and Semaphore (Sample Program) - Article 9
Hi Folks,
In this article, I am going to present my version of producer consumer problem which we need to synchronize.
The producer can produce a maximum of 5 elements and the consumer consumes 1 element at a time. All data is shared and hence, synchronization is needed. In this article, I am going to present the program. In the next article, we shall apply synchronization.
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>
typedef struct node {
struct node *link;
int data;
} NODE;
static int count = 0;
NODE *head = NULL;
NODE *get_node(void) {
int data = rand();
data = data % 13;
NODE *ptr = (NODE *)malloc(sizeof(NODE));
if(ptr != NULL) {
ptr->data = data;
ptr->link = NULL;
}
return ptr;
}
void print_node(NODE *ptr) {
printf("|%d|",ptr->data);
if(ptr->link != NULL) {
printf("->");
}
}
void display(void) {
NODE *ptr = head;
if(head == NULL) {
printf("List is empty\n");
return;
}
while(ptr != NULL) {
print_node(ptr);
ptr = ptr->link;
}
printf("\n");
}
void *producer(void *p) {
NODE *temp;
NODE *ptr;
while(1) {
if(count >= 5) {
printf("The list is full\n");
continue;
}
temp = get_node();
ptr = head;
if(head == NULL) {
head = temp;
count++;
printf("Count: %d\n",count);
continue;
}
while(ptr->link != NULL)
ptr = ptr->link;
ptr->link = temp;
count++;
printf("Count: %d\n",count);
display();
}
}
void *consumer(void *p) {
NODE *temp;
while(1) {
temp = head;
if(head == NULL) {
printf("Nothing to delete\n");
printf("Count: %d\n",count);
continue;
}
head = head->link;
free(temp);
count--;
printf("Count: %d\n",count);
display();
}
}
void main() {
pthread_t tid1, tid2;
pthread_create(&tid1, NULL, producer, NULL);
pthread_create(&tid2, NULL, consumer, NULL);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
}
In this article, I am going to present my version of producer consumer problem which we need to synchronize.
The producer can produce a maximum of 5 elements and the consumer consumes 1 element at a time. All data is shared and hence, synchronization is needed. In this article, I am going to present the program. In the next article, we shall apply synchronization.
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>
typedef struct node {
struct node *link;
int data;
} NODE;
static int count = 0;
NODE *head = NULL;
NODE *get_node(void) {
int data = rand();
data = data % 13;
NODE *ptr = (NODE *)malloc(sizeof(NODE));
if(ptr != NULL) {
ptr->data = data;
ptr->link = NULL;
}
return ptr;
}
void print_node(NODE *ptr) {
printf("|%d|",ptr->data);
if(ptr->link != NULL) {
printf("->");
}
}
void display(void) {
NODE *ptr = head;
if(head == NULL) {
printf("List is empty\n");
return;
}
while(ptr != NULL) {
print_node(ptr);
ptr = ptr->link;
}
printf("\n");
}
void *producer(void *p) {
NODE *temp;
NODE *ptr;
while(1) {
if(count >= 5) {
printf("The list is full\n");
continue;
}
temp = get_node();
ptr = head;
if(head == NULL) {
head = temp;
count++;
printf("Count: %d\n",count);
continue;
}
while(ptr->link != NULL)
ptr = ptr->link;
ptr->link = temp;
count++;
printf("Count: %d\n",count);
display();
}
}
void *consumer(void *p) {
NODE *temp;
while(1) {
temp = head;
if(head == NULL) {
printf("Nothing to delete\n");
printf("Count: %d\n",count);
continue;
}
head = head->link;
free(temp);
count--;
printf("Count: %d\n",count);
display();
}
}
void main() {
pthread_t tid1, tid2;
pthread_create(&tid1, NULL, producer, NULL);
pthread_create(&tid2, NULL, consumer, NULL);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
}
Comments
Post a Comment