Added rand() to producer
This commit is contained in:
parent
1b31acc926
commit
e8f6618df6
6
Makefile
6
Makefile
@ -1,14 +1,16 @@
|
|||||||
INC := -I include
|
INC := -I include
|
||||||
|
|
||||||
all: compile exec
|
all: compile link
|
||||||
|
|
||||||
compile:
|
compile:
|
||||||
gcc $(INC) -c -o build/driver.o src/driver.c
|
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/producer.o src/producer.c
|
||||||
gcc $(INC) -c -o build/consumer.o src/consumer.c
|
gcc $(INC) -c -o build/consumer.o src/consumer.c
|
||||||
|
|
||||||
exec:
|
link:
|
||||||
gcc -o bin/driver.out build/*.o
|
gcc -o bin/driver.out build/*.o
|
||||||
|
|
||||||
|
exec: compile link
|
||||||
./bin/driver.out
|
./bin/driver.out
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
|
BIN
bin/driver.out
BIN
bin/driver.out
Binary file not shown.
BIN
build/consumer.o
BIN
build/consumer.o
Binary file not shown.
BIN
build/driver.o
BIN
build/driver.o
Binary file not shown.
BIN
build/producer.o
BIN
build/producer.o
Binary file not shown.
@ -1,7 +1,7 @@
|
|||||||
#ifndef CONSUMER_H
|
#ifndef CONSUMER_H
|
||||||
#define CONSUMER_H
|
#define CONSUMER_H
|
||||||
|
|
||||||
void Consumer(void);
|
void* Consumer(void* arg);
|
||||||
void consume_item(long int item);
|
void consume_item(long int item);
|
||||||
long int remove_item(void);
|
long int remove_item(void);
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#ifndef PRODUCER_H
|
#ifndef PRODUCER_H
|
||||||
#define PRODUCER_H
|
#define PRODUCER_H
|
||||||
|
|
||||||
void Producer(void);
|
void* Producer(void* arg);
|
||||||
long int produce_item(void);
|
long int produce_item(void);
|
||||||
void insert_item(long int item);
|
void insert_item(long int item);
|
||||||
|
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
#include "consumer.h"
|
#include "consumer.h"
|
||||||
#include "shared.h"
|
#include "shared.h"
|
||||||
|
|
||||||
void Consumer(void)
|
void* Consumer(void* arg)
|
||||||
{
|
{
|
||||||
int item;
|
long int consumeNum;
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
// down(&full);
|
// down(&full);
|
||||||
// down(&mutex);
|
// down(&mutex);
|
||||||
item = remove_item();
|
consumeNum = remove_item();
|
||||||
// up(&mutex);
|
// up(&mutex);
|
||||||
// up(&empty);
|
// up(&empty);
|
||||||
consume_item(item);
|
consume_item(consumeNum);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
12
src/driver.c
12
src/driver.c
@ -1,18 +1,24 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <sys/shm.h>
|
||||||
|
#include <time.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include "consumer.h"
|
#include "consumer.h"
|
||||||
#include "producer.h"
|
#include "producer.h"
|
||||||
#include "shared.h"
|
#include "shared.h"
|
||||||
|
|
||||||
int GetSplitAmount(char* argv);
|
int GetSplitAmount(char* argv);
|
||||||
|
void TestProducerConsumer(void);
|
||||||
|
|
||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
|
srand(time(NULL));
|
||||||
sharedMem testMemory;
|
sharedMem testMemory;
|
||||||
|
|
||||||
int splitLimit = 1;
|
int splitLimit = 1;
|
||||||
if (argc == 2)
|
if (argc == 2)
|
||||||
splitLimit = GetSplitAmount(argv[1]);
|
splitLimit = GetSplitAmount(argv[1]);
|
||||||
|
TestProducerConsumer();
|
||||||
}
|
}
|
||||||
|
|
||||||
int GetSplitAmount(char* argv)
|
int GetSplitAmount(char* argv)
|
||||||
@ -21,3 +27,9 @@ int GetSplitAmount(char* argv)
|
|||||||
sscanf(argv, "%d", &splitAmount);
|
sscanf(argv, "%d", &splitAmount);
|
||||||
return splitAmount;
|
return splitAmount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TestProducerConsumer(void)
|
||||||
|
{
|
||||||
|
Producer(NULL);
|
||||||
|
Consumer(NULL);
|
||||||
|
}
|
||||||
|
@ -1,17 +1,19 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include "producer.h"
|
#include "producer.h"
|
||||||
#include "shared.h"
|
#include "shared.h"
|
||||||
|
|
||||||
void Producer(void)
|
void* Producer(void* arg)
|
||||||
{
|
{
|
||||||
int item;
|
long int insertNum;
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
item = produce_item();
|
insertNum = produce_item();
|
||||||
|
printf("The random number is %li\n", insertNum);
|
||||||
// sem_wait(&semProducer);
|
// sem_wait(&semProducer);
|
||||||
// down(&empty);
|
// down(&empty);
|
||||||
// down(&mutex);
|
// down(&mutex);
|
||||||
insert_item(item);
|
insert_item(insertNum);
|
||||||
// sem_post(&semProducer);
|
// sem_post(&semProducer);
|
||||||
// up(&mutex);
|
// up(&mutex);
|
||||||
// up(&full);
|
// up(&full);
|
||||||
@ -21,7 +23,7 @@ void Producer(void)
|
|||||||
|
|
||||||
long int produce_item(void)
|
long int produce_item(void)
|
||||||
{
|
{
|
||||||
return random();
|
return rand() % 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
void insert_item(long int item)
|
void insert_item(long int item)
|
||||||
|
Loading…
Reference in New Issue
Block a user