Skip to content

Instantly share code, notes, and snippets.

@iTrooz
Created July 8, 2022 17:00
Show Gist options
  • Save iTrooz/55d6bf8d02f379fd713bdf3d0484db9f to your computer and use it in GitHub Desktop.
Save iTrooz/55d6bf8d02f379fd713bdf3d0484db9f to your computer and use it in GitHub Desktop.
Linux XSI message Hello World (msgget msgsnd msgrcv)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/msg.h>
int main(){
char* buf = "Hello world !";
unsigned long len = strlen(buf);
char* buf2 = alloca(len);
key_t key = ftok("/tmp", rand()%100); // I don't think it actually needs to be random
printf("key=%i\n", key);
int qid = msgget(key, IPC_CREAT|0600);
perror("msgget");
printf("qid=%i\n", qid);
msgsnd(qid, buf, len, 0);
perror("msgsnd");
msgrcv(qid, buf2, len, 0, 0);
perror("rcvsnd");
printf("RCV=%s\n", buf2);
}
@iTrooz
Copy link
Author

iTrooz commented Jul 8, 2022

Output :

key=1394868225
qid=32939
RCV=Hello world !
msgget: Success
msgsnd: Success
rcvsnd: Success

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment