system
Given the following post, respond as if you're commenting on Reddit:
human
ELI5: How does a CPU know to keep executing instructions over and over again? What schedules the programs I write and run on the computer?

I can't find a simple understanding of this anywhere. I understand concepts like the reset vector and how the CPU essentially "initializes" and starts working, but once the OS and kernel are introduced into main memory is it basically just a series of jumps and loops that keep the CPU running tasks over and over again until told to either shut off or power is just lost?

Also was wondering what exactly is scheduling the programs I run in say, C, for example. Like, I write a program and compile it and I understand that, but how is it determined by the kernel "Hey, it's a good time to run this guy's program", like how does the CPU know to go fetch my program in memory? What is telling the CPU to do that? I imagine the kernel but I do not know.
gpt
You're exactly right. This is what a multi process kernel is doing. It has a core process called a scheduler which keeps a table of all  "running" programs. If you click or type a command to run a new program, there are standard OS calls that get invoked that add that new process to the scheduler's roster.  

Think of your computer running like a busy gym. There are 20 people (programs) all wanting to use  8 treadmills (say your CPU cores). The kernel's scheduler points to 8 of them and says "ok you guys get 5 minutes" and "you guys get a turn after that." and so on. Some programs are lower priority (old and sore), so don't get or don't want a turn as frequently as others. Some programs are blocked, waiting on some network message (talking on cell phone) to arrive or some disk access (the bathroom/water cooler), so the scheduler will skip their turn until they're ready. Some programs aren't paying attention (busy on phone) and the scheduler goes "hey. Explorer.exe you want a turn on the treadmill? Hey! Get off your - fine. Moving on. Photoshop, you get a turn." and the scheduler just skips over a nonresponsive program. 

Now, how the kernel "switches" to a program in memory, is pretty simple. When the OS "loads" the program into memory it knows its starting address amongst all the other programs. Internally, all the program code jumps and branches are addressed as an _offset_ from its starting point. When its time for that program to start execution, the OS simply points the relevant CPu core to the programs start point and the CPu core goes from there. When the scheduler butts in and says "times up!", a pointer to where in the program the CPU core is currently at gets saved; when that program gets another turn, it simply points the CPU back to that address of where it left off. 

Now, how does the OS scheduler "interrupt" the CPU to switch the code that's running? Those are hardware interrupts. Literally the kernel has a physical switch that tells the CPU to stop and listen to where the scheduler wants it to redirect to.