Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <string.h>
- #include <time.h>
- #include <stdlib.h>
- #define MSGSIZE 100
- #define MSGTYPE1 1
- #define MSGTYPE2 2
- #define MSGTYPE3 3
- struct message
- {
- long mtype;
- char mtext[MSGSIZE];
- };
- typedef struct message message;
- size_t msgln(char *msg, ssize_t code)
- {
- if (code <= 0)
- {
- return 0;
- }
- if (msg[code - 1] == '\n')
- {
- msg[code - 1] = '\0';
- }
- else
- {
- msg[code] = '\0';
- ++code;
- }
- return code;
- }
- void sendm(int queue)
- {
- message buf;
- buf.mtype = MSGTYPE1;
- int isWork = 1;
- do
- {
- ssize_t code;
- code = read(STDIN_FILENO, buf.mtext, MSGSIZE - 1);
- size_t length = msgln(buf.mtext, code);
- if (code == 0)
- {
- buf.mtype = MSGTYPE2;
- isWork = 0;
- }
- msgsnd(queue, &buf, length, 0);
- } while (isWork);
- }
- void waitm(int queue)
- {
- message buf;
- msgrcv(queue, &buf, MSGSIZE, MSGTYPE3, 0);
- printf("\nQuit\n");
- }
- int main()
- {
- int queue;
- queue = msgget(123, IPC_CREAT | 0600);
- sendm(queue);
- waitm(queue);
- msgctl(queue, IPC_RMID, NULL);
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement