Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- struct mymsg {
- long int mtype;
- char mtext[15];
- }buff;
- int main() {
- key_t key = ftok( ".", 'z');
- int id = msgget(key, IPC_CREAT | 0600);
- int r;
- buff.mtype = 2;
- strcpy(buff.mtext, "bylo");
- msgsnd(id, (struct msgbuf*)&buff, sizeof(buff), 0);
- buff.mtype = 3;
- strcpy(buff.mtext, "smaszno");
- msgsnd(id, (struct msgbuf*)&buff, sizeof(buff), 0);
- buff.mtype = 7;
- strcpy(buff.mtext, "a");
- msgsnd(id, (struct msgbuf*)&buff, sizeof(buff), 0);
- buff.mtype = 5;
- strcpy(buff.mtext, "jaszmije");
- msgsnd(id, (struct msgbuf*)&buff, sizeof(buff), 0);
- buff.mtype = 6;
- strcpy(buff.mtext, "smukwijne");
- msgsnd(id, (struct msgbuf*)&buff, sizeof(buff), 0);
- buff.mtype = 3;
- strcpy(buff.mtext, "swidrokretnie");
- msgsnd(id, (struct msgbuf*)&buff, sizeof(buff), 0);
- buff.mtype = 1;
- strcpy(buff.mtext, "na");
- msgsnd(id, (struct msgbuf*)&buff, sizeof(buff), 0);
- buff.mtype = 4;
- strcpy(buff.mtext, "zegwniku");
- msgsnd(id, (struct msgbuf*)&buff, sizeof(buff), 0);
- buff.mtype = 2;
- strcpy(buff.mtext, "wezaly");
- msgsnd(id, (struct msgbuf*)&buff, sizeof(buff), 0);
- buff.mtype = 5;
- strcpy(buff.mtext, "peliczaple");
- msgsnd(id, (struct msgbuf*)&buff, sizeof(buff), 0);
- buff.mtype = 8;
- strcpy(buff.mtext, "staly");
- msgsnd(id, (struct msgbuf*)&buff, sizeof(buff), 0);
- buff.mtype = 2;
- strcpy(buff.mtext, "smutochlijne");
- msgsnd(id, (struct msgbuf*)&buff, sizeof(buff), 0);
- buff.mtext[5]=0;
- // Każdy wypisywany komunikat będzie się składał z max. 5 bajtów
- // Jeżeli jakiś komunikat został już odczytany, to jest on USUWANY z kolejki.
- msgrcv(id, (struct msgbuf*)&buff, 5, 3, IPC_NOWAIT | MSG_NOERROR);
- puts(buff.mtext);
- /* zegwn
- Jeżeli msgtyp ma wartość większą od 0 (w tym przypadku ma wartość 4),
- to odczytuje pierwszy komunikat wskazanego typu, czyli typu 4.
- */
- for(;;) {
- r = msgrcv(id, (struct msgbuf*)&buff, 5, -4, IPC_NOWAIT | MSG_NOERROR);
- if(r < 0)
- break;
- puts(buff.mtext);
- /*
- na
- bylo
- wezal
- smuto
- smasz
- swidr
- Jeżeli msgtyp ma wartość mniejszą od 0, to z kolejki zostaną odczytane
- komunikaty o numerach typów nie większych niż wartość bezwględna msgtyp.
- W tym przypadku mamy -4, a więc czytane są wszystkie komunikaty o numerach
- mniejszych lub równych 4, chyba ża jakieś zostały już odczytane.
- */
- }
- msgrcv(id, (struct msgbuf*)&buff, 5, 0, IPC_NOWAIT | MSG_NOERROR);
- puts(buff.mtext);
- /*
- a
- Jeżeli msgtyp ma wartość równą 0, to odczytywany jest pierwszy dostępny komunikat
- w kolejce. Może to być najdawniej wysłany, ale jeśli został usunięty, to czytany jest
- następny w kolejce komunikat.
- */
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement