HandleSend.c 3.9 KB

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