HandleSend.c 4.1 KB

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