Created
October 19, 2014 18:22
-
-
Save benjic/d7be1b4dcdbf79fd51d0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <sys/errno.h> | |
#include <sys/types.h> | |
#include <sys/ipc.h> | |
#include <sys/msg.h> | |
#include <stdlib.h> | |
int main(int argc,char *argv[]){ | |
// Memory structure to represent message in queue | |
struct { | |
long priority; | |
int number; | |
} msgp; | |
// Instantiate queue item | |
msgp.priority= 2; | |
msgp.number = 0; | |
int msqid; // key for recieve queue | |
int supplierQid; // key for send qeueu | |
int status; // placeholder for status | |
// Add key variables | |
int msqid_key, qidSupplier_key; | |
// get a key based on pwd directory R byte | |
msqid_key = ftok(".", 'R'); | |
// Negative result is error | |
if (msqid_key < 0) | |
perror("Unable to create msqid_key\n"); | |
// get a key based on pwd directory and S byte | |
qidSupplier_key = ftok(".", 'S'); | |
// Negative result is error | |
if ( qidSupplier_key < 0) | |
perror("Unable to create qidSuppler_key\n"); | |
//~ Create a Q for receiving information (you can use key | |
//~ as unique identifier) | |
msqid = msgget( msqid_key, 0660 | IPC_CREAT); | |
//~ Create a Q for sending information to the requester | |
//~ (you can use key-1 as the unique identifier) | |
supplierQid = msgget( qidSupplier_key, 0660 | IPC_CREAT); | |
//~ Loop | |
while ( 1 ) { | |
//~ Receive number from requester | |
status = msgrcv(msqid ,&msgp,sizeof(msgp)-sizeof(long),2,0); | |
if ( status >= 0 ) { | |
//~ Calculate square of that number | |
msgp.number = msgp.number * msgp.number; | |
//~ Send that calculated number back to the requester | |
status = msgsnd( supplierQid, &msgp, sizeof(msgp)-sizeof(long), 0); | |
if ( status ) { | |
printf("Error sending message to requester.\n"); | |
return -1; | |
} | |
} else { | |
printf("Error receiving message from requester.\n"); | |
return -1; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment