void console_buf(struct CharBuf *charbuf) { while(1) { P_kt_sem(consoleWait); P_kt_sem(nslots); charbuf->buffer[charbuf->tail] = console_read(); charbuf->tail = (charbuf->tail+1) % 256; V_kt_sem(nelem); if (bufCount < 256) bufCount++; } }
void *readFromConsole() { while (1) { P_kt_sem(consoleWait); char newChar = console_read(); if (CRBisFull() == 0) { writeCRB(&newChar); V_kt_sem(nelem); } } //V_kt_sem(readok); }
//Console buffer. Code buffers input from the console until a program requests it with a call to read(). CREATE. //a thread whose sole job is to read characters from the console and put them into a buffer. void *console_buffer(void *arg) { //printf("Goes into the console_buffer\n"); // int cbTail = 0; //int cbHead =0; //char *filename = (char *)arg; //these are used to convert the char to int and place into buf int i; char c; //int stop =1; PIPE *pipe= (PIPE *)arg; //printf("Enters console_buffer\n"); //while(stop) while(pipe->ref_count!=0) //bc it it is then you have to deallocate { P_kt_sem(consoleWait); //printf("P_kt_sem(consoleWait): %i\n",kt_getval(consoleWait)); //have to check if the buffer is full or not //printf("Enters while loop in console\n"); //if not full yet //if (cbTail !=cbSize) if (pipe->pbuf_tail!=256) { //printf("We are reading from the console into the buffer\n"); //currently a char c = console_read(); // printf("Reading from console: %c\n", c); //conversion to a int bc buffer holds ints only i = c -'0'; // printf("Coverted c into i: %i\n",i); //enqueue - insert it into the buffer cb, enter through the back! and 256 size of the buffer //cbTail = (cbTail+1)%256; //this is a shortcut pipe->pbuf_tail = (pipe->pbuf_tail+1)%256; pipe->read_buf[pipe->pbuf_tail] = i; //printf("Placed into cb[%i]: %i\n", cbTail, cb[cbTail]); // printf("This is in index %i: %c %i\n", cbTail, c, cb[cbTail]); //Step 17: V_kt_sem(nelem); //printf("V_kt_sem(nelem): %i\n",kt_getval(nelem)); } } // printf("Exits while loop in console buffer\n"); kt_exit(); }
void *console_buf_read(void *arg) { char c; int i; while (sysStopRead != 1) { P_kt_sem(consoleWait); if (!full(consoleBufferTail, consoleSize)) { c = console_read(); i = c - '0'; add_to_queue(consoleBuffer, &consoleBufferTail, i); V_kt_sem(nelem); } } kt_exit(); }
void do_read(PCB *pcb){ int fd = pcb->registers[5], buf = pcb->registers[6], count = pcb->registers[7]; //printf("COUNT COUNT COUNT::::%d",count); if(valid_fd(pcb, fd, FD_READ)){ //todo: or valid fd if(buf < 0){ syscall_return(pcb,-1*EFAULT); } }else{ syscall_return(pcb,-1*EBADF); //Bad file number } struct File_Descriptor *fd_obj = pcb->fd_table[fd]; //if(!fd_obj->console_flag){ //printf("do_read: %d %d %d\n", fd, buf, count); //} if(check_buffer_address(pcb, buf) == FALSE){ syscall_return(pcb, -1*EFAULT); } if(count < 0){ syscall_return(pcb, -1*EINVAL); } int i = 0; int val; //printf("\ngot here to 1\n"); for(; i < count; i++){ //printf("obj id:%d, console flag:%d\n", fd_obj->id, fd_obj->console_flag); if(fd_obj->console_flag){ //printf("\ngot here to 0000\n"); P_kt_sem(nelem); Dllist first = dll_first(console_read_buf); val = jval_i(dll_val(first)); dll_delete_node(first); console_read_buf_size -= 1; }else{ //HMM, do we need a lock for buffer access? //printf("\ngot here to 2\n"); struct Pipe *pipe = fd_obj->pipe; //printf("read from pipe: %p\n", pipe); //printf("r:b\n"); P_kt_sem(pipe->empty_sem); val = pipe->buff[pipe->buff_start]; //printf("r:(%d) %c\n",val, val); pipe->buff_start++; if(pipe->buff_start == PIPE_BUFF_SIZE){ pipe->buff_start = 0; } //printf("\ngot here to 2.5\n"); V_kt_sem(pipe->full_sem); //printf("\ngot here to 3\n") //printf("r:a\n");; if(pipe->buff_start == pipe->buff_end){ break; } } if(val == -1 || val == 0){ break; } main_memory[pcb->User_Base+buf+i] = val; } if(!fd_obj->console_flag){ //printf("read done\n"); } //printf("syscall return\n"); syscall_return(pcb, i); }