HandleSend.c 4.1 KB

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