コード例 #1
0
ファイル: schedule.c プロジェクト: iczyg/cmps111
/*===========================================================================*
 *				play_lottery				     *
 *===========================================================================*/
PRIVATE void play_lottery(void)
{
	struct schedproc *rmp;
	int winning_ticket; 
	int proc_nr;
	int rv;
	int sum=0;
	/*Run a for loop to find the total number of tickets.*/
	for (proc_nr=0, rmp=schedproc; proc_nr < NR_PROCS; proc_nr++, rmp++) {
		if (rmp->flags & IN_USE && PROCESS_IN_USER_Q(rmp)) {
			sum += rmp->tickets;
		}
	}
    	winning_ticket = rand() % tic_num;
	printf("Playing lottery. winning_ticket: %d\n", winning_ticket);
	sum=0;
/*------------------------------------------------------------------------------*/
	/*Determine the winner process. Change the winner priority to Winner
	Queue, Also save the winner queue to temp -type schedproc.*/
	for(proc_nr = 0, rmp = schedproc; proc_nr < NR_PROCS; proc_nr++, rmp++){
		if(rmp->flags & IN_USE && PROCESS_IN_USER_Q(rmp)) {
			if((winning_ticket-=rmp->tickets) < 0){
				rmp->priority=WIN_Q;
				temp = rmp; 
				schedule_process(rmp);
				break;
			}
		}
	}
}
コード例 #2
0
ファイル: schedule.c プロジェクト: imvikash00/Minix_scheduler
/*==========================================================================*
  *				do_lottery				     *
  *===========================================================================*/
int do_lottery()
 {
 	struct schedproc *rmp;
 	int proc_nr;
 	int lucky;
 	int old_priority;
 	int flag = -1;
 	int nTickets = 0;
 
 	for (proc_nr=0, rmp=schedproc; proc_nr < NR_PROCS; proc_nr++, rmp++) {
 		if ((rmp->flags & IN_USE) && PROCESS_IN_USER_Q(rmp)) {
 			if (USER_Q == rmp->priority) {
 				nTickets += rmp->ticketsNum;
 			}
 		}
 	}
 
 	lucky = nTickets ? random() % nTickets : 0;
 	for (proc_nr=0, rmp=schedproc; proc_nr < NR_PROCS; proc_nr++, rmp++) {
 		if ((rmp->flags & IN_USE) && PROCESS_IN_USER_Q(rmp) &&
 				USER_Q == rmp->priority) {
 			old_priority = rmp->priority;
 			/* rmp->priority = USER_Q; */
 			if (lucky >= 0) {
 				lucky -= rmp->ticketsNum;
 				/*
 				   printf("lucky - %d = %d\n", rmp->ticketsNum, lucky);
 				 */
 				if (lucky < 0) {
 					rmp->priority = MAX_USER_Q;
 					flag = OK;
 					/* printf("endpoint %d\n", rmp->endpoint); */
 				}
 			}
 			if (old_priority != rmp->priority) {
 				schedule_process_local(rmp);
				break;
 			}
 		}
 	}
 	/*
	for (proc_nr=0, rmp=schedproc; proc_nr < NR_PROCS; proc_nr++, rmp++) {
 		if ((rmp->flags & IN_USE) && PROCESS_IN_USER_Q(rmp)) {
 			if (USER_Q == rmp->priority)
 				count_17++;
 			else if (MAX_USER_Q == rmp->priority)
 				count_16++;
 		}
	}
	printf("in 16: %d; in 17: %d\n", count_16, count_17); 
 	*/
 	/* printf("do_lottery OK? %d lucky=%d\n", flag, lucky); */
 	return nTickets ? flag : OK;
}
コード例 #3
0
ファイル: schedule.c プロジェクト: imvikash00/Minix_scheduler
int do_noquantum(message *m_ptr)
{
	register struct schedproc *rmp;
	int rv, proc_nr_n;

	if (sched_isokendpt(m_ptr->m_source, &proc_nr_n) != OK) {
		printf("SCHED: WARNING: got an invalid endpoint in OOQ msg %u.\n",
		m_ptr->m_source);
		return EBADEPT;
	}

	rmp = &schedproc[proc_nr_n];
	
	if (PROCESS_IN_USER_Q(rmp)) {
		rmp->priority = USER_Q;
 	} else if (rmp->priority < MAX_USER_Q - 1){
 		rmp->priority += 1;
 	}
 	/* printf("do_noquantum, %d in %d\n", rmp->endpoint, rmp->priority); */

	if ((rv = schedule_process_local(rmp)) != OK) {
		return rv;
	}
	
	if ((rv = do_lottery()) != OK) {
 		return rv;
	}
	return OK;
}
/*===========================================================================*
 *				do_lottery				     *
 *===========================================================================*/
