-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Izaak Coleman
committed
Nov 30, 2015
1 parent
8f59000
commit 38d94b7
Showing
15 changed files
with
270 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
CC=g++ -Wall | ||
|
||
all: producer consumer start | ||
|
||
start: start.o helper.o | ||
$(CC) -o start start.o helper.o | ||
|
||
start.o: start.cc helper.cc | ||
$(CC) -c start.cc helper.cc | ||
|
||
producer: producer.o helper.o | ||
$(CC) -o producer producer.o helper.o | ||
|
||
producer.o: producer.cc helper.cc | ||
$(CC) -c producer.cc helper.cc | ||
|
||
consumer: consumer.o helper.o | ||
$(CC) -o consumer consumer.o helper.o | ||
|
||
consumer.o: consumer.cc helper.cc | ||
$(CC) -c consumer.cc helper.cc | ||
|
||
tidy: | ||
rm -f *.o core | ||
|
||
clean: | ||
rm -f start producer consumer *.o core |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Consumer | ||
|
||
# include "helper.h" | ||
|
||
int main (int argc, char *argv[]) | ||
{ | ||
// TODO | ||
return 0; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Producer(1) time 0: Job id 1 duration 6 | ||
Consumer(1) time 0: Job id 1 executing sleep duration 6 | ||
Producer(1) time 2: Job id 2 duration 6 | ||
Producer(1) time 6: Job id 3 duration 7 | ||
Consumer(1) time 6: Job id 1 completed | ||
Consumer(1) time 6: Job id 2 executing sleep duration 6 | ||
Producer(1) time 6: No more jobs to generate. | ||
Consumer(1) time 12: Job id 2 completed | ||
Consumer(1) time 12: Job id 3 executing sleep duration 7 | ||
Consumer(1) time 19: Job id 3 completed | ||
Consumer(1) time 29: No Jobs left. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
Producer(1) time 0: Job id 1 duration 6 | ||
Producer(3) time 0: Job id 2 duration 6 | ||
Consumer(1) time 0: Job id 1 executing sleep duration 6 | ||
Producer(2) time 0: Job id 3 duration 6 | ||
Consumer(2) time 0: Job id 2 executing sleep duration 6 | ||
Producer(1) time 2: Job id 4 duration 6 | ||
Producer(3) time 2: Job id 5 duration 6 | ||
Producer(2) time 2: Job id 1 duration 6 | ||
Producer(1) time 6: Job id 2 duration 7 | ||
Consumer(1) time 6: Job id 1 completed | ||
Consumer(1) time 6: Job id 3 executing sleep duration 6 | ||
Producer(3) time 6: Job id 3 duration 7 | ||
Consumer(2) time 6: Job id 2 completed | ||
Consumer(2) time 6: Job id 4 executing sleep duration 6 | ||
Producer(2) time 6: Job id 4 duration 7 | ||
Consumer(1) time 12: Job id 3 completed | ||
Consumer(1) time 12: Job id 5 executing sleep duration 6 | ||
Producer(1) time 7: Job id 5 duration 3 | ||
Consumer(2) time 12: Job id 4 completed | ||
Consumer(2) time 12: Job id 1 executing sleep duration 6 | ||
Producer(3) time 7: Job id 1 duration 3 | ||
Consumer(1) time 18: Job id 5 completed | ||
Consumer(1) time 18: Job id 2 executing sleep duration 7 | ||
Producer(2) time 7: Job id 2 duration 3 | ||
Consumer(2) time 18: Job id 1 completed | ||
Consumer(2) time 18: Job id 3 executing sleep duration 7 | ||
Producer(1) time 11: Job id 3 duration 2 | ||
Producer(1) time 11: No more jobs to generate. | ||
Consumer(1) time 25: Job id 2 completed | ||
Consumer(1) time 25: Job id 4 executing sleep duration 7 | ||
Producer(3) time 11: Job id 4 duration 2 | ||
Consumer(2) time 25: Job id 3 completed | ||
Consumer(2) time 25: Job id 5 executing sleep duration 3 | ||
Producer(2) time 11: Job id 5 duration 2 | ||
Producer(3) time 11: No more jobs to generate. | ||
Consumer(2) time 28: Job id 5 completed | ||
Consumer(2) time 28: Job id 1 executing sleep duration 3 | ||
Producer(2) time 11: No more jobs to generate. | ||
Consumer(2) time 31: Job id 1 completed | ||
Consumer(2) time 31: Job id 2 executing sleep duration 3 | ||
Consumer(1) time 32: Job id 4 completed | ||
Consumer(1) time 32: Job id 3 executing sleep duration 2 | ||
Consumer(1) time 34: Job id 3 completed | ||
Consumer(1) time 34: Job id 4 executing sleep duration 2 | ||
Consumer(2) time 34: Job id 2 completed | ||
Consumer(2) time 34: Job id 5 executing sleep duration 2 | ||
Consumer(1) time 36: Job id 4 completed | ||
Consumer(2) time 36: Job id 5 completed | ||
Consumer(1) time 46: No Jobs left. | ||
Consumer(2) time 46: No Jobs left. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/****************************************************************** | ||
* The helper file that contains the following helper functions: | ||
* check_arg - Checks if command line input is a number and returns it | ||
* sem_create - Create number of sempahores required in a semaphore array | ||
* sem_attach - Attach semaphore array to the program | ||
* sem_init - Initialise particular semaphore in semaphore array | ||
* sem_wait - Waits on a semaphore (akin to down ()) in the semaphore array | ||
* sem_timewait - Waits on a semaphore for a particular time | ||
* sem_signal - Signals a semaphore (akin to up ()) in the semaphore array | ||
* sem_close - Destroy the semaphore array | ||
******************************************************************/ | ||
|
||
# include "helper.h" | ||
|
||
int check_arg (char *buffer) | ||
{ | ||
int i, num = 0; | ||
for (i=0; i < (int) strlen (buffer); i++) | ||
{ | ||
if ((0+buffer[i]) > 57) | ||
return -1; | ||
num += pow (10, strlen (buffer)-i-1) * (buffer[i] - 48); | ||
} | ||
return num; | ||
} | ||
|
||
int sem_attach (key_t key) | ||
{ | ||
int id; | ||
if ((id = semget (key, 1, 0)) < 0) | ||
return -1; | ||
return id; | ||
} | ||
|
||
int sem_create (key_t key, int num) // generate semaphore set | ||
{ | ||
int id; // 'unique' sem id, used by process to access semset | ||
if ((id = semget (key, num, 0666 | IPC_CREAT | IPC_EXCL)) < 0) // gen id | ||
return -1; | ||
return id; | ||
} | ||
|
||
int sem_init (int id, int num, int value) // needed to give sem value | ||
{ | ||
union semun semctl_arg; // generate a semun, semun for better management | ||
semctl_arg.val = value; // load value | ||
if (semctl (id, num, SETVAL, semctl_arg) < 0) // set value for one sem | ||
return -1; | ||
return 0; | ||
} | ||
|
||
void sem_wait (int id, short unsigned int num) | ||
{ | ||
struct sembuf op[] = { | ||
{num, -1, SEM_UNDO} | ||
}; | ||
semop (id, op, 1); | ||
} | ||
|
||
int sem_timewait (int id, short unsigned int num, int tim) | ||
{ | ||
struct timespec ts = {tim, 0}; | ||
struct sembuf op[] = { | ||
{num, -1, SEM_UNDO} | ||
}; | ||
if (semtimedop(id, op, 1, &ts ) == -1 ) | ||
if (errno == EAGAIN) | ||
return -1; | ||
return 0; | ||
} | ||
|
||
void sem_signal (int id, short unsigned int num) | ||
{ | ||
struct sembuf op[] = { | ||
{num, 1, SEM_UNDO} | ||
}; | ||
semop (id, op, 1); | ||
} | ||
|
||
int sem_close (int id) | ||
{ | ||
if (semctl (id, 0, IPC_RMID, 0) < 0) | ||
return -1; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/****************************************************************** | ||
* Header file for the helper functions. This file includes the | ||
* required header files, as well as the structures and the shared | ||
* memory/semaphore values (which are to be changed as needed). | ||
******************************************************************/ | ||
|
||
|
||
# include <stdio.h> | ||
# include <stdlib.h> | ||
# include <unistd.h> | ||
# include <sys/types.h> | ||
# include <sys/ipc.h> | ||
# include <sys/shm.h> | ||
# include <sys/sem.h> | ||
# include <sys/time.h> | ||
# include <math.h> | ||
# include <errno.h> | ||
# include <string.h> | ||
|
||
# define SHM_KEY 0x50 // Change this number as needed | ||
# define SHM_W 0200 | ||
# define SHM_R 0400 | ||
# define SHM_MODE (SHM_R | SHM_W) | ||
# define SHM_SIZE 10000 // Change this number as needed | ||
# define SEM_KEY 0x50 // Change this number as needed | ||
|
||
union semun { | ||
int val; /* used for SETVAL only */ | ||
struct semid_ds *buf; /* used for IPC_STAT and IPC_SET */ | ||
ushort *array; /* used for GETALL and SETALL */ | ||
}; | ||
|
||
typedef struct jobtype | ||
{ | ||
int id; | ||
int duration; | ||
} JOBTYPE; | ||
|
||
typedef struct queue | ||
{ | ||
int size; | ||
int front; | ||
int end; | ||
JOBTYPE job[500]; // Can assume this to be maximum queue size | ||
} QUEUE; | ||
|
||
int check_arg (char *); | ||
int sem_create (key_t, int); | ||
int sem_attach (key_t); | ||
int sem_init (int, int, int); | ||
void sem_wait (int, short unsigned int); | ||
int sem_timewait (int, short unsigned int, int); | ||
void sem_signal (int, short unsigned int); | ||
int sem_close (int); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Producer | ||
|
||
# include "helper.h" | ||
|
||
int main (int argc, char *argv[]) | ||
{ | ||
// TODO | ||
return 0; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
./start 5 | ||
sleep 1 | ||
./producer 1 3 & | ||
./consumer 1 & |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
./start 5 | ||
sleep 1 | ||
./producer 1 5 & | ||
./producer 2 5 & | ||
./producer 3 5 & | ||
./consumer 1 & | ||
./consumer 2 & |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/****************************************************************** | ||
* Program for setting up semaphores/shared memory before running | ||
* the Producer and Consumer | ||
******************************************************************/ | ||
|
||
# include "helper.h" | ||
|
||
int main (int argc, char **argv) | ||
{ | ||
|
||
// TODO | ||
|
||
return 0; | ||
} |