r/AskComputerScience • • Mar 06 '20

Why does turning the computer off and on / restarting it solves quite a suite of problems? From screen flickering to sound problem to any other thing Computer are the machines that start behaving properly many a times with just a simple reboot, Why?

28 Upvotes

11 comments sorted by

14

u/jpludens Mar 07 '20 edited Jul 10 '23

fuck reddit

2

u/hayk92 Mar 07 '20

Very interesting explanation :)

8

u/superkom Mar 07 '20

Computers, in an abstract sense, are large state machines (you are in state A, operation X occurs, resulting in state B) and so on. If you imagine thousands of these transitions happening every second, a one in a million chance of a bug would happen roughly every 15 minutes on average. Give that you run a computer for hours (often days) at a time, it is often easier to reset that state, than to determine all the possible transitions that caused the state, and debug.

Simply, it allows the machine to reset its state, fixing bugs that may have arisen during the normal course of execution.

4

u/JuicyCiwa Mar 06 '20

I’ve got many years of IT experience, although I don’t have a degree. My best answer to your question is because it sets everything back to the way it should be.

Think of it like this. You have an etch a sketch. You’re trying to make a thing on it, but things aren’t going exactly the way you plan and there are lines where there shouldn’t be. Shake the etch a sketch and you’re back to the clean slate.

Think of the code of the os/program in error being the etch a sketch.

2

u/[deleted] Mar 06 '20

It re-initializes where in memory the OS file system is running via ASLR and it clears any cache memory errors that are happening. This honestly has waaayyyy to many answers to possibly generalize it down to a logical if this, then that kind of answer. There could be a myriad of answers to your question. To put it simply, a lot of errors or issues that are happening that can be cleared with a restart are most likely high level application based errors from a program usually doing something it shouldn't, like say, memory leaks in an application slowing down your machine over time and after the app is closed the memory was never freed back up thus slowing your machine down. Things that an application causes, that the os doesn't detect or even really care about. All your os cares about is allocating enough memory for your application to run and then scheduling that application to run along with all the other applications on your machine, giving each a few ns frames of time to run all in a loop.

2

u/seriousnotshirley Mar 07 '20

Most software contains subtle errors where part of the program changes a value in memory that it shouldn't. When this happens the parts of the program that depend on the value that got changed by accident may now malfunction. The malfunction may be subtle or it could be catastrophic. If it's catastrophic you'll know, you'll get the famous Blue Screen of Death or a similar error. If it's subtle you may not notice, but eventually enough subtle errors crop up and weird things start to happen.

Another issue is that programs allocate resources, like memory or open files and often may not de-allocate those resources. Sometimes through errors and sometimes because it's not possible given the software the developer is working with. The more resources all the software on your computer has allocated the slower the system can become.

2

u/dado243 Mar 07 '20

It resets the memory that caused the error in the first place.

1

u/Masterzjg Mar 07 '20 edited Mar 07 '20

One answer I can give to you is related to deadlocks. A deadlock is when a process reaches a state in which it can never finish. Let's give a real basic example. There are 5 readers and 5 writers available to all processes. Process1 starts only needing 1 reader and and 1 writers at the beginning. Process 2 starts needing 3 readers and 2 writers. At this point, all is good. There is 1 available reader and 1 available writer. Suddenly though, Process 1 needs 2 more readers and Process2 needs 2 more writers to complete executing. What happens? Neither program has enough resources to complete and go to a state where they wait for the resources to become available. Of course, neither program can get the resources need to complete while the other is still ongoing. Thus, the programs are deadlocked. What this means to you, the user, is that both programs will forever be running in your background. This hogs resources and can cause other programs, which might need those resources, to also have problems. Over time, this is part of why computers slow down if you don't reset them. Deadlocks build up and your computer has less resources to provide to any processes that you kick off by say opening a new program.

So, given that we know deadlocks exist, let's talk about preventing them. After all, if we know that deadlocks occur, aren't there ways to detect and prevent them? For instance, before starting a new process, ensure that all resources that it needs to finishing execution are available and allocate them to the process from the start. Then, the process can always finish executing. Another option is to, before allocating resources to a process, ensure that at least 1 of the currently executing processes will ALWAYS be able to finish. If allocating new resources to a process means that none of the processes can finish, then do not allocate the resources. The problem with both of these options (and others) is that they cost resources. They cost CPU cycles and time. Processes start and end constantly in computers. Since deadlocks are rather rare, operating system designers have essentially just decided to allow deadlocks to happen. After all, those resources being used by deadlocked processes will be released whenever you reboot your computer anyways.

For definition, a process is the most basic unit of work in your computer. You run a 'program' (Google Chrome, Slack, Steam, etc) which is a process to the operating system. Generally speaking, 1 program == 1 process.

My source for all this is: Operating System Concepts, 9th Edition, by Abraham Silberschatz, Peter B. Galvin, Greg Gagne.

It should be Chapter 4 where deadlocks are covered if you want to learn more.

0

u/Ultimegede Mar 07 '20

It's all about the driver load order. Sometimes during runtime, some processes can go out of synch and the necessary causality is broken.

-1

u/JeamBim Mar 07 '20

Not a single living person knows how everything in your five-year-old MacBook actually works. Why do we tell you to turn it off and on again? Because we don’t have the slightest clue what’s wrong with it, and it’s really easy to induce coma in computers and have their built-in team of automatic doctors try to figure it out for us. The only reason coders’ computers work better than non-coders’ computers is coders know computers are schizophrenic little children with auto-immune diseases and we don’t beat them when they’re bad.

0

u/[deleted] Mar 06 '20

[deleted]

13

u/dr1fter Mar 07 '20

Not a bad description. To elaborate a little bit on what can go wrong (especially "session based")

  1. consider the memory leak: a program asked the operating system to reserve some memory, then that program lost track of it or just didn't care to return it when they were done.... and then it keeps doing it. The longer the program runs, the harder it is to find new memory for anything. Even though the error is "likely to happen again," restarting at least gets you back to a blank slate. These days your OS knows to reclaim memory when you have to force-quit the sluggish program, so you don't usually need a reboot -- but other shared resources might not be as safeguarded.

Or, 2. leaving a shared component in a particular "mode." Say I offer a service that a bunch of programs in the system can call on, and one of my operations changes a setting that affects some detail of everything else I do. It might be rare for anyone to call the settings-change method, and many of my clients might just assume that I'll always be in the default state. Now if any client ever does change that setting and they don't revert it when they're done, it sets a time bomb -- it might be a long time before some client asks me to do something where the correctness depends on that setting, but then they won't know what to do. Then a restart takes you back to the default setting. TBH this is a mistake that can be hard enough to avoid within a program, we're far from solving this "once and for all."

any issues the user may have caused

It's probably not the user's fault.

Also I'm pretty sure I've seen this question come up in this sub before (might even expect it to be one of the more popular questions here I guess?). There are probably other examples/reasons, but I guess from my perspective (mostly apps & APIs, some OS & theory) these are things that can go wrong all the time.