int do_lottery() {
	struct schedproc *rmp;
	int proc_nr;
	int lucky;
	int old_priority;
	int flag = -1;
	int nTickets = 0;//Number of tickets hold by all processes in USER_Q

	/*calculates nTickets by adding tickets held by each process in USER_Q*/
	for (proc_nr=0, rmp=schedproc; proc_nr < NR_PROCS; proc_nr++, rmp++) {
		if ((rmp->flags & IN_USE) && PROCESS_IN_USER_Q(rmp)) {
			if(USER_Q == rmp->priority) {
				nTickets += rmp->ticketsNum;
			}
		}
	}

	//lucky no. is generated between 0 to nTickets
	lucky = nTickets ? random() % nTickets : 0;

	/*
        Selects process to schedule based  on lucky no.
        For each process in ready queue we use lucky = lucky - rmp->ticktsNum and if
        lucky < 0 we select that process to run by increasing the priority to 12.
	*/
	for (proc_nr=0, rmp=schedproc; proc_nr < NR_PROCS; proc_nr++, rmp++) {
		if ((rmp->flags & IN_USE) && PROCESS_IN_USER_Q(rmp) && USER_Q == rmp->priority) {//if process is in USER_Q
			old_priority = rmp->priority;												 //saving process priority to check whether process is elligible to run or not
			if (lucky >= 0) {															 //if lucky is greater than 0
				lucky -= rmp->ticketsNum;												 //subtract tickets held by the process from lucky
				if (lucky < 0) {														 //if lucky is less than 0
					rmp->priority = MAX_USER_Q;											 //change default priority of process to MAX_USER_Q
					flag = OK;															 //set flag to be OK
				}
			}
			if (old_priority != rmp->priority) {                                         //if priority of process changed
				schedule_process(rmp, flag);											 //schedule the process
			}
		}
	}
	return nTickets ? flag : OK;														 //return -1 if no process in USER_Q else OK
 }
/* This function in called every 100 ticks to rebalance the queues. The current
 * scheduler bumps processes down one priority when ever they run out of
 * quantum. This function will find all proccesses that have been bumped down,
 * and pulls them back up. This default policy will soon be changed.
 */
static void balance_queues(struct timer *tp)
{
	struct schedproc *rmp;
	int proc_nr;

	for (proc_nr=0, rmp=schedproc; proc_nr < NR_PROCS; proc_nr++, rmp++) {
		if (rmp->flags & IN_USE) {
			if (rmp->priority > rmp->max_priority && !PROCESS_IN_USER_Q(rmp)) {
				rmp->priority -= 1; /* increase priority */
				schedule_process_local(rmp);
			}
		}
	}

	set_timer(&sched_timer, balance_timeout, balance_queues, 0);
}
int do_noquantum(message *m_ptr)
{
	register struct schedproc *rmp;
	int rv, proc_nr_n;

	if (sched_isokendpt(m_ptr->m_source, &proc_nr_n) != OK) {
		printf("SCHED: WARNING: got an invalid endpoint in OOQ msg %u.\n",
		m_ptr->m_source);
		return EBADEPT;
	}

	rmp = &schedproc[proc_nr_n];
	rmp->cputime += rmp->time_slice;
	printf("\nprocess id %u has cputime %ld\n", rmp->endpoint, rmp->cputime);

	/*Lottey Scheduling*/
	/*
        If the process is in the user queue then the priority remains unchanged and we are printing it's tickets number
        time slice and priority else if the process is not in the user queue then lower the priority by 1.
	*/
	if (PROCESS_IN_USER_Q(rmp)) {
		rmp->priority = USER_Q;
		printf("\nProcess %d is executed whose ticket no is %d with time quantum %d with priority %d\n", rmp->endpoint, rmp->ticketsNum, rmp->time_slice, rmp->priority);
	} else if (rmp->priority < MAX_USER_Q - 1) {
		rmp->priority += 1; /* lower priority */
	}

	if ((rv = schedule_process_local(rmp)) != OK) {
		return rv;
	}

	/*
        Lottey Scheduling
        After a process completed it's quantum do_lottery function is called to start another process
	*/
	if((rv = do_lottery()) != OK) {
		return rv;
	}

	return OK;
}