News for hackers. Stuff that matters.
Re:The Programming Interview from Hell by kabdib on Monday February 20, 2017 @10:02AM (#13687807)
The hiring manager of a small software company gave me a quick brief before handing me off to his technical heavy. "He's hard to get along with, but he's really smart. Oh, and he has two PhDs. He'll tell you that."

I was ushered in. The Guy with Two PhDs (he showed me his business card first, and there were indeed two PhDs on it) asked me:

"What is the simplest way to synchronize two threads?"

I rattled off some synchronization primitives. Semaphore. Critical section. I was ready to do an implementation if he wanted.

"No, the simplest."

I dug around. Interlocked operations? A mutex? A spinlock? I mentioned Dekker's algorithm (it sucks, but it's simple). I dredged up a few more.

"No, I want the SIMPLEST possible way to synchronize two threads. What is it?"

I'd run out. He gave a disgusted snort. The next question wasn't much better: "What's the BEST way to share data between programs?"

"Not sure what you mean by best. How many programs? Is it over the network? Hmm, shared memory and a maybe a signal of some kind?"

"No, the best way!"

This interview did not go well. At the end, Dr. DoublePhd scolded me for dropping out of college and told me to go back to school to finish my degree.

That evening I wrote the hiring manager that Dr. DoublePhd appeared to want the answer "raise interrupt priority" for the thread synchronization question -- which doesn't work on a multiprocessor -- and that I had no idea what the heck he was asking for on the other questions.

I didn't add that even the awful questions I was being asked could have been productive interview fodder in the hands of a good interviewer, but that in the hands of a terrible person they were destroying that company's ability to hire.

Turns out that I didn't need to add that last bit. A few months later the hiring manager emailed me, saying that they had fired Dr. DoublePhd and would I consider interviewing again? I politely declined.

I never found out the BEST way to share data between programs. In fact, I'm still looking. I think we all are.

