Interview Question






Comment hidden because of low score. Click to expand.
1
of 1 vote

It depends on the implementation. There are some guarantees, but the implementation is not forced to use a mutex.

The C standard I/O library (stdio) documentation states that each call to printf(), scanf(), getchar(), putchar(), and any other stdio functions behave as if a call to flockfile(3) was made before invoking the specified function and a call to funlockfile(3) was made after invocation.

So, stdio is thread safe, as long as the threads are well behaved and use the accepted, known interface to deal with file streams - the stdio library.

The same guarantees apply to C++, but note that things like

cout << "Hello" << " World";

is actually 2 calls, so this is not atomic.

Note, however, that stdio and any I/O library uses the read(2) and write(2) syscalls under the hood, which are not atomic. If a misbehaving process neglects to use stdio and starts reading and writing with read(2) and write(2), then things can start to break even if everyone else uses stdio to manipulate the file.

So, in short, as long as you use the library, you are mostly safe, but an important point to mention to the interviewer is that the exact implementation details of how stdio manages locking are very platform specific and don't necessarily rely on a mutex.

- Anonymous July 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Got it,
Thanks for your detailed explanation. :)

- Enkesh Gupta July 10, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

It depends on the implementation. There are some guarantees, but the implementation is not forced to use a mutex.

The C standard I/O library (stdio) documentation states that each call to printf(), scanf(), getchar(), putchar(), and any other stdio functions behave as if a call to flockfile(3) was made before invoking the specified function and a call to funlockfile(3) was made after invocation.

So, stdio is thread safe, as long as the threads are well behaved and use the accepted, known interface to deal with file streams - the stdio library.

The same guarantees apply to C++, but note that things like

cout << "Hello" << " World";

is actually 2 calls, so this is not atomic.

Note, however, that stdio and any I/O library uses the read(2) and write(2) syscalls under the hood, which are not atomic. If a misbehaving process neglects to use stdio and starts reading and writing with read(2) and write(2), then things can start to break even if everyone else uses stdio to manipulate the file.

So, in short, as long as you use the library, you are mostly safe, but an important point to mention to the interviewer is that the exact implementation details of how stdio manages locking are very platform specific and don't necessarily rely on a mutex.

- 010010.bin July 09, 2015 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More