-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmsg_queue.c
More file actions
52 lines (42 loc) · 908 Bytes
/
msg_queue.c
File metadata and controls
52 lines (42 loc) · 908 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<unistd.h>
#include<sys/wait.h>
#include <string.h>
#include <sys/msg.h>
#include <sys/types.h>
struct my_msgbuf {
long mtype;
char mtext[200];
};
int main(void){
struct my_msgbuf buf;
int msqid;
key_t key;
if( (key = ftok("msg_queue.c",'B')) == -1){
perror(" error in ftok() \n");
exit(1);
}
if ( (msqid = msgget(key, 0644 | IPC_CREAT)) == -1){
perror("Error in getting msgget \n");
exit(2);
}
printf("Enter lines of text , ^D to exit : \n");
buf.mtype = 1;
while(fgets(buf.mtext,sizeof(buf.mtext),stdin) != NULL){
int len = strlen(buf.mtext);
if( buf.mtext[len-1] == '\n'){
buf.mtext[len-1] = '\0';
}
if(msgsnd(msqid,&buf,len+1,0) == -1){
perror("Error in msgsnd \n");
exit(3);
}
}
// if(msgctl(msqid, IPC_RMID, NULL) == -1){
// perror("Error in msqid \n");
// exit(4);
// }
return 0;
}