/*===========================================================================*
 *				do_stop_scheduling			     *
 *===========================================================================*/
int do_stop_scheduling(message *m_ptr)
{
	register struct schedproc *rmp;
	int proc_nr_n;
	int rv;

	/* check who can send you requests */
	if (!accept_message(m_ptr))
		return EPERM;

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

	rmp = &schedproc[proc_nr_n];
#ifdef CONFIG_SMP
	cpu_proc[rmp->cpu]--;
#endif
	rmp->flags = 0; /*&= ~IN_USE;*/

	/*
        Lottey Scheduling
        After a process has finished its execution do_lottery is called to start another process
    */
	if ((rv = do_lottery()) != OK) {
		return rv;
	}

	return OK;
}
Esempio n. 2
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];
	
	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;
}
/*==========================================================================================*
 ######  ########  #######  ########           ######   ######  ##     ## ######## ########  
##    ##    ##    ##     ## ##     ##         ##    ## ##    ## ##     ## ##       ##     ## 
##          ##    ##     ## ##     ##         ##       ##       ##     ## ##       ##     ## 
 ######     ##    ##     ## ########           ######  ##       ######### ######   ##     ## 
      ##    ##    ##     ## ##                      ## ##       ##     ## ##       ##     ## 
##    ##    ##    ##     ## ##                ##    ## ##    ## ##     ## ##       ##     ##
 ######     ##     #######  ##        #######  ######   ######  ##     ## ######## ########  
 *==========================================================================================*/
PUBLIC int do_stop_scheduling(message *m_ptr)
{
	register struct schedproc *rmp;
	int err, proc_nr_n;

	do_print("CMPS111", "do_stop_scheduling");

	/* check who can send you requests */
	if (!accept_message(m_ptr))
		return EPERM;

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

	rmp = &schedproc[proc_nr_n];
	do_print_process(rmp, "do_stop_scheduling", DEBUG);
	rmp->flags &= ~IN_USE;
	/*if ((err = schedule_process(rmp, "do_stop_scheduling")) != OK) {
		printf("ERROROROAROAROROR\n");
	}*/
	if (rmp->priority == MAX_USER_Q) {
		do_lottery();
	}
	return OK;
}
Esempio n. 4
0
/* CHANGE END */
int do_noquantum(message *m_ptr)
{
    /* CHANGE START */
    struct schedproc *rmp, *rmp_temp;
    /* CHANGE END */
    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];

    /* CHANGE START */
    rmp->time_slice = DEFAULT_USER_TIME_SLICE;
    rmp->priority = HOLDING_Q;

    if ((rv = schedule_process_local(rmp)) != OK) /* move out of quantum process */
        return rv;

    if ((rv = do_lottery()) != OK) /* schedule a new winner */
        return rv;
    /* CHANGE END */

    return OK;
}
Esempio n. 5
0
/*===========================================================================*
 *				do_nice					     *
 *===========================================================================*/
int do_nice(message *m_ptr)
{
	struct schedproc *rmp;
	int rv;
	int proc_nr_n;
	unsigned new_q, old_q, old_max_q;
	int old_ticketsNum;

	/* check who can send you requests */
	if (!accept_message(m_ptr))
		return EPERM;

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

	rmp = &schedproc[proc_nr_n];
	new_q = (unsigned) m_ptr->SCHEDULING_MAXPRIO;
	if (new_q >= NR_SCHED_QUEUES) {
		return EINVAL;
	}

	/* Store old values, in case we need to roll back the changes */
	old_q     = rmp->priority;
	old_max_q = rmp->max_priority;
	old_ticketsNum = rmp->ticketsNum;

	/* Update the proc entry and reschedule the process */
	/*rmp->max_priority = rmp->priority = new_q;*/
	rmp->priority = USER_Q;
	
	
	/**new_q = MAX_USER_Q + (nice-PRIO_MIN) * (MIN_USER_Q-MAX_USER_Q+1) / (PRIO_MAX-PRIO_MIN+1);*/
	
	rmp->ticketsNum += ((new_q - MAX_USER_Q)*(PRIO_MAX-PRIO_MIN+1)/(MIN_USER_Q-MAX_USER_Q+1) )+ PRIO_MIN;

	printf(" *** %d ***\n", rmp->ticketsNum);
	if((int)rmp->ticketsNum < 5)
		rmp->ticketsNum = 5;
	if((int)rmp->ticketsNum > 100)
		rmp->ticketsNum = 100;	
	printf("**** %d ****\n", (int) rmp->ticketsNum);
	if ((rv = schedule_process_local(rmp)) != OK) {
		/* Something went wrong when rescheduling the process, roll
		 * back the changes to proc struct */
		rmp->priority     = old_q;
		rmp->max_priority = old_max_q;
		rmp->ticketsNum = old_ticketsNum;
	}
	
	return do_lottery();
	//return rv;
}
Esempio n. 6
0
PUBLIC int do_noquantum(message *m_ptr) {
    struct schedproc *rmp, *rmp_temp;
    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];

