Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/msg.h>
- #include<unistd.h>
- #include<sys/wait.h>
- struct msgbuf1 {
- long mtype;
- char mtext;
- };
- int msgCreate(key_t key)
- {
- int msgId = msgget(key,IPC_CREAT|0700);
- if( msgId == -1)
- {
- perror("Blad tworzenia kolejki komunikatow");
- exit(1);
- }
- return msgId;
- }
- int msgOpen(key_t key)
- {
- int msgId = msgget(key,IPC_CREAT|0600) ;
- if( msgId == -1)
- {
- perror("Blad uzyskania dostepu do kolejki komunikatow");
- exit(1);
- }
- return msgId;
- }
- void msgRemove(int msgId)
- {
- if(msgctl(msgId,IPC_RMID,NULL) == -1)
- {
- perror("Blad usuniecia kolejki komunikatow");
- exit(-1);
- }
- }
- void msgSend(int msgid, void*msgp,int msgsz)
- {
- if(msgsnd(msgid,msgp,msgsz,IPC_NOWAIT) == -1)
- {
- perror("Blad nadania komunikatu");
- exit(1);
- }
- }
- void msgReceive(int msgid, void*msgp,int msgsz,long msgtyp)
- {
- if(msgrcv(msgid,msgp,msgsz,msgtyp,IPC_NOWAIT) == -1)
- {
- perror("Blad odebrania komunikatu");
- exit(1);
- }
- }
- int main()
- {
- int key;
- key = ftok(".",'2');
- int pid = fork();
- msgCreate(key);
- int msgid = msgOpen(key);
- if(pid == 0)
- {
- struct msgbuf1 element2;
- msgReceive(msgid,&element2,100,5);
- printf("Element: %c \n", element2.mtext);
- }
- else
- {
- struct msgbuf1 element;
- element.mtype = 5;
- element.mtext = 'A';
- msgSend(msgid,&element,sizeof(element.mtext));
- }
- wait(NULL);
- msgRemove(msgid);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement