Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include "wrapper.h"
- #define PLANETS 1000000
- #define PLANETIPC "PlanetLab"
- mqd_t serverHandle = 0;
- mqd_t clientHandle = 0;
- int pid = 0;
- void* listener(void* params) {
- char str[1024];
- MQcreate(&clientHandle,pid); //create flags and intitialize the value of the flags
- //creates a new SPOSFIX to the queue or opens up an
- //already existing queue
- while(MQread(&clientHandle, str) >= 0) { //MQreads contains mq_receive
- // removes the oldest message with the highest priority
- //from the message queue
- printf("%s\n ", str);
- }
- return NULL;
- }
- int main()
- {
- int ec = 0;
- int i = 0;
- planet_type p[4] = { {"Mars", 200.0, 300.0, 0, 0, 10000.0, NULL, 1000, ""},
- {"Jorden", 499.5, 700, 0, 0, 20000.0, NULL, 1999, ""},
- {"Jupiter", 699.5, 700, 0, 0, 389898.0, NULL, 3000, ""},
- {"Uranus", 390.2, 902.0, 0, 0, 92394.0, NULL, 2500, ""} };
- //initialaze struct value
- sprintf(pid,"planet-%d",getpid()); //getpid() tar fram en unik ID för klienten
- //sprintf lagrar informationen i strängen, i vårt fall pid
- for(i = 0; i < 4; i++){
- printf("Planet: %s\n", p[i].name); //skriver ut planetens namn
- memcpy(p[i].pid, pid, 30); //tilldelar p[i].pid det som finns i pid
- }
- if ((ec = MQconnect (&serverHandle, PLANETIPC)) == 0)
- {
- printf("Failed to connect to server!");
- return 0;
- }
- threadCreate(listener,0); //create thread
- while(1)
- {
- //printf("Client running\n");
- usleep(100000);
- }
- for (i=0; i < 4; i++) {
- MQwrite (&serverHandle, (void*)&p[i]); // mq_send - send a message to a message queue
- }
- getchar(); //any key
- if ((ec = MQclose(&clientHandle, PLANETIPC)) == 0){ //om värdet är större än noll lyckades nedstängningen
- printf("Failed to close client, watch out for memory leaks");
- }
- if ((ec = MQclose(&serverHandle, PLANETIPC)) == 0){ //om värdet är större än noll lyckades nedstängningen
- printf("Failed to close server, watch out for memory leaks");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement