Пример #1
0
/*TerminatePcb si preoccupa di terminare ricorsivamente tutta la progenie di p, root dell'albero di processi*/
void terminatePcb(pcb_t *p){
	semd_t* semd;
	if(p->p_first_child != NULL)
		terminatePcb(p->p_first_child);

	if(p->p_first_child == NULL && p->p_sib != NULL)
		terminatePcb(p->p_sib);	
	
	if(p->p_first_child == NULL && p->p_sib == NULL){
		lock(MUTEX_SCHEDULER);
		if(outBlocked(p)){
			/*Rimuovo p dalla coda dei processi del suo semaforo*/
			lessSoftCounter(); /*Decremento il softBlock counter*/
			semd = getSemd(p->p_semkey);
			/*Se p è bloccato su un semaforo di un device, non incremento il value. Sarà l'interrupt a farlo*/
			if(!checkSemaphore(p))
				(*semd->s_key)++;	
		}
		/*Tolgo p dalla ReadyQueue, se presente*/
		outProcQ(&ready_queue[getPRID()], p);
		outChild(p);
		freePcb(p);
		unlock(MUTEX_SCHEDULER);
	} 
}
Пример #2
0
//funtion to run the algorithm
void
rr_run(){
	PcbPtr current_process = NULL;
	int dispatch_timer = 0;
	while(input_head || rr_head || current_process){
		//puts("input q:");
		//print_arrivaltimes(input_head);
		//puts("rr q:");
		//print_arrivaltimes(rr_head);
		while(input_head && input_head->arrivaltime <= dispatch_timer){
			//dequeue the head of the input queue, then add
			//it to the end of the round robin queue.
			//We need to initialize the head if we haven't already
			if(!rr_head){
				rr_head = deqPcb(&input_head);
				rr_head->next = NULL;
			}
			else{
				PcbPtr tmp = deqPcb(&input_head);
				tmp->next = NULL;
				enqPcb(rr_head,tmp);
			}
		}
		if(current_process){
			current_process->remainingcputime--;
			if(current_process->remainingcputime <= 0){
				terminatePcb(current_process);
				free(current_process);
				current_process = NULL;
				
			}
			//if there is something waiting in the RR queue,
			//we need to stop the current process and give
			//another process some time.
			else if(rr_head){
				if(kill(current_process->pid,SIGTSTP)){
					fprintf(stderr,"temp stop of %d failed",
							(int)current_process->pid);
				}
				enqPcb(rr_head,current_process);
				current_process = NULL;
			}
		}
		if(!current_process && rr_head){
			waitpid(-1, NULL, WUNTRACED);
			current_process = deqPcb(&rr_head);
			current_process->next = NULL;
			startPcb(current_process);
		}
		++dispatch_timer;
		sleep(1);
	}

}
Пример #3
0
/*Rimuove il PCB puntato da p dalla coda del semaforo su cui e’ bloccato
outChildBlocked() termina anche tutti i processi discendenti di p.
L'eliminazione avviene visitando l'albero come una DFS*/
void outChildBlocked(pcb_t *p){
	semd_t* semd;
	if(outBlocked(p)){
		/*Rimuovo p dalla coda dei processi del suo semaforo*/
		lessSoftCounter(); /*Decremento il softBlock counter*/
		semd = getSemd(p->p_semkey);
		/*Se p è bloccato su un semaforo di un device, non incremento il value. Sarà l'interrupt a farlo*/
		if(!checkSemaphore(p))
			(*semd->s_key)++;	
	}
	outChild(p);
	/*Se p ha una progenie, terminatePcb si occupa di terminarla*/
	if(p->p_first_child)
		terminatePcb(p->p_first_child); /*Elimina la progenie di p*/

	lock(MUTEX_SCHEDULER);
	freePcb(p); /*Resetto i campi del pcb puntato da p che viene reinserito nella lista pcbFree*/
	unlock(MUTEX_SCHEDULER);
}
Пример #4
0
int main (int argc, char *argv[])
{
    char * inputfile;             // job dispatch file
    FILE * inputliststream;
    PcbPtr inputqueue = NULL;     // input queue buffer
    PcbPtr currentprocess = NULL; // current process
    PcbPtr process = NULL;        // working pcb pointer
    int timer = 0;                // dispatcher timer 

//  0. Parse command line

    if (argc == 2) inputfile = argv[1];
    else PrintUsage (stderr, argv[0]);

//  1. Initialize dispatcher queue;
//     (already initialised in assignments above)
    
//  2. Fill dispatcher queue from dispatch list file;
    
    if (!(inputliststream = fopen(inputfile, "r"))) { // open it
          SysErrMsg("could not open dispatch list file:", inputfile);
          exit(2);
    }

    while (!feof(inputliststream)) {  // put processes into input_queue
        process = createnullPcb();
        if (fscanf(inputliststream,"%d, %d, %d, %d, %d, %d, %d, %d",
             &(process->arrivaltime), &(process->priority),
             &(process->remainingcputime), &(process->mbytes),
             &(process->req.printers), &(process->req.scanners),
             &(process->req.modems), &(process->req.cds)) != 8) {
            free(process);
            continue;
        }
        process->status = PCB_INITIALIZED;
        inputqueue = enqPcb(inputqueue, process);
    }

//  3. Start dispatcher timer;
//     (already set to zero above)
        
//  4. While there's anything in the queue or there is a currently running process:

   while(inputqueue != NULL || currentprocess != NULL){//begin while
	

//      i. If a process is currently running;

        if(currentprocess != NULL){//begin if

//          a. Decrement process remainingcputime;

        currentprocess->remainingcputime--;
            
//          b. If times up:

          if(currentprocess->remainingcputime <=0){//begin if1
                
//             A. Send SIGINT to the process to terminate it;

               currentprocess = terminatePcb(currentprocess);
                
//             B. Free up process structure memory

          free(currentprocess);
          printf("Successful");
        }//end if1
	}//end if
//     ii. If no process now currently running &&
//           dispatcher queue is not empty &&
//           arrivaltime of process at head of queue is <= dispatcher timer:

    if(currentprocess == NULL && inputqueue != NULL && inputqueue->arrivaltime <= timer){//begin if

//          a. Dequeue process and start it (fork & exec)
//          b. Set it as currently running process;
            process = deqPcb(&inputqueue);
	    currentprocess = startPcb(process);
        
}//end if
//     iii. sleep for one second;

       sleep(1);
            
//      iv. Increment dispatcher timer;

       timer++;
            
//       v. Go back to 4.

   continue;
        
}//end while
//    5. Exit

    exit (0);
}    
Пример #5
0
int main (int argc, char *argv[])
{
    char * inputfile;             // job dispatch file
    FILE * inputliststream;
    PcbPtr inputqueue = NULL;     // input queue buffer
	PcbPtr inputqueue_tail = NULL;
	PcbPtr curr = NULL; // for creating queue
    PcbPtr currentprocess = NULL; // current process
    PcbPtr process = NULL;        // working pcb pointer
    int timer = 0;                // dispatcher timer 
	
	char buffer[75]; // for reading current line of file
	int notNeeded[3]; // for storing the unneeded arguements of input line

//  0. Parse command line

    if (argc == 2) inputfile = argv[1];
    else PrintUsage (stderr, argv[0]);

//  1. Initialize dispatcher queue;
//     (already initialised in assignments above)
    
//  2. Fill dispatcher queue from dispatch list file;
    
    if (!(inputliststream = fopen(inputfile, "r"))) { // open it
          SysErrMsg("could not open dispatch list file:", inputfile);
          exit(2);
    }

	inputqueue = createnullPcb();
	curr = inputqueue;
	
	
    while (!feof(inputliststream)) {  // put processes into input_queue
 // your code goes here
// don't forget to initialize the PCB status
// and put PCB on the queue
		// MH DONE
		
		
		curr->next = malloc(sizeof(Pcb));
		curr->args[0] = DEFAULT_PROCESS;
		curr->args[1] = NULL;
		fgets(buffer,sizeof(buffer),inputliststream);
				
		
		sscanf(buffer, 
			   "%d, %d, %d, %d", 
			   &curr->arrivaltime,
			   &curr->priority,
			   &curr->remainingcputime,
			   &curr->mbytes
			   );
		curr = curr->next;
		
    }
	
	inputqueue_tail = curr;

//  3. Start dispatcher timer;
//     (already set to zero above)
        
//  4. While there's anything in the queue or there is a currently running process:

    while (inputqueue || currentprocess ) {// i'll give you this outer loop, now do the rest
/*
      i. If a process is currently running;
	             a. Decrement process remainingcputime;
            b. If times up:
                A. Send SIGINT to the process to terminate it;
                B. Free up process structure memory
       ii. If no process currently running &&
            dispatcher queue is not empty &&
            arrivaltime of process at head of queue is <= dispatcher timer:
            a. Dequeue process and start it (fork & exec)
            b. Set it as currently running process;
      iii. sleep for one second;
       iv. Increment dispatcher timer;
        v. Go back to 4.
*/


	
	//If a process is currently running;
	if(currentprocess)
	{
		//a. Decrement process remainingcputime;
		currentprocess->remainingcputime = currentprocess->remainingcputime - 1;
		// b. If times up:
		if(currentprocess->remainingcputime == 0 ) 
			//A. Send SIGINT to the process to terminate it;
			//B. Free up process structure memory
			if(terminatePcb(currentprocess)!=NULL)
				free(currentprocess);
	 }
	/* 
	 ii. If no process currently running &&
	 dispatcher queue is not empty &&
	 arrivaltime of process at head of queue is <= dispatcher timer:
	 a. Dequeue process and start it (fork & exec)
	 b. Set it as currently running process;
	 */
	 if(!currentprocess 
		&& inputqueue!=NULL 
		&& inputqueue->arrivaltime < timer)
	 {
		 PcbPtr dequed = deqPcb(inputqueue);
		 currentprocess = startPcb(dequed);
	 }	 
		 
		 
			
		
		//iii. sleep for one second;
		sleep(1000);
		
		//increment dispatch timer
		timer++;
		
		/*printf("current process: %d, remainingcputime: $d, 
			   dispatch timer: $d", currentProcess->pid, currentProcess->remainingcputime,
			   timer);*/
	
	}
		// 5. Exit
    exit (0);
}    
Пример #6
0
int main (int argc, char *argv[])
{
    char * inputfile;             // job dispatch file
    FILE * inputliststream;
    PcbPtr inputqueue = NULL;     // input queue buffer
    PcbPtr currentprocess = NULL; // current process
	//    PcbPtr process = NULL;        // working pcb pointer
    int timer = 0;                // dispatcher timer 
	
	//  0. Parse command line
	
    if (argc == 2) inputfile = argv[1];
    else PrintUsage (stderr, argv[0]);
	
	//  1. Initialize dispatcher queue;
	//     (already initialised in assignments above)
    
	//  2. Fill dispatcher queue from dispatch list file;
    
    if (!(inputliststream = fopen(inputfile, "r"))) { // open it
		SysErrMsg("could not open dispatch list file: ", inputfile);
		exit(2);
    }
	
	PcbPtr nextPCB;
    while (!feof(inputliststream)) {  
		// put processes into input_queue
		nextPCB = createnullPcb();
		if(fscanf(inputliststream, "%d, %d, %d, %d, %d, %d, %d, %d", 
				  &(nextPCB->arrivaltime), 
				  &(nextPCB->priority),
				  &(nextPCB->remainingcputime), 
				  &(nextPCB->mbytes),
				  &(nextPCB->req.printers), 
				  &(nextPCB->req.scanners),
				  &(nextPCB->req.modems), 
				  &(nextPCB->req.cds)			)!= 8){
			free(nextPCB);
			continue;
		}
		nextPCB->status = PCB_INITIALIZED;
		inputqueue = enqPcb(inputqueue, nextPCB);		
	}
	
	//  3. Start dispatcher timer;
	//     (already set to zero above)
	
	
	//  4. While there's anything in the queue or there is a currently running process:
	
    while (inputqueue || currentprocess ) {
		if(currentprocess){
			(currentprocess->remainingcputime) -= 1;
			if(currentprocess->remainingcputime == 0){
				//				if(prevprocess == NULL){
				//					// this would have to be the first/only process, or head of the queue
				//					// that would mean the last process added would be the previous process
				//					nextPCB->next = currentprocess->next;
				//				}else {
				//					prevprocess->next = currentprocess->next;
				//				}
				//send SIGINT to terminate process
				
				terminatePcb(currentprocess);
				free(currentprocess);
				currentprocess = NULL;
			}
		}
		
		if (!currentprocess && inputqueue && inputqueue->arrivaltime <= timer){
			currentprocess = deqPcb(&inputqueue);
			if(currentprocess) startPcb(currentprocess);
		}
		sleep(1);
		timer++;
	}
	// 5. Exit
    exit (0);
}    
Пример #7
0
int main (int argc, char *argv[])
{
    char * inputfile;             // job dispatch file
    FILE * inputliststream;
    PcbPtr inputqueue = NULL;     // input queue buffer
    PcbPtr fbqueue[N_FB_QUEUES];  // feedback queues
    PcbPtr currentprocess = NULL; // current process
    PcbPtr process = NULL;        // working pcb pointer
    int timer = 0;                // dispatcher timer
    int quantum = QUANTUM;        // current time-slice quantum
    int i;                        // working index

//  0. Parse command line

    if (argc == 2) inputfile = argv[1];
    else PrintUsage (stderr, argv[0]);

//  1. Initialize dispatcher queues;

    for (i = 0; i < N_FB_QUEUES; fbqueue[i++] = NULL);
    
//  2. Fill dispatcher queue from dispatch list file;
    
    if (!(inputliststream = fopen(inputfile, "r"))) { // open it
          SysErrMsg("could not open dispatch list file:", inputfile);
          exit(2);
    }

    while (!feof(inputliststream)) {  // put processes into input_queue
        process = createnullPcb();
        if (fscanf(inputliststream,"%d, %d, %d, %d, %d, %d, %d, %d",
             &(process->arrivaltime), &(process->priority),
             &(process->remainingcputime), &(process->mbytes),
             &(process->req.printers), &(process->req.scanners),
             &(process->req.modems), &(process->req.cds)) != 8) {
            free(process);
            continue;
        }
        process->status = PCB_INITIALIZED;
        inputqueue = enqPcb(inputqueue, process);
    }

//  3. Start dispatcher timer;
//     (already set to zero above)
	
	
	//initialize memory
	MabPtr memall = createnullMab(1024);
	//split memory into "protected" Real Time process memory and User Memory
	
	MabPtr rtmem = memSplit(memall, 64);
	MabPtr mem = rtmem->next;	//work with "mem" as user memory area
	mem->justused = 1; //initialize, the "justused" represents the most recently used, and the algorithm assumes one exists no matter what
	
    PcbPtr feedbackpending = NULL;	// processes waiting for memory
	
	
	
	
//  4. While there's anything in any of the queues or there is a currently running process:

    while (inputqueue || (CheckQueues(fbqueue) >= 0) || currentprocess ) {
//      i. Unload any pending processes from the input queue:
//         While (head-of-input-queue.arrival-time <= dispatcher timer)
//         dequeue process from input queue and and enqueue on feebackpending queue
		
		
        while (inputqueue && inputqueue->arrivaltime <= timer) {
            process = deqPcb(&inputqueue);          // dequeue process
            process->status = PCB_READY;            // set pcb ready
            process->priority = 0;                  // override any priority 
            feedbackpending = enqPcb(feedbackpending, process);
                                                    // & put on queue
        }
		
		
		PcbPtr nextprocess = feedbackpending;
		//unload pending processes from user job queue:
		
		//while (feedbackpending->mbytes is free somewhere in user mem:
		while(feedbackpending && memChk(mem, feedbackpending->mbytes)){
			nextprocess = deqPcb(&feedbackpending); // dequeue process from user job queue
			nextprocess->memoryblock = memAlloc(mem, nextprocess->mbytes); // allocate memory to the process
			nextprocess->priority = 0;
			fbqueue[nextprocess->priority] = enqPcb(fbqueue[nextprocess->priority], nextprocess);
		}
		
		puts("got it");
		

//     iii. If a process is currently running;
        if (currentprocess) {
            currentprocess->remainingcputime -= quantum;
            if (currentprocess->remainingcputime <= 0) {
                terminatePcb(currentprocess);
				
				memFree(currentprocess->memoryblock);
				
                free(currentprocess);
                currentprocess = NULL;
                
//         c. else if other processes are waiting in feedback queues:
            } else if (CheckQueues(fbqueue) >= 0) {
                suspendPcb(currentprocess);
				
                if (++(currentprocess->priority) >= N_FB_QUEUES)
                    currentprocess->priority = N_FB_QUEUES - 1;
                fbqueue[currentprocess->priority] = 
                    enqPcb(fbqueue[currentprocess->priority], currentprocess);
                currentprocess = NULL;
            }
        }
        
//    iv. If no process currently running && feedback queues are not empty:

        if (!currentprocess && (i = CheckQueues(fbqueue)) >= 0) {
            currentprocess = deqPcb(&fbqueue[i]);
			startPcb(currentprocess);
        }
        
//      v. sleep for quantum;

        quantum = currentprocess && currentprocess->remainingcputime < QUANTUM ?
                  currentprocess->remainingcputime :
                  !(currentprocess) ? 1 : QUANTUM;
        sleep(quantum);
		
        timer += quantum;
    }
	
    exit (0);
}    
Пример #8
0
/**
 *  Main function
 */