/* CHANGE START */
    printf("do_noquantum, priority %d\n", rmp->priority);
    /* system process - change priority and return */
    if (!(rmp->flags & USER_PROCESS) && rmp->priority < WINNING_Q) {
        if (rmp->priority < WINNING_Q - 1) {
            rmp->priority++;
            schedule_process(rmp);
        }
        return OK;
    }
    /* user process */
    if (rmp->priority == WINNING_Q) { /* winner ran out of quantum */
        if (rmp->blocking) {
            change_tickets(rmp, 1);
            printf("IO process out of quantum, blocked %d times\n", rmp->blocking);
        }
        else {
            change_tickets(rmp, -1);
            printf("CPU process out of quantum\n");
        }
        rmp->priority = HOLDING_Q;
    } else { /* a non winning task finished, meaning all winning tasks are io bound */
        for (proc_nr_n = 0, rmp_temp = schedproc; proc_nr_n < NR_PROCS; ++proc_nr_n, ++rmp_temp)
            if (rmp_temp->priority == WINNING_Q)
                rmp_temp->blocking++;
        printf("IO bound process detected - increasing blocking to %d\n", rmp_temp->blocking);
    }

    if ((rv = schedule_process(rmp)) != OK) /* move out of quantum process */
        return rv;

    debug();

    if (rv = do_lottery() != OK) /* schedule a new winner */
        return rv;

    /* CHANGE END */
   
    return OK;
}
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;
}
PUBLIC int do_noquantum(message *m_ptr)
{
	register struct schedproc *rmp;
	int err, proc_nr_n;
	
	printf("%s::%s\n", "CMPS111", "do_noquantum");

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

	rmp = &schedproc[proc_nr_n];
	if (rmp->IS_SYS) {
		if ((err = schedule_process(rmp, "sys_noquantum")) != OK) {
			return err;
		}
		return OK;
	}
	do_print_process(rmp, "not_sys", DEBUG);
	if (rmp->priority == MAX_USER_Q) {
		rmp->priority = MIN_USER_Q;
		rmp->time_slice = -1;
		do_print_process(rmp, "do_noquantum_winner", DEBUG);
		if ((err = schedule_process(rmp, "do_noquantum")) != OK) {
			return err;
		}
	} else if (rmp->priority == MIN_USER_Q) {
		do_print_process(rmp, "do_noquantum_loser", DEBUG);
		do_lottery();
		return OK;
	}
	/*update our proctable@rmp@time_slice to 0?*/
	return OK;
}
/*===========================================================================*
 *				do_nice					     *
 *===========================================================================*/
int do_nice(message *m_ptr)
{
	struct schedproc *rmp;
	int rv;
	int proc_nr_n;
	unsigned new_q, old_q, old_max_q;

	/*Lottery Scheduling*/
	int old_ticketsNum; //old ticketsNum value

	/* check who can send you requests */
	if (!accept_message(m_ptr))
		return EPERM;

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

	rmp = &schedproc[proc_nr_n];

    /*
        m_ptr->SCHEDULING_MAX_PRIO is set to the value of nice in servers/pm/schedule.c
    */
	new_q = (unsigned) m_ptr->SCHEDULING_MAXPRIO;

	rmp->ticketsNum += new_q;
	if(rmp->ticketsNum < 5)
		rmp->ticketsNum = 5;

    /*
        new_q is set the same value as the old_priority i.e. 13
    */
	new_q = rmp->priority;

	if (new_q >= NR_SCHED_QUEUES) {
		return EINVAL;
	}

	/* Store old values, in case we need to roll back the changes */
	old_q     = rmp->priority;
	old_max_q = rmp->max_priority;

	/*Lottery Scheduling*/
	old_ticketsNum = rmp->ticketsNum; //saving ticketsNum value to old_ticketsNum

	/* Update the proc entry and reschedule the process */

	/*Lottery Scheduling*/
	//rmp->max_priority = rmp->priority = new_q;
	rmp->priority = USER_Q; // default priority for user processes


	if ((rv = schedule_process_local(rmp)) != OK) {
		/* Something went wrong when rescheduling the process, roll
		 * back the changes to proc struct */
		rmp->priority     = old_q;
		rmp->max_priority = old_max_q;

		/*Lottery Scheduling*/
		rmp->ticketsNum = old_ticketsNum;//reverting ticketsNum value

	}

	/*Lottery Scheduling*/
	return do_lottery();//do_lottery is called to assign cpu to another process
}
/*===================================================================================================*
 ######  ########    ###    ########  ########          ######   ######  ##     ## ######## ######## 
##    ##    ##      ## ##   ##     ##    ##            ##    ## ##    ## ##     ## ##       ##     ##
##          ##     ##   ##  ##     ##    ##            ##       ##       ##     ## ##       ##     ##
 ######     ##    ##     ## ########     ##             ######  ##       ######### ######   ##     ##
      ##    ##    ######### ##   ##      ##                  ## ##       ##     ## ##       ##     ##
##    ##    ##    ##     ## ##    ##     ##            ##    ## ##    ## ##     ## ##       ##     ##
 ######     ##    ##     ## ##     ##    ##    #######  ######   ######  ##     ## ######## ######## 
 *===================================================================================================*/
