Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Program odbierający komunikaty 0 w porządku malejącym lub rosnącym
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #define MSG_KEY 1234 // klucz kolejki komunikatów
- // struktura komunikatu 0
- struct msgbuf {
- long mtype; // typ komunikatu
- char mtext[1]; // treść komunikatu (pusta)
- };
- int main() {
- int msgid; // identyfikator kolejki komunikatów
- struct msgbuf msg; // komunikat do odebrania
- char order; // wybór porządku odbierania
- long mtype; // typ komunikatu do odebrania
- // otwieranie kolejki komunikatów
- msgid = msgget(MSG_KEY, 0);
- if (msgid == -1) {
- perror("msgget");
- exit(1);
- }
- // pytanie użytkownika o porządek odbierania
- printf("W jakim porządku chcesz odbierać komunikaty 0? (r - rosnąco, m - malejąco)\n");
- scanf("%c", &order);
- if (order == 'r') {
- mtype = 1; // odbieranie od typu 1
- } else if (order == 'm') {
- mtype = 5; // odbieranie od typu 5
- } else {
- printf("Nieprawidłowy wybór\n");
- exit(2);
- }
- // odbieranie komunikatów 0 w wybranym porządku
- while (1) {
- // odbieranie komunikatu z kolejki
- if (msgrcv(msgid, &msg, 0, mtype, IPC_NOWAIT) == -1) {
- // jeśli nie ma komunikatu danego typu, zmienić typ
- if (order == 'r') {
- mtype++; // zwiększyć typ
- } else {
- mtype--; // zmniejszyć typ
- }
- // jeśli przekroczono zakres typów, zakończyć program
- if (mtype < 1 || mtype > 5) {
- break;
- }
- } else {
- // jeśli odebrano komunikat, wyświetlić jego typ
- printf("Odebrano komunikat typu %ld\n", msg.mtype);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement