Skip to content

Instantly share code, notes, and snippets.

@vishrut
Created March 7, 2013 12:25
Show Gist options
  • Save vishrut/5107692 to your computer and use it in GitHub Desktop.
Save vishrut/5107692 to your computer and use it in GitHub Desktop.
Disk scheduling on Linux using C++
#include <fstream>
#include <cerrno>
#include <stdexcept>
#include <cstring>
#include <vector>
#include <iostream>
#include <time.h>
#include <stdlib.h>
#define MAX_REQUESTS 1000000
#define SECTOR_SIZE 512
#define SECTOR_UB 1800000
int request[MAX_REQUESTS];
void execrequests(){
int sector = 0;
clock_t t;
// Which disk?
char diskName[] = "/dev/sda";
std::string diskError = std::string() + diskName + ": ";
// Open device file
std::ifstream disk(diskName, std::ios_base::binary);
if(!disk)
throw(std::runtime_error(diskError + std::strerror(errno)));
int length = disk.tellg(); // tellg() returns the distance from the beginning of the file
std::vector<char> buffer(SECTOR_SIZE);
disk.clear();
// Start clock
t = clock();
for(sector=0; sector<MAX_REQUESTS; sector++){
// Seek to the sector
// std::cout << "Seeking sector: " << request[sector] << "\n";
disk.seekg(SECTOR_SIZE*request[sector]);
if(!disk)
throw(std::runtime_error(diskError + std::strerror(errno)));
// Read in one sector
// std::cout << "Reading sector: " << request[sector] << "\n";
disk.read(&buffer[0], SECTOR_SIZE);
if(!disk)
throw(std::runtime_error(diskError + std::strerror(errno)));
//Print the sector
/*
for(std::vector<char>::const_iterator i = buffer.begin(); i != buffer.end(); ++i)
std::cout << *i;
*/
}
// Stop clock
t = clock()-t;
std::cout <<"Time taken: " <<(float)t/CLOCKS_PER_SEC << "\n";
}
void mergesort(int a[], int low, int high){
int i = 0;
int length = high - low + 1;
int pivot = 0;
int merge1 = 0;
int merge2 = 0;
int working[length];
if(low == high)
return;
pivot = (low + high) / 2;
mergesort(a, low, pivot);
mergesort(a, pivot + 1, high);
for(i = 0; i < length; i++)
working[i] = a[low + i];
merge1 = 0;
merge2 = pivot - low + 1;
for(i = 0; i < length; i++) {
if(merge2 <= high - low)
if(merge1 <= pivot - low)
if(working[merge1] > working[merge2])
a[i + low] = working[merge2++];
else
a[i + low] = working[merge1++];
else
a[i + low] = working[merge2++];
else
a[i + low] = working[merge1++];
}
}
void genrequests(){
srand(time(NULL));
for(int i=0; i<MAX_REQUESTS; i++){
request[i] = rand()%SECTOR_UB + 1;
}
}
void FCFS(){
std::cout << "\n" << "Algorithm: FCFS";
std::cout << "\n" << "---------------" << "\n";
execrequests();
}
void SCAN(){
std::cout << "\n" << "Algorithm: SCAN";
std::cout << "\n" << "---------------" << "\n";
mergesort(request, 0, MAX_REQUESTS - 1);
execrequests();
}
int main(){
genrequests();
std::cout << "\n" << "General Details";
std::cout << "\n" << "---------------" << "\n";
int sectorsize = SECTOR_SIZE;
std::cout << "All requests made to the sectors in the partition /dev/sda" << "\n";
std::cout << "Sector size: " << sectorsize << "\n";
int totalrequests = MAX_REQUESTS;
std::cout << "Total requests: " << totalrequests << "\n";
int upperbound = SECTOR_UB;
std::cout << "Sectors requested between 1 and " << upperbound << "\n";
FCFS();
SCAN();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment