int main() { clock_t ticks; time_t start_time, end_time; time(&start_time); int i; int status = 0; log_file = fopen("log_file.txt", "w+"); if (log_file == NULL) { fprintf(stderr, "LOG FILE CANNOT BE OPEN\n"); } // initialize cpus for (i = 0; i < NUMBER_OF_PROCESSORS; i++) { CPU[i] = NULL; } init_(); initializeProcessQueue(&readyQueue); initializeProcessQueue(&waitingQueue); // read in process and initialize process values while ((status = (readProcess(&processes[number_of_processes])))) { if (status == 1) { number_of_processes++; } if (number_of_processes > MAX_PROCESSES || number_of_processes == 0) { break; } } //sort process by their arrival times qsort(processes, number_of_processes, sizeof (process), compareByArrival); // main execution loop while (TRUE) { ticks = clock(); incoming_process_init(); running_process_to_waiting(); most_ready_running_in_cpu(); waiting_to_ready(); refresh_processes(); cpu_utilized_time += runningProcesses(); simulation_time++; // break when there are no more running or incoming processes, and the waiting queue is empty if (runningProcesses() == 0 && (number_of_processes - nextProcess) == 0 && waitingQueue.size == 0) { break; } } // calculations based on CPU simulations int total_waiting_time = 0; int turn_around_time = 0; for (i = 0; i < number_of_processes; i++) { turn_around_time += processes[i].endTime - processes[i].arrivalTime; total_waiting_time += processes[i].waitingTime; } printf("********************************************************************\n"); printf("Average Waiting Time\t\t\t:%.2f\n", total_waiting_time / (double) number_of_processes); printf("Average Turn Around Time\t\t:%.2f\n", turn_around_time / (double) number_of_processes); printf("Time all for all CPU processes\t\t:%d\n", simulation_time); printf("CPU Utilization Time\t\t\t:%.2f%c\n", (double) (cpu_utilized_time * 100.0) / (double) (simulation_time), (int) 37); printf("Total Number of Context Switches\t:%d\n", context_switches); printf("Last Process to finish "); for (i = 0; i < number_of_processes; i++) { if (processes[i].endTime == last_process_end_time) { printf("PID\t\t:%d\n", processes[i].pid); } } //printf("%d\n" , processes[number_of_processes].pid); printf("********************************************************************\n"); time(&end_time); double prg_time = (end_time - start_time) * 0.001; double cpu_time = (double) ticks / CLOCKS_PER_SEC; fprintf(log_file, "Program Time\t:%.2fsecs\n", prg_time); fprintf(log_file, "CPU Time\t:%.2fsecs\n", cpu_time); fclose(log_file); return 0; }
int main(int argc, char **argv) { if (argc < 3) { fprintf(stderr, "Please enter a time quantums . . . . \n"); exit(1); } time_slice = atoi(argv[1]); time_slice_1 = atoi(argv[2]); if (time_slice <= 0 || time_slice_1 <= 0) { fprintf(stderr, "Error! program usage rr < positive time quantum> < data file . . .\n"); exit(1); } init_(); clock_t ticks; time_t start_time, end_time; time(&start_time); int i; int status = 0; log_file = fopen("log_file.txt", "w+"); if (log_file == NULL) { fprintf(stderr, "LOG FILE CANNOT BE OPEN\n"); } //initialize cpu's for (i = 0; i < NUMBER_OF_PROCESSORS; i++) { CPU[i] = NULL; } initializeProcessQueue(&readyQueue); initializeProcessQueue(&waitingQueue); initializeProcessQueue(&level_one); initializeProcessQueue(&second_level); //initializeProcessQueue(&promoted); // read in process and initialize process values while ((status = (readProcess(&processes[number_of_processes])))) { if (status == 1) { number_of_processes++; } } if (number_of_processes > MAX_PROCESSES) { return -2; } if (number_of_processes == 0) { return -1; } int remaining_process = 0; //sort process by their arrival times qsort(processes, number_of_processes, sizeof (process), compareByArrival); // main execution loop while (TRUE) { ticks = clock(); waiting_to_ready(); incoming_process_init(); running_process_to_waiting(); most_ready_running_in_cpu(); refresh_processes(); increase_io_work(); increase_cpu_work(); cpu_utilized_time += runningProcesses(); remaining_process = ex(); // break when there are no more running or incoming processes, and the waiting queue is empty if (remaining_process == 0 && runningProcesses() == 0 && waitingQueue.size == 0) { break; } simulation_time++; } int total_waiting_time = 0; int turn_around_time = 0; for (i = 0; i < number_of_processes; i++) { turn_around_time += processes[i].endTime - processes[i].arrivalTime; total_waiting_time += processes[i].waitingTime; } printf(">>>>>>>>>>>>> FBQ with Q1 :%d\tQ2 :%d <<<<<<<<<<<<<<<\n", time_slice, time_slice_1); printf("********************************************************************\n"); printf("Average Waiting Time\t\t\t:%.2f\n", total_waiting_time / (double) number_of_processes); printf("Average Turn Around Time\t\t:%.2f\n", turn_around_time / (double) number_of_processes); printf("Time all for all CPU processes\t\t:%d\n", simulation_time); printf("CPU Utilization Time\t\t\t:%.2f%c\n", (double) (cpu_utilized_time * 100.0) / (double) (simulation_time), (int) 37); printf("Total Number of Context Switches\t:%d\n", context_switches); printf("Last Process to finish "); for (i = 0; i < number_of_processes; i++) { if (processes[i].endTime == simulation_time) { printf("PID\t\t:%d\n", processes[i].pid); } } printf("********************************************************************\n"); time(&end_time); double prg_time = (end_time - start_time) * 0.001; double cpu_time = (double) ticks / CLOCKS_PER_SEC; fprintf(log_file, "Program Time\t:%.2fsecs\n", prg_time); fprintf(log_file, "CPU Time\t:%.2fsecs\n", cpu_time); fclose(log_file); return 0; }
void RoundRobin(){ //print algorithm out << alg.name << " " << alg.time_quantum << std::endl; int quantum_counter = 0, timer = 0; //determine which queue to enqueue, if ready queue set start time while(!master.empty()){ struct Qu t = master.front(); if(t.arrival_time == 0) ready.push(t); else waiting.push(t); master.pop(); } while(completed.size() != alg.number_of_proc){ int flag_inner = 0; //print process and timer, also save value if not been touched yet if(!ready.empty()){ if(ready.front().flag_touched == 0){ ready.front().time_start = timer; ready.front().flag_touched = 1; } out << timer << "\t" << ready.front().proc_num << std::endl; } //task being processesd while(quantum_counter<alg.time_quantum && ready.front().cpu_burst != 0){ flag_inner = 1; ++timer; ++quantum_counter; --ready.front().cpu_burst; if(ready.size() > 1){ int i; cycle(&ready); for(i=0; i<ready.size()-1; ++i){ ++ready.front().time_waiting; cycle(&ready); } } } //reset counter quantum_counter = 0; if(flag_inner == 0) ++timer; //check for new processes, add old one after if same time int i; for(i=0; i<waiting.size(); ++i){ if(waiting.front().arrival_time <= timer){ waiting_to_ready(timer); --i; } } if(ready.front().cpu_burst == 0) ready_to_completed(timer); else cycle(&ready); } //now calculate waiting time, cannot use cpu_burst since is zero double total_w = 0; int i; for(i=0; i<alg.number_of_proc; ++i){ total_w += completed.front().time_waiting; completed.pop(); } total_w /= alg.number_of_proc; out << std::setprecision(3) << "AVG WaitingTime: " << total_w << std::endl; }