Skip to content

Commit b159b4e

Browse files
committed
Added
1 parent 921a6a1 commit b159b4e

File tree

3 files changed

+260
-0
lines changed

3 files changed

+260
-0
lines changed

code/central44t.c

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
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+
}

code/external44t.c

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
8+
struct {
9+
long priority; //message priority
10+
int temp; //temperature
11+
int pid; //process id
12+
int stable; //boolean for temperature stability
13+
} msgp, cmbox;
14+
15+
//MAIN function
16+
int main(int argc, char *argv[]) {
17+
18+
/* Validate that a temperature and a Unique process ID was given via the command */
19+
if(argc != 4) {
20+
printf("USAGE: Too few arguments --./central.out Temp UID");
21+
exit(0);
22+
}
23+
24+
//Setup local variables
25+
int unstable = 1;
26+
int result, length, status;
27+
int initTemp = atoi(argv[1]);
28+
int uid = atoi(argv[2]);
29+
int mailbox = atoi(argv[3]);
30+
31+
//Create the Central Servers Mailbox
32+
int msqidC = msgget(mailbox, 0600 | IPC_CREAT);
33+
34+
//Create the mailbox for this process and store it's IDs
35+
int msqid = msgget((mailbox + uid), 0600 | IPC_CREAT);
36+
37+
//Initialize the message to be sent
38+
cmbox.priority = 1;
39+
cmbox.pid = uid;
40+
cmbox.temp = initTemp;
41+
cmbox.stable = 1;
42+
43+
/* The length is essentially the size of the structure minus sizeof(mtype) */
44+
length = sizeof(msgp) - sizeof(long);
45+
46+
//While all the processes have different temps
47+
while(unstable == 1){
48+
//Send the current temp to the central server
49+
result = msgsnd( msqidC, &cmbox, length, 0);
50+
51+
//Wait for a new message from the central server
52+
result = msgrcv( msqid, &msgp, length, 1, 0);
53+
54+
//If the new message indicates all the processes have the same temp
55+
//break the loop and print out the final temperature
56+
if(msgp.stable == 0) {
57+
unstable = 0;
58+
}
59+
else { //otherwise calculate the new temp and store it
60+
int newTemp = (10*cmbox.temp + msgp.temp) / 11;
61+
cmbox.temp = newTemp;
62+
}
63+
}
64+
65+
//Remove the mailbox
66+
status = msgctl(msqid, IPC_RMID, 0);
67+
68+
//Validate nothing when wrong when trying to remove mailbox
69+
if(status != 0){
70+
printf("\nERROR closing mailbox\n");
71+
}
72+
}

code/multi

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
3+
./external44t 10 1 123456700 &
4+
./external44t 100 2 123456700 &
5+
./external44t 1000 3 123456700 &
6+
./external44t 10000 4 123456700 &
7+
./external44t 50 5 123456710 &
8+
./external44t 500 6 123456710 &
9+
./external44t 5000 7 123456710 &
10+
./external44t 50000 8 123456710 &
11+
./central44t 10 100 123456700 123456710 &

0 commit comments

Comments
 (0)