// TODO for cow void FreePCB(PCB *pcb) { while (!queueIsEmpty(pcb->children)) { queuePop(pcb->children); } while(!queueIsEmpty(pcb->deadChildren)) { ZCB *zombie = queuePop(pcb->deadChildren); queueRemove(process_queue, zombie); free(zombie); } free(pcb->children); free(pcb->deadChildren); for (int i = 0; i < MAX_PT_LEN; i++) { if (pcb->cow.pageTable[i].valid == 1) { if (pcb->cow.refCount[i]) { if (*(pcb->cow.refCount[i]) == 1) { addFrame(pcb->cow.pageTable[i].pfn); free(pcb->cow.refCount[i]); } else { *(pcb->cow.refCount[i])--; } } else { addFrame(pcb->cow.pageTable[i].pfn); } } } free(pcb); }
void DoWait(UserContext *context) { if (BufferCheck(context->regs[0], INT_LENGTH) == ERROR || BufferWriteCheck(context->regs[0], INT_LENGTH) == ERROR) { TracePrintf(1, "DoWait: Pointer given not valid. Returning Error\n"); context->regs[0] = ERROR; return; } if (queueIsEmpty(current_process->children) && queueIsEmpty(current_process->deadChildren)) { TracePrintf(2, "DoWait: PCB %d Has no children. Returning Error.\n", current_process->id); TracePrintf(2, "DoWait: Children queue count: %d, deadChildren count: %d\n", current_process->children->length, current_process->deadChildren->length); context->regs[0] = ERROR; } else { if (queueIsEmpty(current_process->deadChildren)) { current_process->status = WAITING; queuePush(wait_queue, current_process); LoadNextProc(context, BLOCK); } ZCB *child = queuePop(current_process->deadChildren); queueRemove(process_queue, child); *((int *)context->regs[0]) = child->status; context->regs[0] = child->id; } }
int qExit(Queue *q, productList *products) { int code; clearScreen(); printf("\n\n\n\tThank you for using SUPER MARKET COUNTER!\n"); printf("\n\tPress any key to exit!"); while(queueIsEmpty(q)==0) freeCustomer(queueRemove(q)); queueFree(q); code = freeProductList(products); if(code==0) { getch(); exit(code); } else { printf("Error: Freeing memory failed!"); wait(); exit(code); } }
/* * Remove thread de fila. Não remove a thread da ponta (como em uma fila), * mas sim a thread igual à trd. Retorna true caso trd exista na fila. */ bool queueRemoveAsList(s_queue *queue, s_tcb *trd) { s_tcb *thread, *aux; bool achou = false; for(thread = queue->first; thread != NULL; thread = thread->next) { if(thread == trd) { achou = true; break; } } if(achou) { if(thread == queue->first) { PRINT("queueRemoveAsList: Caso do first.\n"); queueRemove(queue); } else if(thread == queue->last) { PRINT("queueRemoveAsList: Caso do last.\n"); queue->last = thread->prev; queue->last->next = NULL; } else { PRINT("queueRemoveAsList: Caso do meio.\n"); aux = thread->prev; aux->next = thread->next; aux = thread->next; aux->prev = thread->prev; } thread->prev = NULL; thread->next = NULL; trd = thread; return true; } else { return false; } }
int qProcess(Queue *q) { Customer *c; clearScreen(); c = queueRemove(q); printBill(c); freeCustomer(c); wait(); }
void setTaskEnabled(cfTaskId_e taskId, bool enabled) { if (taskId == TASK_SELF || taskId < TASK_COUNT) { cfTask_t *task = taskId == TASK_SELF ? currentTask : &cfTasks[taskId]; if (enabled && task->taskFunc) { queueAdd(task); } else { queueRemove(task); } } }
void setTaskEnabled(const int taskId, bool enabled) { if (taskId == TASK_SELF || taskId < (int)taskCount) { cfTask_t *task = taskId == TASK_SELF ? currentTask : &cfTasks[taskId]; if (enabled && task->taskFunc) { queueAdd(task); } else { queueRemove(task); } } }
void KillProc(PCB *pcb) { TracePrintf(2, "KillProc\n"); if (1 == current_process->id) { TracePrintf(1, "KillProc: init program being killed. Now calling halt.\n"); Halt(); } PCB *parent = pcb->parent; if (parent) { if (parent->status == WAITING) { queueRemove(wait_queue, parent); queuePush(ready_queue, parent); } ZCB *zombie = (ZCB *) malloc(sizeof(ZCB)); zombie->id = pcb->id; zombie->status = DEAD; zombie->exit_status = pcb->status; queuePush(parent->deadChildren, zombie); queuePush(process_queue, zombie); } for (List *child = current_process->children->head; child; child = child->next) ((PCB *) child->data)->parent = NULL; if (pcb->parent) { queueRemove(pcb->parent->children, pcb); } // Update process queue queueRemove(process_queue, pcb); FreePCB(pcb); }
int ReclaimLock(Lock *lock) { // Return error if some proc is holding on to this lock // or some proc is waiting to acquire this lock // or if there have been calls to CvarWait associated with this lock if (lock->owner || !queueIsEmpty(lock->waiting) || lock->cvars) { return ERROR; } else { queueRemove(locks, lock); free(lock->waiting); free(lock); return SUCCESS; } }
void rbtreeToDot(RBTree tree, const char* racine, const char* dossier) { assert(!rbtreeEmpty(tree)); static int numerofichier=0; char final[30]; sprintf(final,"%s/%s%d.dot",dossier,racine,numerofichier++); FILE*fd = fopen(final,"wt"); fprintf(fd,"digraph G { \n"); Node node; QUEUE queue; queueCreate(&queue); queueAdd(queue,rbfirst(tree)); do { node = queueRemove(queue); if(node->color==red) fprintf(fd,"\t%d [color=red];\n",keyPut(node->key)); else fprintf(fd,"\t%d [color=black];\n",keyPut(node->key)); if(node->left != tree->nil) { fprintf(fd,"\t%d -> %d;\n",keyPut(node->key),keyPut(node->left->key)); if(node->left->color==red) fprintf(fd,"\t%d [color=red];\n",keyPut(node->left->key)); else fprintf(fd,"\t%d [color=black];\n",keyPut(node->left->key)); } if(node->right != tree->nil) { fprintf(fd,"\t%d -> %d;\n",keyPut(node->key),keyPut(node->right->key)); if(node->right->color==red) fprintf(fd,"\t%d [color=red];\n",keyPut(node->right->key)); else fprintf(fd,"\t%d [color=black];\n",keyPut(node->right->key)); } if(node->left != tree->nil) queueAdd(queue,node->left); if(node->right != tree->nil) queueAdd(queue,node->right); } while(!queueEmpty(queue)); fprintf(fd,"}\n"); fclose(fd); }
void queueGet (tQueue* q, char* c) { /* ** Odstraní znak ze zaèátku fronty a vrátí ho prostøednictvím parametru c. ** Pokud je fronta prázdná, o¹etøete to voláním funkce queueError(QERR_GET). ** ** Pøi implementaci vyu¾ijte døíve definovaných funkcí queueEmpty, ** queueFront a queueRemove. */ if(queueEmpty(q)){ // Zkontroluj, zda-li neni fronte prazdna queueError(QERR_GET); // Vyhod chybovou hlasku return; // Ukonci } queueFront(q, c); // Vrat znak queueRemove(q); // A odstran ho }
void queueGet (tQueue* q, char* c) { /* ** Odstraní znak ze zaèátku fronty a vrátí ho prostøednictvím parametru c. ** Pokud je fronta prázdná, o¹etøete to voláním funkce queueError(QERR_GET). ** ** Pøi implementaci vyu¾ijte døíve definovaných funkcí queueEmpty, ** queueFront a queueRemove. */ if (queueEmpty(q)) { queueError(QERR_GET); return; } queueFront(q, c); /* c bude ukazova» na prvý znak fronty */ queueRemove(q); /* Index prvého prvku sa posunie na nasledujúci */ }
int qPrint(Queue *q) { int i=getQueueLength(q); Customer *c; clearScreen(); do { c = queueRemove(q); printBill(c); wait(); if(c!=NULL) queueInsert(q, c); i--; }while(i>0); }
void queueGet (tQueue* q, char* c) { /* ** Odstraní znak ze zaèátku fronty a vrátí ho prostøednictvím parametru c. ** Pokud je fronta prázdná, o¹etøete to voláním funkce queueError(QERR_GET). ** ** Pøi implementaci vyu¾ijte døíve definovaných funkcí queueEmpty, ** queueFront a queueRemove. */ if(queueEmpty(q) == 0) //fronta nie je prazdna { queueFront(q, c); //najprv vratime znak queueRemove(q); //potom odstranime } else //fronta je prazdna { queueError(QERR_GET); } }
void queueGet (tQueue* q, char* c) { /* ** Odstran znak ze zatku fronty a vrt ho prostednictvm parametru c. ** Pokud je fronta przdn, oetete to volnm funkce queueError(QERR_GET). ** ** Pi implementaci vyuijte dve definovanch funkc queueEmpty, ** queueFront a queueRemove. */ if(queueEmpty(q) == 0) { queueFront(q, c); queueRemove(q); } else { queueError(QERR_GET); } }
void rbtreeMapDebug(RBTree tree) { assert(!rbtreeEmpty(tree)); Node node; QUEUE queue; queueCreate(&queue); queueAdd(queue,rbfirst(tree)); printf("\033[01;35m==== Début de l'arbre ====\n\033[0m"); do { node = queueRemove(queue); if(rbExists(node->left) || rbExists(node->right) || rbfirst(tree)==node) { if(node->father != tree->root) printf("(pere: %d)",keyPut(node->father->key)); if(node->color==red) printf("\033[01;31m"); if(rbfirst(tree)==node) printf("\033[01;32m"); printf("Noeud %d\033[0m",keyPut(node->key)); if(rbExists(node->left)) { if(node->left->color==red) printf("\033[01;31m"); printf(", "); if(node->left->father != tree->root) printf("(pere: %d) ",keyPut(node->left->father->key)); printf("%d fils gauche\033[0m",keyPut(node->left->key)); } if(rbExists(node->right)) { printf(", "); if(node->right->color==red) printf("\033[01;31m"); if(node->right->father != tree->root) printf("(pere: %d) ",keyPut(node->right->father->key)); printf("%d fils droit\033[0m",keyPut(node->right->key)); } printf("\n"); } if(node->left != tree->nil) queueAdd(queue,node->left); if(node->right != tree->nil) queueAdd(queue,node->right); } while(!queueEmpty(queue)); printf("\n"); }
int main(void) { queueHead* queue; int i1 = 1; int i2 = 2; int i3 = 3; int i4 = 4; /* * Test 1 */ /* Create queue */ assert(NULL != (queue = queueCreate())); /* Fill the queue */ assert(1 == (queueAdd(queue, &i1))); /* Insert 1 */ assert(1 == (queueAdd(queue, &i2))); /* Insert 2 */ assert(1 == (queueAdd(queue, &i3))); /* Insert 3 */ assert(1 == (queueAdd(queue, &i4))); /* Insert 4 */ /* Check the queue */ assert(1 == (*(int*)queueFirst(queue))); /* Check 1 */ assert(1 == (*(int*)queueRemove(queue))); /* Remove 1 */ assert(2 == (*(int*)queueFirst(queue))); /* Check 2 */ assert(2 == (*(int*)queueRemove(queue))); /* Remove 2 */ assert(3 == (*(int*)queueFirst(queue))); /* Check 3 */ assert(3 == (*(int*)queueRemove(queue))); /* Remove 3 */ assert(4 == (*(int*)queueFirst(queue))); /* Check 4 */ assert(4 == (*(int*)queueRemove(queue))); /* Remove 4 */ assert(NULL == (queueFirst(queue))); /* Check empty */ assert(NULL == (queueRemove(queue))); /* Queue is empty */ /* Clear the queue */ queueFree(queue); printf("Queue: Test1 success!\n"); /* * Test 2 */ /* Create queue */ assert(NULL != (queue = queueCreate())); /* Fill the queue */ assert(1 == (queueAdd(queue, &i1))); /* Insert 1 */ assert(1 == (*(int*)queueRemove(queue))); /* Remove 1 */ assert(1 == (queueAdd(queue, &i1))); /* Insert 1 */ assert(1 == (queueAdd(queue, &i2))); /* Insert 2 */ assert(1 == (*(int*)queueRemove(queue))); /* Remove 1 */ assert(2 == (*(int*)queueRemove(queue))); /* Remove 2 */ assert(1 == (queueAdd(queue, &i1))); /* Insert 1 */ assert(1 == (*(int*)queueFirst(queue))); /* Check 1 */ assert(1 == (queueAdd(queue, &i2))); /* Insert 2 */ assert(1 == (*(int*)queueRemove(queue))); /* Remove 1 */ assert(1 == (queueAdd(queue, &i3))); /* Insert 3 */ assert(2 == (*(int*)queueRemove(queue))); /* Remove 2 */ assert(1 == (queueAdd(queue, &i4))); /* Insert 4 */ assert(3 == (*(int*)queueRemove(queue))); /* Remove 3 */ assert(4 == (*(int*)queueRemove(queue))); /* Remove 4 */ /* Fill the queue */ assert(1 == (queueAdd(queue, &i1))); /* Insert 1 */ assert(1 == (queueAdd(queue, &i2))); /* Insert 2 */ assert(1 == (queueAdd(queue, &i3))); /* Insert 3 */ assert(1 == (queueAdd(queue, &i4))); /* Insert 4 */ /* Check the queue */ assert(1 == (*(int*)queueRemove(queue))); /* Remove 1 */ assert(2 == (*(int*)queueFirst(queue))); /* Check 2 */ assert(2 == (*(int*)queueRemove(queue))); /* Remove 2 */ assert(3 == (*(int*)queueRemove(queue))); /* Remove 3 */ assert(4 == (*(int*)queueRemove(queue))); /* Remove 4 */ assert(NULL == (queueFirst(queue))); /* Check empty */ assert(NULL == (queueRemove(queue))); /* Queue is empty */ /* Clear the queue */ queueFree(queue); printf("Queue: Test2 success!\n"); return 0; }
void *consumer (void *carg) { queue *fifo; int item_consumed; pcdata *mydata; int my_tid; int *total_consumed; // int consumerLoopCondition; mydata = (pcdata *) carg; fifo = mydata->q; total_consumed = mydata->count; my_tid = mydata->tid; /* * Continue producing until the total consumed by all consumers * reaches the configured maximum */ while (1) { /* * If the queue is empty, there is nothing to do, so wait until it * si not empty. */ pthread_mutex_lock(fifo->mutex); while (fifo->empty && *total_consumed != WORK_MAX) { printf ("con %d: EMPTY.\n", my_tid); pthread_cond_wait(fifo->notEmpty,fifo->mutex); // consumerLoopCondition=fifo->empty && *total_consumed != WORK_MAX; } /* * If total consumption has reached the configured limit, we can * stop */ if (*total_consumed >= WORK_MAX) { pthread_mutex_unlock(fifo->mutex); break; } /* * Remove the next item from the queue. Increment the count of the * total consumed. Note that item_consumed is a local copy so this * thread can retain a memory of which item it consumed even if * others are busy consuming them. */ queueRemove (fifo, &item_consumed); (*total_consumed)++; pthread_cond_broadcast(fifo->notFull); pthread_mutex_unlock(fifo->mutex); /* * Do work outside the critical region to consume the item * obtained from the queue and then announce its consumption. */ do_work(CONSUMER_CPU,CONSUMER_CPU); printf ("con %d: %d.\n", my_tid, item_consumed); } printf("con %d: exited\n", my_tid); return (NULL); }