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