Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <mqueue.h>
- #include <sys/stat.h>
- #include <sys/wait.h>
- #define MESSAGE_COUNT 5
- #define TEXT_SIZE 50
- char text_message[TEXT_SIZE];
- struct message {
- char mtext[TEXT_SIZE];
- };
- struct mq_attr message_attr = {
- .mq_maxmsg = MESSAGE_COUNT,
- .mq_msgsize = sizeof(struct message)
- };
- const char *queue_name = "/gotowe";
- int main(int argc, char *argv[]){
- printf("start %d\n");
- mqd_t mqid = mq_open(queue_name, O_CREAT | O_RDWR, 0666, &message_attr);
- if (mqid == (mqd_t) -1) {
- perror("mq_open"); exit(1);
- }
- pid_t child_a, child_b;
- child_a = fork();
- if (child_a == 0) {
- printf("proces wpisujacy priorytet 1\n");
- int i;
- for (i=0; i<MESSAGE_COUNT; i++) {
- sprintf(text_message, "Message id = %d with priority %d", i, 1);
- int result = mq_send(mqid, text_message, strlen(text_message) + 1, 1);
- if (result == -1) {
- perror("mq_send");
- }
- sleep(1);
- }
- } else {
- child_b = fork();
- if (child_b == 0) {
- printf("proces wpisujacy priorytet 2\n");
- int i;
- for (i=0; i<MESSAGE_COUNT; i++) {
- sprintf(text_message, "Message id = %d with priority %d", i+10, 2);
- int result = mq_send(mqid, text_message, strlen(text_message) + 1, 2);
- if (result == -1) {
- perror("mq_send");
- }
- sleep(1);
- }
- } else {
- sleep(2);
- printf("proces wypisujacy\n");
- do {
- unsigned int priority;
- struct message msg;
- int len = mq_receive(mqid, (char *) &msg,sizeof(msg), &priority);
- if (len == -1) {
- perror("mq_receive");
- break;
- }
- printf("Otrzymalem wiadomosc (priorytet = %d): '%s'\n", priority, msg.mtext);
- int r = mq_getattr(mqid, &message_attr);
- if (r == -1) {
- perror("mq_getattr");
- break;
- }
- sleep(1);
- } while (message_attr.mq_curmsgs);
- }
- }
- /*
- //tworzenie
- int i;
- for (i=0; i<MESSAGE_COUNT; i++) {
- printf("Obrot numer: %d ", i);
- sprintf(text_message, "Message id = %d with priority %d", i, 1);
- int result = mq_send(mqid, text_message, strlen(text_message) + 1, 1);
- if (result == -1) {
- perror("mq_send");
- }
- printf("stop\n");
- sleep(1);
- }
- //czytanie
- do {
- unsigned int priority;
- struct message msg;
- int len = mq_receive(mqid, (char *) &msg,sizeof(msg), &priority);
- if (len == -1) {
- perror("mq_receive");
- break;
- }
- printf("Otrzymalem wiadomosc (priorytet = %d): '%s'\n", priority, msg.mtext);
- int r = mq_getattr(mqid, &message_attr);
- if (r == -1) {
- perror("mq_getattr");
- break;
- }
- } while (message_attr.mq_curmsgs);
- */
- mq_close(mqid);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement