Advertisement
Guest User

Untitled

a guest
Feb 28th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "wrapper.h"
  4.  
  5. #define PLANETS 1000000
  6. #define PLANETIPC "PlanetLab"
  7.  
  8.  
  9. mqd_t serverHandle = 0;
  10. mqd_t clientHandle = 0;
  11. int pid = 0;
  12.  
  13. void* listener(void* params) {
  14. char str[1024];
  15.  
  16. MQcreate(&clientHandle,pid); //create flags and intitialize the value of the flags
  17. //creates a new SPOSFIX to the queue or opens up an
  18. //already existing queue
  19. while(MQread(&clientHandle, str) >= 0) { //MQreads contains mq_receive
  20. // removes the oldest message with the highest priority
  21. //from the message queue
  22. printf("%s\n ", str);
  23. }
  24.  
  25. return NULL;
  26. }
  27.  
  28. int main()
  29. {
  30. int ec = 0;
  31. int i = 0;
  32.  
  33. planet_type p[4] = { {"Mars", 200.0, 300.0, 0, 0, 10000.0, NULL, 1000, ""},
  34. {"Jorden", 499.5, 700, 0, 0, 20000.0, NULL, 1999, ""},
  35. {"Jupiter", 699.5, 700, 0, 0, 389898.0, NULL, 3000, ""},
  36. {"Uranus", 390.2, 902.0, 0, 0, 92394.0, NULL, 2500, ""} };
  37. //initialaze struct value
  38. sprintf(pid,"planet-%d",getpid()); //getpid() tar fram en unik ID för klienten
  39. //sprintf lagrar informationen i strängen, i vårt fall pid
  40.  
  41. for(i = 0; i < 4; i++){
  42. printf("Planet: %s\n", p[i].name); //skriver ut planetens namn
  43. memcpy(p[i].pid, pid, 30); //tilldelar p[i].pid det som finns i pid
  44. }
  45.  
  46. if ((ec = MQconnect (&serverHandle, PLANETIPC)) == 0)
  47. {
  48. printf("Failed to connect to server!");
  49. return 0;
  50. }
  51.  
  52. threadCreate(listener,0); //create thread
  53. while(1)
  54. {
  55. //printf("Client running\n");
  56. usleep(100000);
  57. }
  58. for (i=0; i < 4; i++) {
  59. MQwrite (&serverHandle, (void*)&p[i]); // mq_send - send a message to a message queue
  60.  
  61. }
  62.  
  63. getchar(); //any key
  64.  
  65. if ((ec = MQclose(&clientHandle, PLANETIPC)) == 0){ //om värdet är större än noll lyckades nedstängningen
  66. printf("Failed to close client, watch out for memory leaks");
  67. }
  68.  
  69. if ((ec = MQclose(&serverHandle, PLANETIPC)) == 0){ //om värdet är större än noll lyckades nedstängningen
  70.  
  71. printf("Failed to close server, watch out for memory leaks");
  72. }
  73.  
  74. return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement