N-Step-SCAN disk scheduling

N-Step-SCAN (also called N-Step-LOOK) is a disk scheduling algorithm that improves upon the traditional SCAN algorithm by introducing a parameter N that limits the number of requests processed in one direction before changing direction. Unlike SCAN, which continues moving in one direction until no more requests exist in that direction, N-Step-SCAN processes exactly N requests before reversing direction.

This approach provides better control over disk arm movement and can reduce starvation compared to pure SCAN scheduling.

How N-Step-SCAN Works

The algorithm operates by maintaining the current disk arm position and processing requests in batches of size N in each direction. When N requests have been serviced in the current direction, the algorithm reverses direction regardless of whether more requests exist in that direction.

N-Step-SCAN Algorithm (N=3) 0 200 40 45 Start: 50 55 58 60 70 75 80 Step 1: Process 3 requests ? Step 2: Continue ? (3 more)

Algorithm Steps

  • Determine the current position of the disk arm

  • Sort pending I/O requests by track number

  • Choose initial direction (typically towards nearest request)

  • Service exactly N requests in the current direction

  • Reverse direction after processing N requests

  • Repeat until all requests are processed

Example Implementation

Here's a Python implementation demonstrating N-Step-SCAN with N=3:

def n_step_scan(current_position, requests, n):
    direction = 1  # 1 for right, -1 for left
    requests = sorted(requests)
    total_seek_time = 0
    sequence = []
    
    while requests:
        processed_count = 0
        current_batch = []
        
        if direction == 1:
            # Moving right - find requests greater than current position
            candidates = [r for r in requests if r >= current_position]
            candidates.sort()
        else:
            # Moving left - find requests less than current position
            candidates = [r for r in requests if r <= current_position]
            candidates.sort(reverse=True)
        
        # If no candidates in current direction, change direction
        if not candidates:
            direction *= -1
            continue
            
        # Process up to N requests in current direction
        for i in range(min(n, len(candidates))):
            next_request = candidates[i]
            total_seek_time += abs(next_request - current_position)
            current_position = next_request
            current_batch.append(next_request)
            requests.remove(next_request)
            processed_count += 1
            
        sequence.extend(current_batch)
        print(f"Batch (direction {'right' if direction == 1 else 'left'}): {current_batch}")
        
        # Change direction after processing N requests (or fewer if at end)
        direction *= -1
    
    print(f"Total seek time: {total_seek_time}")
    print(f"Sequence: {sequence}")
    return sequence, total_seek_time

# Example usage
current_position = 50
requests = [40, 45, 55, 58, 60, 70, 75, 80]
n = 3

n_step_scan(current_position, requests, n)

Output

Batch (direction right): [55, 58, 60]
Batch (direction right): [70, 75, 80]
Batch (direction left): [45, 40]
Total seek time: 85
Sequence: [55, 58, 60, 70, 75, 80, 45, 40]

Advantages

  • Reduced Direction Changes Limits frequent direction reversals by processing N requests per direction

  • Fairness Prevents indefinite postponement of requests in one direction

  • Configurable Performance Parameter N can be tuned for different workloads

  • Better than Pure SCAN Reduces starvation issues present in traditional SCAN

Disadvantages

  • Suboptimal Seek Time May not achieve minimum possible seek time due to forced direction changes

  • Parameter Selection Choosing optimal N value requires understanding of workload characteristics

  • Inefficient for Sparse Requests May perform poorly when requests are unevenly distributed

  • Fixed Pattern Lacks dynamic adaptation to changing request patterns

Comparison with Other Algorithms

Algorithm Direction Changes Starvation Seek Time
FCFS Many None High
SCAN Minimal Possible Medium
N-Step-SCAN Controlled Reduced Medium
C-SCAN Regular None Medium

Conclusion

N-Step-SCAN provides a balanced approach between the efficiency of SCAN and fairness considerations. By processing exactly N requests per direction, it reduces starvation while maintaining reasonable seek times. The algorithm is particularly useful in systems where preventing indefinite delays is more important than achieving absolute minimum seek time.

Updated on: 2026-03-17T09:01:39+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements