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 <string.h>
- #include <unistd.h>
- #include <ctype.h>
- struct msgbuf1{
- long mtype;
- char text[10];
- };
- int check(int code, const char *msg){
- if (code == -1) {
- perror(msg);
- exit(0);
- }
- return code;
- }
- int msgCreate(key_t key) {
- return check(msgget(key, IPC_CREAT | 0700), __FUNCTION__);
- }
- int msgOpen(key_t key) {
- return check(msgget(key, IPC_CREAT | 0700), __FUNCTION__);
- }
- int msgRemove(int msgId) {
- check(msgctl(msgId, IPC_RMID, NULL), __FUNCTION__);
- }
- int msgSend(int msgid, void* msgp, size_t msgsz) {
- const int msgflg = 0;
- check(msgsnd(msgid, msgp, msgsz, msgflg), __FUNCTION__);
- }
- int msgReceive(int msgid, void* msgp, size_t msgsz,long msgtyp){
- const int msgflg = 0;
- check(msgrcv(msgid,msgp,msgsz,msgtyp,msgflg),__FUNCTION__);
- }
- int main(){
- int key;
- key = ftok(".",'g');
- msgCreate(key);
- int msgiiid = msgOpen(key);
- struct msgbuf1 element;
- element.mtype = 2;
- strcpy(element.text, "abcdefghj");
- msgSend(msgiiid,&element,sizeof(element.text));
- return 0;
- }
- #include <stdlib.h>
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/msg.h>
- #include <string.h>
- #include <unistd.h>
- #include <ctype.h>
- struct msgbuf1 {
- long mtype;
- char text[10];
- };
- int check(int code, const char *msg) {
- if (code == -1) {
- perror(msg);
- exit(0);
- }
- return code;
- }
- int msgCreate(key_t key) {
- return check(msgget(key, IPC_CREAT | IPC_EXCL | 0700), __FUNCTION__);
- }
- int msgOpen(key_t key) {
- return check(msgget(key, IPC_CREAT | 0700), __FUNCTION__);
- }
- int msgRemove(int msgId) {
- check(msgctl(msgId, IPC_RMID, NULL), __FUNCTION__);
- }
- int msgSend(int msgid, void* msgp, size_t msgsz) {
- const int msgflg = 0;
- check(msgsnd(msgid, msgp, msgsz, msgflg), __FUNCTION__);
- }
- int msgReceive(int msgid, void* msgp, size_t msgsz,long msgtyp){
- const int msgflg = 0;
- check(msgrcv(msgid,msgp,msgsz,msgtyp,msgflg),__FUNCTION__);
- }
- int main(){
- int key;
- key = ftok(".",'g');
- int msgiiid1 = msgOpen(key);
- struct msgbuf1 element2;
- msgReceive(msgiiid1,&element2,sizeof(element2),2);
- for(int i = 0; i < sizeof(element2.text); i++){
- printf("Element: %c \n", toupper(element2.text[i]));
- sleep(1);
- }
- msgRemove(msgiiid1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement