Ipc.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include "pal.h"
  2. #include "pal_debug.h"
  3. #include "api.h"
  4. int main(int argc, char ** argv)
  5. {
  6. char * name = "parent";
  7. if (argc == 1) {
  8. const char * args[3];
  9. char uri[20];
  10. args[0] = "Ipc";
  11. args[1] = uri;
  12. args[2] = NULL;
  13. void * mem = (void *) DkVirtualMemoryAlloc(NULL,
  14. pal_control.alloc_align, 0,
  15. PAL_PROT_READ|PAL_PROT_WRITE);
  16. pal_printf("mem = %p\n", mem);
  17. snprintf((char *) mem, 4096, "Hello World");
  18. PAL_NUM key = 0;
  19. PAL_HANDLE chdl = DkCreatePhysicalMemoryChannel(&key);
  20. if (chdl == NULL) {
  21. pal_printf ("(parent) DkCreatePhysicalMemoryChannel Failed,"
  22. " Make sure gipc module is loaded\n");
  23. return 0;
  24. }
  25. snprintf(uri, 20, "gipc:%ld", key);
  26. PAL_HANDLE phdl = DkProcessCreate("file:Ipc", args);
  27. if (phdl == NULL)
  28. pal_printf ("ProcessCreate Failed\n");
  29. PAL_PTR addr = (PAL_PTR) mem;
  30. PAL_NUM size = pal_control.alloc_align;
  31. DkPhysicalMemoryCommit(chdl, 1, &addr, &size);
  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. } else {
  40. name = argv[1];
  41. PAL_HANDLE chdl = DkStreamOpen(name, 0, 0, 0, 0);
  42. if (chdl == NULL) {
  43. pal_printf("(child) StreamOpen Failed\n");
  44. return 0;
  45. }
  46. PAL_PTR addr = NULL;
  47. PAL_NUM size = pal_control.alloc_align;
  48. PAL_FLG prot = PAL_PROT_READ|PAL_PROT_WRITE;
  49. int len = DkPhysicalMemoryMap (chdl, 1, &addr, &size, &prot);
  50. if (!len) {
  51. pal_printf("PhysicalMemoryMap Failed\n");
  52. return 0;
  53. }
  54. pal_printf("(child) mem = %p\n", addr);
  55. pal_printf("(child) receive string: %s\n", (char *) addr);
  56. DkStreamDelete(chdl, 0);
  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. }