Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Marti LEVEN 13 Sept 2014
- //
- #include<stdio.h>
- #include<cstdlib>
- #include<iostream>
- #include<cstring>
- #include<vector>
- #include<math.h>
- using namespace std;
- #define DISK_RPM 5400
- #define DISK_SECTORS 63
- #define SECTORS_PER_SEC (DISK_RPM/60.0*DISK_SECTORS)
- int main(int argc, char *argv[]) {
- // Check that we have the correct number of arguments - a event file and number
- if ( argc < 2 ) {
- fprintf(stderr, "Incorrect number of arguments\n");
- return EXIT_FAILURE;
- }
- // Open the schedule file
- FILE *schd_file = fopen(argv[1],"r");
- // Assume the event file has lines of less than 132 chars
- char line[133];
- // Count the number of lines in the schedule file == number of events
- int num = 0;
- //while(fgetc(schd_file) != EOF){
- while( !feof(schd_file) ) {
- fgets(line,128,schd_file);
- num++;
- }
- printf(" Read %i lines in the event file\n", num );
- // Rewind the input file
- rewind( schd_file );
- // Generate some vectors to store the schedule data
- int n = num +3; // allow a margin for error
- int *time = new int[n]; // allocates an array of 'n' adjacent integers (for time, pid and d_sect)
- int *extype = new int[n]; // allocates an array of 'n' different chars (for event type)
- int *pid = new int[n];
- int *d_sect = new int[n];
- char event_type[24];
- int t, e, p, d;
- // Read thru the event file storing the data
- int i = 0;
- int num_fields = 0;
- // printf(" Read thru the event file\n");
- fgets(line,128,schd_file);
- while( !feof(schd_file) ) {
- //printf("\n %s \n",line);
- num_fields = sscanf(line,"%i %s %i %i", &t, event_type, &p, &d);
- if (num_fields == 4) {
- time[i] = t;
- e = 0;
- if ( strncmp(event_type, "spawn", 5 ) == 0) {
- e = 1;
- }
- if ( strncmp(event_type, "io", 2 ) == 0 ) {
- e = 2;
- }
- if ( strncmp(event_type, "exit", 4 ) == 0 ) {
- e = 3;
- }
- extype[i] = e;
- pid[i] = p;
- d_sect[i] = d;
- printf("\n %6i %4i %8i %8i \n", time[i], extype[i], pid[i], d_sect[i] );
- i++;
- }
- fgets(line,128,schd_file);
- }
- // printf("\n\n%6i \n", extype[i-6]);
- int *abtime = new int[n];
- int k = 0;
- while(k < i) {
- switch(extype[k]){
- case 1:
- abtime[k] = time[k];
- k++;
- break;
- case 2:
- abtime[k] = time[k] + abtime[k-1];
- k++;
- break;
- case 3:
- abtime[k] = time[k] + abtime[k-1];
- k++;
- break;
- default:
- continue;
- }
- }
- //create a microsecond absolute time vector
- int *micro_time = new int[n];
- for(int b=0; b < i; b++){
- micro_time[b] = (abtime[b] *1000);
- printf("\n %i \n", micro_time[b]);
- }
- int current_time = micro_time[0];
- int time_blocked = 0;
- int check_overloop = 0;
- int task_num = 0;
- float disk_sector = 0;
- //float disk_incr = 0.00567;
- // takes 11.11 ms for the disk to rotate,leeway gives 12 ms and include every event
- while(current_time < micro_time[k-1] ) {
- if( current_time >= micro_time[task_num] && extype[task_num] == 2 && (int)(disk_sector) == d_sect[task_num]) {
- // the current IO task is completed successfully
- time_blocked++;
- disk_sector = ((int)((current_time/1000000.0) * SECTORS_PER_SEC)) % DISK_SECTORS;
- //disk_sector = fmod((disk_sector + disk_incr), 62);
- check_overloop++;
- } else if(current_time >= micro_time[task_num] && extype[task_num] == 2 && int(disk_sector) != d_sect[task_num]) {
- // the current IO task is blocked and the disk must rotate to reach the required disk sector
- current_time++;
- //disk_sector = fmod((disk_sector + disk_incr), 62);
- task_num++;
- disk_sector = ((int)((current_time/1000000.0) * SECTORS_PER_SEC)) % DISK_SECTORS;
- check_overloop++;
- } else if(current_time >= micro_time[task_num] && extype[task_num] != 2) {
- // the current spawn/exit task is completed successfully
- task_num++;
- current_time++;
- check_overloop++;
- disk_sector = ((int)((current_time/1000000.0) * SECTORS_PER_SEC)) % DISK_SECTORS;
- //disk_sector = fmod((disk_sector + disk_incr), 62);
- } else if(current_time < micro_time[task_num]){
- // the processor is waiting for the next new task
- current_time++;
- check_overloop++;
- disk_sector = ((int)((current_time/1000000.0) * SECTORS_PER_SEC)) % DISK_SECTORS;
- //disk_sector = fmod((disk_sector + disk_incr),62);
- } else {
- // something is wrong!
- printf("\n there is some sort of error! \n");
- }
- }
- /*
- while( current_time < micro_time[k-1] ) {
- //printf("\n %i %i \n", current_time, micro_time[task_num]);
- if( current_time >= micro_time[task_num] ) {
- printf("\n ex type is %i \n", extype[task_num]);
- //printf("\n %i %i \n", current_time, micro_time[task_num]);
- if( extype[task_num] == 2 ) {
- printf("\n disk sectors %i %i \n", int(disk_sector), d_sect[task_num]);
- if( round(disk_sector) == d_sect[task_num] ) {
- printf("\n disk sector 2 %f \n", disk_sector);
- disk_sector = fmod((disk_sector + disk_incr), 62);
- time_blocked++;
- check_overloop++;
- } else {
- current_time++;
- disk_sector = fmod((disk_sector + disk_incr), 62);
- //printf("\n %f time %i \n", disk_sector, current_time);
- task_num++;
- check_overloop++;
- }
- } else {
- //printf("\n sector %f time %i \n", disk_sector, current_time);
- task_num++;
- //printf("\n %i %i \n", current_time, micro_time[task_num]);
- current_time++;
- check_overloop++;
- disk_sector = fmod((disk_sector + disk_incr), 62);
- }
- } else {
- //printf("\n %i %i \n", current_time, micro_time[task_num]);
- current_time++;
- check_overloop++;
- disk_sector = fmod((disk_sector + disk_incr),62);
- }
- }
- */
- int total_time = abtime[k-1] - abtime[0] + time_blocked;
- printf("\n %i %i \n", abtime[k-1], abtime[0]);
- printf("\n %i %i \n", total_time, time_blocked);
- printf("\n %i \n", check_overloop);
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement