15 ms·
How to programmatically find out if computer is on
- largbae 5y agoThis reminds me of http://www.hasthelargehadroncolliderdestroyedtheworldyet.com/ http://www.hasthelargehadroncolliderdestroyedtheworldyet.com...
- DubiousPusher 5y agoI have recently wondered if there is a way to guarantee that a processor is idling. By this I mean, is there a way to ensure another computer has been idle for a time when it promises that it has been. Kind of the opposite of a crypto mining problem.
- rowanG077 5y agointegrate power consumption seems like an easy answer.
- h2odragon 5y ago> 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.
- eminence32 5y agoHmm, 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...
- salawat 5y agoNah. Just bump that responsibility to the caller, and power supply manufacturer. Have the caller call power_on_computer() first always. This will ensure you always get defined state! ...What do you mean not every power supply manufacturer implements the power_on_computer() function as a NOOP if the computer is already on?!
- h2odragon 5y agoSome implement it that way, some it returns and error message or throws an NMI; then ya got the cheap clones that use it to frob a toggle without regard to the state it was in (which there's no way to read)... Then ya got the weird ones (HP) where it only works right if you call it twice with a 40ms delay between while streaming a sample from "Swan Lake" over their NDA'd 1wire thingy.
- marcodiego 5y agoI know that at least Solaris implemented SIGPWR signal to inform programs of a power failure: https://community.bmc.com/s/article/Inquira-KA334556 https://community.bmc.com/s/article/Inquira-KA334556
- marcodiego 5y agoTIL: linux also implements SIGPWR. Using "kill -PWR `pidof dd`" killed it with "power failure".
- ancode 5y agoThere's a syscall to check whether the computer is on so just call that before this and you'll be fine
- Someone 5y agoThat introduces a time-of-check to time-of-use bug (https://en.wikipedia.org/wiki/Time-of-check_to_time-of-use https://en.wikipedia.org/wiki/Time-of-check_to_time-of-use).
- nvader 5y agoAh, good ole TOCTOU
- waterhouse 5y agoAha. If only the API was "Execute a user-provided function in a context where the computer is guaranteed to be on".
- bryan_w 5y agoYou need to halt this thinking at once
- jcwayne 5y agoSeems 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.
- jeremyjh 5y agoI agree on the implementation, but I'm pretty sure if the computer is off you aren't getting a 1.
- yholio 5y ago{return 1;} is clearly a faulty implementation, it's entirely possible that the computer is already turned off by the time the function is executed using power stored in the decoupling capacitors of the CPU.
- azundo 5y agoSince the behavior is undefined when the computer is off this implementation seems correct in your hypothetical as well.
- josefx 5y agoI 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.
- Andy_G11 5y agoReally have to object to manuals of this kind - the B_INFINITE_TIMEOUT() function - see the miscellaneous functions link at the top of the page - seems to have been adopted by too many automated call centres.
- IndexCardBox 5y agoI like how it also has support for the ENIAC platform. I kicked my family out of the house so I had room to install the ENIAC. Happy to hear there's an OS I can run on it to get real work done, like posting unfunny jokes for strangers on Hacker News.
- wk_end 5y agoIt upsets me that this function's interface allows it to describe silly things like "an ENIAC running on a 68000" or "an Apple II running on SPARC". Wouldn't be an issue if C++ supported GADTs.
- msla 5y agoAn Apple II style of computer built around a SPARC would be a fascinating hobbyist platform.
- DonHopkins 5y agoAsynchronous JavaScript browser DOM callback API: <html onon="alert('Computer turned on!')" onoff="alert('Computer turned off!')" >
- kjhughes 5y agoAsynchronous is definitely the way to go. I once tried synchronous code. Had to move it to a service worker so as to not bog down the UI, and you don't even want to know what service workers do when the computer is off. Let's just say it's undocumented.
- wizzwizz4 5y agoXP's behaviour is interesting in this case. It's one of the few things I actually like about Vista (sue me, I like things about Vista): Vista will abort the shutdown, but XP will hang for an unspecified length of time, when you have an event handler that makes a blocking call like `alert`.
- deleted 5y ago[deleted]
- sharmin123 5y agoAvoid Hacking Threats: Hacking Risk You Face With Gadgets: https://www.hackerslist.co/avoid-hacking-threats-hacking-risk-you-face-with-gadgets/ https://www.hackerslist.co/avoid-hacking-threats-hacking-ris...
- firefoxd 5y agoWhere 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.
- rock_artist 5y agois_computer_on_fire() seems more usable...
- makepanic 5y agoweird how it's returning a constant https://github.com/haiku/haiku/blob/8f16317a5b6db5c672f331814273e5857555020f/src/system/libroot/os/system_info.cpp#L195 https://github.com/haiku/haiku/blob/8f16317a5b6db5c672f33181... Interesting it seems to come from BeOS: https://github.com/haiku/haiku/commit/bcf475ec65f1060c0a8abf2cd408be4adb79cf9a https://github.com/haiku/haiku/commit/bcf475ec65f1060c0a8abf... where the same function had a hn thread about it already a couple years ago :D https://news.ycombinator.com/item?id=8780606 https://news.ycombinator.com/item?id=8780606
- salawat 5y agoNot 451. I'm disappointed at the missed opportunity.
- lucb1e 5y agoI can find various results where that number is negative (Faltings height of LMFDB elliptic curve 1688.c1; north east longitude of el Baix Segura) but not where the number is positive. It's part of a DOI string though, but that's not really a number. Probably I'm being nerd sniped. https://xkcd.com/356/ https://xkcd.com/356/
- speed_spread 5y agoDoc doesn't specify the unit of measure used. Also would be nice to know if fire was caused by computer itself or from an external cause.
- thoughtsimple 5y agoWhere is the function is_halting() ?
- yjftsjthsd-h 5y agoRight? Especially since the implementation is so simple, and even runs in O(1) time:) https://xkcd.com/1266/ https://xkcd.com/1266/
- marcodiego 5y agoAlthough many people are laughing, I believe the function works ok. The only complicated part is to find a way to call it when the computer is not on.
- someonewhocar3s 5y agoIf you take the memory unit, and attach it to another computer, does >interpreting< the code count as running the software on the new computer? I suppose it would. So perhaps a biocomputer of sorts could be offline and return undefined behavior by spec. Guess we'll keep looking for that perfect OS code.
- guerrilla 5y agoEasy peasy, just call it from a different computer.
- YXNjaGVyZWdlbgo 5y agoI know it's not as common anymore but if the computer is in a "hold and catch fire" incident it won't report temperature sensor readings. What happens? Is it considered off?
- 13of40 5y agoOdd they have a TI 99/4a platform but no TMS990 CPU.
- rcakebread 5y agoI was expecting this to be about using IPMI on a BMC to see if the motherboard is powered on.
- Waterluvian 5y agoI’d like to see the unit tests for the non-true return state.
- 1_player 5y agoResult is undefined if computer isn't on, so any value is OK, no tests necessary.
- OJFord 5y agoGP said for the 'non-true state', not the 'computer off state'. I.e. verify that the computer is off, given that the fn returns false (which is valid only as part of its undefined computer-off response).
- chizhik-pyzhik 5y agoI think that would have to be an integration test
- 13of40 5y agoIt'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.
- deleted 5y ago[deleted]
- ancode 5y agoThis 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.
- kjhughes 5y agoFunctional programming with monads is the right way to implement this.
- marcosdumay 5y agoIt's obviously `IO Bool`, so it's equivalent to directly evaluating in an imperative language and you gain nothing from the monadic interface. I'm sure some impurity "pragmatist" will try to argue you can make a perfectly good version of it as `Bool`. That kind of pragmatism is better ignored because it interferes with compiler optimization and standardization. As an example, you can look at the recent discussion around `==` and `/=`. (And I just noticed some people may seriously argue that, so here comes the /s.)
- perihelions 5y agoThere's an elegant FP solution that involves searching an infinite data structure using only a finite amount of computation -- actually, with precisely zero computation, because the machine isn't on. It's like lazy evaluation, only more lazy.
- h2odragon 5y agoWait. When the machine isn't on, P=NP! So our security systems are (useless | perfect)! I think there might even be other implications...
- MikeTheGreat 5y agoNice job - having shown that P is the same as NP factorial will allow us to solve a lot of really hard problems!
- rezonant 5y agoGolf clap.
- marcosdumay 5y ago
- praptak 5y agoSee also isUserAMonkey and isUserAGoat. I think the following always holds: isUserAGoat==!isComputerOn, in other words the user is a goat if and only if the computer is off.
- omgwtf1000 5y agoKeeps timing out.
- rossdavidh 5y agoJavascript equivalent function is kind of wonky so they had to make is_the_computer_really_on() to do what you want here.
- finnh 5y agothere's a convenient npm package to handle that for you
- h2odragon 5y agoYeah but you never know whose computer the result applies to
- psyc 5y agoAnd a separate package by the same prolific author for not_on that depends on the first package.
- tobyhinloopen 5y agoYou really should use onoff.js, which is a wrapper that is more actively maintained and has a typescript support
- deleted 5y ago[deleted]
- salawat 5y agoI posit a much more interesting question. Given a lump of silicon, how does one programmatically tell if it is a computer?
- h2odragon 5y ago`rock.exists()`? see also the last line of most "weather rock" forecasting stations.
- zatarc 5y agoI think the is_computer_on_fire function is far more useful
- syngrog66 5y agowrite a function whose sig is like "boolean isOn()". call it. if it returns true, the computer is on. if it returns false, the computer is also on. done. ship it! add a // TODO in v2: handle case if it never returns, or the process crashes, or caller times out waiting
- a-dub 5y agowoah! i had no idea beos lived on in an oss project!
- sneeeeeed 5y agoIt works great on my Thinkpad. I run it on the metal.
- capableweb 5y agoInteresting, but I think parent was asking how to run it on silicon.
- a-dub 5y agonow i'm confused.
- capableweb 5y ago"On metal" would refer to running the OS without any additional layers like virtualization, in this context. But it could also refer to the material "metal" (or the collection name for bunch of materials really), whereas computers are commonly known for being made with a lot of silicon instead of metal. Hopefully I didn't dissect too much of the frog to kill it.
- kazinator 5y ago// power cycle computer bool was_on = set_computer_on(true); // returns prior value if (was_on) { set_computer_on(false); set_computer_on(true); } else { // was previously off, is now on: nothing to do! }
- rudian 5y agoHave you tried turning it off and on again?
- DeathArrow 5y agoIn Soviet Russia the computer checks you to see if you are on.
- deleted 5y ago[deleted]
- lifthrasiir 5y agoWhile it is clearly a joke, it has a legitimate use as a syscall that does nothing so can be used to test the syscall process itself [1]. (Its cousin `is_computer_on_fire` is not a syscall.) [1] https://github.com/haiku/haiku/commit/ccaac177f64038c160de6f22f99d472c96e6ac7d https://github.com/haiku/haiku/commit/ccaac177f64038c160de6f...
- tialaramex 5y agoSure. Of course, one important trick here is that no matter how fast your system call is, not making a system call is just faster. io_uring is an example, as are futexes. So it's worth spending more effort on figuring out tricks to avoid doing the system call than exploiting tricks to shave a cycle or two off the time taken to execute one.
- spicybright 5y agoCompletely depends on your use case. If you're checking if sys calls work, you'd only need to run one first and see if it succeeds. Then you can be reasonably sure you have access to the rest. And different programs have different speed requirements.
- draw_down 5y agoI suppose not checking whether syscalls work would indeed be faster than checking whether they work...
- jrumbut 5y agoI was thinking the same thing, although in reverse: Imagine putting a few of these in as a joke and then you take it out and your parallelized code doesn't work anymore. Not sure how all that's handled on Haiku specifically.
- marcodiego 5y agohttps://xkcd.com/1172/ https://xkcd.com/1172/
- 5y ago
- dottrap 5y agoSince the not-on result is 'undefined', a devilish implementer could return 1 for the off case, just to screw with people. (How could this work in theory? Maybe the computer has a separate watchdog controller board which allows the computer to be turned on and off remotely, and that controller acts as a proxy for the API request.)
- rezonant 5y agoOnly if the watchdog also has a CPU that can run programs when the "real" CPU is off. But then we'd need a is_watchdog_on() to know if that one is powered on, in the case where the wall plug has been pulled.
- yccs27 5y agoMaybe we can forbid the function from returning 1 in the off case. The existence of such a function means that "is this machine running?" is semidecidable - but it's surely not decidable by the machine itself.
- FartyMcFarter 5y agoPresumably there's a non-joke version of this at some level in the hardware / firmware stack?
- rezonant 5y agohttps://docs.microsoft.com/en-us/windows/win32/power/system-power-states https://docs.microsoft.com/en-us/windows/win32/power/system-...
- kreeben 5y agoI read "Soft Off" as "sort of" and though, well, that's probably the best answer one can get in certain situations.
- seoaeu 5y agoYes, the ‘ipmitool power status’ command lets you query a server over the network to see if it is on
- atkailash 5y agoI love it when this sort of just fun random thing is thrown into code.
- zoomablemind 5y ago> 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) https://en.wikipedia.org/wiki/Halt_and_Catch_Fire_(computing...
- p_l 5y agoI admire for how you nerdsniped several other comments without anyone noticing the platform, or how "halt" would drop you into console monitor on said platform. My standing ovation to you :D (The platform is Alpha, right? Given the prompt and name for the console, though it was SRM, not SRE) :)
- zoomablemind 5y agoSRM it is. Thanks! (Alpha) System Reference Manual: https://en.wikipedia.org/wiki/System_Reference_Manual https://en.wikipedia.org/wiki/System_Reference_Manual Funny thing, for some historic reason, the system terminal attached to cabinet with those memorable Alpha Servers was of the monochrome lineage, but the phosphors color was orange. So the >>> prompt was unmistakably a carrot one! However it would rarely be seen, as bulk of the time the console would busily beep and boop with operator messages, as any respectful Alphas would be on 24/7/365 duty. The only forced interruption I witnessed was when the attached storage controller froze. Literally. Someone left a window open in the server room, with -20C outdoors. So with all that circulation there was ICE accumulation on one side of storage controler cabinet. Apparently that put it beyond service temp range. Well, thawed it out - back to bizznizz, so to speak. It that case, should've been is_system_iced() system call, it could've saved us the trip on such a freezing night. We would've just called HCF... briefly :)
- p_l 5y agoOrange, or more specifically Amber, was one of the popular options for monochromatic terminals from Digital and not only. Personally I had an amber VT510 attached to my AS255/233, and that one was essentially a budget model (effectively a slightly upgraded VT220 with PS/2 keyboard) and was apparently common choice for system console with Alphas. Unfortunately from what I heard default text mode on colour displays when using them instead of serial console was apparently white-on-blue (familiar from winnt BSOD among other things). Interestingly enough, there are few systems in Alpha lineage that have what's known as "external console", meaning that instead of running SRM on the normal CPUs, they had separate frontend processor and main cpus only had shims to communicate with it. Marvel was one of those, but apparently there were older ones as well. Also, if you had partition-capable Alpha, the carrot prompt would be made into Pxx> where xx referred to currently selected cpu. With VMS, you could then boot into "VMS Galaxy" mode where you had multiple VMS systems running without hypervisor, just with all of them carefully avoiding stepping on each others' resources.
- AnimalMuppet 5y agoI was working on an embedded system. I told the hardware guy that the CPU clock input wasn't running. He said, "Any reasonably intelligent software should have detected that condition and handled it." I replied, "Seems to me the software acted appropriately for the conditions."
- h2odragon 5y agoI tried inverting the clock and running a one way hash and my CPU vanished...
- BurningFrog 5y agoDescartes for the digital age: "I compute, therefore I am on"
- aequitas 5y agoReminds me of 'onstat' in Brendan Gregg's special tools: https://www.brendangregg.com/specials.html https://www.brendangregg.com/specials.html
- dkga 5y ago"How to find out computer is on fire"
- corpMaverick 5y agoParaphrasing Descartes. "I think, therefore I am on".
- cesarb 5y agoWhile this is a joke, there are some contexts in which it does make sense to ask whether the computer is on. For instance, most laptops have an embedded controller which runs all the time, even when the laptop is powered off, and manages things like battery charging and the power state. Within that embedded controller, an is_computer_on() function could legitimately return "false".
- capableweb 5y agoYeah, would make sense in that case. Probably would name the function differently though, to indicate the difference. "is_computer_on" makes me think it's checking the state of the computer this code is running on, not the state of another computer than "myself".
- lost_soul 5y agoI've often wondered why WOL is so poorly designed. WOL should be a function of the NIC, completely independent of the OS and capable of returning status codes even if the computer is "off".
- xyzzy_plugh 5y agoIs... this not how your WoL works? Hitting the NIC is equivalent to pressing the on button, boots the OS, etc. I've been using this for years to wake hosts at home when I'm abroad. It's invaluable.
- derefr 5y agoI think the parent means that you should be able to talk to the NIC about the computer, without the NIC needing to get the CPU powered on so that the CPU can tell the NIC what to do. Like a very-minimally-functional BMC.
- hackmiester 5y agoIntel vPro?
- mare5x 5y agoSimilar situation: a process queries the operating system (CPU scheduler) to find out if the process is currently being executed on the CPU.
- slmjkdbtl 5y agoI'm curious if there's any significance in number 0.63739 in this implementation double is_computer_on_fire(void) { return 0.63739; } https://github.com/haiku/haiku/blob/8f16317a5b6db5c672f331814273e5857555020f/src/system/libroot/os/system_info.cpp#L192-L196 https://github.com/haiku/haiku/blob/8f16317a5b6db5c672f33181...
- smallstepforman 5y agoWhen you convert float->hex->int->unix time, you get Sun Jul 27 2003 01:33:50 GMT+0000, which kind or corresponds to when the function might gave been implemented in Haiku. But if it matches BeOS R5, then the number may have another significance.
- raldi 5y agoDoesn’t seem to be a date or notable postal code
- ericye16 5y agoMy first thought is that it looked close to 1-e^-1, but that doesn't seem to be it.
- I_complete_me 5y agoClosest I came was sqrt(3)/e = 0.637185
- ummonk 5y agoI want to build a computer than can run in supercooled conditions, to prove that implementation has a bug.
- batch12 5y agoDunno. Referencing a US telephone keypad, it spells NERDY.
- Animats 5y agoWhat's so amusing today is that "off" tends to be an illusion today. It's more like "sleeping". There's still some stuff running. Listening for the On button, Wake on LAN, timers, the system management engine...
- rickreynoldssf 5y agoLots of weed hand grown by Jean Loius-Gasse was smoked in the offices of Be back in the day...
- deltasixeight 5y agohttp://www.ismycomputeron.com http://www.ismycomputeron.com
- CursedUrn 5y agoIs that talking about my computer or his computer?
- deltasixeight 5y agoYour computer. It hacks your computer to find out the state.
- gpvos 5y agoAh yes, the annoying Windows message "Do not turn off your computer" when it is running updates. Why is it telling me what to do with another computer? And I can turn it off since it isn't mine?
- pokot0 5y agoHow do you know you are on (exist)? This is the equivalent for computers trying to become sentient.
- Krisjohn 5y agoboolean did_customer_cause_fire(); Returns TRUE.
- poindontcare 5y ago"Returns 1 if the computer is on. If the computer isn't on, the value returned by this function is undefined."
- charcircuit 5y agoThese functions are useless since there's no way to know if the function "fails." If the computer is on it returns 1, but if it's off it can also return 1. So if it returns a 1 you don't actually know if it's on (unless you happen to know from somewhere else that the computer is on which at that point why bother calling this function)
- keypusher 5y agoWhy would is_computer_on_fire() return a double and not true/false? Even for a joke function, this seems like bad practice. If the function is named is_state(), I expect a boolean return. > Returns the temperature of the motherboard if the computer is currently on fire. Smoldering doesn't count. If the computer isn't on fire, the function returns some other value.
- rschachte 5y agoMaybe it’s a probability function. Something like “hey there’s a .7 percent chance your computer is engulfed in flames”
- Bost 5y agoConstant probability function. (Please be precise!)
- SamuelAdams 5y agoWonder if this is a throwback to the HCF command. https://en.wikipedia.org/wiki/Halt_and_Catch_Fire_(computing https://en.wikipedia.org/wiki/Halt_and_Catch_Fire_(computing...
- stffrdhrn 5y agoI was hoping this would be implemented by actually reading a memory location that was set to 0, when the system is hibernating/sleeping. But it always returns 1. If it was reading a flag from memory it would be useful when examining memory dumps. https://github.com/haiku/haiku/blob/8f16317a5b6db5c672f331814273e5857555020f/src/system/kernel/syscalls.cpp#L184 https://github.com/haiku/haiku/blob/8f16317a5b6db5c672f33181...
- suvakov 5y ago"Returns 1 if the computer is on. If the computer isn't on, the value returned by this function is undefined." Better description would be: Returns 1 if the computer is on. If the computer isn't on, the value returned by this function is not computable.
- a9h74j 5y agoI like to call this at the end of an intialization sequence, with a throw to is_computer_even_plugged_in() .
- radus 5y agoDefensive programming at its best
- Kaze404 5y agoThis reminds me of the time when I had a TV attached to my computer through HDMI and I wanted to write a program that checked whether it was on or off. It turned out to be impossible, because even when it was turned off, it still appeared as connected on Windows because it was technically on stand by.
- bee_rider 5y agoReally it is a matter of opinion -- you and the TV just disagreed about the definition of "on" I guess.
- hcrean 5y agoFor some it is a joke, for others it just reminds them of what the codebase looks like after the juniors have been peer reviewing each other...
- brianwitte 5y agoThanks for sharing, OP. I thought I was going to have to do a Haiku rewrite in Rust to do something like this.
- motohagiography 5y agoEven if it were, how could it actually know, really?
- cranium 5y agoThis is actually a good example to explain the concept of undecidability in computation theory. If you get a True you know for sure that the computer is on. If you don't get anything immediately, it may be that the computer is on but really slow to answer or it may be off and never going to answer. But you'll never be sure, thus its state is undecidable in this case.
- jbverschoor 5y agoNobody mentions the decades old BeOS blog www.iscomputeron.com ?
- jbverschoor 5y ago> but also something related to our beloved OS, and with a little humour to top it off, so after a brainstorming session we came up with Is Computer On, or ICO (cute uh?) for short. The developers out there (and many others) will most certainly recognize the is_computer_on() function, a very important one in our day to day computer usage :)
- deleted 5y ago[deleted]
- deleted 5y ago[deleted]
- deleted 5y ago[deleted]
- jmkim 5y agoImpl code here[1]: int32 is_computer_on(void) { return _kern_is_computer_on(); } double is_computer_on_fire(void) { return 0.63739; } [1] https://github.com/haiku/haiku/blob/master/src/system/libroot/os/system_info.cpp#L186 https://github.com/haiku/haiku/blob/master/src/system/libroo... Was used by the Haiku doc, namely "System calls"[2], as an example. [2] https://www.haiku-os.org/documents/dev/system_calls/ https://www.haiku-os.org/documents/dev/system_calls/
- jmkim 5y agoLeh, 0.63739 means "on fire"? Any conversions?
- charles_f 5y agoMy new favorite review comment on while(true) : prefer system.isOn() which is more reliable to detect computer off.