Skip to content

Instantly share code, notes, and snippets.

@benjic
Last active August 29, 2015 14:07
Show Gist options
  • Save benjic/2c577e9028cc5cf517a1 to your computer and use it in GitHub Desktop.
Save benjic/2c577e9028cc5cf517a1 to your computer and use it in GitHub Desktop.
A collection of files related to producer consumer programming models.
#include <sys/errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//removed by doug
//#include "message.h"
int main(int argc,char *argv[]){
// Memory structure to represent message in queue
struct {
long priority;
int number;
} msgp;
int msqid,status; // id for receive queue and status
int qidSupplier; // id for send queue plus key for getting queues
int i, random_number; // incremental variable and placeholder from random
// Instantiate queue item
msgp.priority= 2;
msgp.number = 0;
// Let's seed the random number generator with the clock
srand(time(NULL));
// Add key variables
int msqid_key, qidSupplier_key;
// get a key based on pwd directory R byte
msqid_key = ftok(".", 'S');
// 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(".", 'R');
// Negative result is error
if ( qidSupplier_key < 0)
perror("Unable to create qidSuppler_key\n");
//~ Create a Q for receiving information (you can use
//~ the variable key as the unique identifier)
msqid = msgget( msqid_key, 0660 | IPC_CREAT);
//~ Create a Q for sending information to the supplier
//~ (you can use key+1 as the unique identifier)
qidSupplier = msgget( qidSupplier_key, 0660 | IPC_CREAT);
//~ Loop 100 times
for ( i = 0; i < 100; i++ ) {
//~ Generate a random number (between 0 and 500)
//
random_number = rand() % 500; // In order to pair a base n and response n^2 we save n
msgp.number = random_number; // Set queue item equal to random number
//~ Send the number to the supplier
status = msgsnd( qidSupplier, &msgp, sizeof(msgp)-sizeof(long), 0);
if ( status >= 0 ) {
//~ Receive the answer back from the supplier
status = msgrcv(msqid, &msgp, sizeof(msgp)-sizeof(long), 2, 0);
if (status >= 0) {
//~ Display the solution to the console
printf("%i**2 = %i\n", random_number, msgp.number);
if ( ( random_number * random_number ) != msgp.number ) {
printf("Error: recived value (%i) is not square of sent value(%i).\n", msgp.number, i);
return -1;
}
} else {
printf("Error receiving response from supplier.\n");
return -1;
}
} else {
printf("Error sending number to supplier(%i).\n", status);
return -1;
}
}
printf("all done\n");
return(0);
}
#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;
}
package main
import "fmt"
func main() {
fmt.Printf("Hello, world!")
}
@benjic
Copy link
Author

benjic commented Oct 19, 2014

You can see I used the ftok(char* path, char key) to get id's relavtive to the execution path of the program. This way there is no collisions.

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