1

I was asked this during an interview, and I wasn't able to come up with an answer.

The idea is to write a program using only the C standard library, and find out how many CPU cores the box has. The solution needs to be platform independent, and using external libraries or system calls is not allowed.

Does anybody have experience with this? Thanks!

menphix
  • 309
  • 4
  • 12
  • 12
    C doesn't know what a CPU is. – R. Martinho Fernandes Jun 09 '13 at 22:13
  • 4
    There is no such functionality in the standard library. So I guess the answer to your interview question should have been "that's not possible". – Oliver Charlesworth Jun 09 '13 at 22:13
  • If I asked this as an interview question, the correct response I would be looking for is an explanation of why and how this can not be done as stated. – thecoshman Jun 09 '13 at 22:15
  • This is impossible to do in a platform-independent manner. The language specification for C (and C++, by the way) speaks only of an "abstract machine", with no mention of CPU cores. Therefore, if you want to talk about CPU cores, you have to go outside the standard C libraries. – In silico Jun 09 '13 at 22:16
  • Well, the standard library includes `popen` whith which you could run a system dependent command-line to get the answer... 'Course there is no reliable way to learn what system your on. – dmckee --- ex-moderator kitten Jun 09 '13 at 22:16
  • @dmckee `popen` is from POSIX. C has `system`, though. However, given that it returns an implementation defined value, there's no way you can get a meaningful result out of it unless you know your implementation. – R. Martinho Fernandes Jun 09 '13 at 22:18
  • @Insilico: You're wrong there. C++ has std::thread::hardware_concurrency. This, of course, does not exactly correspond, but it could be construed as "close". C may have something similar. – Puppy Jun 09 '13 at 22:19
  • 3
    The other comments are correct, they are no sure way of determining this. But they might have been looking for a program where you would spawn threads and see how many are running concurrently. This is do-able on an idle-ish machine. But it is shaky. – Guillaume Jun 09 '13 at 22:20
  • @DeadMG: It's certainly the closest, and depending on what the OP's interviewer is looking for, could be what they want. But even then, it only gives the "number of concurrent threads supported", which may or may not correspond to number of physical CPU cores. On the other hand, perhaps the interviewer doesn't care about the physical/logical CPU distinction, who knows. – In silico Jun 09 '13 at 22:21
  • @DeadMG that's not the number of cores, and even if it was, it is merely a hint. – R. Martinho Fernandes Jun 09 '13 at 22:21
  • 1
    Yes, I'm not saying that it is the number of cores or an exact answer. All I am saying is that saying that C++ has no notion of this isn't really correct. – Puppy Jun 09 '13 at 22:22
  • 1
    @DeadMG: I'll grant you that. However, as far as I can see through the C11 standard, there is no C equivalent for `std::thread::hardware_concurrency` that C++ has, which is I presume is what the OP's interviewer is looking for. – In silico Jun 09 '13 at 22:29
  • You can use an assembler for your type of CPU. Like this: http://stackoverflow.com/questions/2901694/ – ntfs.hard Jun 10 '13 at 00:04
  • Even still, there's no way with C11 threads to measure concurrency. – R.. GitHub STOP HELPING ICE Jun 10 '13 at 00:42
  • @Guillaume That was the answer I told them, something like "spawn multiple threads and measure when the performance starts to drop". Then they asked what if there's no way to measure the performance of the system. – menphix Jun 10 '13 at 03:11

1 Answers1

-1

Thanks everyone who participated. I think now it seems like the answer will be "there's no reliable way of doing this." Great discussions guys!

menphix
  • 309
  • 4
  • 12