HandleSend.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* This Hello World demostrate a simple multithread program */
  2. #include "api.h"
  3. #include "pal.h"
  4. #include "pal_debug.h"
  5. int main(int argc, char** argv) {
  6. int nsend = 5, i;
  7. PAL_HANDLE handles[nsend];
  8. if (argc == 1) {
  9. /* parent */
  10. pal_printf("Parent: Executing the program\n");
  11. const char* args[3] = {"HandleSend", "child", NULL};
  12. char* data = "Hello World";
  13. char content[20];
  14. char uri[80];
  15. PAL_HANDLE child;
  16. int bytes;
  17. pal_printf("Parent: Creating handles\n");
  18. // Sending pipe handle
  19. handles[0] = DkStreamOpen("pipe.srv:012", PAL_ACCESS_RDWR, 0, PAL_CREATE_TRY, 0);
  20. if (!handles[0]) {
  21. pal_printf("Parent: DkStreamOpen for pipe failed\n");
  22. goto out;
  23. }
  24. // Sending pipe handle
  25. handles[1] = DkStreamOpen("udp:127.0.0.1:8000", PAL_ACCESS_RDWR, 0, PAL_CREATE_TRY, 0);
  26. if (!handles[1]) {
  27. pal_printf("Parent: DkStreamOpen for socket failed\n");
  28. goto out;
  29. }
  30. for (i = 2; i < nsend; i++) {
  31. snprintf(uri, 80, "file:test_file_%d", i - 2);
  32. handles[i] = DkStreamOpen(uri, PAL_ACCESS_RDWR, 0600, PAL_CREATE_TRY, 0);
  33. if (!handles[i]) {
  34. pal_printf("Parent: DkStreamOpen failed\n");
  35. goto out;
  36. }
  37. DkStreamSetLength(handles[i], 0);
  38. }
  39. for (i = 0; i < nsend; i++) {
  40. /* do some write */
  41. snprintf(content, sizeof(content), "%s%d", data, i);
  42. bytes = DkStreamWrite(handles[i], 0, sizeof(content), content, NULL);
  43. if (!bytes) {
  44. pal_printf("Parent: DKStreamWrite failed\n");
  45. goto out;
  46. }
  47. DkStreamFlush(handles[i]);
  48. }
  49. pal_printf("Parent: Forking child\n");
  50. child = DkProcessCreate("file:HandleSend", args);
  51. if (!child) {
  52. pal_printf("Parent: Failed creating process\n");
  53. DkProcessExit(1);
  54. }
  55. for (i = 0; i < nsend; i++) {
  56. pal_printf("Parent: Sending Handle %d\n", i);
  57. if (!DkSendHandle(child, handles[i])) {
  58. pal_printf("Send handle failed\n");
  59. goto out;
  60. }
  61. DkObjectClose(handles[i]);
  62. }
  63. pal_printf("Parent: Finished execution\n");
  64. DkObjectClose(child);
  65. } else {
  66. /* child */
  67. PAL_HANDLE parent = pal_control.parent_process;
  68. for (i = 0; i < nsend; i++) {
  69. pal_printf("Child: Receiving Handle %d\n", i);
  70. handles[i] = DkReceiveHandle(parent);
  71. if (!handles[i]) {
  72. pal_printf("Child: Failed receiving handle\n");
  73. DkProcessExit(1);
  74. }
  75. }
  76. pal_printf("Child: Reading the handles\n");
  77. for (i = 0; i < nsend; i++) {
  78. /* do some read */
  79. pal_printf("Child: Handle %d Type ", i);
  80. char data[20];
  81. switch (PAL_GET_TYPE(handles[i])) {
  82. case pal_type_file:
  83. if ((DkStreamRead(handles[i], 0, 20, data, NULL, 0)))
  84. pal_printf("File Data: %s\n", data);
  85. else
  86. pal_printf("Couldn't read\n");
  87. break;
  88. case pal_type_pipesrv:
  89. pal_printf("Pipe\n");
  90. break;
  91. case pal_type_udp:
  92. pal_printf("Udp\n");
  93. break;
  94. default:
  95. pal_printf("Unknown\n");
  96. }
  97. DkObjectClose(handles[i]);
  98. }
  99. pal_printf("Child: Finished execution\n\n");
  100. DkObjectClose(parent);
  101. }
  102. out:
  103. for (i = 0; i < nsend; i++) {
  104. DkObjectClose(handles[i]);
  105. }
  106. return 0;
  107. }