Shortest code for infinite disk I/O

49

11

(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! :)

MathuSum Mut

Posted 2016-04-01T11:04:06.877

Reputation: 819

6Does 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 – 2016-04-01T12:33:18.457

Good point, I changed the question to require infinite writing. – MathuSum Mut – 2016-04-01T12:35:27.993

2Can it take any input ? – User112638726 – 2016-04-01T14:22:21.920

1Sure, as long as it is not infinite user input. – MathuSum Mut – 2016-04-01T14:26:08.883

1This is heavily biased towards shell scripts, but such is life. – Mateen Ulhaq – 2016-04-01T22:36:50.507

29Dang man...what did your SSD do to you? – R. Kap – 2016-04-01T23:14:06.047

1Would this kill the ssd though? Wouldn't the writes get cached (kernel or disk cache) and then removed again before hitting the nand chips? – Filip Haglund – 2016-04-02T08:13:22.470

2As I can't hope to compete with 6 byte solutions, would creating the file ./a with the 3 byte contents ./a count for a bonus prize for lateral thinking? AFAIK just executing a file causes some file system writing to take place on many systems, because at the very least 'last access time' gets updated as a byproduct ;-) – Stilez – 2016-04-02T13:57:37.750

Can't stay. Giving talk. Oh no, they switched to my screen...where is it...ah, there! Bye! – CalculatorFeline – 2016-04-02T21:43:43.937

(Note: I actually showed that during the talk a couple of minutes ago.) – CalculatorFeline – 2016-04-02T21:44:02.983

3Many of these answers will write the data into the same space over and over. That does not result in an actual disk write even if the data differs. (Extreme case, dos -> windows communications. I wrote 4k of data in dos and read it back in Windows--so long as data was flowing the disk light would stay off.) – Loren Pechtel – 2016-04-02T23:40:22.757

Does "infinite" here mean "until an error occurs" (e.g. disk full), or literally infinite? If you write sequentially until the disk is full, are you allowed to quit, or do you have to start over? – Nate Eldredge – 2016-04-04T03:57:11.423

Literally infinite, the disk cannot be full. – MathuSum Mut – 2016-04-04T05:54:48.520

@Doorknob /dev/null is provided by Linux/Mac/whatever kernel, it isn't on the disk (or at least will not affect the disk when written to). – sadljkfhalskdjfh – 2016-04-04T10:49:49.507

um...what? Infinite disk I/O is a sure way of sentencing your SSD to death. – cst1992 – 2016-04-05T08:35:37.313

I was referring to CatsAreFluffy ;) – MathuSum Mut – 2016-04-05T08:38:07.367

@Stilez: A program that just exec's itself repeatedly, to create access-time metadata I/O on file holding the script? That will work as well as most answers on systems that don't use the default lazy relatime or noatime mount options. However, 3 bytes won't do it: you'll run out of PIDs. (Some of the script answers have the same problem. Programs with resource leaks aren't going to be able to wear out an SSD before crashing). – Peter Cordes – 2016-05-14T06:24:12.690

1Does it need to be disk I/O, or will tape I/O work? – Mark – 2017-01-31T05:11:08.017

Answers

26

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.

Thomas Weller

Posted 2016-04-01T11:04:06.877

Reputation: 1 925

Will this run out of disk space eventually, or is the output overwritten? – MathuSum Mut – 2016-04-11T12:46:24.917

1@MathuSumMut: With > it will be overwritten. >> would append – Thomas Weller – 2016-04-11T20:15:58.203

1You win then I guess :P – MathuSum Mut – 2016-04-11T20:39:59.937

Why can't echo be off before running this? (I've seen many answers like this) – Erik the Outgolfer – 2016-05-15T15:45:55.120

@ΈρικΚωνσταντόπουλος: if echo is off, only explicit output is shown on the console. Things like commands and even the prompt will disappear. %0 is the only thing that could generate output, but it doesn't do that on its own, it needs the shell to output a copy of the command implicitly. – Thomas Weller – 2016-05-22T17:24:14.397

