Skip to content

Instantly share code, notes, and snippets.

@Blackhawk95
Created February 10, 2021 03:40
Show Gist options
  • Save Blackhawk95/7dd881e3c0f89201595a652ebcdb4c0f to your computer and use it in GitHub Desktop.
Save Blackhawk95/7dd881e3c0f89201595a652ebcdb4c0f to your computer and use it in GitHub Desktop.
producer consumer simple
HEY RANDOM STRANGER, NOTHIN' TO SEE HERE, BUT DO LEAVE A COMMENT IF U HAVE ANY POINTS TO ADD
---------------------------------------------------------------------------------------------
[run]
gcc main.c buffer.c -lpthread && ./a.out
[comments]
* This is too simple and I have used a fixed sized ring ( circular queue ) buffer, so I don't have to deal with overflow
* hardcorded the randomness into the code : see "sleep(rand()%5);" please adjust accordingly
* Seems to be working, but i dunno if this satisfies all the condition
* Also I dunno how to write c
/* main.c */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include "buffer.h"
int count = 0;
void *producer(void *vargp){
while(1){
//0 to 1023
while (count == SIZE - 1){
printf("Buffer full\n");
}
time_t t;
/* Intializes random number generator */
srand((unsigned) time(&t));
//sleep random
sleep(rand()%5);
int a = rand()%50;
write_data_to_buffer(a);
printf("count -> %d : Written %d\n", count ,a);
//increments count
++count;
}
}
void *consumer(void *vargp){
while(1){
while (count == 0){
printf("Buffer empty\n");
}
time_t t;
/* Intializes random number generator */
srand((unsigned) time(&t));
//sleep random
sleep(rand()%5);
int a;
read_data_from_buffer(&a);
printf("count -> %d : Read -> %d\n", count ,a);
//decrements count
--count;
}
}
int main(){
init_buffer();
// read_full_buffer();
pthread_t producer_thread_id; /* thread's ID (just an integer)*/
pthread_t consumer_thread_id;
pthread_create(&producer_thread_id, NULL, producer, NULL);
pthread_create(&consumer_thread_id, NULL, consumer, NULL);
pthread_exit(NULL);
return 0;
}
/* buffer.h */
#define SIZE 1024
typedef struct Data {
int d;
int index;
int* next;
} data;
const static data* buffer = 0;
static struct Buffer_params {
data *start;
data *tail;
data *readp;
data *writep;
} bp;
void init_buffer();
void read_full_buffer();
void write_data_to_buffer(int);
void read_data_from_buffer(int*);
/* buffer.c */
#include "buffer.h"
#include <stdio.h>
#include <stdlib.h>
#define BUFFER
// creates a fixed size circular queue
void init_buffer(){
for(int i=0;i< SIZE; i++){
data* ptr = (data*) malloc (sizeof( data ));
ptr->d = i;
ptr->next = 0;
ptr->index = i;
if(i == 0){
buffer = ptr;
bp.start = ptr;
bp.readp = ptr;
bp.writep = ptr;
bp.tail = ptr;
}
else if(i == SIZE - 1 ){
ptr->next = (int*) bp.start;
bp.tail->next = (int*)ptr;
bp.tail = (data*)bp.tail->next;
}
else{
bp.tail->next = (int*)ptr;
bp.tail = (data*)bp.tail->next;
}
}
}
//write data to top
void write_data_to_buffer(int d){
bp.writep = (data*) bp.writep->next;
bp.writep->d = d;
printf("index-> %d : ", bp.writep->index);
}
//read data from read pointer place
void read_data_from_buffer(int* d){
*d = bp.readp->d;
printf("index-> %d : ", bp.readp->index);
bp.readp = (data*) bp.readp->next;
}
void read_full_buffer(){
data* ptr;
ptr = bp.readp;
int a = SIZE;
while(a > 0){
printf("value-> %d\n", ptr->d);
ptr = (data*)ptr->next;
a--;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment