Пример #1
0
// main scheduling program
int main()
{
	Process P[max_numP];
	int schedule[max_totalT];
	char policy_name[5];
	scanf("%s", policy_name);
	int numP;
	scanf("%d", &numP);
	getchar();
	int totalT = 0;
	for(int i = 0; i < numP; i++)
	{
		char *process_name = malloc(max_name_length * sizeof(char));
		int ready_time;
		int execution_time;
		scanf("%s %d %d", process_name, &ready_time, &execution_time);
		getchar();
		totalT += execution_time;
		P[i].name = process_name;
		P[i].readyT = ready_time;
		P[i].execT = execution_time;
		P[i].ID = i;
	}
	// sort P according to readyT
	qsort(P, numP, sizeof(Process), compare_Process);
	int policy = 0;
	char policy_list[4][5] = {"FIFO", "RR", "SJF", "PSJF"};
	for(int i = 0; i < 4; i++)
		if(strcmp(policy_name, policy_list[i]) == 0)
			policy = i;
	Process *waiting_list[max_numP];
	for(int i = 0; i < max_numP; i++)
		waiting_list[i] = NULL;
	Process *exec_order[max_totalT];
	int index_now = 0;
	int t = 0;
	while(t < totalT)
	{
		while(P[index_now].readyT <= t)
		{
			insertP(waiting_list, policy, (P + index_now));
			index_now++;
		}
		if(waiting_list[0] != NULL)
		{
			Process *exec_process = waiting_list[0];
			int exec_length = execP(waiting_list, policy);
			for(int i = 1; i <= exec_length; i++)
			{
				exec_order[t] = exec_process;
				t++;
			}
		}
		else
		{
			exec_order[t] = NULL; // no waiting process now
			t++;
		}
	}
	// test result
	for(int i = 0; i < totalT; i++)
	{
		if(exec_order[i] != NULL)
			printf("%d time unit : execute %s\n", i + 1, exec_order[i]->name);
		else 
			printf("%d time unit : No process\n", i + 1);
	}
	return 0;
}
Пример #2
0
// main scheduling program
int main()
{
    Process P[max_numP];
    char policy_name[5];
    scanf("%s", policy_name);
    int numP;
    scanf("%d", &numP);
    for(int i = 0; i < numP; i++)
    {
        char *process_name = malloc(max_name_length * sizeof(char));
        int ready_time;
        int execution_time;
        scanf("%s %d %d", process_name, &ready_time, &execution_time);
        P[i].name = process_name;
        P[i].readyT = ready_time;
        P[i].execT = execution_time;
        P[i].ID = i;
    }
    // sort P according to readyT
    qsort(P, numP, sizeof(Process), compare_Process);
    int policy = 0;
    char policy_list[4][5] = {"FIFO", "RR", "SJF", "PSJF"};
    for(int i = 0; i < 4; i++)
        if(strcmp(policy_name, policy_list[i]) == 0)
            policy = i;
    Process *waiting_list[max_numP];
    for(int i = 0; i < max_numP; i++)
        waiting_list[i] = NULL;
    
    //set CPU
    
    cpu_set_t cpu_mask;
    CPU_ZERO(&cpu_mask);
    CPU_SET(0, &cpu_mask);
    
    if(sched_setaffinity(0, sizeof(cpu_set_t), &cpu_mask) != 0){
        perror("sched_setaffinity error");
        exit(EXIT_FAILURE);
    }

    //raise the priority
    
    const int priorityH = 80;//sched_get_priority_max(SCHED_RR);
    const int priorityL = 10;//sched_get_priority_min(SCHED_RR);
    //printf("H:%d L:%d\n",priorityH,priorityL); 
    struct sched_param param;
    param.sched_priority = 50;
    //Just want to make sure it won't get preempted by other processes on its CPU
    pid_t pidP = getpid();
    if(sched_setscheduler(pidP, SCHED_FIFO, &param) != 0) {
        perror("sched_setscheduler error");
        exit(EXIT_FAILURE);  
    }
    
    // P already sorted in ascending order of readyT
    int time_count = 0;
    int fork_count = 0;
    Process *exec_process = NULL;
    Process *exec_process_last = NULL;
    int numP_finish_last = 0;
    int exec_length = 0;
    pid_t pids[max_numP];
    while(!(numP_finish == numP && exec_length == 0))
    {
        //fork the processes that are ready at time_count
        long long start_time = syscall(328);
        long long start_time_s = start_time / n;
        long long start_time_ns = start_time % n;
        while(P[fork_count].readyT <= time_count && fork_count < numP)
        {
            printf("%s : fork at time %d\n", P[fork_count].name, time_count);
            pid_t pid = fork();
            
            if(pid < 0)   
                printf("error in fork!");   
            else if(pid == 0) {
                // child
                char exec_time[10];
                sprintf(exec_time, "%d", P[fork_count].execT);
                char start_time_s_string[20]; 
                char start_time_ns_string[20];
                sprintf(start_time_s_string, "%lld", start_time_s);
                sprintf(start_time_ns_string, "%lld", start_time_ns);
                if(execlp("./process", "process", P[fork_count].name, exec_time, start_time_s_string, start_time_ns_string, (char *)NULL) < 0){
                    perror("execlp error");
                    exit(EXIT_FAILURE);
                }
            }  
            // parent
            //change the newly forked child's CPU
            cpu_set_t cpu_mask;
            CPU_ZERO(&cpu_mask);
            CPU_SET(1, &cpu_mask);

            if(sched_setaffinity(pid, sizeof(cpu_set_t), &cpu_mask) != 0){
                perror("sched_setaffinity error");
                exit(EXIT_FAILURE);
            }

            pids[P[fork_count].ID] = pid; // use ID (also 0 ~ numP - 1) as index to store pids
            insertP(waiting_list, policy, (P + fork_count));
            fork_count++;
        }
        
        // decide next process  
        
        // no process is waiting
        if(waiting_list[0] == NULL && exec_length == 0)
        {
            time_count++;
            volatile unsigned long i;
            for(i=0;i<1000000UL;i++); 
            exec_process_last = NULL;
        }
        else
        {
            // find next process in the list
            if(exec_length == 0)
            {
                exec_process = waiting_list[0];
                exec_length = execP(waiting_list, policy);
                //change priority if a different process is going to run
                if(exec_process_last == NULL || exec_process_last->execT == 0)
                {
		printf("Set %s to high at time %d\n", exec_process->name, time_count);
                    pid_t pid = pids[exec_process->ID];
		    printf("PID: %d\n",pid);
                    param.sched_priority = priorityH;
                    if(sched_setscheduler(pid, SCHED_RR, &param) != 0) {
                        perror("sched_setscheduler error");
                        exit(EXIT_FAILURE);
                    }
			if(sched_getscheduler(pid) == SCHED_RR)
			puts("RR");
                }
                // recover priority of last process
                else
		{
		printf("Set %s to high, recover %s to low at time %d\n", exec_process->name, exec_process_last->name, time_count);
                    pid_t pid_last = pids[exec_process_last->ID];
                    pid_t pid = pids[exec_process->ID];
		    printf("PID:%d\n",pid);
                    param.sched_priority = priorityL;
                    if(sched_setscheduler(pid_last, SCHED_RR, &param) != 0) {
                        perror("sched_setscheduler error");
                        exit(EXIT_FAILURE);  
                    }

                    param.sched_priority = priorityH;
                    if(sched_setscheduler(pid, SCHED_FIFO, &param) != 0) {
                        perror("sched_setscheduler error");
                        exit(EXIT_FAILURE);  
                    }
                }	
            }

            exec_length--;
            time_count++;
            volatile unsigned long i;
            for(i=0;i<1000000UL;i++); 
            
            // exec time section for this process is over
            if(exec_length == 0)
            {
                // check if the process has terminated, if so, wait for it
                if(numP_finish == numP_finish_last + 1)
                {
                    printf("%s : end at time %d\n", exec_process->name, time_count);
                    int status;
                    if(waitpid(pids[exec_process->ID], &status, 0) == -1)
                    {
                        perror("waitpid error");
                        exit(EXIT_FAILURE);
                    }
                }

                exec_process_last = exec_process;
                numP_finish_last = numP_finish;
            }   
        }    
    }

    for(int i = 0; i < numP; i++){
        printf("%s ", P[i].name);
        printf("%d\n", pids[P[i].ID]);
    }

}