Process.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include "api.h"
  2. #include "pal.h"
  3. #include "pal_debug.h"
  4. int main(int argc, char** argv, char** envp) {
  5. char buffer1[20] = "Hello World 1", buffer2[20] = "Hello World 2";
  6. char buffer3[20], buffer4[20], buffer5[20];
  7. int ret;
  8. if (argc > 1 && !memcmp(argv[1], "Child", 6)) {
  9. pal_printf("Child Process Created\n");
  10. /* check manifest name */
  11. char manifest[30] = "";
  12. DkStreamGetName(pal_control.manifest_handle, manifest, 30);
  13. pal_printf("Loaded Manifest: %s\n", manifest);
  14. /* check arguments */
  15. pal_printf("# of Arguments: %d\n", argc);
  16. for (int i = 0; i < argc; i++) {
  17. pal_printf("argv[%d] = %s\n", i, argv[i]);
  18. }
  19. DkStreamWrite(pal_control.parent_process, 0, 20, buffer1, NULL);
  20. if (pal_control.broadcast_stream == NULL) {
  21. pal_printf(
  22. "Warning: broadcast stream is not open. "
  23. "Do you have a multicast route configured?\n");
  24. } else {
  25. ret = DkStreamRead(pal_control.broadcast_stream, 0, 20, buffer5, NULL, 0);
  26. if (ret > 0)
  27. pal_printf("Broadcast Read: %s\n", buffer5);
  28. }
  29. ret = DkStreamWrite(pal_control.parent_process, 0, 20, buffer1, NULL);
  30. if (ret > 0)
  31. pal_printf("Process Write 1 OK\n");
  32. ret = DkStreamRead(pal_control.parent_process, 0, 20, buffer4, NULL, 0);
  33. if (ret > 0)
  34. pal_printf("Process Read 2: %s\n", buffer4);
  35. } else {
  36. PAL_STR args[3] = {"Process", "Child", 0};
  37. PAL_HANDLE children[3];
  38. for (int i = 0; i < 3; i++) {
  39. pal_printf("Creating process\n");
  40. children[i] = DkProcessCreate("file:Process", args);
  41. if (children[i]) {
  42. pal_printf("Process created %d\n", i + 1);
  43. DkStreamRead(children[i], 0, 20, buffer4, NULL, 0);
  44. }
  45. }
  46. pal_printf("Broadcasting message\n");
  47. if (pal_control.broadcast_stream == NULL) {
  48. pal_printf(
  49. "Warning: broadcast stream is not open. "
  50. "Do you have a multicast route configured?\n");
  51. } else {
  52. ret = DkStreamWrite(pal_control.broadcast_stream, 0, 20, buffer1, NULL);
  53. if (ret > 0)
  54. pal_printf("Broadcast Write OK\n");
  55. }
  56. for (int i = 0; i < 3; i++)
  57. if (children[i]) {
  58. ret = DkStreamRead(children[i], 0, 20, buffer3, NULL, 0);
  59. if (ret > 0)
  60. pal_printf("Process Read 1: %s\n", buffer3);
  61. ret = DkStreamWrite(children[i], 0, 20, buffer2, NULL);
  62. if (ret > 0)
  63. pal_printf("Process Write 2 OK\n");
  64. }
  65. }
  66. return 0;
  67. }