Oren Eini

CEO of RavenDB

a NoSQL Open Source Document Database

Get in touch with me:

oren@ravendb.net +972 52-548-6969

Posts: 7,546
|
Comments: 51,163
Privacy Policy · Terms
filter by tags archive
time to read 1 min | 178 words

What would be the output of the following code?

var list = new List<Task>();

var fileStream = File.Open("test", FileMode.Create, FileAccess.ReadWrite, FileShare.None);
for (int i = 0; i < 150; i++)
{
    var index = i;
    var task = Task.Factory.StartNew(() =>
    {
        var buffer = Encoding.ASCII.GetBytes(index + "\r\n");
        fileStream.Write(buffer, 0, buffer.Length);
    });
    list.Add(task);
}


Task.WaitAll(list.ToArray());
fileStream.Flush(true);
fileStream.Dispose();

Please note that:

  • What order the data is saved is not important.
  • Preventing data corruption is important.

Can you guess?

time to read 1 min | 108 words

What would be the result of this?

public string Wrap(string x, string y)
{
   return "[" + x ?? y + "]";
}

Can you see the bug?

time to read 1 min | 200 words

What would you expect the following code to output?

File.WriteAllText("a","1");
DirectoryBackup.CreateHardLink("b", "a", IntPtr.Zero);
File.WriteAllText("a","2");
if(File.Exists("c"))
    File.Delete("c");
File.Move("b","c");
File.WriteAllText("a", "3");
Console.WriteLine(File.ReadAllText("c")); 

CreateHardLink is:

[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern bool CreateHardLink(string lpFileName, string lpExistingFileName, IntPtr lpSecurityAttributes);

FUTURE POSTS

  1. Partial writes, IO_Uring and safety - one day from now
  2. Configuration values & Escape hatches - 4 days from now
  3. What happens when a sparse file allocation fails? - 6 days from now
  4. NTFS has an emergency stash of disk space - 8 days from now
  5. Challenge: Giving file system developer ulcer - 11 days from now

And 4 more posts are pending...

There are posts all the way to Feb 17, 2025

RECENT SERIES

  1. Challenge (77):
    20 Jan 2025 - What does this code do?
  2. Answer (13):
    22 Jan 2025 - What does this code do?
  3. Production post-mortem (2):
    17 Jan 2025 - Inspecting ourselves to death
  4. Performance discovery (2):
    10 Jan 2025 - IOPS vs. IOPS
View all series

Syndication

Main feed Feed Stats
Comments feed   Comments Feed Stats
}