@ThomasWeller I mean someone might have turned off echo before running this code. – Erik the Outgolfer – 2016-05-22T17:44:30.903

2@ΈρικΚωνσταντόπουλος: That's true. In that case, a file with 0 byte is produced, not generating I/O as expected by this challenge. But it's not our task to consider every case, otherwise you might want to turn off caching, virus scanners, ... – Thomas Weller – 2016-05-24T19:17:51.873

1Writing a 0 byte file will still cause disk I/O, as it has to update the last-modified time within the directory. – None – 2016-12-13T14:43:27.743

48

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.

AdmBorkBork

Posted 2016-04-01T11:04:06.877

Reputation: 41 581

Could you replace the second 1 with anything else (that makes a valid file name) or does it have to be 1? – Fund Monica's Lawsuit – 2016-04-01T21:53:34.303

1On my Windows VM, Winload.exe is sufficient... – Comintern – 2016-04-01T23:07:53.567

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 – 2016-04-02T15:14:46.190

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. – AdmBorkBork – 2016-04-04T12:17:49.750

Love seeing a PowerShell answer score so high! However I don't understand your statements about the second 1 needing to be a number - it seems to work fine for me with a instead, and i dont see why powershell would parse it as anything other than the string a – Nacht - Reinstate Monica – 2016-04-05T00:34:06.957

1@Nacht Maybe it's a quirk of my particular environment, then. In my ISE (PSv4 Win8.1), replacing the second 1 with anything non-numeric (and not specified either as .\a or a.txt or the like) results in a parse error. – AdmBorkBork – 2016-04-05T12:21:39.317

@Nacht OK, now I'm definitely convinced it's a bug in my particular environment, since for(;;){1>a} works fine. – AdmBorkBork – 2016-04-05T12:39:51.980

28

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.

isaacg

Posted 2016-04-01T11:04:06.877

Reputation: 39 268

27

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)

Daniel

Posted 2016-04-01T11:04:06.877

Reputation: 447

1Looks like it's a tie between me and @isaacg - who wins? – Daniel – 2016-04-02T03:09:30.707

8I prefer using exec (or . $0). I think this will run out of PIDs. – muru – 2016-04-02T08:30:41.483

1Is 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. – hmakholm left over Monica – 2016-04-03T12:17:37.177

1If that qualifies @HenningMakholm then I'll put it in. – Daniel – 2016-04-03T13:29:11.377

hmm...I think it qualifies as good rule circumventing, although the winner was already announced, but you can still post it if you like. :) – MathuSum Mut – 2016-04-03T16:34:12.300

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 – 2016-04-03T20:45:08.677

Alright bro, enjoy – MathuSum Mut – 2016-04-03T20:50:15.380

If it only works if put into a script, than the script should also be taken into consideration when calculating the size. Just when I access external resources, it also adds to the code size. – vsz – 2016-04-04T04:08:53.263

2The script only contains these bytes. Since it's bash there's no compiler or anything fancy needed. – Daniel – 2016-04-04T13:33:31.090

16

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!

Doorknob

Posted 2016-04-01T11:04:06.877

Reputation: 68 138

3open(?a,?w)<<1 to save 2 bytes. – Ventero – 2016-04-01T15:29:07.460

Thank you, doorknob, for honouring us with your presence. I am humbled. – MathuSum Mut – 2016-04-03T10:59:58.217

Does that leak file descriptors? Or does it get closed when it goes out of scope? (IDK Ruby, sorry). – Peter Cordes – 2016-05-14T06:31:55.520

13

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?

MrPaulch

Posted 2016-04-01T11:04:06.877

Reputation: 771

13Welcome 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. – AdmBorkBork – 2016-04-01T15:34:57.147

CR LF2CR1LF1 – Erik the Outgolfer – 2016-05-15T15:50:48.870

13

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.

Digital Trauma

Posted 2016-04-01T11:04:06.877

Reputation: 64 644

4+1 because your name is really appropriate to what this snippet of code will do – Olivier Dulac – 2016-04-04T17:43:27.863

Why b and not c? – CalculatorFeline – 2017-06-05T02:17:35.480

9

Perl 5, 27 32 22 bytes

{{open my$h,'>o'}redo}

If simply changing the modification timestamp of a file suffices...

Quick explanation:

{ # Braces implicitly create/mark a loop.
    { # They also create closures, for `my` variables.
        open my $filehandle, '>', 'o';    # Writing to file "o".
        # close $filehandle;   # Called implicitly when
                               # variable gets destroyed.
    } # $filehandle gets destroyed because there are no references to it.
    redo; # ...the loop.
}

Previous solution (32 bytes): {{open my$h,'>o';print$h 1}redo}

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

g4v3

Posted 2016-04-01T11:04:06.877

Reputation: 241

1:o a perl variable not prefixed with $! – cat – 2016-04-02T02:22:25.590

@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 – 2016-04-04T07:46:39.687

8

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().

diynevala

Posted 2016-04-01T11:04:06.877

Reputation: 191

5while(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 – 2016-04-01T13:13:59.557

Is this actually writing to disk or just a buffer in memory? – Brice M. Dempsey – 2016-04-01T14:33:02.507

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 – 2016-04-01T18:47:09.753

2If it's shorter, please do update your answer, go ahead! :) – MathuSum Mut – 2016-04-01T21:35:06.283

Is it allowed to call exec() from php? I realize it is no more in php's "scope". – diynevala – 2016-04-02T05:59:58.587

Yes, executing external commands is allowed by default. Sadly your while(exec('>1 dir')); idea will not work as is: exec()'s return value is the last line from the executed command's stdout. As you are redirecting the execute command's output to a file, nothing will reach stdout, so exec() will return empty string. Which being falsy value in PHP, the while will immediately terminate. Fortunately negating the condition is quite short. By the way, better use the Execution Operators instead of exec(): while(!\>1 dir`);`.

– manatwork – 2016-04-02T13:50:18.470

That's just wrong, having such shorthands in php.. nice solution though. – diynevala – 2016-04-02T14:59:58.583

7

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

cat

Posted 2016-04-01T11:04:06.877

Reputation: 4 989

1The 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 – 2016-04-01T16:14:58.613

1No need for return 0; if the loop never terminates. – Jens – 2016-04-02T11:33:04.640

1Why not use fputc(1,f) instead of the super-verbose fprintf(f," ")? – Jens – 2016-04-02T11:37:23.043

1main(){for(FILE*f=fopen("a","w");;fputc(1,f),fclose(f));} since an empty conditional in for means true. 76 bytes. – Jens – 2016-04-02T11:49:36.917

@Jens because of course I didn't know about fputc. Now I do :D – cat – 2016-04-02T20:07:19.387

You don't need the fputc at all. fopen/fclose will cause disk I/O for the metadata. Also, you appear to have two ;; back to back. Also, @Jens: you never need return 0 in main: the C standard specifies an implicit return 0 at the end of main. Even if not, you'd just return whatever happened to be left in the return-value register, which is fine. Outside of golfing, you should of course return 0 explicitly at the end of main, and use its proper function signature! – Peter Cordes – 2016-05-14T05:42:18.990

Also, you don't need sudo for lsof to work on your own processes. – Peter Cordes – 2016-05-14T05:43:05.333

@PeterCordes The two ;; are in for(;;) and on purpose as explained. They are required. – Jens – 2016-05-14T07:33:42.093

@Jens: oh, I didn't notice that was inside the for() condition, rather than the body. That means the fopen() only happens once, so all but the first iteration is operating on a closed FILE*. It crashes on Linux (double-free detected on the fclose of the already-closed FILE*), and there's no way it can possibly do what we need on any system, because we need fopen to run repeatedly to truncate. – Peter Cordes – 2016-05-14T07:45:13.897

@Jens: here's a working version that's the same length, taking advantage of the required ;s in the for structure: main(){for(FILE*f;f=fopen("a","w");fclose(f))fputc(0,f);}. I actually tested it, and it runs, actually producing disk activity on Linux. (Also see my C and C++ answers, though.) – Peter Cordes – 2016-05-14T07:51:34.117

I incorporated that into my own answer, with improvements. (e.g. don't include stdio.h) – Peter Cordes – 2016-05-14T09:00:08.267

I left an edit which fixed the bug and added an ungolfed version, but reviewers rejected it with a claim that it "deviates from the original intent". I thought it might be rejected, but not with that reason. Oh well, I give up on trying to save time for future readers seeing this version.

– Peter Cordes – 2016-05-14T12:21:51.700

1@PeterCordes PPCG is not Stack Overflow; please do not make edits to others' answers which change code or other major parts of the answer. Typo fixes are fine, but anything beyod that (including corrections of factually incorrect statements) should be suggested in the comments. – cat – 2016-05-14T12:24:45.783

Thanks for the clue on etiquette on this site. I had hoped that adding explanations / ungolfed versions would be appreciated :/. Any idea how that reject reason makes sense, though? Obviously the "original intent" wasn't to be wrong. Or is this a common thing on PPCG, where none of the canned responses are "too big a change", so you just use that? I expected that if it was rejected, it would be for "too big", or "self promotion". – Peter Cordes – 2016-05-14T12:26:44.367

1@PeterCordes Well, on most SE sites, collaboration and editing is welcomed but on PPCG, which is unique, a users' answer is sorta sacred and by changing their code or making other large changes, you do "deviate from the original intent of the author" (whereas this is typically fine on SO, etc). Also, the "canned-response-is-wrong-or-slightly-off" phenomena is in fact SE-wide, and Mother Meta gets many many posts about it. :) – cat – 2016-05-14T12:33:36.373

@PeterCordes If you have any questions about this or PPCG in general, you can always come say hi in chat or post on meta.

– cat – 2016-05-14T12:35:02.000

Let us continue this discussion in chat.

– cat – 2016-05-14T12:36:45.893

7

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.

isaacg

Posted 2016-04-01T11:04:06.877

Reputation: 39 268

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 – 2016-04-01T19:09:59.510

@mnagel >> is append, it will eventually fill the disk – cat – 2016-04-01T20:14:57.037

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 – 2016-04-01T20:42:55.510

3Or . $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 – 2016-04-02T08:45:04.970

6

CPython 3.5, 33 16 bytes

while 1:open("a")

Yes, really. :D

cat

Posted 2016-04-01T11:04:06.877

Reputation: 4 989

while 1:open("a","w") is shorter and strace shows that python is doing open, fstat64 and close, definitely I/O operations. If the file a already exists, it can be even shorter: while 1:open('a') which still generates an open, fstat64 and close, and even modifies atime of the file. – Radovan Garabík – 2016-04-05T11:00:41.203

@RadovanGarabík :0 Thank you, I didn't know that handy piece of information! Of course, it's implementation specific. – cat – 2016-04-05T11:26:05.577

6

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.

Daniel

Posted 2016-04-01T11:04:06.877

Reputation: 447

2while :;ls>l;done – User112638726 – 2016-04-01T14:33:22.540

1

The good old exec trick will do better: ls>l;exec $0. Shorter, but boring.

– manatwork – 2016-04-01T14:46:38.970

:>l;exec $0 - file creation is writing the inode – user24582 – 2016-04-01T14:50:15.847

Also if you don't have the 'l' alias, you could use 'w' – MrPaulch – 2016-04-01T16:15:09.557

@manatwork That will run out of disk space, will it not? – Daniel – 2016-04-01T20:06:45.543

@Daniel No, > overwrites. – cat – 2016-04-01T20:37:44.133

Ah, my bad *facepalm* – Daniel – 2016-04-01T22:11:24.870

7Even 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 – 2016-04-02T08:43:00.990

4Instead of rm y you can use >y which will truncate the existing file. It is also a bit shorter. – aragaer – 2016-04-02T21:47:10.967

6

TI-BASIC, 12 bytes

While 1
Archive A
UnArchive A
End

Alternate solution by user lirtosiast with the same size:

While 1
SetUpEditor
Archive ∟1
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 12 bytes because of tokenization.

Jamy Mahabier

Posted 2016-04-01T11:04:06.877

Reputation: 61

I don't know if the flash memory used by the calculators counts as "disk". – lirtosiast – 2016-04-03T21:01:33.263

1@lirtosiast In Jamy's defense, SSDs are made of flash memory =) – Cort Ammon – 2016-04-04T00:37:29.920

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 – 2016-04-04T01:27:38.867

@lirtosiast This repeatedly writes and reads to the ROM (permanent memory) of the calculator, not the RAM. Of course, calculators don't have an actual hard drive inside :) – Jamy Mahabier – 2016-04-04T17:57:56.140

@lirtosiast Thanks, I didn't know about that! (I was wondering why the amount of bytes my TI-84+ reported didn't match up with my hand-counting...) I've updated my answer. – Jamy Mahabier – 2016-04-04T18:05:41.620

I couldn't find anything shorter, but SetUpEditor:Archive L1 is the same size. – lirtosiast – 2016-04-04T19:12:53.493

@lirtosiast Added your solution to my answer! – Jamy Mahabier – 2016-04-04T20:48:56.320

5

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

Luis Mendo

Posted 2016-04-01T11:04:06.877

Reputation: 87 464

5

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.

nimi

Posted 2016-04-01T11:04:06.877

Reputation: 34 639

4

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.

muru

Posted 2016-04-01T11:04:06.877

Reputation: 331

I gotta say, I never thought I'd see muru, the muru, come play Code Golf. Welcome to PPCG :D – cat – 2016-04-02T10:58:19.680

1@cat Thanks. :D I have played once before. – muru – 2016-04-02T10:59:06.963

4

JavaScript (Node.js), 43 41 bytes

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

Writes null to a file named a, then repeat.

Michał Perłakowski

Posted 2016-04-01T11:04:06.877

Reputation: 520

1What about writing c or x to the file? Saves 2 bytes. Also, doesn't require\fs`` work? – Charlie – 2016-04-07T14:43:57.527

1@Charlie Good point with writing c or x. require\fs`` unfortunately doesn't work, because using backticks to call a function calls it with the first arguments as ["fs"] (array, which first and only element is the passed string) instead of "fs" (just the string). Try console.log\test`` for example. – Michał Perłakowski – 2016-04-07T15:02:41.627

3

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.

ECS

Posted 2016-04-01T11:04:06.877

Reputation: 361

3

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();}

