Live data from Hacker News

How to programmatically find out if computer is on

haiku-os.org

61–70 of 197 posts

Re: How to programmatically find out if computer is on

#61
post #51

I’d like to see the unit tests for the non-true return state.

It's actually pretty straightforward: turn_computer_off(); Assert(!is_computer_on(), "Computer should have been off but it's on."); The opposite is a bit tricky, though.

This assert doesn't work like you think it does. If the computer is off is_computer_on is undefined, so if this is called when the computer is already off it could return a nonzero value, thus resolving to true even though the computer is already off.

Re: How to programmatically find out if computer is on

#65
post #56

Earlier quoted context omitted.

Hmm, it seems like you need to make sure the computer is on before calling this function, or else you run the risk of getting back undefined data. Seems like we also need a function that can be used to tell if the computer is on...

There's a syscall to check whether the computer is on so just call that before this and you'll be fine

That introduces a time-of-check to time-of-use bug (https://en.wikipedia.org/wiki/Time-of-check_to_time-of-use).

Re: How to programmatically find out if computer is on

#67
post #4

Where does the code run when the computer is off? Edit: I see the downvotes, yet i still don't know if it's a joke. I was genuinely asking the question.

Genuine answer: the code cant run when the computer is off, thus the joke. The question is worth asking (all are) but then soliciting "quantum computing" as an answer makes it a doubly worthy set up line, so thanks twice.

When I first read the headline, I assumed this was some kind of query to the Intel Management Engine. But in this context, it's some good programming humor.

Re: How to programmatically find out if computer is on

#69
post #22

> If the computer isn't on, the value returned by this function is undefined. Shouldn't it just block until the computer turns back on? Probably interferes with pthreads.

Seems like a reasonable compiler would inline the most likely implementation as 1. So, while technically undefined, it's safe to assume that it will 'return' 1 regardless of the actual on/off state of the computer.

I would be unreasonable. The power state appears to me as highly asynchronous to program execution. I see no explicit guarantees on how the state is retrieved or synchronized, without synchronization the state wont be properly updated. We know that the computer was of at some point, so it starts of as undefined. Given undefined as starting state and the lack of update guarantee we can assume that it will always return an undefined value. This opens up a lot more optimization possibilities than assuming a 1.

Re: How to programmatically find out if computer is on

#70
> is_computer_on()

How does this relate to computer being in Halt state, which is quite valid state for servers (there could be even a dedicated Halt button)? Technically computer hardware is ON. I remember that in such state it shows a "carrot" prompt (>>>) from the SRE console.

By the way, there is also HCF instruction (Halt and Catch Fire).

https://en.wikipedia.org/wiki/Halt_and_Catch_Fire_(computing...

Post reply on HN