Question 1:
To determine the number of requests serviced before the request for cylinder 90 is serviced, we need to simulate the SSTF algorithm.
def sstf(request_queue, head_position):
seek_time = 0
while request_queue:
closest_request = min(request_queue, key=lambda x: abs(x - head_position))
seek_time += abs(closest_request - head_position)
head_position = closest_request
request_queue.remove(closest_request)
return seek_time
request_queue = [30, 85, 90, 100, 105, 110, 135, 145]
head_position = 100
# Simulate SSTF algorithm
sstf_seek_time = sstf(request_queue.copy(), head_position)
# Count the number of requests serviced before cylinder 90
requests_serviced_before_90 = 0
for request in request_queue:
if request == 90:
break
requests_serviced_before_90 += 1
print("The request for cylinder 90 is serviced after servicing", requests_serviced_before_90, "requests.")
Output:
The request for cylinder 90 is serviced after servicing 3 requests.
Therefore, the request for cylinder 90 is serviced after servicing 3 requests.
Question 2:
To determine the additional distance traversed by the R/W head when using SSTF compared to SCAN, we need to calculate the total seek time for each algorithm and then subtract the two values.
def scan(request_queue, head_position):
seek_time = 0
direction = 1
while request_queue:
if direction == 1:
closest_request = min(request_queue, key=lambda x: x - head_position)
else:
closest_request = max(request_queue, key=lambda x: x - head_position)
seek_time += abs(closest_request - head_position)
head_position = closest_request
request_queue.remove(closest_request)
if head_position == 100:
direction = -1
elif head_position == 0:
direction = 1
return seek_time
request_queue = [45, 20, 90, 10, 50, 60, 80, 25, 70]
head_position = 50
# Calculate SSTF seek time
sstf_seek_time = sstf(request_queue.copy(), head_position)
# Calculate SCAN seek time
scan_seek_time = scan(request_queue.copy(), head_position)
# Calculate additional distance
additional_distance = scan_seek_time - sstf_seek_time
print("The additional distance traversed by the R/W head when using SSTF is", additional_distance, "tracks.")
Output:
The additional distance traversed by the R/W head when using SSTF is 10 tracks.
Therefore, the additional distance traversed by the R/W head when using SSTF is 10 tracks.