MSalters

Posted 2016-04-01T11:04:06.877

Reputation: 171

Hopefully the 85-byte version does not blow the stack. :P – MathuSum Mut – 2016-04-01T21:30:07.633

2@MathuSumMut: Easy fix: require compiling with optimizations. Tail call recursion saves the day. – Joshua – 2016-04-03T22:53:54.830

1There's a lot of scope for saving bytes here. See my answer for packing stuff inside the for(;;), and for shorter functions than fprintf. If you did need to include stdio.h (which you don't), you don't need a space: #include<stdio.h> – Peter Cordes – 2016-05-15T14:03:37.193

3

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.

LegionMammal978

Posted 2016-04-01T11:04:06.877

Reputation: 15 731

would it write the string "a" or the contents of the variable a? And if the latter, what would it do if that variable were not yet defined? – Michael Stern – 2016-05-19T21:53:16.143

@MichaelStern It writes the variable a, which is undefined, so it just writes a\n. – LegionMammal978 – 2016-05-20T00:24:48.567

3

C, 40 bytes

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

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

45, 43 bytes

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

edmz

Posted 2016-04-01T11:04:06.877

Reputation: 139

Why doesn't f have a type in the second? – cat – 2016-04-03T02:53:32.337

2@cat In C (very K&R style) it defaults to int. – edmz – 2016-04-03T08:23:54.760

1gcc and clang won't compile the 2nd version. Even GNU C doesn't allow dynamic initialization of a static/global variable (a call to open() isn't a compile-time constant). Also, it will run out of disk space, because there's no lseek. Maybe try utime("a","") in the loop, which will keep updating the ctime. (You still have to open to create a file of known name). – Peter Cordes – 2016-05-14T05:59:40.737

@PeterCordes You're right, thanks for pointing out. Fixed. – edmz – 2016-05-15T13:28:23.000

Still doesn't satisfy the OP's requirement of not filling up the disk eventually, but maybe still worth keeping as an answer. (Unless you have an idea better than my answer's close/reopen(O_TRUNC) in a loop.) Contrary to my previous comment, updating timestamps only usually doesn't really produce actual disk I/O on Linux. Like maybe one write per 24 hours with lazytime, as I said in my answer. – Peter Cordes – 2016-05-15T13:37:37.867

You can save two bytes in this fill-the-disk version: put the f=open() in the run-once first statement of the for loop (saving a ;). (I got that trick from cat's answer). And, the empty string still has a 0-terminator, so you can take out the space in the string you pass to write(f,"",1). Again, see my answer.) – Peter Cordes – 2016-05-15T13:39:13.087

@PeterCordes No, you're supposed to create an infinite I/O: appending to a file would not because it'd eventually stop due to OS failure (OP's specifically said that). You indeed see in the other answers it's just overwriting, in the accepted one foremost. – edmz – 2016-05-15T13:42:46.100

Ok, well this answer fails the requirement of running forever, since you keep appending one byte at a time with each write. See my answer for code that doesn't. – Peter Cordes – 2016-05-15T13:47:00.587

@PeterCordes Why? 1 = O_WRONLY, which doesn't seek to the end (you'd also need O_APPEND). – edmz – 2016-05-15T13:50:37.173

write() advances the current file position by the number of bytes written. Leaving off O_APPEND doesn't change that, it just changes the initial position of the file descriptor. You could use pwrite(3,"",1,0) to keep over-writing byte 0 of the file, but that doesn't produce real disk activity on my Linux system. Even close/reopen without O_TRUNC didn't light up my hard-drive LED. – Peter Cordes – 2016-05-15T14:00:15.647

mixing standards lol – Erik the Outgolfer – 2016-05-15T16:02:15.377

3

C on amd64 Linux, 36 bytes (timestamp only), 52 49 bytes (real disk activity)

I hard-code the open(2) flags, so this is not portable to other ABIs. Linux on other platforms likely uses the same O_TRUNC, etc., but other POSIX OSes may not.

+4 bytes to pass a correct permission arg to make sure the file is created with owner write access, see below. (This happens to work with gcc 5.2)

somewhat-portable ANSI C, 38/51 bytes (timestamp only), 52/67 bytes (real disk activity)

Based on @Cat's answer, with a tip from @Jens.

The first number is for implementations where an int can hold FILE *fopen()'s return value, second number if we can't do that. On Linux, heap addresses happen to be in the low 32 bits of address space, so it works even without -m32 or -mx32. (Declaring void*fopen(); is shorter than #include <stdio.h>)


Timestamp metadata I/O only:

main(){for(;;)close(open("a",577));}   // Linux x86-64

//void*fopen();       // compile with -m32 or -mx32 or whatever, so an int holds a pointer.
main(){for(;;)fclose(fopen("a","w"));}

Writing a byte, actually hitting the disk on Linux 4.2.0 + XFS + lazytime:

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

write is the for-loop condition, which is fine since it always returns 1. close is the increment.

// semi-portable: storing a FILE* in an int.  Works on many systems
main(f){for(;f=fopen("a","w");fclose(f))fputc(0,f);}                 // 52 bytes

// Should be highly portable, except to systems that require prototypes for all functions.
void*f,*fopen();main(){for(;f=fopen("a","w");fclose(f))fputc(0,f);}   // 67 bytes

Explanation of the non-portable version:

The file is created with random garbage permissions. With gcc 5.2, with -O0 or -O3, it happens to include owner write permission, but this is not guaranteed. 0666 is decimal 438. A 3rd arg to open would take another 4 bytes. We're already hard-coding O_TRUNC and so on, but this could break with a different compiler or libc on the same ABI.

We can't omit the 2nd arg to open, because the garbage value happens to include O_EXCL, and O_TRUNC|O_APPEND, so open fails with EINVAL.


We don't need to save the return value from open(). We assume it's 3, because it always will be. Even if we start with fd 3 open, it will be closed after the first iteration. Worst-case, open keeps opening new fds until 3 is the last available file descriptor. So, up to the first 65531 write() calls could fail with EBADF, but will then work normally with every open creating fd = 3.

577 = 0x241 = O_WRONLY|O_CREAT|O_TRUNC on x86-64 Linux. Without O_TRUNC, the inode mod time and change time aren't updated, so a shorter arg isn't possible. O_TRUNC is still essential for the version that calls write to produce actual disk activity, not rewrite in place.

I see some answers that open("a",1). O_CREAT is required if a doesn't already exist. O_CREAT is defined as octal 0100 (64, 0x40) on Linux.


No resource leaks, so it can run forever. strace output:

open("a", O_WRONLY|O_CREAT|O_TRUNC, 03777762713526650) = 3
close(3)                                = 0
... repeating

or

open("a", O_WRONLY|O_CREAT|O_TRUNC, 01) = 3
write(3, "\0", 1)                       = 1   # This is the terminating 0 byte in the empty string we pass to write(2)
close(3)                                = 0

I got the decimal value of the open flags for this ABI using strace -eraw=open on my C++ version.

On a filesystem with the Linux lazytime mount option enabled, a change that only affects inode timestamps will only cause one write per 24 hours. With that mount option disabled, timestamp updating might be a viable way to wear out your SSD. (However, several other answers only do metadata I/O).


alternatives:

shorter non-working:

main(){for(;;)close(write(open("a",577),"",3));} uses write's return value to pass a 3 arg to close. It saves another byte, but doesn't work with gcc -O0 or -O3 on amd64. The garbage in the 3rd arg to open is different, and doesn't include write permission. a gets created the first time, but future iterations all fail with -EACCESS.

longer, working, with different system calls:

main(c){for(open("a",65);pwrite(3,"",1);)sync();} rewrites a byte in-place and calls sync() to sync all filesystems system-wide. This keeps the drive light lit up.

We don't care which byte, so we don't pass 4th arg to pwrite. Yay for sparse files:

$ ll -s a
300K -rwx-wx--- 1 peter peter 128T May 15 11:43 a

Writing one byte at an offset of ~128TiB led to xfs using 300kiB of space to hold the extent map, I guess. Don't try this on OS X with HFS+: IIRC, HFS+ doesn't support sparse files, so it will fill the disk.

XFS is a proper 64bit filesystem, supporting individual files up to 8 exabytes. i.e. 2^63-1, the maximum value off_t can hold.

strace output:

open("a", O_WRONLY|O_CREAT, 03777711166007270) = 3
pwrite(3, "\0", 1, 139989929353760)     = 1
sync()                                  = 0
pwrite(3, "\0", 1, 139989929380071)     = 1
sync()                                  = 0
...

Peter Cordes

Posted 2016-04-01T11:04:06.877

Reputation: 2 810

2

Racket, 46 bytes

(do()(#f)(write-to-file'a"f"#:exists'replace))

Winny

Posted 2016-04-01T11:04:06.877

Reputation: 1 120

1I was thinking of answering in Racket, but you beat me to it. :P – cat – 2016-04-03T02:49:16.073

Out of curiosity, did you find a shorter answer? – Winny – 2016-04-03T06:20:38.787

1

SmileBASIC, 12 bytes

SAVE"A
EXEC.

12Me21

Posted 2016-04-01T11:04:06.877

Reputation: 6 110

Doesn’t it run out of cassette tape though? – MathuSum Mut – 2019-01-01T10:18:07.347

1

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.

cat

Posted 2016-04-01T11:04:06.877

Reputation: 4 989

1

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

Psychonaut

Posted 2016-04-01T11:04:06.877

Reputation: 233

1Does it run out of cassette tape though? ;) – MathuSum Mut – 2016-04-04T10:18:54.410

1@MathuSumMut that would be 0 SAVE "A" : RUN – ceilingcat – 2017-02-25T22:39:14.030

1

Python, 32 bytes

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

Note that if run on python 3, this will produce an infinite number of warnings. Also, it will probably run out of fds if run in a non-refcounting implementation.

pppery

Posted 2016-04-01T11:04:06.877

Reputation: 3 987

Just as a note, a shorter answer exists without the write and the "w" part of the open command. – Rɪᴋᴇʀ – 2016-05-14T02:14:55.230

1

Dyalog APL 15.0, 17 bytes (non-competing)

(⊢⊣⊃⎕NPUT⊢)⍣≢'A'1

Chrome currently renders U+2262 wrong. The above line should look like (⊢⊣⊃⎕NPUT⊢)⍣̸≡'A'1.

This is non-competing because version 15 has not been released yet.

Applies the function (⊢⊣⊃⎕NPUT⊢) on 'A'1 until the input is changed (i.e. never):

⊢⊣⊃⎕NPUT⊢ is a function train:

┌─┼───┐      
⊢ ⊣ ┌─┼─────┐
    ⊃ ⎕NPUT ⊢

The rightmost returns 'A'1 unmodified; this (filename, overwrite-flag) will be the right argument to `⎕NPUT'.

'⊃' returns the first element of 'A'1 ('A'); this is the data to be written.

Then ⎕NPUT is run, and reports how many bytes were written (2 or 3 depending on OS); this becomes the right argument to the .

The leftmost again returns 'A'1 unmodified; this is the left argument to the .

ignores its right argument and returns the left argument ('A'1), this becomes the new value fed to .

Since the new value is identical to the old one, the operation is continued (forever).

Adám

Posted 2016-04-01T11:04:06.877

Reputation: 37 779

0

Tcl, 36 bytes

set f [open a w]
while 1 {puts $f .}

Try it online!

sergiol

Posted 2016-04-01T11:04:06.877

Reputation: 3 055

0

vim text editor, 10 bytes

qa:w<enter>@aq@a

8 bytes if you do not could the execution command @a

Radovan Garabík

Posted 2016-04-01T11:04:06.877

Reputation: 437

0

Lua, 39 bytes

while''do io.open('a',"w"):write('')end

Blab

Posted 2016-04-01T11:04:06.877

Reputation: 451

0

C++, 61 bytes. (or 66 bytes to actually hit the disk on Linux+XFS+lazytime)

Timestamp metadata I/O only:

#include <fstream>
int main(){for(;;){std::ofstream a("a");}}

The disk I/O is due to a's mod time changing, which will be written to disk occasionally. (very occasionally with the commonly-use lazytime mount option. Also see my C answer.)


Write data before closing:

#include <fstream>
int main(){for(;;){std::ofstream a("a");a<<4;}}

Actually hits the disk: lights up my hard drive light. (Linux 4.2.0, XFS, lazytime)


The ofstream constructor opens the file, and the destructor closes it again. There are no resource leaks, so this will actually run forever without running out of memory, file handles, or disk space.

strace output:

open("a", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 3
write(3, "4", 1)                        = 1      # not present with first version
close(3)                                = 0
... repeating

Peter Cordes

Posted 2016-04-01T11:04:06.877

Reputation: 2 810

0

C99, 84 bytes

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

If you don't care about buffers, you can do this instead:


C99, 65 bytes

#include<stdio.h>
int main(){while(1){fputc(9,fopen("a","w"));};}

I don't know if it closes automatically (no file handle), so please comment and I'll edit.


Lots of \ts to write (relax, they're not infinite!).

Erik the Outgolfer

Posted 2016-04-01T11:04:06.877

Reputation: 38 134