Added shared struct and made code run

This commit is contained in:
TriantaTV 2022-10-12 15:44:12 -05:00
parent 160dc9a99e
commit b5b11c2979
12 changed files with 40 additions and 19 deletions

View File

@ -8,7 +8,8 @@ compile:
gcc $(INC) -c -o build/consumer.o src/consumer.c
exec:
./driver.out
gcc -o bin/driver.out build/*.o
./bin/driver.out
clean:
rm driver.out consumer.out producer.out

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -2,7 +2,7 @@
#define CONSUMER_H
void Consumer(void);
void consume_item(int item);
void consume_item(long int item);
long int remove_item(void);
#endif

View File

@ -3,6 +3,6 @@
void Producer(void);
long int produce_item(void);
void insert_item(int item);
void insert_item(long int item);
#endif

13
include/shared.h Normal file
View File

@ -0,0 +1,13 @@
#ifndef SHARED_H
#define SHARED_H
#include <semaphore.h>
typedef struct sharedMem
{
int buffer[10];
sem_t semConsumer;
sem_t semProducer;
} sharedMem;
#endif

View File

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

View File

@ -1,15 +1,15 @@
#include <stdio.h>
#include <semaphore.h>
#include <unistd.h>
semaphore mutex = 1;
semaphore empty;
semaphore full = 0;
#include "consumer.h"
#include "producer.h"
#include "shared.h"
int GetSplitAmount(char* argv);
int main(int argc, char* argv[])
{
sharedMem testMemory;
int splitLimit = 1;
if (argc == 2)
splitLimit = GetSplitAmount(argv[1]);

View File

@ -1,5 +1,6 @@
#include <stdlib.h>
#include "producer.h"
#include "shared.h"
void Producer(void)
{
@ -7,11 +8,14 @@ void Producer(void)
while (1)
{
item = produce_item();
down(&empty);
down(&mutex);
// sem_wait(&semProducer);
// down(&empty);
// down(&mutex);
insert_item(item);
up(&mutex);
up(&full);
// sem_post(&semProducer);
// up(&mutex);
// up(&full);
break;
}
}
@ -20,7 +24,7 @@ long int produce_item(void)
return random();
}
void insert_item(int item)
void insert_item(long int item)
{
;
}

1
src/shared.c Normal file
View File

@ -0,0 +1 @@
#include "shared.h"