コード例 #1
0
/* performs basic initialization on all global variables */
void initializeGlobals(void) {
    int i = 0;
    for (;i<NUMBER_OF_PROCESSORS;i++) {
        cpus[i] = NULL;
    }

    simulationTime = 0; 
    cpuTimeUtilized = 0;
    totalWaitingTime = 0;
    totalContextSwitches = 0;
    numberOfProcesses = 0;
    nextProcess = 0;

    preReadyQueueSize = 0;

    initializeProcessQueue(&readyQueue);
    initializeProcessQueue(&waitingQueue);
}
コード例 #2
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;
}
コード例 #3
0
int main(int argc, char **argv) {

	/* initialzie some global and local variables */
	int status = 0;
	int lastPid, j;
	int totalUtilized = 0, totalWaiting = 0, totalTurnAround = 0;
	double avgWaiting, avgTurnAround, avgUtil;
	clockTime = 0;
	processIndex = 0;
  tempArrayIndex = 0;
  timeSlice = atoi(argv[1]);

	/* initialize the CPUs, set all cpus to idle status and no process is assigned to any one of them */
	for(j = 0; j < NUMBER_OF_PROCESSORS; j++) {
		cpus[j] = NULL;
	}

	/* initialize process queue to store processes */
	initializeProcessQueue(&ready_queue);
	initializeProcessQueue(&device_queue);

	/* read from the file and sort them in order for later on enqueue */
	while((status = readProcess(&processes[numberOfProcesses]))) {
		if(status == 1) {
			numberOfProcesses++;
		}
		if (numberOfProcesses > MAX_PROCESSES || numberOfProcesses == 0){
			error_invalid_number_of_processes(numberOfProcesses);
		}
	}
	qsort(processes, numberOfProcesses, sizeof(process), compareByArrival);

	/** the idea is: as we increase the clockTime from 0 to a very large number until some conditions,
	*	at every time spot, we need to allocate cpu to a process, enqueue process to device_queque or
	*	enqueue process back to ready_queue.
	*/
	while(1) {
		/* some actions at current time spot */
		/** first we want to call nextUnitProcess() to get a temporary array of process(es) which are
		*	arrange by arrival time.
		*	second do all the checks for all queue and process to determin where are they going to
		*	and what actions they have to do.
		*/
		nextUnitProcess();		// at each time spot, add matched process to be run to tempArray

		cpuOut(); 						// initially not executed since no cpus is running.
		ioToReadyQ();					// to check io queue if some processes should go back to ready_queue.
		readyQtoCPU();				// equeue all processes from sorted tempArray into ready_queue and allocate cpus for them.
		/* now we have to make progress, which is going to next unit of time */
		nextUnitTime();
		/* for each clockTime spot, we sum up the total */
		totalUtilized += isAllIdle();

		/**	now it is time to do some termination check
		*	exit the progress-making when:
		*	1: all cpus are idle status
		*	2: no next unit of processes to be added to tempArray
		*	3: the waiting queue is empty
		*/
		if((isAllIdle() == 0) && ((numberOfProcesses - processIndex) == 0) && (device_queue.size == 0) ) {
			break;
		}
		/* add one more unit time to next step */
		clockTime++;
  }
	/* calculation and display result */
	for(j = 0; j < numberOfProcesses; j++) {

		totalWaiting += processes[j].waitingTime;
		totalTurnAround += (processes[j].endTime - processes[j].arrivalTime);
		if(processes[j].endTime == clockTime) {

			lastPid = processes[j].pid;
		}
	}
	avgWaiting = totalWaiting / (double)numberOfProcesses;
	avgTurnAround = totalTurnAround / (double)numberOfProcesses;
	avgUtil = totalUtilized / (double)clockTime;

	printf("The average waiting time is:      %.2f\n"
					"The average turnaround time is:   %.2f\n"
					"The CPUs finished at:             %d\n"
          "The average cpu utilization is:   %.2f%%\n"
					"Total context switches:           %d\n"
					"The last process is:              %d\n", avgWaiting, avgTurnAround, clockTime, avgUtil*100, totalCW, lastPid);

}
コード例 #4
0
ファイル: rr.c プロジェクト: raksonibs/Experiments2
int main(int argc, char *argv[]) {
  int waitingIndex;
  setUpProcesses();

  totalProcesses = numberOfProcesses;

  //error management of invalid number of processes
  quantum = readInt(&argv[1]);
  if (checkErrors(quantum) == -1) {
    error("Invalid number of processes");
    return -1;
  } 

  //sort processes by arrival time and priority
  qsort(processes, numberOfProcesses, sizeof(process), compareByArrival);

  initializeProcessQueue(&readyQueue);
  initializeProcessQueue(&waitingQueue);

  setupCPUS();

  //set the quantum slice
  if (checkErrors(quantum) == -2) {
    error_bad_quantum();
    return -1;
  }

  while (1) {
    preparePreQueue();

    // if finished cpu burst, move to waiting queue if it is not the last cpu burst
    int cpuIndex = 0;
    int cpuUse = 0;

    // set up the current CpuBursts
    cpuBurstAllocation(cpuIndex, cpuUse);

    //sort waiting queue by processes priority
    int waitingQueueSize = waitingQueue.size;
    
    // iterate over queue where there are no indexes, and are waiting
    for (waitingIndex = 0; waitingIndex < waitingQueueSize; waitingIndex++) {
      dequeueEnqueueWaitingBusts(waitingIndex, waitingQueueSize);
    }

    //sort preReadyQueue and queue the expired time slices to the ready queue
    qsort(preReadyQueue, preReadyQueueSize, sizeof(process*), comparePIDs);
    qsort(tempProcess, tempProcessSize, sizeof(process*), comparePIDs);
    int j;

    //previous processes with expired time slices go into ready queue first as is in fifo
    for (j = 0; j < tempProcessSize; j++) {
      enqueueProcess(&readyQueue, tempProcess[j]);
    }

    tempProcessSize = 0;

    //enqueue everything in the ready queue
    for (j = 0; j < preReadyQueueSize; j++) {
      enqueueProcess(&readyQueue, preReadyQueue[j]);
    }

    preReadyQueueSize = 0;

    //assign the available cpu to first process of ready queue
    for (cpuIndex = 0; cpuIndex < NUMBER_OF_PROCESSORS; cpuIndex++) {
      conditionalCPUProcessQueue(cpuIndex, preReadyQueueSize, tempProcessSize);
    }

    //update burst of all processes in every queue
    int r;
    for (r = 0; r < readyQueue.size; r++) {
      updateQueues();
    }


    if (cpuUse == 0 && waitingQueue.size == 0 && (numberOfProcesses - nextProcess) == 0) {
      break; // exit loop if no cpu in use or no running processes and none in waiting queue
    }

    timer++; //increment one time unit for each loop
  }

  setUpPrintVariables();
  //print values
  printf("Average waiting time                 : %.2f units\n", totalWaitingTime/(double)numberOfProcesses);
  printf("Average turnaround Time              : %.2f units\n", totalTurnAroundTime/(double)numberOfProcesses);
  printf("Time all processes finished          : %d\n", timer);
  printf("Average CPU utilization              : %.1f%%\n", 100.0 * cpuUseTotal / timer);
  printf("Number of context switches           : %d\n", context);
  printf("PID(s) of last process(es) to finish : %d\n", lastPID);
  
  return 0;
}
コード例 #5
0
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;
}