Untitled

 avatar
unknown
plain_text
a year ago
3.6 kB
5
Indexable
static void * cpu_routine(void * args) {
	// lôi các argument của cpu_args tương ứng vs cpu ra cụ thể ở đây là timer_id_t
	struct timer_id_t * timer_id = ((struct cpu_args*)args)->timer_id;
	int id = ((struct cpu_args*)args)->id;
	/* Check for new process in ready queue */
	int time_left = 0;
	struct pcb_t * proc = NULL;

	while (1) {
		/* Check the status of current process */
		// printf("\tMAX_PRIO#60 = %d\n", MAX_PRIO);
		if (proc == NULL) {
			/* No process is running, the we load new process from
		 	* ready queue */
			proc = get_proc();
			if (proc == NULL) {
				/* Tell to timer that we have done our job in current slot */
                           next_slot(timer_id);
                           continue; /* First load failed. skip dummy load */
                        }
		}else if (proc->pc == proc->code->size) {
			/* The porcess has finish it job */ // by coming at the end 
			printf("\tCPU %d: Processed %2d has finished\n",
				id ,proc->pid);
			free(proc);
			proc = get_proc();
			time_left = 0;
		}
		else if (time_left == 0 && check_empty_queue_at_index(proc->prio) == 1 && check_slot_empty(proc->prio) == 0 )
		{
			struct pcb_t* proc_check = NULL;
			proc_check = get_prior_of_next_proc();
			if(proc_check != NULL && proc_check->prio <= proc->prio)
			{
				// printf("\tsignal at 82 os.c\n");
				printf("\tCPU %d: Put process %2d to run queue\n",
					id, proc->pid);
				put_proc(proc);
				proc = get_proc();
			} 
			else
			{
				// printf("\tsignal at 90 os.c\n");
				time_left = time_slot;
				decrease_slot(proc->prio);
				// printf("\tproc->prio: %d\n", proc->prio);
				// printf("\tMAX_PRIO#95 = %d\n", MAX_PRIO);
				if(proc->prio == (MAX_PRIO - 1))
				{
					// printf("\t -> restore slot line 96 in os.c\n");
					heal_slot();
				}
			}
		}
		else if (time_left == 0) {
			// kiểm tra xem hàng đợi của process đó có trống ko
			// nếu trống -> chạy cái process đó đến khi nào hết slot thì thôi
			// vấn đề vậy khi có process độ ưu tiên cao hơn thì sao 
			//		-> giải quyết: 	trước khi thực sự quyết định có nên chạy proc đó không 
			//						kiểm tra có process nào có độ ưu tiên cao hơn không -> yêu cầu 1 hàm lấy proc ra nhưng chỉ lấy thông tin prior của nó
			//							-> nếu ko chạy 1 slot r kiểm tra lại 1 lần nữa
			//							-> nếu có thì mới thực sự bỏ code vào run_queue và get
			// implement ở khối else if phía trên
			// printf("\tsignal at 104 os.c\n");
			printf("\tCPU %d: Put process %2d to run queue\n",
					id, proc->pid);
			put_proc(proc);
			// printf("\tsignal line 109 os,c\n");
			proc = get_proc();
		}
		// printf("\tdone: %d\n", done);
		/* Recheck process status after loading new process */
		if (proc == NULL && done) {
			/* No process to run, exit */
			printf("\tCPU %d stopped\n", id);
			break;
		}else if (proc == NULL) {
			/* There may be new processes to run in
			 * next time slots, just skip current slot */
			next_slot(timer_id);
			continue;
		}else if (time_left == 0) {
			// nhận process và chạy
			printf("\tCPU %d: Dispatched process %2d\n",
				id, proc->pid);
			time_left = time_slot;
		}
		
		/* Run current process */
		run(proc); // real processing running
		// proc->pc++; // comment this after testing scheduler succesfull
		time_left--;
		next_slot(timer_id);

		// check here if proc reach the end ? proc->prio == MAX_PRIO -1 
	}
	detach_event(timer_id);
	pthread_exit(NULL);
}
Editor is loading...
Leave a Comment