forked from monicadelaine/Spring_cs300_project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsgsnd_pr.c
82 lines (67 loc) · 2.1 KB
/
msgsnd_pr.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
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include "longest_word_search.h"
#include "queue_ids.h"
size_t /* O - Length of string */
strlcpy(char *dst, /* O - Destination string */
const char *src, /* I - Source string */
size_t size) /* I - Size of destination string buffer */
{
size_t srclen; /* Length of source string */
/*
* Figure out how much room is needed...
*/
size --;
srclen = strlen(src);
/*
* Copy the appropriate amount...
*/
if (srclen > size)
srclen = size;
memcpy(dst, src, srclen);
dst[srclen] = '\0';
return (srclen);
}
int main(int argc, char**argv)
{
int msqid;
int msgflg = IPC_CREAT | 0666;
key_t key;
prefix_buf sbuf;
size_t buf_length;
if (argc <= 1 || strlen(argv[1]) <2) {
printf("Error: please provide prefix of at least two characters for search\n");
printf("Usage: %s <prefix>\n",argv[0]);
exit(-1);
}
key = ftok(CRIMSON_ID,QUEUE_NUMBER);
if ((msqid = msgget(key, msgflg)) < 0) {
int errnum = errno;
fprintf(stderr, "Value of errno: %d\n", errno);
perror("(msgget)");
fprintf(stderr, "Error msgget: %s\n", strerror( errnum ));
}
else
fprintf(stderr, "msgget: msgget succeeded: msgqid = %d\n", msqid);
// We'll send message type 1
sbuf.mtype = 1;
strlcpy(sbuf.prefix,argv[1],WORD_LENGTH);
sbuf.id=0;
buf_length = strlen(sbuf.prefix) + sizeof(int)+1;//struct size without long int type
// Send a message.
if((msgsnd(msqid, &sbuf, buf_length, IPC_NOWAIT)) < 0) {
int errnum = errno;
fprintf(stderr,"%d, %ld, %s, %d\n", msqid, sbuf.mtype, sbuf.prefix, (int)buf_length);
perror("(msgsnd)");
fprintf(stderr, "Error sending msg: %s\n", strerror( errnum ));
exit(1);
}
else
fprintf(stderr,"Message(%d): \"%s\" Sent (%d bytes)\n", sbuf.id, sbuf.prefix,(int)buf_length);
exit(0);
}