int main(void) { QUEUE_t myQueue; STACK_t myStack; stack_init(&myStack); queue_init(&myQueue); for(int i = 0; i < 10; i++) { insertQueue(&myQueue,(void*) (rand() % 100)); push(&myStack, (void*) (rand() % 50)); } itemS_t* holdS = pop(&myStack); printf("Item popped: %d\n", holdS->keyValue); free(holdS); printf("Pushing 99...\n"); push(&myStack, (void*) 99); holdS = pop(&myStack); printf("Item popped: %d", holdS->keyValue); free(holdS); printQueue(&myQueue); printStack(&myStack); itemQ_t* holdQ = removeQueue(&myQueue); printf("Item removed from queue: %d.\n", holdQ->keyValue); free(holdQ); printQueue(&myQueue); printStack(&myStack); clearQueue(&myQueue); clearStack(&myStack); return 0; }
void main(){ printf("\n"); queue qu; queue *q = &qu; initQueue(4,q); printQueue(q); push(1,q); push(2,q); push(3,q); printQueue(q); push(4,q); printInOrder(q); push(5,q); push(6,q); push(7,q); push(8,q); printQueue(q); printInOrder(q); }
int main(int argc, char* argv[]) { int test_array[MAX_NUM] = {NIL, NIL, NIL, NIL, NIL, NIL, 15, 6, 9, 8, 4, NIL}; queue q; for (int i = 0; i < MAX_NUM; i++) q.array[i] = test_array[i]; q.head = 7; q.tail = 12; printf("the deque is:\n"); printQueue(q); backEnqueue(&q, 17); printf("after back enqueue 17, deque is:\n"); printQueue(q); frontEnqueue(&q, 3); printf("after front enqueue 3, deque is:\n"); printQueue(q); backDequeue(&q); printf("after back dequeue, deque is:\n"); printQueue(q); backEnqueue(&q, 5); printf("after back enqueue 5, deque is:\n"); printQueue(q); frontDequeue(&q); printf("after front dequeue, deque is:\n"); printQueue(q); return 0; }
PROC *kfork(char *filename) { int j, segment; PROC *p = dequeue(&freeList); if (p == 0) { printf("Failed to kfork()\n"); getc(); return(0); } p->status = READY; p->priority = 1; p->ppid = running->pid; p->parent = running; segment = (p->pid + 1) * 0x1000; /* * INITIALIZE p's kstack to make it start from body() when it runs. * To do this, PRETNED that the process called tswitch() from the * the entry address of body() and executed the SAVE part of tswitch() * to give up CPU before. * Initialize its kstack[ ] and ksp to comform to these. */ for (j=1; j<10; j++) { p->kstack[SSIZE - j] = 0; // all saved registers = 0 } p->kstack[SSIZE-1]=(int)goUmode; // called tswitch() from body p->ksp = &(p->kstack[SSIZE-9]); // ksp -> kstack top printf("Loading executable\n"); //FOR TESTING load("/bin/u1", segment); // Load executable printf("Executable loaded\n"); //FOR TESTING for (j=1; j<13; j++) { put_word(0, segment, -j*2); // Set all registers to 0 } put_word(0x0200, segment, -2); // Flag put_word(segment, segment, -4); // CS put_word(segment, segment, -22); // ES put_word(segment, segment, -24); // DS p->uss = segment; p->usp = -24; //printProc(p); enqueue(&readyQueue, p); printQueue(readyQueue, freeList, sleepList); /*printf("Ready queue:\n"); print_queue(readyList);*/ return(p->pid); }
int body() { char c; printf("proc %d resumes to body()\n", running->pid); while(1) { printf("-----------------------------------------\n"); //print freeList; printf("Free List:\n"); printQueue(freeList); // print readyQueue; printf("Ready Queue:\n"); printQueue(readyQueue); // print sleepList; printf("Sleep List:\n"); // printSleepQueue(sleepList); printSleepQueue(sleepList); printf("-----------------------------------------\n"); printf("proc %d[%d] running: parent=%d\n", running->pid, running->priority, running->ppid); printf("type a command [s|f|q|w| u ] : "); c = getc(); printf("%c\n", c); switch(c){ case 's' : do_tswitch(); break; case 'f' : do_kfork(); break; case 'q' : do_exit(); break; case 'w' : do_wait(); break; case 'u' : // printf("Running->uss: %x\n", running->uss); // printf("Running->usp: %x\n", running->usp); goUmode(); break; // let process goUmode } } }
/* A rudimentary test of my library */ void testLib() { printf("testing createQueue, enqueue, dequeue, and freeQueue...\n"); Queue q = createQueue(4); enqueue(0,q); enqueue(1,q); enqueue(2,q); enqueue(3,q); enqueue(3,q); enqueue(3,q); enqueue(3,q); int success = q->length == 4; printQueue(q); dequeue(q); dequeue(q); dequeue(q); dequeue(q); dequeue(q); dequeue(q); success = q->length == 0; printQueue(q); enqueue(0,q); enqueue(1,q); enqueue(2,q); enqueue(3,q); enqueue(3,q); enqueue(3,q); success = q->length == 4; printQueue(q); if(success) printf(" ☺ success.\n"); else printf(" ☹ failure.\n"); }
int main(void) { PriorityQueue<int> queue; //create queue int test = 1; //assign test case value testCase(test); //print out test case no. queue.add(19); //add items to queue queue.add(7); queue.add(1); queue.add(5); queue.add(3); queue.add(20); queue.add(15); queue.add(5); printQueue(queue); //print queue testCase(test); //print out test case no. sizeQueue(queue); //queue size testCase(test); //print out test case no. minQueue(queue); //retrieve minimum element testCase(test); //print out test case no. while (!queue.empty()) //cycle until empty { deleteMin(queue); //delete minimum element printQueue(queue); //print queue } testCase(test); //print out test case no. sizeQueue(queue); //queue size testCase(test); //print out test case no. minQueue(queue); //retrieve minimum element testCase(test); //print out test case no. deleteMin(queue); //delete minimum element return EXIT_SUCCESS; //exit success }
/* function main begins program execution */ int main() { int customerCounter = 1; int time1 = 0; int random = valRand(); int serviceTime = 0; int serviceTime2 = 0; int counter1 = 0; int counter2 = 0; srand( (unsigned)time(NULL) ); QueueNodePtr headPtr = NULL; QueueNodePtr tailPtr = NULL; while( time1 <= 720 ) { system( "sleep 0.5" ); system( "clear" ); printf( "CustomerCounter: %d\n", customerCounter ); printf( "Time: %d\n", time1 ); printQueue( headPtr ); if( time1 > random ) { enqueue( &headPtr, &tailPtr, customerCounter ); customerCounter++; random = random + valRand(); } else if( time1 > serviceTime ) { if( !isEmpty( headPtr ) ) { counter1 = dequeue( &headPtr, &tailPtr ); } serviceTime = time1 + valRand2(); } else if( time1 > serviceTime2 ) { if( !isEmpty( headPtr ) ) { counter2 = dequeue( &headPtr, &tailPtr ); } serviceTime2 = time1 + valRand2(); } printf( "Counter 1 is servicing: %d\n", counter1 ); printf( "Counter 2 is servicing: %d\n", counter2 ); time1++; } return 0; }
static void test_remove_from_queue() { Queue *queue = (Queue *)malloc(sizeof(Queue)); initializeQueue(queue); Thread *thread1 = malloc(sizeof(Thread)); Thread *thread2 = malloc(sizeof(Thread)); Thread *thread3 = malloc(sizeof(Thread)); insertIntoQueue(queue, thread1); insertIntoQueue(queue, thread2); insertIntoQueue(queue, thread3); assert(sizeOfQueue(queue) == 3); removeFromQueue(queue, thread3); printQueue(queue); assert(sizeOfQueue(queue) == 2); }
//performs compaction on the queue void pcbQueue::compact() { cout << "-BEFORE COMPACTION-" << endl; printQueue(); pcb* tempPCB = new pcb; tempPCB -> processName = "empty"; tempPCB -> memory = 0; for (int i = 0; i < queueSize; i++) { tempPCB -> memory = tempPCB -> memory + heldItems[i] -> memory; } heldItems.clear(); addPCB(tempPCB); cout << "-AFTER COMPACTION-" << endl; printQueue(); queueSize = heldItems.size(); cout << endl; }
int body() { char c; printf("proc %d resumes to body()\n", running->pid); while(1){ color = running->pid + 7; printf("proc %d running : enter a key : ", running->pid); c = getc(); switch(c){ case 'f' : do_kfork(); break; case 's' : tswitch(); break; case 'q': running->status = ZOMBIE; running->priority = 0; put_proc(&freeList, running); tswitch(); break; case 'p': printf("*****readyQueue: "); printQueue(readyQueue); printf("\n"); printf("*****freeList: "); printQueue(freeList); printf("\n"); break; default: printf("ERROR: invalid key\n"); break; } } }
int main() { std::vector<int> nums; nums.push_back(1); nums.push_back(3); nums.push_back(-1); nums.push_back(-3); nums.push_back(5); nums.push_back(3); nums.push_back(6); nums.push_back(7); std::vector<int> results = maxSlidingWindow(nums, 3); printQueue(results); return 1; }
int main() { int i; createQueue(2); // 20인 경우는 queueFull이 발생하지 않아서 queue 확장이 이루어지지 않음. // 동작이 정상적으로 이루어지면 2로 바꾸어서 queueFull을 유도하여 queueExtension()이 실행되도록 하여 테스트함. for (i = 0; i < 10; i++) { enQueue(i); printf("%3d enqueued\n", i); printf("---- 개수 = %d -----\n", i); printQueue(); } printf("----- dequeue 시작 -----\n"); while (!isEmpty()) { printf("%3d dequeued\n", deQueue()); printf("---- 개수 = %d -----\n", i); printQueue(); } printf("\n"); free(queue); }
int body() { char c; printf("proc %d resumes to body()\n", running->pid); while(1) { printf("-----------------------------------------\n"); //print freeList; printf("Free List:\n"); printQueue(freeList); // print readyQueue; printf("Ready Queue:\n"); printQueue(readyQueue); // print sleepList; printf("Sleep List:\n"); printSleepQueue(sleepList); printf("-----------------------------------------\n"); printf("proc %d[%d] running: parent=%d\n", running->pid, running->priority, running->ppid); printf("type a char [s|f|q| p|z|a| w ] : "); c = getc(); printf("%c\n", c); switch(c){ case 's' : do_tswitch(); break; case 'f' : do_kfork(); break; case 'q' : do_exit(); break; case 'p' : do_ps(); break; case 'z' : do_sleep(); break; case 'a' : do_wakeup(); break; case 'w' : do_wait(); break; } } }
int main() { printf("init\n"); init(); printQueue(freeList); printf("\nkfork\n"); kfork("/bin/u1"); // create P1 printf("\nP0 switch to P1\n"); while(1) { if(readyQueue) tswitch(); } }
int main() { graph g(6); g.addEdge(0,1,4); g.addEdge(0,2,1); g.addEdge(1,2,2); g.addEdge(1,3,5); g.addEdge(1,4,6); g.addEdge(2,3,3); g.addEdge(3,5,7); g.printEdges(); queue<edge> q = g.runKruskal(); printQueue(q); g.BFS(0); g.DFS(0); return 0; }
bool evaluate(char* command) { if (strcmp(PUSH, command) == 0) { int value = 0; printf("value = "); scanf("%i", &value); enqueue(value); } else if (strcmp(POP, command) == 0) { dequeue(); } else if (strcmp(PRINT, command) == 0) { printQueue(); } else if (strcmp(EXIT, command) == 0) { return false; } else { WRN("Undefined command: %s", command); } return true; }
void HoldbackQueue::updateAseq(Message2 msg, bool lock){ printQueue(); if(lock)this->lock(); for(int i=0; i<queue_locked.size();i++){ Message2 mq = queue_locked[i].m; // cout << "Received Aseq: processId "<<msg.processId<<" messageId: "<<msg.messageId<<endl; if(mq.processId.compare(msg.processId)==0 && mq.messageId==msg.messageId){ // cout << "Find the deliverable msg in holdback queue" << endl; queue_locked[i].Seq = atoi(msg.data.c_str()); queue_locked[i].deliverable=true; break; } } if(lock)this->unlock(); findDeliverable(lock); }
int main (void) { Queue q = CreateQueue(); Enqueue( 3, q ); Enqueue( 4, q ); Enqueue( 5, q ); Dequeue( q ); Enqueue( 6, q ); Dequeue( q ); Enqueue( 7, q ); Dequeue( q ); Enqueue( 8, q ); printQueue(q); return 0; }
void doProducerThread(TMANAGER *m, tid_t id, TCTRL *ctrl, void *p) { producer_data_t *data = p; int i = 0; while(TRUE) { TCTRL_check(ctrl); if(!TMANAGER_running(m)) break; if(!QUEUE_push(data->queue, &data->s[i])) { i = (i + 1) % data->l; printQueue(data->queue); TCTRL_sleep(ctrl, MXT_SLEEP_PRODUCER); } } }
void buildTree(void) { buildQueue(); printQueue(); int lvalue,rvalue; if ( !head ) exit(EXIT_FAILURE); while ( head -> right ) { Node* temp = createNode(); temp -> bottomleft = head; temp -> bottomright = head -> right; head = head -> right -> right; temp -> bottomleft -> right = NULL; temp -> bottomright -> right = NULL; if ( temp -> bottomleft -> data.datatype.type == LETTER ) lvalue = characters[ temp -> bottomleft -> data.dataletter.letter ].count; else lvalue = temp -> bottomleft -> data.datanum.num; if ( temp -> bottomright -> data.datatype.type == LETTER ) rvalue = characters[ temp -> bottomright -> data.dataletter.letter ].count; else rvalue = temp -> bottomright -> data.datanum.num; temp -> data.datatype.type = NUMBER; temp -> data.datanum.num= lvalue + rvalue; if ( !head ) head = temp; else addToQueue( temp ); } buffer = (unsigned char*)malloc ( currsize ); traverseTree(head,0UL); //creation of code is done. // tester started.. for ( int var = 0; var <128 ;++ var ) { if ( characters[var].count ){ printf("%c\t%s\n",var,characters[var].bitstring); // writeBits( characters[var].bitstring ); } } }
void doConsumerThread(TMANAGER *m, tid_t id, TCTRL *ctrl, void *p) { QUEUE *queue = p; while(TRUE) { char c; TCTRL_check(ctrl); if(!TMANAGER_running(m)) break; if(!QUEUE_poll(queue, &c)) { printQueue(queue); printCharacter(c); TCTRL_sleep(ctrl, MXT_SLEEP_CONSUMER); } } }
int main () { // SongToPlayPtr* songArray = malloc (sizeof(SongToPlayPtr)*NUM_SONGS); //heap SongToPlayPtr songArrayStack[NUM_SONGS]; //Stack version QueuePtr myQueue; myQueue = initQueue(); for (int i = 0; i < NUM_SONGS; i++) { char name[40]; printf("Name of song to add\n"); gets(name); // scanf("%39c", name); SongToPlay *newsong = initSong(name); songArrayStack[i] = newsong; if (i > 0) { songArrayStack[i-1]->next = songArrayStack[i]; } } myQueue->head = songArrayStack[0]; myQueue->tail = songArrayStack[NUM_SONGS-1]; // go use your queue here printQueue (myQueue); // destroy your queue for (int j = 0; j < NUM_SONGS; j++) { destroySong (songArrayStack[j]); } // last step is to free up the allocated Queue object free((void *) myQueue); }
int body() { char c, CR, buf[64]; while(1){ printf("=======================================\n"); printQueue(readyQueue); printf("proc %d %s in Kmode\n", running->pid, running->name); printf("input a command (s|f|u|q|i|o) : "); c=getc(); putc(c); CR=getc(); putc(CR); switch(c){ case 's' : tswitch(); break; case 'u' : printf("\nProc %d ready to go U mode\n", running->pid); goUmode(); break; case 'f': fork(); break; case 'q' : kexit(); break; case 'i' : iline(); break; case 'o' : oline(); break; } } }
vector<int> maxSlidingWindow(vector<int>& nums, int k) { vector<int> results; int size1 = nums.size(); vector<int> myqueue; if (k<=0 || size1 == 0) return results; int max = -1000; for(int i=0; i<k; i++) { myqueue.push_back(nums[i]); if (max <= nums[i]) max = nums[i]; } results.push_back(max); for (int i=k; i< size1; i++) { int tmp = myqueue[0]; myqueue.erase(myqueue.begin()); myqueue.push_back(nums[i]); std::cout<<"tmp........."<< tmp <<"\n"; printQueue(myqueue); if (tmp==max) { max = getMax(myqueue); results.push_back(max); } else if (nums[i]> max) { max = nums[i]; results.push_back(max); } else results.push_back(max); } return results; }
// A process dispatcher program // @par: int number of arguments // @par: pointer pointer process list file // @ret: int int main(int argc, char **argv) { if (argc != 2) { // Print directions if the file is not supplied and exit printf("You must supply a file to the program:\n" "$ hostd filename.txt\n"); exit(0); } initSystem(); char *filename = argv[1]; readFile(filename, dispatcher); if (VERBOSE) printf("Done processing %s!\n", filename); // Set CPU start time to 0 int time = 0; // Flag when a real time process is added to the real time queue int rtUpdated; // The system is running /*while (time < runtime) {*/ while (time < 20) { printf("===============================================================================================================================\n"); printf("Time (Quantum): %d\n", time); printf("===============================================================================================================================\n"); rtUpdated = updateDispatcher(time); if (VERBOSEMEMMAP) { printf("Current %d MByte memory map (each line is 64 Mbyte).\n", host.memSpaceMax); int memUnit; for(memUnit = 0; memUnit < MAX_MEMSPACE; memUnit++) { if (host.memSpace[memUnit] != 0) { printf("%d ", host.memSpace[memUnit]); } else { if (memUnit < MAX_RTMEMSPACE) { printf(". "); } else { printf("_ "); } } if ((memUnit + 1) % 64 == 0) { printf("\n"); } } printf("\n"); } if (VERBOSEQ) { printf("REAL TIME QUEUE: "); printQueue(realTimeQueue); printf("PRIORITY 1 QUEUE: "); printQueue(p1Queue); printf("PRIORITY 2 QUEUE: "); printQueue(p2Queue); printf("PRIORITY 3 QUEUE: "); printQueue(p3Queue); //TODO //print info about system resources and per process resources? } doProcessing(time, rtUpdated); time++; sleep(1); printf("\n"); } printf("===============================================================================================================================\n"); printf("Time (Quantum): %d, program terminating\n", time); printf("===============================================================================================================================\n"); closeSystem(); return 0; }
int main() { int i; int o; CREATE(queue1) ; //queue1[10]; //Try and Dequeue the empty queue1 printf("\nTrying to Dequeue..."); deQueue(queue1); //Try and Peek the empty queue1 printf("\nTrying to Peek..."); peek(queue1); //initialize 5,10,15,20,25,30,35,40,45,50 enQueue(queue1,5); enQueue(queue1,10); enQueue(queue1,15); enQueue(queue1,20); enQueue(queue1,25); enQueue(queue1,30); enQueue(queue1,35); enQueue(queue1,40); enQueue(queue1,45); enQueue(queue1,50); printQueue(queue1); if(DEBUG) printQueue(queue1); //Print queue1 //Add 55 to full queue printf("\nTry and Enqueue..."); enQueue(queue1,55); //Dequeue o=deQueue(queue1); printf("\nTake out: %d",o); if(DEBUG) printQueue(queue1); //Print queue1 enQueue(queue1,100); if(DEBUG) printQueue(queue1); //Print queue1 deQueue(queue1); deQueue(queue1); deQueue(queue1); deQueue(queue1); deQueue(queue1); deQueue(queue1); enQueue(queue1, 1000); if(DEBUG) printQueue(queue1); //Print queue1 return 0; };
void OrderController::printAll(){ printQueue(); printInProgress(); printCompleted(); }
PathNode* BFS(char * start,char * target){ printf("Start:%s Target:%s \n",start,target); Node* head = NULL; Node* tail = NULL; int i; int j; PathNode *toAdd = malloc(sizeof(PathNode)); memset(toAdd, 0, sizeof(PathNode)); toAdd->movie = malloc(sizeof(char)*65); memset(toAdd->movie, 0, sizeof(char)*65); strcpy(toAdd->movie,"START"); toAdd->actor = malloc(sizeof(char)*65); memset(toAdd->actor, 0, sizeof(char)*65); strcpy(toAdd->actor,start); toAdd->nextPath = NULL; Node *newNode = malloc(sizeof(Node)); newNode->value = toAdd; newNode->nextNode = NULL; enqueue(&head,&tail,&newNode); printQueue(head); puts("Using queue"); while(1){ Node *toCheck = dequeue(&head,&tail); PathNode *path = toCheck->value; char * prevActor = path->actor; strlist *allMovies = getMovies(prevActor); for(i = 0; i< allMovies->len; i++){ printQueue(head); char * movie = allMovies->items[i]; printf("Checking movie: #%i %s\n",i, movie); strlist * allActors = getActors(movie); printf("Actors Len: %i\n",allActors->len); for(j = 0; j< allActors->len; j++){ char * actor = allActors->items[j]; printf("Checking actor: #%i %s\n",j, actor); if(strcmp(actor,prevActor)){ PathNode *toAdd = malloc(sizeof(PathNode)); memset(toAdd, 0, sizeof(PathNode)); toAdd->movie = malloc(sizeof(char)*65); memset(toAdd->movie, 0, sizeof(char)*65); strcpy(toAdd->movie,movie); toAdd->actor = malloc(sizeof(char)*65); memset(toAdd->actor, 0, sizeof(char)*65); strcpy(toAdd->actor,actor); toAdd->nextPath = path; if (!strcmp(target,actor)){ return toAdd; }else{ Node *newNode = malloc(sizeof(Node)); newNode->value = toAdd; newNode->nextNode = NULL; enqueue(&head,&tail,&newNode); //printQueue(head); } } } } free(toCheck); } }
int main(int argc, char **argv) { PCB *job; // pointer used to move jobs between queues int jobPriority; int processStatus; //open file of jobs if(argc < 2) { printf("Dispatch list not found!\n"); return 0; } FILE *fd; fd = fopen(argv[1], "r"); if(fd == NULL) { printf("Could not open file %s.\n", argv[1]); return 0; } initQueues(); printf("Queues initialized successfully!\n"); createDispatchList(fd); printf("Read and stored all jobs in dispatch list!\n"); fclose(fd); // print out initial dispatch list if (VERBOSE) printQueue(dispatchName, dispatchQ); // START DISPATCHER while(1) { printf("\n-----------------------------------------\n"); printf("DISPATCHER TIME: %d SECONDS\n", clock); printf("-----------------------------------------\n"); if (SUPERVERBOSE) printf("DISPATCHER RESOURCE REPORT:\n"); if (SUPERVERBOSE) printf("Available Memory: %d\n", availableMem); if (SUPERVERBOSE) printf("Printers: %d\n", printers); if (SUPERVERBOSE) printf("Scanner: %d\n", scanner); if (SUPERVERBOSE) printf("Modem: %d\n", modem); if (SUPERVERBOSE) printf("CD Drives: %d\n", cddrives); // move all jobs with this time from dispatch to the submission queues // this happens on EVERY tick if (!isEmpty(dispatchQ)) { //assign all the jobs that are currently in the dispatch Queue while (dispatchQ->process->arrival_time <= clock) { //assign the jobs to the correct submission queue jobPriority = dispatchQ->process->priority; if (jobPriority == 0) { // realtimeq priority = 0 if (VERBOSE) printf("A new realtime job has arrived.\n"); job = dequeueFront(&dispatchQ); enqueueJob(realtimeQ, job); if (SUPERVERBOSE) printQueue(rtName, realtimeQ); } else if (jobPriority==1 || jobPriority==2 || jobPriority==3) { if (VERBOSE) printf("A new user job has arrived.\n"); job = dequeueFront(&dispatchQ); enqueueJob(userQ, job); if (SUPERVERBOSE) printQueue(userName, userQ); } if (isEmpty(dispatchQ)) break; } } // distribute user jobs into their priority queues bases on resources // happens on EVERY TICK if(isEmpty(userQ)==false){ int userQlength = getLength(userQ); int currJob = 0; // Go through entire queue only once while(currJob < userQlength){ //checking to make sure all the resources are avalable for the job if(resourcesAvailable(userQ)){ assignResources(userQ); printf("Successfuly allocated resources to a new user job.\n"); //gets the priority and the job off the userQ int userPriority; userPriority = userQ->process->priority; job = dequeueFront(&userQ); if (SUPERVERBOSE) printf("User Priority: %d\n", userPriority); //puts the job in the correct priority userQ if(userPriority==1){ enqueueJob(p1Q, job); } if(userPriority==2){ enqueueJob(p2Q, job); } if(userPriority ==3){ enqueueJob(p3Q, job); } // if resources arent avalable then job goes to the end of the queue } else { // safety check on if job requires too many resources if(userQ->process->mem_req > MAX_USER_MEMORY || userQ->process->printers > PRINTERS || userQ->process->scanners > SCANNERS || userQ->process->modems > MODEMS || userQ->process->cds > CDDRIVES) { // simply remove job job = dequeueFront(&userQ); free(job); userQlength--; }else { // cycle job to back of queue printf("A user job is waiting on resources...\n"); job = dequeueFront(&userQ); enqueueJob(userQ, job); } } currJob++; if (SUPERVERBOSE) printQueue(p1Name, p1Q); if (SUPERVERBOSE) printQueue(p2Name, p2Q); if (SUPERVERBOSE) printQueue(p3Name, p3Q); } // end while } // end userQ distributions /* Now check all the queues for a job to run. */ // Check the realtimeQ for a job first! if(isEmpty(realtimeQ)==false){ if(realtimeQ->process->pid < 0) { // job hasnt started yet so fork and exec realtimeQ->process->pid = fork(); if (realtimeQ->process->pid < 0) { fprintf(stderr, "Dispatcher failed to fork new process."); return 0; } else if(realtimeQ->process->pid == 0) { printJobDetails(realtimeQ); execl("./process", "process", "",NULL); } else { sleep(1); } } else { sleep(1); // RT processes never pause so just let it run } if (SUPERVERBOSE) printQueue(rtName, realtimeQ); realtimeQ->process->time_left--; //decrement time //check to see if it is zero if (VERBOSE) printf("Time left in real time process: %d\n", realtimeQ->process->time_left); if(realtimeQ->process->time_left == 0){ kill(realtimeQ->process->pid, SIGINT); // kill the process waitpid(realtimeQ->process->pid, &processStatus, WUNTRACED); freeMemSpace(realtimeQ); job = dequeueFront(&realtimeQ); free(job); } /* Now check lower priority Queues. The code for the lower priority queues will be very symmetrical */ } else if(isEmpty(p1Q)==false) { if(p1Q->process->pid < 0) { // job hasnt started yet so fork and exec p1Q->process->pid = fork(); if (p1Q->process->pid < 0) { fprintf(stderr, "Dispatcher failed to fork new process."); return 0; } else if(p1Q->process->pid == 0) { printJobDetails(p1Q); execl("./process", "process", "",NULL); } else { sleep(1); } } else { // it was previously paused, so resume it if (SUPERVERBOSE) printf("Attempting to resume process...\n"); kill(p1Q->process->pid, SIGCONT); sleep(1); // let it run for 1 s } if (SUPERVERBOSE) printQueue(p1Name, p1Q); //decrement time p1Q->process->time_left--; //check to see if it is zero if (VERBOSE) printf("Time left in p1Q process: %d\n", p1Q->process->time_left); if(p1Q->process->time_left == 0){ kill(p1Q->process->pid, SIGINT); // kill the process waitpid(p1Q->process->pid, &processStatus, WUNTRACED); freeMemSpace(p1Q); // free its memory // free all resources printers += p1Q->process->printers; scanner += p1Q->process->scanners; modem += p1Q->process->modems; cddrives += p1Q->process->cds; // remove the PCB from the queue job = dequeueFront(&p1Q); free(job); }else { // pause it and decrease its priority kill(p1Q->process->pid, SIGTSTP); waitpid(p1Q->process->pid, &processStatus, WUNTRACED); job = dequeueFront(&p1Q); enqueueJob(p2Q, job); } //check second user priority queue }else if(isEmpty(p2Q)==false){ if(p2Q->process->pid < 0) { // job hasnt started yet so fork and exec p2Q->process->pid = fork(); if (p2Q->process->pid < 0) { fprintf(stderr, "Dispatcher failed to fork new process."); return 0; } else if(p2Q->process->pid == 0) { printJobDetails(p2Q); execl("./process", "process", "",NULL); } else { sleep(1); } } else { // it was previously paused, so resume it if (SUPERVERBOSE) printf("Attempting to resume process...\n"); kill(p2Q->process->pid, SIGCONT); sleep(1); // let it run for 1 s } if (SUPERVERBOSE) printQueue(p2Name, p2Q); //decrement time p2Q->process->time_left--; //check to see if it is zero if (VERBOSE) printf("Time left in p2Q process: %d\n", p2Q->process->time_left); if(p2Q->process->time_left == 0){ kill(p2Q->process->pid, SIGINT); // kill the process waitpid(p2Q->process->pid, &processStatus, WUNTRACED); freeMemSpace(p2Q); // free its memory // free all resources printers += p2Q->process->printers; scanner += p2Q->process->scanners; modem += p2Q->process->modems; cddrives += p2Q->process->cds; // remove the PCB from the queue job = dequeueFront(&p2Q); free(job); }else { // pause it and decrease its priority kill(p2Q->process->pid, SIGTSTP); waitpid(p2Q->process->pid, &processStatus, WUNTRACED); job = dequeueFront(&p2Q); enqueueJob(p3Q, job); } //check third priority queue }else if(isEmpty(p3Q)==false){ if(p3Q->process->pid < 0) { // job hasnt started yet so fork and exec p3Q->process->pid = fork(); if (p3Q->process->pid < 0) { fprintf(stderr, "Dispatcher failed to fork new process."); return 0; } else if(p3Q->process->pid == 0) { printJobDetails(p3Q); execl("./process", "process", "",NULL); } else { sleep(1); } } else { // it was previously paused, so resume it if (SUPERVERBOSE) printf("Attempting to resume process...\n"); kill(p3Q->process->pid, SIGCONT); sleep(1); // let it run for 1 s } if (SUPERVERBOSE) printQueue(p3Name, p3Q); //decrement time p3Q->process->time_left--; //check to see if it is zero if (VERBOSE) printf("Time left in p3Q process: %d\n", p3Q->process->time_left); if(p3Q->process->time_left == 0){ kill(p3Q->process->pid, SIGINT); // kill the process waitpid(p3Q->process->pid, &processStatus, WUNTRACED); freeMemSpace(p3Q); // free its memory // free all resources printers += p3Q->process->printers; scanner += p3Q->process->scanners; modem += p3Q->process->modems; cddrives += p3Q->process->cds; // remove the PCB from the queue job = dequeueFront(&p3Q); free(job); } else { // pause it and cycle the queue b/c its now Round Robin kill(p3Q->process->pid, SIGTSTP); waitpid(p3Q->process->pid, &processStatus, WUNTRACED); job = dequeueFront(&p3Q); enqueueJob(p3Q, job); } } else { sleep(1); // if nothing new happens, sleep anyway } // increment clock clock++; // exit the dispatcher only once all queues are empty if(isEmpty(dispatchQ) && isEmpty(userQ) && isEmpty(realtimeQ) && isEmpty(p1Q) && isEmpty(p2Q) && isEmpty(p3Q)){ break; } } printf("All jobs ran to completion. Terminating dispatcher...\n"); // free all allocated mem before exiting freeQueues(); return 0; }