Fork.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* This Hello World simply print out "Hello World" */
  2. #include "pal.h"
  3. #include "pal_debug.h"
  4. struct stack_frame {
  5. struct stack_frame* next;
  6. void* ret;
  7. };
  8. PAL_HANDLE _fork (void * args)
  9. {
  10. register struct stack_frame * fp __asm__ ("ebp");
  11. struct stack_frame * frame = fp;
  12. if (args == NULL) {
  13. struct stack_frame cur_frame = *frame;
  14. pal_printf("return address is %p\n", cur_frame.ret);
  15. return DkThreadCreate(&_fork, &cur_frame);
  16. }
  17. else {
  18. struct stack_frame * las_frame = (struct stack_frame *) args;
  19. pal_printf("(in child) return address is %p\n", las_frame->ret);
  20. return NULL;
  21. }
  22. }
  23. int main (int argc, char ** argv)
  24. {
  25. pal_printf("Enter Main Thread\n");
  26. PAL_HANDLE out = DkStreamOpen("dev:tty", PAL_ACCESS_WRONLY, 0, 0, 0);
  27. if (out == NULL) {
  28. pal_printf("DkStreamOpen failed\n");
  29. return -1;
  30. }
  31. void * param = NULL;
  32. PAL_HANDLE child = _fork(param);
  33. if (child == NULL) {
  34. pal_printf("in the child\n");
  35. char *str = (void *) DkVirtualMemoryAlloc(NULL, 20, 0,
  36. PAL_PROT_READ|PAL_PROT_WRITE);
  37. if (str == NULL) {
  38. pal_printf("DkVirtualMemoryAlloc failed\n");
  39. return -1;
  40. }
  41. str[0] = 'H';
  42. str[1] = 'e';
  43. str[2] = 'l';
  44. str[3] = 'l';
  45. str[4] = 'o';
  46. str[5] = ' ';
  47. str[6] = 'W';
  48. str[7] = 'o';
  49. str[8] = 'r';
  50. str[9] = 'l';
  51. str[10] = 'd';
  52. str[11] = '\n';
  53. str[12] = 0;
  54. int bytes = DkStreamWrite(out, 0, 12, str, NULL);
  55. if (bytes < 0) {
  56. pal_printf("DkStreamWrite failed\n");
  57. return -1;
  58. }
  59. DkVirtualMemoryFree(str, 20);
  60. DkThreadExit();
  61. }
  62. else {
  63. pal_printf("in the parent\n");
  64. DkThreadDelayExecution(3000);
  65. }
  66. DkObjectClose(out);
  67. pal_printf("Leave Main Thread\n");
  68. return 0;
  69. }