Skip to content

Question to the design of disk manager #57

@JigaoLuo

Description

@JigaoLuo

I would like to ask a simple question: Is disk manager in bustub thread safe?

I would try to give my answer: it is not thread safe.

  • reason 1: it has some int attributes.
  • reason 2: std::fstream is not thread safe, refering to C++ Standard

27.1.3 Thread safety [iostreams.thread-safety]

Concurrent access to a stream object [string.streams, file.streams], stream buffer object [stream.buffers], or C Library stream [c.files] by multiple threads may result in a data race [intro.multithread] unless otherwise specified [iostream.objects]. [Note: Data races result in undefined behavior [intro.multithread].


Following words are under the assumption, that disk manager is not thread safe. If the assumption is not the truth, please ignore the following words.

Then I would like to ask: why to design the disk manager not to be thread safe?

Yes. We can have a mutex outside of the disk manager doing the protection. In the Project 1, we should have a mutex to protect the disk manager inside of buffer manager.

There is a way maybe better than that: to use POXIS pread, pwrite to implement the ReadPage and WritePage functions. Since the page_id represents a unique part of a file and different page_ids share no overlap in file. POSIX pread, pwrite would be thread safe and easy to
be implemented. (Possible disadvantage: not modern C++.)

I am hoping and being grateful to see an answer. 😄


The motivation of the question:
I want to have low lock(mutex) granularity in my implementation: the buffer manager have to release it's global_mutex during ReadPage and WritePage these I/O operations. Then I came to this question and did some reading. I even have upload a version to gradescope and passed the concurrency test, in which the disk manager is not protected by any mutex.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions