1
+ #include <sys/ipc.h>
2
+ #include <sys/types.h>
3
+ #include <sys/msg.h>
4
+ #include <stdio.h>
5
+ #include <stdlib.h>
6
+ #include <unistd.h>
7
+ #include <semaphore.h>
8
+
9
+ #define NUM_PROCESSES 4
10
+
11
+ struct mInfo {
12
+ long priority ; //message priority
13
+ int temp ; //temperature
14
+ int pid ; //process id
15
+ int stable ; //boolean for temperature stability
16
+ };
17
+
18
+ struct mInfo msgp [2 ];
19
+ struct mInfo cmbox [2 ];
20
+
21
+ struct pInfo {
22
+ int mailbox ;
23
+ int initTemp ;
24
+ int p_Num ;
25
+ int p_Range ;
26
+ int counter ;
27
+ int g_Num ;
28
+ };
29
+
30
+ int j = 0 ;
31
+
32
+ void * calc_temp (void * arg ) {
33
+ struct pInfo * p = (struct pInfo * )arg ;
34
+ //Set up local variables
35
+ //counter for loops
36
+ int i , result , length , status , temperature ;
37
+
38
+ //central process ID
39
+ int uid = 0 ;
40
+
41
+ //mailbox IDs for all processes
42
+ int msqid [NUM_PROCESSES ];
43
+
44
+ //boolean to denote temp stability
45
+ int unstable = 1 ;
46
+
47
+ //array of process temperatures
48
+ int tempAry [NUM_PROCESSES ];
49
+
50
+ //Create the Central Servers Mailbox
51
+ int msqidC = msgget (p -> mailbox , 0600 | IPC_CREAT );
52
+
53
+ for (i = p -> p_Num ; i <= p -> p_Range ; i ++ ){
54
+ msqid [(p -> counter - 1 )] = msgget ((p -> mailbox + i ), 0600 | IPC_CREAT );
55
+ p -> counter ++ ;
56
+ }
57
+
58
+ //Initialize the message to be sent
59
+ msgp [p -> g_Num ].priority = 1 ;
60
+ msgp [p -> g_Num ].pid = uid ;
61
+
62
+ msgp [p -> g_Num ].temp = p -> initTemp ;
63
+ msgp [p -> g_Num ].stable = 1 ;
64
+
65
+ /* The length is essentially the size of the structure minus sizeof(mtype) */
66
+ length = sizeof (struct mInfo ) - sizeof (long );
67
+
68
+ //While the processes have different temperatures
69
+ while (unstable == 1 ){
70
+ int sumTemp = 0 ; //sum up the temps as we loop
71
+ int stable = 1 ; //stability trap
72
+
73
+ // Get new messages from the processes
74
+ for (i = 0 ; i < NUM_PROCESSES ; i ++ ){
75
+ result = msgrcv ( msqidC , & cmbox [p -> g_Num ],
76
+ length , 1 , 0 );
77
+
78
+ /* If any of the new temps are different from the old temps then we are still unstable. Set the new temp to the corresponding process ID in the array */
79
+ if (tempAry [(cmbox [p -> g_Num ].pid - 1 )] != cmbox [p -> g_Num ].temp ) {
80
+ stable = 0 ;
81
+ tempAry [(cmbox [p -> g_Num ].pid - 1 )] = cmbox [p -> g_Num ].temp ;
82
+ }
83
+
84
+ //Add up all the temps as we go for the temperature algorithm
85
+ sumTemp += cmbox [p -> g_Num ].temp ;
86
+ }
87
+
88
+ /*When all the processes have the same temp twice: 1) Break the loop 2) Set the messages stable field to stable*/
89
+ if (stable ){
90
+ unstable = 0 ;
91
+ msgp [p -> g_Num ].stable = 0 ;
92
+ }
93
+ else { //Calculate a new temp and set the temp field in the message
94
+ int newTemp = (msgp [p -> g_Num ].temp + 1000 * sumTemp ) / (1000 * NUM_PROCESSES + 1 );
95
+ usleep (100000 );
96
+ msgp [p -> g_Num ].temp = newTemp ;
97
+ printf ("The new temp in GROUP %d is: %d\n" ,p -> g_Num ,newTemp );
98
+ temperature = newTemp ;
99
+ }
100
+
101
+ /* Send a new message to all processes to inform of new temp or stability */
102
+ for (i = 0 ; i < NUM_PROCESSES ; i ++ ){
103
+ result = msgsnd ( msqid [i ], & msgp [p -> g_Num ], length , 0 );
104
+ }
105
+ }
106
+
107
+ //Remove the mailbox
108
+ status = msgctl (msqidC , IPC_RMID , 0 );
109
+
110
+ //Validate nothing when wrong when trying to remove mailbox
111
+ if (status != 0 ){
112
+ printf ("\nERROR closing mailbox\n" );
113
+ }
114
+
115
+ pthread_exit (temperature );
116
+ }
117
+
118
+ int main (int argc , char * argv []) {
119
+ struct timeval t1 , t2 ;
120
+ double elapsedTime ;
121
+
122
+ // start timer
123
+ gettimeofday (& t1 , NULL );
124
+
125
+ int i = 0 ;
126
+ int tempg1 , tempg2 ;
127
+
128
+ printf ("\nStarting Server...\n" );
129
+
130
+ //Validate that a temperature was given via the command line
131
+ if (argc != 5 ) {
132
+ printf ("USAGE: Too few arguments --./central.out Temp" );
133
+ exit (0 );
134
+ }
135
+
136
+ struct pInfo process [2 ];
137
+
138
+ /* First Group */
139
+ process [0 ].mailbox = atoi (argv [3 ]);
140
+ process [0 ].initTemp = atoi (argv [1 ]);
141
+ process [0 ].p_Num = 1 ;
142
+ process [0 ].p_Range = 4 ;
143
+ process [0 ].counter = 1 ;
144
+ process [0 ].g_Num = 0 ;
145
+
146
+ /* Second Group */
147
+ process [1 ].mailbox = atoi (argv [4 ]);
148
+ process [1 ].initTemp = atoi (argv [2 ]);
149
+ process [1 ].p_Num = 5 ;
150
+ process [1 ].p_Range = 8 ;
151
+ process [1 ].counter = 1 ;
152
+ process [1 ].g_Num = 1 ;
153
+
154
+ pthread_t thread [2 ];
155
+
156
+ /* Thread Creation */
157
+ pthread_create (& thread [0 ], NULL , & process , & process [0 ]);
158
+ pthread_create (& thread [1 ], NULL , & process , & process [1 ]);
159
+
160
+ /* Wait for all threads to finish before continuing */
161
+ pthread_join (thread [0 ], (void * * )& tempg1 );
162
+ pthread_join (thread [1 ], (void * * )& tempg2 );
163
+
164
+ printf ("Temperature Stabilized in Group1: %d \n" , tempg1 );
165
+ printf ("Temperature Stabilized in Group2: %d \n" , tempg2 );
166
+
167
+ // stop timer
168
+ gettimeofday (& t2 , NULL );
169
+
170
+ // compute and print the elapsed time in millisec
171
+ elapsedTime = (t2 .tv_sec - t1 .tv_sec ) * 1000.0 ; // sec to ms
172
+ elapsedTime += (t2 .tv_usec - t1 .tv_usec ) / 1000.0 ; // us to ms
173
+
174
+ printf ("The elapsed time is %fms\n" , elapsedTime );
175
+
176
+ return EXIT_SUCCESS ;
177
+ }
0 commit comments