-
Notifications
You must be signed in to change notification settings - Fork 1
/
a.c
194 lines (180 loc) · 3.24 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
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
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<semaphore.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#include<sys/shm.h>
#include<sys/types.h>
#include<errno.h>
int idAs,idBs,idAr,idBr,Asem,Bsem;
char fflag[10];char w[10];
int P(int sem_id)
{
struct sembuf p;
p.sem_num=0;
p.sem_op=-1;
p.sem_flg=SEM_UNDO;
if(semop(sem_id,&p,1)==-1)
{
return 0;
}
return 1;
}
int V(int id)
{
struct sembuf p;
p.sem_num=0;
p.sem_op=1;
p.sem_flg=SEM_UNDO;
if(semop(id,&p,1)==-1)
{
return 0;
}
return 1;
}
union semun
{
int val;
struct semid_ds *buf;
unsigned short *array;
};
int setsem(int id,int x)
{
union semun s;
s.val=x;
if(semctl(id,0,SETVAL,s)==-1)
{
return 0;
}
else return 1;
}
int main()
{
if((idAs=semget((key_t)1,1,IPC_CREAT|0660))==-1)
return 1;
if((idBs=semget((key_t)2,1,IPC_CREAT|0660))==-1)
return 1;
if((idAr=semget((key_t)3,1,IPC_CREAT|0660))==-1)
return 1;
if((idBr=semget((key_t)4,1,IPC_CREAT|0660))==-1)
return 1;
if((Asem=semget((key_t)5,1,IPC_CREAT|0660))==-1)
return 1;
if((Bsem=semget((key_t)6,1,IPC_CREAT|0660))==-1)
return 1;
setsem(idAs,5);
setsem(idBs,5);
setsem(idAr,0);
setsem(idAr,0);
setsem(Asem,1);
setsem(Bsem,1);
struct token
{
char ch[10];
int flag;
};
struct tags
{
char ch[10];
char flag[10];
};
struct tags tag[100];
int size=5*sizeof(struct token);
int pid1,pid2;
int i=0,j=0,z=0;
char c;
pid1 = shmget((key_t)7,size,IPC_CREAT|0777);
pid2 = shmget((key_t)8,size,IPC_CREAT|0777);
struct token *add1,*add2;
add1 = (struct token *)shmat(pid1,0,0);
add2 = (struct token *)shmat(pid2,0,0);
add1->flag=0;add2->flag=0;
int number;
while(1)
{
printf("please input the order: \n");
scanf("%c",&c);
if(c=='r'&&add1[i].flag==1)//接受信息
{
P(idAr);
P(Asem);
printf("port a receive mail %s sucessfully\n",add1[i].ch);
add1[i].flag=0;
V(Asem);
V(idBs);
printf("would you want to give this mail a tag?(yes/no)\n");
scanf("%s",fflag);
if(strcmp(fflag,"yes")==0)
{
strcpy(tag[z].ch,add1[i].ch);
printf("please input the tag:\n");
scanf("%s",w);
strcpy(tag[z].flag,w);
z=(z+1)%100;
}
i=(i+1)%5;
}
else if(c=='r'&&add1[i].flag==0)
{
printf("waiting..\n");
}
else if(c=='s')//发送信息
{
printf("input the content: \n");
P(idAs);
P(Bsem);
scanf("%s",add2[j].ch);
add2[j].flag=1;
printf("send mail %s sucessfully\n",add2[j].ch);
j=(j+1)%5;
V(Bsem);
V(idBr);
}
else if(c=='f')//按tag查找信息
{
printf("please input the tag you want to find\n");
char tag1[10];
scanf("%s",tag1);
int i1=0,findflag=0;
while(i1<=z)
{
if(strcmp(tag1,tag[i1].flag)==0)
{
printf("the mail is %s\n",tag[i1].ch);
findflag=1;
break;
}
i1++;
}
if(findflag==0)
{
printf("can't find\n");
}
}
else if(c=='h')//查找历史纪录(前5次收信)
{
int m=i-1;
printf("please input the number:\n");
scanf("%d",&number);
if(number>=5)
{
printf("can't find\n");
}
else
{
if(number-i>=0)
printf("the mail is %s\n",add1[i-number].ch);
else
printf("the mail is %s\n",add1[i-number+5].ch);
}
}
else if(c=='e')
break;
else
{}
getchar();
}
}