HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppModerate

Disk Scheduling Algorithm

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
algorithmschedulingdisk

Problem

Aim is to provide total head movement in different disk scheduling algorithms namely FCFS, SSTF, LOOK, C-LOOK, SCAN, C-SCAN.

```
#include
#include
#include
#include

using namespace std;

int compare (const void a, const void b)
{
if ( (int)a (int)b ) return 1;
}
void shift(int a[],const int size)
{
for(int i=1;i>max;
}

void receive_request()
{
enter_request_number:
cout>number_of_request;
if(number_of_request>100)
goto enter_request_number;

current_location:
cout>request[0];
sorted_request[0]=request[0];
if(request[0]>max||request[0]>direction;
if(direction!=0&&direction!=1)
goto current_direction;

for(int i=1;i>request[i];
sorted_request[i]=request[i];
if(request[i]>max||request[i]sorted_request[0]&&flag==0)
flag=i;
request[i]=sorted_request[i];
}
while(nor)
{
if(flag==0)
{
head_movement+=request[0]-request[nor];
request[0]=request[nor];
}
else if(flag==1)
{
head_movement+=abs(request[nor]-request[0]);
break;
}
else if((request[flag]-request[0])>(request[0]-request[flag-1]))
{
head_movement+=request[0]-request[flag-1];
request[0]=request[flag-1];
flag--;
shift(request+flag,nor-flag);
}
else
{
head_movement+=request[flag]-request[0];
request[0]=request[flag];
shift(request+flag,nor-flag);
}
nor--;
}
return head_movement;
}
int SCAN()
{
int head_movement=0,flag=0;

for(int i=1;isorted_request[0]&&flag==0)
flag=i;

if(direction==1)
{
if(flag==1)
head_movement+=sorted_request[number_of_request]-sorted_request[0];

else
{
head_movement+=max-sorted_request[0];
head_movement+=max-sorted_request[1];
}
}

Solution

You are using C++, but overall, your code looks like C. There are numerous ways to improve your code readability by replacing C standard features by C++ ones. I will give some examples.

using namespace std;

Wirting using namespace std; is generally considered bad practice, especially when written in the global namesapce: it leads to potential name clashes and namespace pollution. You should just add std:: before every function/calss/variable from the standard library.

Headers

Several remarkes here:

  • ` is a C header, in C++, you should include instead, which is its C++ equivalent.



  • is an old non-portable header. For input/output operations, you should generally use and stick with it.



  • Also, it's good practice to organize your include directives in alphabetical ordering. That helps to check whether some header has already been included or not, and to avoid including twice some header.



Standard library collections

Instead of using old C arrays and pointers, you can use standard library containers. For example, a fixed sized array
int arr[30] can be replaced by the equivalent std::array in C++11 (this container is not directly available in the older versions of the standard).

Standard library algorithms

Instead of the old
qsort, which requires to take a function pointer with void* pointers and do some ugly casts, you should use std::sort in the standard header . It should be at least as fast (if not faster) and easier to read and write. You can replace this line:

qsort(sorted_request+1,number_of_request,sizeof(int),compare);


by this one:

std::sort(std::begin(sorted_request), std::end(sorted_request));


Miscellaneous notes

I can't cover all your code because it would have to be refactored to use more standard library features beforehand, but here are some notes:

  • Upper case names should only be used for preprocessor macros. Therefore, your functions CSCAN and LOOK (and the other ones) should be named cscan and look instead.



  • I'm not at ease with using std::cin in the constructor of disk. Such user interaction should not appear at construction, but be handled later by the client code of the class.



  • if(direction=1) is probably a bug, it should be if(direction==1), using proper compiler flags (-Wall, -Wextra, etc...) should tell you when you use an assignment instead of a comparison in if.



  • There is a typo in recieve_request: it should be receive_request`.



Overall, you should really consider looking at the C++ standard library and using its classes and algorithms to replace many things in your code.

Code Snippets

qsort(sorted_request+1,number_of_request,sizeof(int),compare);
std::sort(std::begin(sorted_request), std::end(sorted_request));

Context

StackExchange Code Review Q#48326, answer score: 13

Revisions (0)

No revisions yet.