-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmsgq.cpp
More file actions
34 lines (28 loc) · 849 Bytes
/
msgq.cpp
File metadata and controls
34 lines (28 loc) · 849 Bytes
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
#include <sys/types.h>
#include <sys/ipc.h>
#include <stdio.h>
#include <sys/msg.h>
struct message {
long mtype;
char mtext[80];
};
int main(int argc, char **argv)
{
const char SYNCFNAME[] = "/tmp/msg.temp";
FILE *f = fopen(SYNCFNAME, "w");
if( f == NULL ) { printf("fopen err\n"); return -1; }
fclose(f);
key_t key = ftok(SYNCFNAME, 1);
if( key == -1 ) { printf("ftok err\n"); return -1; }
int msgid = msgget(key, (IPC_CREAT | 0666) );
if( msgid == -1 ) { printf("msgget error\n"); return -1; }
message buf;
ssize_t rcvRes = msgrcv(msgid, &buf, sizeof(buf.mtext), 0, 0);
if( rcvRes < 0 ) { printf("msgrcv error\n"); return -1; }
const char MSGFILENAME[] = "/home/box/message.txt";
f = fopen(MSGFILENAME, "w");
if( fprintf(f, "%s\n", buf.mtext) < 0 ) { printf("fprintf err\n"); return -1; }
fflush(f);
fclose(f);
return 0;
}