-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
143 lines (134 loc) · 5.29 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* main.c
To be used with producer.h and consumer.h.
Version 1.0 -- Steven Wheeler
Target operating system: Linux
Last updated: 25/11/2019
This program is a simulation of a printer queue with a producer to create jobs and a consumer to destroy jobs.
This program WILL require the producer.h and consumer.h files to run.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h> // This file does not exist in Windows. Linux only!
#include <sys/shm.h> // This file does not exist in Windows. Linux only!
#include <unistd.h> // This file does not exist in Windows. Linux only!
#include "producer.h"
#include "consumer.h"
int main(int argc, char *argv[])
{
int i;
char programMode[2] = {"NA"}; //Program mode is set to NA if not defined, C if consumer, P if producer.
int maxCreatePerCycle = 0;
int maxDeletePerCycle = 0;
int maxSystem = 0;
int cycles = 0;
printf("\n Simulation of printer queue with Producer and Consumer.\n C Programming assignment\n Steven Wheeler\n [email protected]\n\n--PROGRAM STARTING--\n");
for (i = 1; i < argc; i++){ //RUN THROUGH ARGUMENTS TO FIND MODE
// FIND PROGRAM MODE
if (strcmp(argv[i], "-producer") == 0) {
if (strcmp(programMode, "NA") != 0) {
printf("\n [ WARNING ]: Program mode is already set, ignoring %s flag.", argv[i]);
}
else {
printf("\n [ MODE ]: PROGRAM LAUNCHING IN PRODUCER MODE");
strcpy(programMode, "P");
}
}
else if (strcmp(argv[i], "-consumer") == 0) {
if (strcmp(programMode, "NA") != 0) {
printf("\n [ WARNING ]: Program mode is already set, ignoring %s flag.", argv[i]);
}
else {
printf("\n [ MODE ]: PROGRAM LAUNCHING IN CONSUMER MODE");
strcpy(programMode, "C");
}
}
}
if (strcmp(programMode, "NA") == 0) { //CHECK THAT PROGRAM MODE IS DEFINED, IF NOT CLOSE
printf("\n [ FATAL ERROR ]: NO PROGRAM MODE WAS SUPPLIED, PLEASE RUN THE PROGRAM WITH THE -producer OR -consumer ARGUMENT.\n\n");
exit(0);
}
for (i=1;i < argc;i++) { //RUN THROUGH ARGUMENTS TO SET SETTINGS
if (strcmp(programMode, "P") == 0) { //IF SYSTEM IS RUNNING IN PRODUCER MODE
// MAX CREATED BY PRODUCER PER CYCLE
if (strcmp(argv[i], "-maxcreatecycle") == 0) {
if (atoi(argv[i + 1]) > 0) {
printf("\n [ INFO ]: Max jobs created by producer per cycle set to %s", argv[i + 1]);
maxCreatePerCycle = atoi(argv[i + 1]);
}
else {
printf("\n [ WARNING ]: -maxcreatecycle requires a numerical input, such as '-maxcreatecycle 20'. Continuing with default max of 2.");
maxCreatePerCycle = 2;
}
}
// MAX FOR SYSTEM
else if (strcmp(argv[i], "-maxsystem") == 0) {
if (atoi(argv[i + 1]) > 500) {
printf("\n [ WARNING ]: -maxsystem cannot be above 500. Continuing with default max of 50.");
maxSystem = 50;
}
else if (atoi(argv[i + 1]) > 0 && atoi(argv[i + 1]) <= 500) {
printf("\n [ INFO ]: Max jobs for system set to %s", argv[i + 1]);
maxSystem = atoi(argv[i + 1]);
}
else {
printf("\n [ WARNING ]: -maxsystem requires a numerical input, such as '-maxsystem 100'. Continuing with default max of 50.");
maxSystem = 50;
}
}
// WARN NON PRODUCER ARGUMENTS
if (strcmp(argv[i], "-maxdeletecycle") == 0) {
printf("\n [ WARNING ]: -maxdeletecycle is a consumer mode command. Ignoring -maxdeletecycle flag.");
}
}
if (strcmp(programMode, "C") == 0) { //IF SYSTEM IS RUNNING IN CONSUMER MODE
// MAX DELETED BY CONSUMER PER CYCLE
if (strcmp(argv[i], "-maxdeletecycle") == 0) {
if (atoi(argv[i + 1]) > 0) {
printf("\n [ INFO ]: Max jobs deleted by consumer per cycle set to %s", argv[i + 1]);
maxDeletePerCycle = atoi(argv[i + 1]);
}
else {
printf("\n [ WARNING ]: -maxdeletecycle requires a numerical input, such as '-maxdeletecycle 20'. Continuing with default max of 2.");
maxDeletePerCycle = 2;
}
}
// WARN NON CONSUMER ARGUMENTS
if (strcmp(argv[i], "-maxcreatecycle") == 0) {
printf("\n [ WARNING ]: -maxcreatecycle is a producer mode command. Ignoring -maxcreatecycle flag.");
}
if (strcmp(argv[i], "-maxsystem") == 0) {
printf("\n [ WARNING ]: -maxsystem is a producer mode command. Ignoring -maxsystem flag.");
}
}
}
if (strcmp(programMode, "P") == 0) {// IF COMMAND WASNT SPECIFIED
if (maxCreatePerCycle == 0) { //DEFAULT MAXCREATE ARGUMENT
printf("\n [ INFO ]: No max jobs created by producer per cycle was specified. Defaulting to 2.");
maxCreatePerCycle = 2;
}
if (maxSystem == 0) { //DEFAULT MAXSYSTEM ARGUMENT
printf("\n [ INFO ]: No max jobs for system was specified. Defaulting to 50.");
maxSystem = 50;
}
}
if (strcmp(programMode, "C") == 0) {// IF COMMAND WASNT SPECIFIED
if (maxDeletePerCycle == 0) { //DEFAULT MAXDELETE ARGUMENT
printf("\n [ INFO ]: No max jobs deleted by consumer per cycle was specified. Defaulting to 2.");
maxDeletePerCycle = 2;
}
}
if (strcmp(programMode, "P") == 0) { // PRODUCER PROCESS
printf("\n [ INITIALIZE ]: Starting producer...");
waitForConsumer();
producer(maxCreatePerCycle, maxSystem);
}
if (strcmp(programMode, "C") == 0) { // CONSUMER PROCESS
printf("\n [ INITIALIZE ]: Starting consumer...");
lookForProducer();
consumer(maxDeletePerCycle);
}
printf("\n\n"); // END OF PROGRAM
}