Advertisement
Guest User

Untitled

a guest
May 21st, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. #include <syscall.h>
  2. #include <types.h>
  3. #include <lib.h>
  4.  
  5. //per la exit
  6. #include <types.h>
  7. #include <kern/unistd.h>
  8. #include <clock.h>
  9. #include <copyinout.h>
  10. #include <proc.h>
  11. #include <thread.h>
  12. #include <addrspace.h>
  13. // per la wait_proc
  14. #include <synch.h>
  15. #include <current.h>
  16. #include <proc.h>
  17.  
  18.  
  19. #include <opt-syscall.h>
  20. #include <opt-pidtable.h>
  21. #if OPT_SYSCALL==1
  22. ssize_t sys_read(int fd, void *buf, size_t count) {
  23. unsigned int i;
  24. char* str = (char*) buf;
  25. if(fd != 0) //standard input
  26. return 0;
  27. for(i=0; i<count; i++) {
  28. str[i] = getch();
  29. if( str[i] == '\n') {
  30. str[i+1] = '\0';
  31. }
  32. }
  33. return i+1;
  34. }
  35.  
  36.  
  37. ssize_t sys_write(int fd, const void *buf, size_t count) {
  38. unsigned int i;
  39. char* str = (char*) buf;
  40. if(fd != 1) //standard output
  41. return 0;
  42. for(i=0; i<count; i++) {
  43. if( str[i] == '\0') break;
  44. putch(str[i]);
  45. }
  46. return i;
  47. }
  48.  
  49. void sys__exit(int code) {
  50.  
  51. kprintf("Code returned to system: %d\n", code);
  52.  
  53. //added to work with wait() -> join
  54.  
  55. curproc->lastStatus = code; //salvo lo stato che verrà preso nella wait_pid
  56. V(curproc->sem);
  57.  
  58. //
  59. /*
  60. as_destroy(as); // libera as
  61. */
  62.  
  63. thread_exit(); //esce dal thread
  64.  
  65. panic("thread_exit returned (should not happen)\n");
  66. //(void) status; // TODO: status handling
  67.  
  68. }
  69.  
  70. #if OPT_PIDTABLE==1
  71. // testata correttamente :: //
  72. int sys_waitpid(pid_t pid) {
  73. struct proc * p = proc_getproc((int)pid);
  74.  
  75. P(p->sem); //il processo si ferma e aspetta
  76. int status = p->lastStatus;
  77.  
  78. /* get address space of current process and destroy */
  79. while( p->p_numthreads != 0); //destroy si può fare solo quando numthread è 0
  80. proc_destroy(p);
  81. //as_destroy(as); // libera as: già fatto in proc destroy
  82.  
  83. return status;
  84.  
  85. }
  86.  
  87. pid_t sys_getpid(void) {
  88. return proc_getpid();
  89. }
  90.  
  91. int sys_fork(void) {
  92. ///*
  93. struct proc* newp = NULL;
  94. struct addrspace* as = NULL;
  95. newp = proc_create_runprogram("Test");
  96. as = proc_getas();
  97.  
  98. if ( newp == NULL) return -1;
  99. spinlock_acquire(&newp->p_lock);
  100. newp->p_addrspace = as;
  101. spinlock_release(&newp->p_lock);
  102.  
  103. return (int) newp->pid;
  104. //*/ return 2;
  105. }
  106.  
  107. #endif
  108.  
  109.  
  110. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement