int main (int argc, char **argv) { struct msgbuff{ long mtype; pid_t pid; }message; pid_t pid, npid; int i; int msqid; key_t keyx; struct msqid_ds msq; Dllist q = new_dllist(); Dllist n; keyx = ftok(KEYFILE_PATH, (int)ID); msqid = msgget(keyx, 0666 | IPC_CREAT); if (msqid == -1) { perror("msgget"); exit(1); } while(1) { if((msgrcv(msqid, &message, sizeof(pid_t), 1, 0)) == MSGQ_NG){ perror("msgrcv"); exit(1); } pid = message.pid; if(pid != 1) { if(dll_empty(q)) kill(pid, SIGUSR1); else { n = dll_first(q); npid = jval_i(dll_val(n)); if(pid == npid) { kill(pid, SIGUSR1); dll_delete_node(n); } else dll_append(q, new_jval_i(pid)); } } else { if(!dll_empty(q)) { n = dll_first(q); npid = jval_i(dll_val(n)); kill(npid, SIGUSR1); dll_delete_node(n); } } } return 0; }
void scheduler() { //readyq = new_dllist(); if(dll_empty(readyq)) { //printf("empty ready q\n"); current = NULL; noop(); } //takes the first PCB off the readyq and calls run_user_code() on its registers else { // pop off and delete from readyq printf("non-empty q\n"); current = (PCB *) jval_v(dll_val(dll_first(readyq))); printf("non-empty q\n"); dll_delete_node(dll_first(readyq)); printf("non-empty q\n"); run_user_code(current->registers); } }
void scheduler() { // Init no longer has children. Needs to close. if (jrb_empty(init->children)) { SYSHalt(); } if(dll_empty(readyQ)) { noop_flag = 1; noop(); } else { PCB *process; Dllist node; Jval registers; noop_flag = 0; process = (PCB *) malloc(sizeof(PCB)); node = dll_first(readyQ); // Get next node in queue dll_delete_node(node); // Node in memory. Delete off readyQ // Get registers from node registers = dll_val(node); process = (PCB *)jval_v(registers); // Update current process currentProcess = process; User_Base = process->base; User_Limit = process->limit; run_user_code(process->registers); } }
main(int argc, char **argv) { IS is; int n; Dllist l; Dllist tmp; if (argc != 2) { fprintf(stderr, "usage: dlltail n\n"); exit(1); } n = atoi(argv[1]); if (n < 0) { fprintf(stderr, "usage: dlltail n -- n must be >= 0\n"); exit(1); } is = new_inputstruct(NULL); l = new_dllist(); while (get_line(is) >= 0) { dll_append(l, new_jval_s(strdup(is->text1))); if (is->line > n) { tmp = dll_first(l); free(jval_s(dll_val(tmp))); dll_delete_node(tmp); } } dll_traverse(tmp, l) printf("%s", jval_s(tmp->val)); }
//Each elevator is a while loop. //Check the global list and if it’s empty, block on the condition variable for blocking elevators. //When the elevator gets a person to service, it moves to the appropriate flo0or and opens its door. //It puts itself into the person’s e field, then signals the person and blocks until the person wakes it up. //When it wakes up, it goes to the person’s destination floor, opens its door, signals the person and blocks. //When the person wakes it up, it closes its door and re-executes its while loop. //• Your elevator’s should call open door(), close door() from move to floor() appropriately. //All sleeping and printing should be done in elevator skeleton.c. //Thus, your final program that you hand in should not do any sleeping or any printing. void *elevator(void *arg) { Person *person_in_transit; for(;;) { pthread_mutex_lock(((Elevator*)arg)->es->lock); while (!((Queue*)((Elevator*)arg)->es->v)->count) pthread_cond_wait(((Queue*)((Elevator*)arg)->es->v)->cond, ((Elevator*)arg)->es->lock); if(!dll_empty(((Queue*)((Elevator*)arg)->es->v)->passengers)){ person_in_transit = (Person*)jval_v(dll_val(dll_first(((Queue*)((Elevator*)arg)->es->v)->passengers))); dll_delete_node(dll_first(((Queue*)((Elevator*)arg)->es->v)->passengers)); } // unlock the critial section. pthread_mutex_unlock(((Elevator*)arg)->es->lock); //move the elevator. --((Queue*)((Elevator*)arg)->es->v)->count; person_in_transit->from == ((Elevator*)arg)->onfloor?:move_to_floor(((Elevator*)arg), person_in_transit->from); //open the door open_door(((Elevator*)arg)); //add the people to the elevator person_in_transit->e = ((Elevator*)arg); //signal the person pthread_mutex_lock(person_in_transit->lock); pthread_cond_signal(person_in_transit->cond); pthread_mutex_unlock(person_in_transit->lock); // have the elevator wait pthread_mutex_lock(((Elevator*)arg)->lock); pthread_cond_wait(((Elevator*)arg)->cond, ((Elevator*)arg)->lock); pthread_mutex_unlock(((Elevator*)arg)->lock); // close door close_door(((Elevator*)arg)); // elevator moves move_to_floor(person_in_transit->e,person_in_transit->to); // open the door once move has completed. open_door(((Elevator*)arg)); // signal the person pthread_mutex_lock(person_in_transit->lock); pthread_cond_signal(person_in_transit->cond); pthread_mutex_unlock(person_in_transit->lock); // block the elevator pthread_mutex_lock(((Elevator*)arg)->lock); pthread_cond_wait(((Elevator*)arg)->cond, ((Elevator*)arg)->lock); pthread_mutex_unlock(((Elevator*)arg)->lock); // close the door close_door(((Elevator*)arg)); // restart. } }
static int sendHint(int newsockfd, int workingSize) { /* Initialize hint */ char* hintGraph = NULL; char *hintGraphSize = NULL; char hintMessage[READBUFFERSIZE]; char cliqueCount[BUFSIZ]; /* Hint message structure: [result flag]:[matrixsize]:[cliquecount]:[graphmatrix] */ /* Decide which hint to send */ if(workingSize <= _Scheduler->currCEsize+1) { /* Send counterexample */ asprintf(&hintGraphSize, "%d", _Scheduler->currCEsize); /* Get next counterexample from list */ int *key_g; key_g = (int *)jval_v(dll_val(_Scheduler->currPtr)); /* Update pointer */ _Scheduler->currPtr = _Scheduler->currPtr->flink; hintGraph = GraphtoChar(key_g, _Scheduler->currCEsize); sprintf(cliqueCount, "%d", 0); } /* Finish building message */ hintMessage[0] = RESULT; strcat(hintMessage, ":"); strcat(hintMessage, hintGraphSize); strcat(hintMessage, ":"); strcat(hintMessage, cliqueCount); strcat(hintMessage, ":"); strcat(hintMessage, hintGraph); /* just checking */ if(hintGraph == NULL) { fprintf(stderr,"Error: failed to build hint message.\n"); return -1; } int n = write(newsockfd, hintMessage, strlen(hintMessage)); if (n < 0) { fprintf(stderr,"Error: failed to write to socket\n"); return -1; } /* Free memory */ free(hintGraph); }
/* * insert bid in descending cost */ void insert_bid(char *msg) { char *p; Bid *b, *tb; int i, id; Dllist ptr; // create a new bid b = (Bid *) malloc(sizeof(Bid)); p = strstr(msg, "B"); p++; b->cost = atoi(p); p = strstr(p, "$"); p++; b->numrobot = atoi(p); b->coalition = new_dllist(); for (i = 0; i < b->numrobot; i++) { p = strstr(p, "$"); p++; id = atoi(p); if (dll_empty(b->coalition)) b->leaderID = id; dll_append(b->coalition, new_jval_i(id)); } printf("insert bid: %s\n", msg); // insert the bid to bidList, increasing cost if (dll_empty(bidList)) { dll_append(bidList, new_jval_v(b)); } else { dll_traverse(ptr, bidList) { tb = (Bid *) jval_v(dll_val(ptr)); if (tb->cost > b->cost) { dll_insert_b(ptr, new_jval_v(b)); break; } if (ptr == dll_last(bidList)) {// append it to the last dll_append(bidList, new_jval_v(b)); break; } } }
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); }