Re:The Programming Interview from Hell by harveywi on Monday February 20, 2017 @10:17AM (#13687928)
> I never found out the BEST way to share data between programs

Come on, add another layer of indirection! When given a problem to solve, hand back another problem.

In this case:

Q: "I want the SIMPLEST possible way to synchronize two threads. What is it?"

A: The SIMPLEST possible way to synchronize two threads is a global maximal element (not necessarily unique) of a poset induced by a partial ordering on possible methods for synchronizing two threads. If the induced poset is totally ordered, and assuming that at least one possibly way of synchronizing two threads exists, then there exists a SIMPLEST possible way to synchronize two threads. If you hand me a method for comparing two synchronization methods for SIMPLICITY, then an algorithm retrieving such a best possible solution is trivial, but at this point you must define define what you mean by SIMPLEST before we can move any further.

Mr. Double PhD was trying to be clever.

Re:The Programming Interview from Hell by phyushin on Monday February 20, 2017 @12:33PM (#13689127)
I think we've found Dr double Phd ;)
Re:The Programming Interview from Hell by mping on Monday February 20, 2017 @10:40AM (#13688136)
Just run one after the other.
Re:The Programming Interview from Hell by johndubchak on Monday February 20, 2017 @10:45AM (#13688174)
OMG...too funny.
Re:The Programming Interview from Hell by satysin on Monday February 20, 2017 @10:16AM (#13687912)
If someone asks you the "best" way to do something but does not give you context for the problem being solved then the question is stupid. There is no all-purpose "best" way to do anything when it comes to complex systems. You use the "best" way for the problem in hand that may be shared memory, pipes, writing to a file, RPC, etc.
Re:The Programming Interview from Hell by harveywi on Monday February 20, 2017 @10:22AM (#13687973)
Exactly. When handed an under-specified problem (semantics), hand back a lambda compiled in the target language (syntax). "What do you mean? African or European?"
Re:The Programming Interview from Hell by kabdib on Monday February 20, 2017 @10:42PM (#13693104)
Upvote for "hand back a lambda". I was laughing.

"Here, run this unsigned code for me in your head." That has all kinds of possibilities . . .

[See John Barnes' book _Candle_ if you want some scary prognostication. Warning, it's kind of graphic and disturbing in spots]

Re:The Programming Interview from Hell by havetocharge on Monday February 20, 2017 @10:31AM (#13688052)
Well put. If there was the best way, then it would be the only way.
Re:The Programming Interview from Hell by kazinator on Monday February 20, 2017 @02:30PM (#13690163)
"Raise interrupt priority" is not a way to synchronize threads, let alone the simplest. I know what I'm talking about; I worked on multithreading for Linux before the rewrite to NPTL and I invented the PTHREAD_MUTEX_ADAPTIVE_NP type that you still find in Glibc. On a project several years ago, I did use interrupt priority in conjunction with threads as a complete hack. The issue was this: the interrupt service routines for an IDE-based compact flash were running CPU intensive polling loops, taking away app CPU time. I created a priority scheme where certain interrupt priority levels overlapped with thread priority levels. That is, it was possible to suppress the interrupts for this IDE device while certain important threads were dispatched. As in, automatically: when the kernel dispatched a thread in that priority range, interrupts below that value would be blocked (not by disabling CPU interrupts, but the actual interrupt controller's detailed mask of interrupts, where we pick the specific ones that get masked). When the thread lost the CPU, they would be unblocked. This solution/workaround worked well enough to deal with the issue it was intended for.
Re:The Programming Interview from Hell by kabdib on Monday February 20, 2017 @10:47PM (#13693123)
You can raise IRQL and get okay synchronization on simple systems. It was fine for early versions of Unix, and I've used it quite a bit on uniprocessor embedded systems. Just a few instructions, hard to get wrong.

Of course it fails if you're working in user mode. Or you have non-maskable interrupts that you need to sync with.

In this case the poor PhD x 2 had been working for years on consumer PCs, which were at the time nearly all single CPU. I'd been mucking about with multiprocessing a fair amount, and the IRQL thing never even occurred to me during the interview. Just as well.

Re:The Programming Interview from Hell by kazinator on Monday February 20, 2017 @11:52PM (#13693363)
Early versions of Unix were co-operative. While a task executed kernel code, it was not preemptable. That might be the simplest thread synchronization. Masking interrupts is then just for exclusion between the one and only kernel thread, and interrupt context.
Re:The Programming Interview from Hell by kabdib on Tuesday February 21, 2017 @03:26PM (#13699546)
That's true.
Re:The Programming Interview from Hell by zhte415 on Monday February 20, 2017 @10:33AM (#13688071)
> I never found out the BEST way to share data between programs. In fact, I'm still looking. I think we all are.

Copy-paste.

Re:The Programming Interview from Hell by Ntrails on Monday February 20, 2017 @10:37AM (#13688106)
See, I was gonna go with Data.txt in a shared network drive.

Btw = if you're worried about conflicts - it's Ok there's a second file called "writeStatus.txt" which you have to claim by replacing the String NULL with your processId - thereby claiming write access to data.txt and causing any other processes to Thread.Sleep until it's free.

Unrelated, who's hiring...

Re:The Programming Interview from Hell by willis77 on Monday February 20, 2017 @10:50AM (#13688214)
But who has access to writeStatus.txt? Classic rookie mistake. You needed a writeStatusWriteStatus.txt.
Re:The Programming Interview from Hell by btschaegg on Tuesday February 21, 2017 @06:35AM (#13694828)
Sounds like playing Byzantine Generals on the file system :)
Re:The Programming Interview from Hell by zhte415 on Monday February 20, 2017 @10:52AM (#13688236)
Flat files FTW, Just need to re-invent server-client architecture using freely available tools and you're at management level kudos,
Re:The Programming Interview from Hell by TuringTest on Monday February 20, 2017 @12:16PM (#13688957)
>> I never found out the BEST way to share data between programs. In fact, I'm still looking. I think we all are.

>Copy-paste.

You may joke, but it's the most universally supported, and therefore the most likely to be available.

Re:The Programming Interview from Hell by TheGrassyKnoll on Monday February 20, 2017 @01:20PM (#13689564)
For Linux users its gotta be the pipe. Works great.
Re:The Programming Interview from Hell by StreamBright on Wednesday February 22, 2017 @03:13AM (#13704022)
You guys... Forgot about email...
Re:The Programming Interview from Hell by llukas on Monday February 20, 2017 @04:59PM (#13691373)
My guess would be that simplest way to sync two threads is exit them both. ;)
Re:The Programming Interview from Hell by btschaegg on Tuesday February 21, 2017 @06:36AM (#13694834)
...or not running them in the first place.