Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* basic operations with messages, UNIX-native */
- /* pointer arithmetics:
- *
- * push(pointer, v) INDICATES *(pointer++) = v
- * pop(pointer) INDICATES *(--pointer)
- * (*++v)[0] INDICATES (**++v)
- * INDICATES first char in v
- * INDICATES name of string/vector v
- * likewise, *v[0] INDICATES **v
- * and *v[n] INDICATES **(v + n)
- * returntype (*funct)(args) INDICATES a function funct with arguments args which returns...
- * char **argv INDICATES pointer to char pointer
- * int(*v)[len] INDICATES pointer "v" to a vector of "len" int elements
- * int *v[len] INDICATES vector "v" of "len" pointers to int elements
- * void *funct() INDICATES function "funct" that returns a pointer-to-void
- * void (*funct)() INDICATES pointer to a function "funct" that returns void
- *
- */
- /* useful characters: [] # */
- # include <stdio.h>
- # include <sys/ipc.h> // system utilities
- # include <sys/msg.h> // system utilities
- # include <string.h>
- # define SIZE 1024; // message size
- typedef struct { // struct that represents a message
- long type;
- char text[]; // original: char text[SIZE], generates compilation error
- } msg;
- int main(int argc, char * argv[])
- {
- /* message queue creation */
- key_t key = 30; // univocally identifies the queue
- int descriptor = msgget(key, IPC_CREAT|0666); // creates the queue and returns its ID
- if(descriptor == -1) printf("Call failure in msgget (0)\n");
- /* queue operations */
- // DESCRIPTOR and KEY will be used without being re-instantiated
- key = 40;
- descriptor = msgget(key, IPC_CREAT|IPC_EXCL|0666);
- if(descriptor == -1)
- {
- printf("Call failure in msgget (1)\n");
- exit(1);
- }
- sleep(1);
- printf("End sleep\n");
- msgctl(descriptor, IPC_RMID, NULL); // does something with the queue; in this case, removes the ID (RMID)
- /* messages: create and send */
- // DESCRIPTOR and KEY will be used without being re-instantiated
- key = 50;
- msg message; // struct!
- int sendsuccess; // if > 0 indicates success with send operation
- descriptor = msgget(key, IPC_CREAT|0666);
- if(descriptor == -1)
- {
- printf("Call failure in msgget (2)\n");
- exit(1);
- }
- message.type = 1; // message initialisation
- * message.text = "Hello world!"; // message initialisation
- sendsuccess = msgsnd(descriptor, &message, strlen(message.text) + 1, IPC_NOWAIT); // sends the message, non-blocking
- /* receive and remove */
- sendsuccess = msgrcv(descriptor, &message, strlen(message.text) + 1, 1, 0); // type: 1; blocking flag: 0 (nonblock)
- if(sendsuccess != -1) printf("%s\n", message.text); // NOTE: return unexpected; why?
- sendsuccess = msgctl(descriptor, IPC_RMID, NULL); // removes the descriptor
- /* Sample at: pg. 38
- * One function for the message producer, one for the consumer, both in a do-while(!quit)
- * main() contains:
- * if(fork() != 0)
- * {
- * if(fork() != 0)
- * {
- * wait(&status);
- * wait(&status);
- * }
- * else producer(queue_descriptor);
- * }
- * else consumer(queue_descriptor);
- * ...Which allows the reading of all messages first, and then the send.
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement