Skip to content
Advertisement

How can I write a I/O bound C program?

I must write programs that are I/O Bound and that will make my I/O scheduler work like never done before for a Operating Systems homework, but I have no idea how to do it. I’ve tried writing a simple C program that counts the lines of big text files, but it executes too fast and I can’t measure the effectiveness of my scheduler with it. This is worth 25% of my grade, any help would be much appreciated.

Advertisement

Answer

The previous answer appears more CPU bound than i/o bound from my tests.

I suggest opening many files and seeking like crazy within each file using the low-level non-cached C routines. Here is the C code that performs the seeks:

#define _GNU_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>

int main(int argc, char **argv) {

    if (argc != 2) {
        fprintf(stderr, "specify a file to read!n");
        return EXIT_FAILURE;
    }

    int fd = open(argv[1], O_DIRECT);
    if (fd < 0) {
        perror("open error");
        return EXIT_FAILURE;
    }

    off_t size = lseek(fd, 0, SEEK_END);

    for (int i = 0; i < 1000000; i++)                                                                                
        lseek(fd, rand() % size, SEEK_SET);

    close(fd);

    return EXIT_SUCCESS;
}

Then, in a shell, run it through find for every file in the filesystem:

find / -exec ./io_bound {} ; 2>/dev/null

On my system it works quite well, one can spot the rcu_sched task:

top - 20:44:48 up 57 min,  1 user,  load average: 0.84, 0.76, 0.59
Tasks: 266 total,   2 running, 264 sleeping,   0 stopped,   0 zombie
%Cpu0  : 10.0 us, 11.3 sy,  0.0 ni, 78.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu1  : 10.3 us,  8.3 sy,  0.0 ni, 81.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu2  : 15.3 us, 15.9 sy,  0.0 ni, 68.4 id,  0.3 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu3  : 16.4 us, 14.7 sy,  0.0 ni, 68.9 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem : 16314192 total,  9431208 free,  3312716 used,  3570268 buff/cache
KiB Swap: 15624188 total, 15624188 free,        0 used. 12630464 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND     
13087 gluckf    20   0    4224    784    708 R   3.3  0.0   0:00.10 io_bound    
    7 root      20   0       0      0      0 S   0.3  0.0   0:03.54 rcu_sched   
 1586 root      20   0  455832  74824  62736 S   0.3  0.5   0:41.49 Xorg        
 2160 gluckf    20   0 1389916 137096  52548 S   0.3  0.8   0:41.27 cinnamon    
 2285 gluckf    20   0  498388  46752  28632 S   0.3  0.3   0:14.15 gnome-term+ 
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement