c producer consumer pthread semaphore
C
/* buffer.h */
typedef int buffer_item;
#define BUFFER_SIZE 5
/* main.c */
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include "buffer.h"
#define RAND_DIVISOR 100000000
#define TRUE 1
/* The mutex lock */
pthread_mutex_t mutex;
/* the semaphores */
sem_t full, empty;
/* the buffer */
buffer_item buffer[BUFFER_SIZE];
/* buffer counter */
int counter;
pthread_t tid; //Thread ID
pthread_attr_t attr; //Set of thread attributes
void *producer(void *param); /* the producer thread */
void *consumer(void *param); /* the consumer thread */
void initializeData() {
/* Create the mutex lock */
pthread_mutex_init(&mutex, NULL);
/* Create the full semaphore and initialize to 0 */
sem_init(&full, 0, 0);
/* Create the empty semaphore and initialize to BUFFER_SIZE */
sem_init(&empty, 0, BUFFER_SIZE);
/* Get the default attributes */
pthread_attr_init(&attr);
/* init buffer */
counter = 0;
}
/* Producer Thread */
void *producer(void *param) {
buffer_item item;
while(TRUE) {
/* sleep for a random period of time */
int rNum = rand() / RAND_DIVISOR;
sleep(rNum);
/* generate a random number */
item = rand();
/* acquire the empty lock */
sem_wait(&empty);
/* acquire the mutex lock */
pthread_mutex_lock(&mutex);
if(insert_item(item)) {
fprintf(stderr, " Producer report error condition\n");
}
else {
printf("producer produced %d\n", item);
}
/* release the mutex lock */
pthread_mutex_unlock(&mutex);
/* signal full */
sem_post(&full);
}
}
/* Consumer Thread */
void *consumer(void *param) {
buffer_item item;
while(TRUE) {
/* sleep for a random period of time */
int rNum = rand() / RAND_DIVISOR;
sleep(rNum);
/* aquire the full lock */
sem_wait(&full);
/* aquire the mutex lock */
pthread_mutex_lock(&mutex);
if(remove_item(&item)) {
fprintf(stderr, "Consumer report error condition\n");
}
else {
printf("consumer consumed %d\n", item);
}
/* release the mutex lock */
pthread_mutex_unlock(&mutex);
/* signal empty */
sem_post(&empty);
}
}
/* Add an item to the buffer */
int insert_item(buffer_item item) {
/* When the buffer is not full add the item
and increment the counter*/
if(counter < BUFFER_SIZE) {
buffer[counter] = item;
counter++;
return 0;
}
else { /* Error the buffer is full */
return -1;
}
}
/* Remove an item from the buffer */
int remove_item(buffer_item *item) {
/* When the buffer is not empty remove the item
and decrement the counter */
if(counter > 0) {
*item = buffer[(counter-1)];
counter--;
return 0;
}
else { /* Error buffer empty */
return -1;
}
}
int main(int argc, char *argv[]) {
/* Loop counter */
int i;
/* Verify the correct number of arguments were passed in */
if(argc != 4) {
fprintf(stderr, "USAGE:./main.out <INT> <INT> <INT>\n");
}
int mainSleepTime = atoi(argv[1]); /* Time in seconds for main to sleep */
int numProd = atoi(argv[2]); /* Number of producer threads */
int numCons = atoi(argv[3]); /* Number of consumer threads */
/* Initialize the app */
initializeData();
/* Create the producer threads */
for(i = 0; i < numProd; i++) {
/* Create the thread */
pthread_create(&tid,&attr,producer,NULL);
}
/* Create the consumer threads */
for(i = 0; i < numCons; i++) {
/* Create the thread */
pthread_create(&tid,&attr,consumer,NULL);
}
/* Sleep for the specified amount of time in milliseconds */
sleep(mainSleepTime);
/* Exit the program */
printf("Exit the program\n");
exit(0);
}
Also in C:
- Title
- functions in c
- Category
- C
- Title
- sdl bouton
- Category
- C
- Title
- what is strikethrough in markdown
- Category
- C
- Title
- functions return type in c
- Category
- C
- Title
- left me on read
- Category
- C
- Title
- es par
- Category
- C
- Title
- get_session` is not available when using TensorFlow 2.0.
- Category
- C
- Title
- full screen on c
- Category
- C
- Title
- too few arguments to function in c
- Category
- C
- Title
- xmlns='' was not expected
- Category
- C
- Title
- binary tree geekd for geeks
- Category
- C
- Title
- how to get random numbers in c
- Category
- C
- Title
- how to print in c
- Category
- C
- Title
- incompatible types when initializing type ‘float’ using type ‘point {aka struct point}’
- Category
- C
- Title
- stack
- Category
- C
- Title
- how to do matrix multiplication in c
- Category
- C
- Title
- .\main.c:4:8: error: expected declaration specifiers or '...' before '\x6f726c64'
- Category
- C
- Title
- c print hello world
- Category
- C
- Title
- Switch Mode C Programming
- Category
- C
- Title
- jock cranley
- Category
- C
- Title
- how to free memory in c
- Category
- C
- Title
- chat c socket tcp geeksforgeeks
- Category
- C
- Title
- limit of integer in c
- Category
- C
- Title
- get a remote branch git
- Category
- C
- Title
- confirm sweet alert
- Category
- C
- Title
- disable gnu++11 option
- Category
- C
- Title
- append to list in c
- Category
- C
- Title
- vbl share price
- Category
- C
- Title
- code in c skipping over scanf
- Category
- C
- Title
- select all file from date powershell
- Category
- C
- Title
- for loop c
- Category
- C
- Title
- sbatch array set max jobs at once
- Category
- C
- Title
- last element from list javascript
- Category
- C
- Title
- how to comment in arduino
- Category
- C
- Title
- How to make a printf in c
- Category
- C
- Title
- qtableview get selected row
- Category
- C
- Title
- networkx remove attributes
- Category
- C
- Title
- c printf uint32_t
- Category
- C
- Title
- 233 pounds to inr
- Category
- C
- Title
- root in C
- Category
- C
- Title
- string to int c
- Category
- C
- Title
- switch case c
- Category
- C
- Title
- strtoul C
- Category
- C
- Title
- print in c
- Category
- C
- Title
- c check if char is an operator
- Category
- C
- Title
- what is size_t in c
- Category
- C
- Title
- c copy string
- Category
- C
- Title
- does strcat null terminate
- Category
- C
- Title
- get current used proxy windows 7
- Category
- C
- Title
- how to check the size of a file in linux c
- Category
- C
- Title
- char to int c
- Category
- C
- Title
- Declare macro
- Category
- C
- Title
- prime number
- Category
- C
- Title
- c program to perform transpose of a matrix
- Category
- C
- Title
- primo
- Category
- C
- Title
- california
- Category
- C
- Title
- cantidad de digitos recursiva
- Category
- C
- Title
- coin row problem in linear time
- Category
- C
- Title
- c bit access union
- Category
- C
- Title
- arduino digital read
- Category
- C
- Title
- how to open chrome using cmd
- Category
- C
- Title
- delete string function in c
- Category
- C
- Title
- download file by command line windows
- Category
- C
- Title
- Gemfile.lock`. It is likely that you need to grant write permissions for that path.
- Category
- C
- Title
- c program to find the sum of given number using recursion
- Category
- C
- Title
- how to pass an array to a thread in c?
- Category
- C
- Title
- 'int' is not a subtype of type 'double' dart
- Category
- C
- Title
- How to pull images from Docker Registry
- Category
- C
- Title
- File "h5py\h5g.pyx", line 161, in h5py.h5g.create ValueError: Unable to create group (name already exists)
- Category
- C
- Title
- fibonacci in c
- Category
- C
- Title
- changing tuple values
- Category
- C
- Title
- double return type in c
- Category
- C
- Title
- how to put a char into a string c
- Category
- C
- Title
- what are the causes of memory leaks in c
- Category
- C
- Title
- reset c array to zero
- Category
- C
- Title
- update ubuntu in terminal
- Category
- C
- Title
- docker images command
- Category
- C
- Title
- iterate through map
- Category
- C
- Title
- sum of arrays
- Category
- C
- Title
- c check if file was created
- Category
- C
- Title
- passing 2d array as parameter to function in c
- Category
- C
- Title
- c isdigit function
- Category
- C
- Title
- malloc in c
- Category
- C
- Title
- reverse function in c
- Category
- C
- Title
- write a program to ask the user 8 integer numbers. your program will then move all negative elements of your array to the end of the array without changing the order of positive elements and negative elements
- Category
- C
- Title
- write array of char to file in c
- Category
- C
- Title
- what is x:Name Xamarin forms
- Category
- C
- Title
- how to do Employing defensive code in the UI to ensure that the current frame is the most top level window
- Category
- C
- Title
- lelcetric fied
- Category
- C
- Title
- keras conv2d batchnorm
- Category
- C
- Title
- a enum data type in c with loop
- Category
- C
- Title
- printf("%3d ",XX);
- Category
- C
- Title
- turn a char array into double C
- Category
- C
- Title
- es vocal
- Category
- C
- Title
- what is a long long int in c
- Category
- C
- Title
- multiplication operator in c
- Category
- C
- Title
- xamarin command listview button
- Category
- C
- Title
- how to ascii art in c
- Category
- C
- Title
- c substring
- Category
- C
- Title
- ModuleNotFoundError: No module named 'easydict'
- Category
- C