Ipc.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. #include "pal.h"
  4. #include "pal_debug.h"
  5. int main(int argc, char ** argv)
  6. {
  7. char * name = "parent";
  8. if (argc == 1) {
  9. const char * args[3];
  10. char uri[20], uri2[20];
  11. pal_snprintf(uri2, 20, "file:%s", argv[0]);
  12. args[0] = "Ipc";
  13. args[1] = uri;
  14. args[2] = NULL;
  15. void * mem = DkVirtualMemoryAlloc
  16. (NULL, 4096, 0, PAL_PROT_READ|PAL_PROT_WRITE);
  17. pal_printf("mem = %p\n", mem);
  18. pal_snprintf((char *) mem, 4096, "Hello World");
  19. uint64_t key = 0;
  20. PAL_HANDLE chdl = DkCreatePhysicalMemoryChannel(&key);
  21. if (chdl == NULL) {
  22. pal_printf ("(parent) StreamOpen Failed ---"
  23. " Make sure gipc module is loaded\n");
  24. return 0;
  25. }
  26. pal_snprintf(uri, 20, "gipc:%lld", key);
  27. PAL_HANDLE phdl = DkProcessCreate (uri2, 0, args);
  28. if (phdl == NULL)
  29. pal_printf ("ProcessCreate Failed\n");
  30. unsigned long size = 4096;
  31. DkPhysicalMemoryCommit (chdl, 1, &mem, &size, 0);
  32. DkObjectClose(chdl);
  33. char x;
  34. int rv = DkStreamRead(phdl, 0, 1, &x, NULL, 0);
  35. if (rv != 1) {
  36. pal_printf("Failed to get exit signal from child, %d\n", rv);
  37. return -1;
  38. }
  39. }
  40. else {
  41. name = argv[1];
  42. PAL_HANDLE chdl = DkStreamOpen (name, 0, 0, 0, 0);
  43. if (chdl == NULL) {
  44. pal_printf("(child) StreamOpen Failed\n");
  45. return 0;
  46. }
  47. PAL_BUF addr = NULL;
  48. PAL_NUM size = 4096;
  49. PAL_FLG prot = PAL_PROT_READ|PAL_PROT_WRITE;
  50. int len = DkPhysicalMemoryMap (chdl, 1, &addr, &size, &prot);
  51. if (!len) {
  52. pal_printf("PhysicalMemoryMap Failed\n");
  53. return 0;
  54. }
  55. pal_printf("(child) mem = %p\n", addr);
  56. pal_printf("(child) receive string: %s\n", (char *) addr);
  57. DkObjectClose(chdl);
  58. // Write a byte to the parent
  59. int rv = DkStreamWrite(pal_control.parent_process, 0, 1, "z", NULL);
  60. if (rv < 0) {
  61. pal_printf("Failed to write an exit byte\n");
  62. return -1;
  63. }
  64. }
  65. pal_printf("Enter Main Thread (%s)\n", name);
  66. DkThreadDelayExecution (3000);
  67. pal_printf("Leave Main Thread\n");
  68. return 0;
  69. }