Ipc.c 2.4 KB

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