-
Notifications
You must be signed in to change notification settings - Fork 1
/
a.c
executable file
·138 lines (99 loc) · 3.13 KB
/
a.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
//////////////////////////////////////////////////
//Program Part 1 //
// Abhineet Pandey //
// //
//////////////////////////////////////////////////
//////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include<unistd.h>
#include <semaphore.h>
#define MAX 100
int queue_array[MAX];
int rear = - 1;
int front = - 1;
int add_item =1;
sem_t sem_count;
pthread_mutex_t the_mutex;
pthread_cond_t condc, condp;
void* produce(void *ptr)
{
int i;
for (i = 0; i < MAX; i++) {
pthread_mutex_lock(&the_mutex); /* protect buffer */
while (sem_count ==32)
// while (rear-front ==31)
pthread_cond_wait(&condp, &the_mutex);
pthread_mutex_unlock(&the_mutex); /* release the buffer */
pthread_mutex_lock(&the_mutex); /* protect buffer */
insert();
pthread_cond_signal(&condc); /* wake up consumer */
pthread_mutex_unlock(&the_mutex); /* release the buffer */
}
pthread_exit(0);
}
void* consume(void *ptr)
{
int i;
for (i = 0; i < MAX; i++) {
pthread_mutex_lock(&the_mutex); /* protect buffer */
while ((front == - 1 || front > rear)) /* If there is nothing in the buffer then wait */
pthread_cond_wait(&condc, &the_mutex);
pthread_mutex_unlock(&the_mutex); /* release the buffer */
sleep(1);
pthread_mutex_lock(&the_mutex); /* protect buffer */
delete();
pthread_cond_signal(&condp); /* wake up consumer */
pthread_mutex_unlock(&the_mutex); /* release the buffer */
}
pthread_exit(0);
}
main()
{
void * exit_status;
sem_init(&sem_count, 0, 0);
pthread_t pro;
pthread_t con;
pthread_mutex_init(&the_mutex, NULL);
pthread_cond_init(&condc, NULL);
pthread_cond_init(&condp, NULL);
pthread_create(&pro, NULL, produce,NULL);
pthread_create(&con, NULL, consume,NULL);
pthread_join(pro, &exit_status);
pthread_join(con, &exit_status);
pthread_mutex_destroy(&the_mutex); /* Free up the_mutex */
pthread_cond_destroy(&condc); /* Free up consumer condition variable */
pthread_cond_destroy(&condp);
pthread_exit(NULL);
exit(0);
} /*End of main()*/
insert()
{
if (rear == MAX - 1)
printf("Queue Overflow \n");
else
{
if (front == - 1)
front = 0;
rear = rear + 1;
queue_array[rear] = add_item*4096;
sem_count++;
add_item++;
printf("Inset the element in queue : %d and semaphore count is %d \n",queue_array[rear],sem_count);
}
}
delete()
{
if (front == - 1 || front > rear)
{
printf("Queue Underflow \n");
}
else
{
printf("\t\tElement deleted from queue is : %d\t", queue_array[front]);
front = front + 1;
sem_count--;
printf("new front item is %d and rear item is %d and semaphore value is %d\n",queue_array[front],queue_array[rear],sem_count);
}
} /*End of delete() */