Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

(Just open 50 tabs in Google Chrome :D (just kidding, no you can't))

Shortest code for infinite disk I/O any language, C# example:

using System.IO;

namespace FileApp {
    static class Program {
        public static void Main() {
            do {
                File.WriteAllText("a", "a");
                File.Delete("a");
            } while (true);
        }
    }
}

You can't just fill the entire disk though, as then it would halt in the end and would be finite.

And you can't do reading only, infinite writing has to happen. (It has to kill my SSD after enough runtime.)

Get cracking! :)

share|improve this question
4  
Does reading files instead of writing them also count as disk I/O? What about writing to /dev/null? (Is yes>/dev/null a valid Bash answer?) – Doorknob Apr 1 at 12:33
2  
Can it take any input ? – User112638726 Apr 1 at 14:22
1  
Sure, as long as it is not infinite user input. – MathuSum Mut Apr 1 at 14:26
16  
Dang man...what did your SSD do to you? – R. Kap 2 days ago
3  
um...what?..... – MathuSum Mut yesterday

25 Answers 25

up vote 25 down vote accepted

If you want a shorter (but more boring than my other one) answer:

Bash, 5 bytes

>w;$0

I could make that shorter if there's a command (less than 3 bytes long) that writes something to disk I/O. Something like sync would work, but sync is 4 bytes 😛

Note: this doesn't work when run straight from bash, only when put in a script and run as the script name. (i.e. echo 'w>w;$0' > bomb; chmod 755 bomb; ./bomb)

share|improve this answer
1  
Looks like it's a tie between me and @isaacg - who wins? – Daniel 2 days ago
7  
I prefer using exec (or . $0). I think this will run out of PIDs. – muru 2 days ago
1  
Is the first w needed here? For me simply >w creates an empty file w, and doing that in a loop will create infinite I/O because the mtime metadata needs to be updated continually. – Henning Makholm yesterday
1  
If that qualifies @HenningMakholm then I'll put it in. – Daniel yesterday
1  
@MathuSumMut I made an edit. Personally I feel that mine should be counted simply as the bash code itself for the bytecount. Every other entry here must be executed as a script in a file, so how is mine different? – Daniel 21 hours ago

PowerShell v2+, 10 bytes

for(){1>1}

Simply loops infinitely with an empty for loop. Each iteration, we output the integer 1 (implicitly converted to a string) with the > redirect operator, which overwrites the file named 1 in the local directory.

share|improve this answer
    
Could you replace the second 1 with anything else (that makes a valid file name) or does it have to be 1? – QPaysTaxes 2 days ago
    
On my Windows VM, Winload.exe is sufficient... – Comintern 2 days ago
    
I think it has to be a number because if it is a letter then it would be treated as a variable, to treat it as a string quotes are required, and they waste bytes. :P – MathuSum Mut 2 days ago
1  
@QPaysTaxes MathuSum has it correct. The second 1 needs to be a number of some sort in order for the implicit parsing to work correctly. Anything from [0-9] would work the same. – TimmyD 5 hours ago

Pyth, 6 bytes

#.w]]0

Pyth's only file output command is .w. When called on a string, it writes that string to a file in append mode, which is no good for the purpose of this question. When called on a 2-d array, it writes the corresponding image to that file, overwriting the file contents. That's what this program does. The default file output name is o.png, so this program infinitely overwrites the file o.png with a 1-pixel white image. # is an infinite loop.

share|improve this answer

Ruby, 22 20 bytes

loop{open(?a,?w)<<1}

Repeatedly truncates and writes a 1 to the file a.

Thanks to Ventero for 2 bytes!

share|improve this answer
3  
open(?a,?w)<<1 to save 2 bytes. – Ventero Apr 1 at 15:29
    
Thank you, doorknob, for honouring us with your presence. I am humbled. – MathuSum Mut yesterday

cmd, 14 bytes

:a
cd>1
goto a

Infinitly overwrites the file 1 with the string to the current directory


I'm new here: Are windows new lines (CR LF) counted as two bytes?

share|improve this answer
11  
Welcome to PPCG! Windows, at least modern systems, should be able to handle just LF without issue. The above works for me with just LF on Windows 8.1, so your 14 bytes is correct. – TimmyD Apr 1 at 15:34
5  
Newlines in code are always counted as one byte, not two, unless the CRLF has another meaning from linebreak. – cat 2 days ago
    
@cat CRLF is two bytes: CR, LF. Whereas newline is one byte: LF. However in this case CR is not necessary even on Windows – Steven Penny 35 mins ago

Bash + coreutils, 10

yes \>b|sh

Writes a continuous stream of >b, which is piped to sh for evaluation. >b simply truncates a file called b to zero bytes each time.

share|improve this answer
    
+1 because your name is really appropriate to what this snippet of code will do – Olivier Dulac 3 mins ago

Perl 5, 27 32 bytes

{{open my$h,'>o';print$h 1}redo}

Quick explanation:

{    # <-- Braces implicitly create/mark a loop.
    {    # <-- New, more localized scope, for `my` variable.
        open my $filehandle, '>', 'o';      # Writing to file "o".
        print $filehandle 1;    # Yes, no comma there!
                                # Search "Indirect Object Notation".
        close $filehandle;    # Called automatically when the
                              # variable gets deleted.
    }    # <-- Scope gets destroyed.
    redo;  #...the loop!
}

Edit: {open F,'O';print F 1;redo} ← Didn't test the code before posting; now I had to correct it.

share|improve this answer
1  
:o a perl variable not prefixed with $! – cat 2 days ago
    
@cat: It's not a regular variable, like a scalar, array, or hash. It is simply a bareword. Depending on context, a bareword can be taken as a sub (a function), a glob, I think, or a filehandle. (Maybe others too?) – g4v3 10 hours ago

Bash, 26 bytes

yes>y&while :;do rm y;done

If I were to expand this one-liner, I would get this:

yes > y &      # Write out infinitely to the file y in the current directory
while true     # Let's do something forever
do             # Here's what we're going to do
    rm y       # delete y
done           # That's all we're going to do

This can't exactly compete with the 10 byte PowerShell line, but it'll hold its own against the others. See my other answer for the 6 byte version.

share|improve this answer
2  
while :;ls>l;done – User112638726 Apr 1 at 14:33
1  
The good old exec trick will do better: ls>l;exec $0. Shorter, but boring. – manatwork Apr 1 at 14:46
    
:>l;exec $0 - file creation is writing the inode – user24582 Apr 1 at 14:50
4  
Even though you delete y, yes will still continue to write to the same file handle that it had. Run lsof | grep yes and you should see something like /path/to/y (deleted). This will fill up the disk and fail. – muru 2 days ago
2  
Instead of rm y you can use >y which will truncate the existing file. It is also a bit shorter. – aragaer yesterday

sh, 11 bytes

w>w;exec $0

Save this to a file without special characters, such as loop.sh, make it executable, and run it with ./loop.sh or similar.

This writes the output of the command w to the file w, overwriting the previous value each time. Then, it replaces itself with a fresh version of the same program, so it can run infinitely.

share|improve this answer
    
this is missing a second >. also, assuming you have a "special" $PATH and "special" umask/filesystem, you can go for w>>w;$0, brining it down to 7 chars – mnagel 2 days ago
    
@mnagel >> is append, it will eventually fill the disk – cat 2 days ago
1  
@mnagel but you're right about not needing exec,I didn't realize that. Someone else has done it, so I won't update, though – isaacg 2 days ago
2  
Or . $0 instead of exec $0, perhaps? I don't know if that will run cause a stack overflow or something, though. ... Yep, it segfaulted. – muru 2 days ago

Python, 33 bytes

while 1:open("a","w+").write(" ")

It's hard to tell if this is actually doing the right thing but watch -n1 lsof -p `pidof python3` seems to say it's right.

share|improve this answer

PHP, 60 30 17 16 15 bytes

Updated yet again as per @manatwork suggested:

while(!`cd>1`);

Also now tested.


A bit of cheating 22 bytes:

while(exec('>1 dir'));

Earlier suggestion by @manatwork 30 bytes:

while(file_put_contents(1,1));

NOT TESTED (no php available on this computer) 43 bytes:

for($a=fopen(1,'w');fputs($a,1);fclose($a))

A golfed original 45 bytes:

$a=fopen(1,'w');while(fputs($a,1))rewind($a);

My first post here, joined because I just had to try this out: as long as file write succeeds, rewind file pointer to start.


Just can't get smaller than the file_put_contents().

share|improve this answer
5  
while(file_put_contents(1,1)); should be enough. Note that running full scripts from command line as php -r '…' is acceptable as per consensus on meta Running PHP with -r instead of code tags. – manatwork Apr 1 at 13:13
    
Is this actually writing to disk or just a buffer in memory? – Brice M. Dempsey Apr 1 at 14:33
1  
@manatwork Oh man! I knew there's always room for improvement, but that much... too bad that function hasn't got shorter name. :D I don't know about the buffer.. I wonder if I should update the answer with that shorter solution. – diynevala 2 days ago
2  
If it's shorter, please do update your answer, go ahead! :) – MathuSum Mut 2 days ago
    
Is it allowed to call exec() from php? I realize it is no more in php's "scope". – diynevala 2 days ago

MATL, 10 bytes

`1[]T3$Z#T

Explanation

This is an infinite loop that writes number 1 to a file called inout in current directory, overwriting previous file's contents.

`       % do...while loop
  1     %   push number 1
  []    %   push empty array
  T     %   push "true"
  3$Z#  %   fwrite function. First input is file content. Second is file name;
        %   defaults to "inout" if empty. Third indicates that any previous
        %   file contents should be discarded
  T     %   push "true": loop condition 
        % implicitly end loop. Loop condition is "true", so the loop is infinite
share|improve this answer

C, 95 94 93 89 78 90 89 76 75 bytes

#include<stdio.h>
main(){for(FILE*f=fopen("a","w");;fputc(0,f),fclose(f));}   

Again, sudo watch -n1 lsof -p `pidof inf` seems to say this is valid.

HOW DID I NOT SEE THAT SPACE D:<

Thanks @Jens for shaving off 13 bytes :D

share|improve this answer
1  
The w+ mode is read and write, initially truncating the file. Since you don't need to read, you can shave off a byte with just w, which also truncates the file, but doesn't open the file in read mode. – Mego Apr 1 at 16:14
1  
No need for return 0; if the loop never terminates. – Jens 2 days ago
1  
Why not use fputc(1,f) instead of the super-verbose fprintf(f," ")? – Jens 2 days ago
1  
main(){for(FILE*f=fopen("a","w");;fputc(1,f),fclose(f));} since an empty conditional in for means true. 76 bytes. – Jens 2 days ago
    
@Jens because of course I didn't know about fputc. Now I do :D – cat yesterday

TI-BASIC, 26 bytes

While 1
Archive A
UnArchive A
End

This will work on the TI-83+ and TI-84+ series of calculators.

Yes, this also works if A is already archived or is not initialized at all at the start of the program! The program is only 26 bytes because of tokenization.

share|improve this answer
    
I don't know if the flash memory used by the calculators counts as "disk". – lirtosiast 20 hours ago
    
@lirtosiast In Jamy's defense, SSDs are made of flash memory =) – Cort Ammon 17 hours ago
    
In any case, the byte count is at least 10 bytes less. The 2nd Mem screen counts a header equal to 9 bytes + the length of the program name, but we don't here so you can subtract it out. – lirtosiast 16 hours ago

Rust, 84 Bytes

fn main(){loop{use std::io::Write;std::fs::File::create("a").unwrap().write(b"a");}}

File::create truncates an existing file, thus ensuring that we don't run out of disk space.

The used Compiler (1.9 Nightly) issues a warning about the unused result of write(...) but compiles nevertheless.

share|improve this answer

C, 92 bytes

#include <stdio.h>
main(){for(FILE*f=fopen("a","w+");fprintf(f," "),!fclose(f);;);return 0;}

While it looks like you could save 1 byte by

  for(FILE*f=fopen("a","w+");fprintf(f," ")+fclose(f);;){}

the problem with that loop is that + doesn't give you the guaranteed order.

Or recursive - shouldn't overflow if the compiler properly implements tail recursion (f is in an explicit inner scope)

85 bytes

#include <stdio.h>
main(){{FILE*f=fopen("a","w+");fprintf(f," ");fclose(f);}main();}
share|improve this answer
    
Hopefully the 85-byte version does not blow the stack. :P – MathuSum Mut 2 days ago
1  
@MathuSumMut: Easy fix: require compiling with optimizations. Tail call recursion saves the day. – Joshua 18 hours ago

Haskell, 20 bytes

f=writeFile"b""a">>f

Write the string "a" to a file named "b" and repeat. writeFile overwrites the file if it exists.

share|improve this answer

C, 40 bytes

main(){for(;;)write(open("a",1)," ",1);}

It will quickly run out of file descriptors, though; this can be overcome with:

f=open("a",1);main(){for(;;)write(f," ",1);}

at the cost of 4 additional bytes.

share|improve this answer
    
Why doesn't f have a type in the second? – cat yesterday
    
@cat In C (very K&R style) it defaults to int. – black yesterday

Mathematica, 14 bytes

For[,1>0,a>>a]

Repeatedly writes the string "a" to a file named a in the current directory, creating it if it doesn't exist.

share|improve this answer

ZSH, 14 bytes

for ((;;)) :>:

Zsh, unlike Bash and other Bourne-like shells, allows loops without the do ... done fence, provided the condition is suitably delimited.

Alternatively, with while:

while {} {:>:}

Note that : is a builtin. You can't suspend this loop.

The principle is the same as in Digital Trauma's answer - nothing is written to the file, the IO is purely from creating and truncating the file.

share|improve this answer
    
I gotta say, I never thought I'd see muru, the muru, come play Code Golf. Welcome to PPCG :D – cat 2 days ago
1  
@cat Thanks. :D I have played once before. – muru 2 days ago

JavaScript (Node.js), 43 bytes

(c=x=>require("fs").writeFile("a","a",c))()

Writes a to a file named a, then repeat.

share|improve this answer

Racket, 46 bytes

(do()(#f)(write-to-file'a"f"#:exists'replace))
share|improve this answer
1  
I was thinking of answering in Racket, but you beat me to it. :P – cat yesterday
    
Out of curiosity, did you find a shorter answer? – Winny yesterday

Factor, 73 bytes

USING: io.files io.encodings
[ 0 "a" utf8 set-file-contents ] [ t ] while

Sets the file contents to the nul byte forever.

share|improve this answer

CBM BASIC 7.0, 9 bytes

0dS"a":rU

This program, when run, repeatedly saves itself to disk. Here's a more readable version which doesn't use BASIC keyword abbreviations:

0 dsave "a" : run
share|improve this answer
    
Does it run out of cassette tape though? ;) – MathuSum Mut 7 hours ago

DOS/Batch: 4 bytes

%0>x

This batch file will call itself (%0) and redirect (>) the output to a file called x. Since echo is on by default, this will output the path and command.

share|improve this answer

protected by Community 2 days ago

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.