-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLab9.cpp
54 lines (42 loc) · 965 Bytes
/
Lab9.cpp
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
#include <sys/stat.h> // For mode constants
#include <fcntl.h> // For O_ constants
#include <cstdio>
#include <string.h>
#include <sys/mman.h>
#include <mqueue.h>
#include <iostream>
#include <unistd.h>
int flag = 1;
struct mq_attr attr;
mqd_t des;
int mq;
void* funcOne(void* pthreadData){
char *buf = new char[256];
while(flag == 1){
*buf = 'a';
mq = mq_send(des,buf,256,1);
if(mq >= 0) {
std::cout << buf << std::flush;
} else {
perror("mq_send");
}
sleep(1);
}
}
int main(){
pthread_t tide;
attr.mq_msgsize = 256;
attr.mq_maxmsg = 5;
des = mq_open("/QueueMessage",O_RDWR | O_CREAT,0664,&attr);
if(des < 0){
perror("mq_open");
}
mq_getattr(des,attr);
std::cout << attr.mq_maxmsg << std::endl;
pthread_create(&tide,NULL,funcOne,NULL);
getchar();
flag = 0;
pthread_join(tide,NULL);
mq_close(des);
mq_unlink("/QueueMessage");
}