Repository navigation
Expand file tree
/
Copy pathminor5test.c
More file actions
150 lines (119 loc) · 2.83 KB
/
Copy pathminor5test.c
File metadata and controls
150 lines (119 loc) · 2.83 KB
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
144
145
146
147
148
149
150
/*
* minor5.c - using producer-consumer paradigm.
*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define NITEMS 10 // number of items in shared buffer
// shared variables
char shared_buffer[NITEMS]; // echo buffer
int shared_count; // item count
pthread_mutex_t mutex; // pthread mutex
unsigned int prod_index = 0; // producer index into shared buffer
unsigned int cons_index = 0; // consumer index into shard buffer
// function prototypes
void * producer(void *arg);
void * consumer(void *arg);
int main()
{
pthread_t prod_tid, cons_tid1, cons_tid2;
// initialize pthread variables
pthread_mutex_init(&mutex, NULL);
// start producer thread
pthread_create(&prod_tid, NULL, producer, NULL);
// start consumer threads
pthread_create(&cons_tid1, NULL, consumer, NULL);
pthread_create(&cons_tid2, NULL, consumer, NULL);
// wait for threads to finish
pthread_join(prod_tid, NULL);
pthread_join(cons_tid1, NULL);
pthread_join(cons_tid2, NULL);
// clean up
pthread_mutex_destroy(&mutex);
return 0;
}
// producer thread executes this function
void * producer(void *arg)
{
char key;
long double usage;
double load[1];
int cpu_load;
FILE *fp;
while (1)
{
fp = fopen("/proc/loadavg","r");
if(fp == NULL){ // File open error
perror("Error");
}
else{
fscanf(fp,"%Lf",&usage);
fclose(fp);
printf("The current CPU utilization is : %Lf\n",usage);
cpu_load = (int)(usage * 100);
//return cpu_load;
printf("The percent of usage is: %d\n",cpu_load);
}
/*
getloadavg(load,1);
printf("load average : %f\n",load[0]);
*/
// read input key
scanf("%c", &key);
while (1)
{
// acquire mutex lock
pthread_mutex_lock(&mutex);
// if buffer is full, release mutex lock and check again
if (shared_count == NITEMS)
pthread_mutex_unlock(&mutex);
else
break;
}
// store key in shared buffer
shared_buffer[prod_index] = key;
// update shared count variable
shared_count++;
// update producer index
if (prod_index == NITEMS - 1)
prod_index = 0;
else
prod_index++;
// release mutex lock
pthread_mutex_unlock(&mutex);
}
return NULL;
}
// consumer thread executes this function
void * consumer(void *arg)
{
char key;
int id = (int)pthread_self();
while (1)
{
while (1)
{
// acquire mutex lock
pthread_mutex_lock(&mutex);
// if buffer is empty, release mutex lock and check again
if (shared_count == 0)
pthread_mutex_unlock(&mutex);
else
break;
}
// read key from shared buffer
key = shared_buffer[cons_index];
// echo key
printf("consumer %d %c\n", id, key);
// update shared count variable
shared_count--;
// update consumer index
if (cons_index == NITEMS - 1)
cons_index = 0;
else
cons_index++;
// release mutex lock
pthread_mutex_unlock(&mutex);
}
return NULL;
}