Added rand() to producer

This commit is contained in:
TriantaTV 2022-10-12 21:11:15 -05:00
parent 1b31acc926
commit e8f6618df6
10 changed files with 29 additions and 13 deletions

View File

@ -1,14 +1,16 @@
INC := -I include
all: compile exec
all: compile link
compile:
gcc $(INC) -c -o build/driver.o src/driver.c
gcc $(INC) -c -o build/producer.o src/producer.c
gcc $(INC) -c -o build/consumer.o src/consumer.c
exec:
link:
gcc -o bin/driver.out build/*.o
exec: compile link
./bin/driver.out
clean:

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,7 +1,7 @@
#ifndef CONSUMER_H
#define CONSUMER_H
void Consumer(void);
void* Consumer(void* arg);
void consume_item(long int item);
long int remove_item(void);

View File

@ -1,7 +1,7 @@
#ifndef PRODUCER_H
#define PRODUCER_H
void Producer(void);
void* Producer(void* arg);
long int produce_item(void);
void insert_item(long int item);

View File

@ -1,17 +1,17 @@
#include "consumer.h"
#include "shared.h"
void Consumer(void)
void* Consumer(void* arg)
{
int item;
long int consumeNum;
while (1)
{
// down(&full);
// down(&mutex);
item = remove_item();
consumeNum = remove_item();
// up(&mutex);
// up(&empty);
consume_item(item);
consume_item(consumeNum);
break;
}
}

View File

@ -1,18 +1,24 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/shm.h>
#include <time.h>
#include <unistd.h>
#include "consumer.h"
#include "producer.h"
#include "shared.h"
int GetSplitAmount(char* argv);
void TestProducerConsumer(void);
int main(int argc, char* argv[])
{
srand(time(NULL));
sharedMem testMemory;
int splitLimit = 1;
if (argc == 2)
splitLimit = GetSplitAmount(argv[1]);
TestProducerConsumer();
}
int GetSplitAmount(char* argv)
@ -21,3 +27,9 @@ int GetSplitAmount(char* argv)
sscanf(argv, "%d", &splitAmount);
return splitAmount;
}
void TestProducerConsumer(void)
{
Producer(NULL);
Consumer(NULL);
}

View File

@ -1,17 +1,19 @@
#include <stdlib.h>
#include <stdio.h>
#include "producer.h"
#include "shared.h"
void Producer(void)
void* Producer(void* arg)
{
int item;
long int insertNum;
while (1)
{
item = produce_item();
insertNum = produce_item();
printf("The random number is %li\n", insertNum);
// sem_wait(&semProducer);
// down(&empty);
// down(&mutex);
insert_item(item);
insert_item(insertNum);
// sem_post(&semProducer);
// up(&mutex);
// up(&full);
@ -21,7 +23,7 @@ void Producer(void)
long int produce_item(void)
{
return random();
return rand() % 10;
}
void insert_item(long int item)