int main(int argc, char **argv) {
	PcbPtr curPs = NULL, tmp;
	initialise();

	/* read job dispatch list from the file */
	if(argc != 2)
		error("Error: <file> argument missing or incorrect");
	readFile(argv[1]);
	
	
	/* while there's currently running process or anything in queues */
	while(curPs || inputQ || userJobQ || realTimeQ || priorityQ_1 || priorityQ_2 || priorityQ_3) {
		deqInputQ();
		deqUserJobQ();

		/* if there is a currently running process */
		if(curPs) {
			curPs->remainingCpuTime -= TIME_QUANTUM;	// decrement process remaining time
			
			/* if time's up */
			if(curPs->remainingCpuTime <= 0) {
				/* terminate process */
				curPs = terminatePcb(curPs);

				/* free memory */
				if(curPs->priority == 0)
					curPs->mab->allocated = 0;
				else
					memFree(curPs->mab);
	
				/* free resource */
				rsrcFree(curPs);
	
				free(curPs);
				curPs = NULL;
			}

			/* if user process and there's any process are waiting */
			else if(curPs->priority > 0 && (realTimeQ || priorityQ_1 || priorityQ_2 || priorityQ_3) ) {
				curPs = suspendPcb(curPs);
				
				/* decrease priority, and enqueue on appropriate queue */
				switch(curPs->priority) {
					case 1:
						priorityQ_2 = enqPcb(priorityQ_2, curPs);
						break;
					default:
						priorityQ_3 = enqPcb(priorityQ_3, curPs);
						break;
				}
				if(curPs->priority < 3)
					curPs->priority++;

				curPs = NULL;
			}

		}

		/* if no currently running process */
		if(!curPs) {
			/* dequeue a highest priority process */
			tmp = NULL;
			if(realTimeQ)
				tmp = deqPcb(&realTimeQ);
			else if(priorityQ_1)
				tmp = deqPcb(&priorityQ_1);
			else if(priorityQ_2)
				tmp = deqPcb(&priorityQ_2);
			else if(priorityQ_3)
				tmp = deqPcb(&priorityQ_3);

			/* if there's anything in dispatch queues */
			if(tmp != NULL) {
				curPs = tmp;
				curPs = startPcb(curPs);	 // start or restart 
			}
		}
		
		struct timespec req, rem;
		req.tv_sec = TIME_QUANTUM;
		req.tv_nsec = 0; 
		nanosleep(&req, &rem);
		cpuTimer += TIME_QUANTUM;
	}
	
	/* free memory */
	realTimeMemory = memFree(realTimeMemory);
	memory = memFree(memory);

	return 0;
}