I/O with raw disk

Discussion in 'Perl Misc' started by dcruncher4@aim.com, Sep 14, 2006.

  1. Guest

    How can I open a raw disk file and write to it. I have to write a
    program which will
    write few gigs of data to both raw disk and a cooked file to compare
    the write
    performance.

    Which functions to use for that.

    Thanks.
    , Sep 14, 2006
    #1
    1. Advertising

  2. -berlin.de Guest

    <> wrote in comp.lang.perl.misc:
    > How can I open a raw disk file and write to it. I have to write a


    "Raw disk" and "file" don't go together. A raw disk has blocks and/or
    bytes but no files.

    > program which will
    > write few gigs of data to both raw disk and a cooked file to compare
    > the write
    > performance.
    >
    > Which functions to use for that.


    That has nothing to do with Perl and everything with your OS. What is
    your OS? You'd best ask that question on an OS-specific group.

    Anno
    -berlin.de, Sep 14, 2006
    #2
    1. Advertising

  3. Ben Morrow Guest

    Quoth :
    > How can I open a raw disk file and write to it. I have to write a
    > program which will
    > write few gigs of data to both raw disk and a cooked file to compare
    > the write
    > performance.


    Do you mean read(2)/write(2) vs. fread(3)/fwrite(3)? read(2) and
    write(2) are called sysread and syswrite in Perl. See their entries in
    perlfunc. You can specify that all IO to a file should use these
    syscalls directly by using the :unix PerlIO layer (yes, :unix on all
    platforms currently): see PerlIO for a start.

    If this is not what you mean you will have to clarify.

    Ben

    --
    #!/bin/sh
    quine="echo 'eval \$quine' >> \$0; echo quined"
    eval $quine
    # []
    Ben Morrow, Sep 14, 2006
    #3
  4. wrote:
    > How can I open a raw disk file and write to it. I have to write a
    > program which will
    > write few gigs of data to both raw disk and a cooked file to compare
    > the write
    > performance.
    >
    > Which functions to use for that.
    >


    Can't you use the usual functions for both? At least on Unix like systems.

    Untested ...

    ----------------------------------8<---------------------------------
    #!perl
    use strict;
    use warnings;

    my $rawdisk = '/dev/hdb2';
    my $cookedfile = '/tmp/foo';

    write_gigabytes($rawdisk);
    write_gigabytes($cookedfile);

    sub write_gigabytes {
    my $targetname = shift;
    my $start=time;
    open my $fh, '>', $targetname
    or die "Unable to open '$targetname' for writing because $!\n";
    print $fh 'a few gigs of data'
    or die "Unable to write to '$targetname' because $!\n";
    close $fh
    or die "Unable to close '$targetname' because $!\n";
    print "writing a few gigs of data to '$targetname' took ",
    time-$start, "s.\n";
    }
    ----------------------------------8<---------------------------------

    Even assuming the above is remotely like a functional perl program, I
    have doubts that this sort of exercise will measure anything useful.

    P.S. the point of the above is not to identify I/O operations suited to
    the OP's real problem domain but to illustrate that on some platforms
    the O/S presents a fairly consistent file-like interface for all sorts
    of odd things. I can't remember if /dev/hdb2 is likely to be character
    mode or block mode, or whether either case would prevent the above
    program from "working".
    RedGrittyBrick, Sep 14, 2006
    #4
  5. wrote:
    > How can I open a raw disk file and write to it. I have to write a
    > program which will
    > write few gigs of data to both raw disk and a cooked file to compare
    > the write
    > performance.
    >
    > Which functions to use for that.


    Depending on your OS, you could (on Linux systems) use the appropriate
    /dev entries for the disks, but that, too, will send it through the
    buffer cache and all you measure is file system overhead.
    On 2.4 kernels, you could set up /dev/raw devices to get around the
    buffer cache, on 2.6 kernels you'd need to open the device with O_DIRECT.

    But, as Anno pointed out, it has not much to do with Perl.

    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
    Josef Moellers, Sep 15, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jas Shultz
    Replies:
    0
    Views:
    936
    Jas Shultz
    Dec 3, 2003
  2. Replies:
    12
    Views:
    508
    santosh
    Nov 15, 2006
  3. Daniel Rudy

    Raw disk access

    Daniel Rudy, Mar 23, 2007, in forum: C Programming
    Replies:
    7
    Views:
    508
    Flash Gordon
    Mar 24, 2007
  4. khanta
    Replies:
    2
    Views:
    56
    Mark Lawrence
    Feb 21, 2014
  5. Chris Angelico
    Replies:
    0
    Views:
    45
    Chris Angelico
    Feb 19, 2014
Loading...

Share This Page