-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpc11.c
206 lines (159 loc) · 3.82 KB
/
pc11.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#define __LIBRARY__
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <linux/kernel.h>
#include <fcntl.h>
#include <sys/types.h>
#define BUFFERSIZE 10
static _syscall2(sem_t *,sem_open,const char *,name,int,value);
static _syscall1(int,sem_post,sem_t *,sem);
static _syscall1(int,sem_wait,sem_t *,sem);
static _syscall1(int,sem_getvalue,sem_t *,sem);
static _syscall1(int,sem_unlink,const char*,name);
sem_t *empty;
sem_t *full;
sem_t *mutex;
int main(void)
{
char err_desc[255];
char empty_sem[]= "empty";
char full_sem[]= "full";
char mutex_sem[]="mutex";
int in = open("pc.log", O_CREAT|O_TRUNC|O_RDWR, 0666);
int of = open("pc.log", O_CREAT|O_TRUNC|O_RDWR, 0666);
int log = open("pclog.log", O_CREAT|O_TRUNC|O_RDWR, 0666);
char buflog[255];
int tmp;
int itemValue = -1;
int fileLen,tmpValue,i,j;
if(fcntl(in,F_CHSIZE,0)!=0)
{
printf("in main process. ftruncate error!\n");
close(in);
close(of);
return 0;
}
empty = sem_open(empty_sem,BUFFERSIZE);
if(empty == NULL)
{
printf("create semaphore empty error!\n");
exit(1);
}
full = sem_open(full_sem,0);
if(full ==NULL)
{
printf("create semaphore full error!\n");
exit(1);
}
mutex = sem_open(mutex_sem,1);
if(mutex == NULL)
{
printf("create semaphore mutex error!\n");
exit(1);
}
tmpValue=sem_getvalue(empty);
printf("now empty's value = %d\n",tmpValue);
tmpValue=sem_getvalue(mutex);
printf("now mutex's value = %d\n",tmpValue);
tmpValue=sem_getvalue(full);
printf("now full's value = %d\n",tmpValue);
if(!fork())
{
printf("producer process %u !now itemValue=%d\n",getpid(),itemValue);
while(itemValue<50)
{
itemValue++;
if(sem_wait(empty)!=0)
{
printf("in producer sem_wait(empty) error!\n");
perror(err_desc);
break;
}
if(sem_wait(mutex)!=0)
{
printf("in producer sem_wait(mutex) error!\n");
perror(err_desc);
break;
}
fileLen=lseek(in,0,SEEK_END);
write(in,&itemValue,sizeof(itemValue));
if(sem_post(mutex)!=0)
{
printf("in producer sem_post(mutex) error!\n");
perror(err_desc);
break;
}
if(sem_post(full)!=0)
{
printf("in producer sem_post(full) error!\n");
perror(err_desc);
break;
}
}
tmpValue=sem_getvalue(empty);
printf("now empty's value = %d\n",tmpValue);
tmpValue=sem_getvalue(mutex);
printf("now mutex's value = %d\n",tmpValue);
tmpValue=sem_getvalue(full);
printf("now full's value = %d\n",tmpValue);
while(1)
;
close(in);
}
for(i=0; i < 5; i++)
{
if(!fork())
{
printf("customer process(%u) begin to run!\n",getpid());
while(1)
{
if(sem_wait(full)!=0)
{
printf("in customer %u sem_wait(full) error!\n",getpid());
perror(err_desc);
break;
}
if(sem_wait(mutex)!=0)
{
printf("in customer %u,sem_post(empty) error!",getpid());
perror(err_desc);
break;
}
lseek(of,0,SEEK_SET);
read(of,&itemValue,sizeof(itemValue));
/*printf("%u:%d\n",getpid(),itemValue);*/
lseek(log,0,SEEK_END);
sprintf(buflog,"%u:%d\n",getpid(),itemValue);
write(log,&buflog,sizeof(char)*strlen(buflog));
fileLen=lseek(in,0,SEEK_END);
for(j=1;j<(fileLen/sizeof(itemValue));j++)
{
lseek(in,j*sizeof(itemValue),SEEK_SET);
read(in,&tmpValue,sizeof(tmpValue));
lseek(in,(j-1)*sizeof(itemValue),SEEK_SET);
write(in,&tmpValue,sizeof(tmpValue));
}
if(fcntl(in,F_CHSIZE,fileLen-sizeof(tmpValue))!=0)
{
printf("ftruncate error!\n");
break;
}
if(sem_post(mutex)!=0)
{
printf("in customer %u,sem_post(empty) error!\n",getpid());
perror(err_desc);
break;
}
if(sem_post(empty)!=0)
{
printf("in customer %u,sem_post(empty) error!\n",getpid());
perror(err_desc);
}
}
close(of);
}
}
printf("now, main process exit!\n");
return 0;
}