PUBLIC int do_start_scheduling(message *m_ptr)
{
	register struct schedproc *rmp;
	int err, proc_nr_n, parent_nr_n, nice;
	char *tag;
	
	/* we can handle two kinds of messages here */
	assert(m_ptr->m_type == SCHEDULING_START || 
		m_ptr->m_type == SCHEDULING_INHERIT);

	/* check who can send you requests */
	if (!accept_message(m_ptr))
		return EPERM;

	/* Resolve endpoint to proc slot. */
	if ((err = sched_isemtyendpt(m_ptr->SCHEDULING_ENDPOINT, &proc_nr_n))
			!= OK) {
		return err;
	}
	rmp = &schedproc[proc_nr_n];

	/* Populate process slot */
	rmp->endpoint 			= m_ptr->SCHEDULING_ENDPOINT;
	rmp->parent 			= m_ptr->SCHEDULING_PARENT;
	rmp->max_priority 		= (unsigned) m_ptr->SCHEDULING_MAXPRIO;
	if (rmp->max_priority >= NR_SCHED_QUEUES) {
		return EINVAL;
	}
	
	switch (m_ptr->m_type) {

		case SCHEDULING_START:
			/* We have a special case here for system processes, for which
			 * quantum and priority are set explicitly rather than inherited 
			 * from the parent */
			rmp->priority   = rmp->max_priority;/*TODO? try MAX_USER_Q*/
			rmp->time_slice = (unsigned) m_ptr->SCHEDULING_QUANTUM;
			rmp->num_tix    = (unsigned) 0;
			rmp->IS_SYS     = 1;

			tag = "SCHEDULING_START";
			break;
			
		case SCHEDULING_INHERIT:
			/* Inherit current priority and time slice from parent. Since there
			 * is currently only one scheduler scheduling the whole system, this
			 * value is local and we assert that the parent endpoint is valid */
			if ((err = sched_isokendpt(m_ptr->SCHEDULING_PARENT,
					&parent_nr_n)) != OK)
				return err;

			rmp->priority   = MIN_USER_Q;
			rmp->time_slice = -1;
			rmp->num_tix    = (unsigned) NR_TIX_DEFAULT;
			rmp->IS_SYS     = 0;

			tag = "SCHEDULING_INHERIT";
			break;
			
		default: 
			/* not reachable */
			assert(0);
	}

	do_print_process(rmp, tag, DEBUG);

	/* Take over scheduling the process. The kernel reply message populates
	 * the processes current priority and its time slice */
	if ((err = sys_schedctl(0, rmp->endpoint, 0, 0)) != OK) {
		printf("do_start_scheduling: Error taking over scheduling for %d, kernel said %d\n",
			rmp->endpoint, err);
		return err;
	}
	rmp->flags = IN_USE;

	/* Schedule the process, giving it some quantum */
	if ((err = schedule_process(rmp, tag)) != OK) {
		/*printf("do_start_scheduling: Error while scheduling process, kernel replied %d\n",
			err);*/
		return err;
	}

	/* Mark ourselves as the new scheduler.
	 * By default, processes are scheduled by the parents scheduler. In case
	 * this scheduler would want to delegate scheduling to another
	 * scheduler, it could do so and then write the endpoint of that
	 * scheduler into SCHEDULING_SCHEDULER
	 */
	m_ptr->SCHEDULING_SCHEDULER = SCHED_PROC_NR;

	/*do_lottery if not sys*/
	if (!rmp->IS_SYS) {
		do_lottery();
	}

	return OK;
}