Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

According to my professor,

"while switching from user mode to kernel mode, the status of the user process has to be saved completely. But when switching from kernel mode to user mode, nothing has to be saved, as the kernel process has nothing to save."

Is he right? Doesn't the kernel process have anything to save?

share|improve this question

closed as not constructive by Mehrdad, BBlake, KronoS, CharlieRB, 8088 Jan 9 '13 at 0:30

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

Good question, but I don't know if this is the best site. I think may get you a better answer. You can flag your own question for a moderator and he can move the question there for you. – Scott Chamberlain Jan 7 '13 at 22:40
Should be on StackOverflow. – Mehrdad Jan 7 '13 at 23:15
Was he talking about a particular OS by the way? I think this is OS-dependent... one could write an OS that does need to save some things, I think. – Mehrdad Jan 7 '13 at 23:17
This question has nothing specific to do with programming and thus suggesting migrations to Programmers and Stack Overflow aren't the right way to deal with this question; it lies more to computers and there internals, I wouldn't say it's on-topic on Super User and it feels almost like or But well, let's see what the community decides to do with it... – Tom Wijsman Jan 8 '13 at 1:09
It's more computer science than anything else, voting to close doesn't offer the beta site as an option. – Tog Jan 8 '13 at 9:26
up vote 3 down vote accepted

This is better understood if we dissect your story and attempt to use better terminology:

  • while switching from user mode to kernel mode

    This is also called a mode switch.

    When does this happen? This happens when a kernel routine is called; which can for instance happen when it is called from the user mode through elevation or through a hardware interrupt.

    What is such a routine? It's a piece of code that handles the call / interrupt.

  • the status of the user process has to be saved completely.

    Exactly, since the kernel will be running a kernel routine we need to temporarily save the status of the user process that was running.

  • But when switching from kernel mode to user mode, nothing has to be saved

    There is nothing to be saved since we called a routine which has done executing, there is no longer a status in the kernel process since the routine is no longer running.

    What does happen instead is that the status of the user process needs to be loaded again to continue executing the user process.

So, we have two different things here:

  • an user process, which needs to continue executing on a mode switch, so its status is stored.

  • a kernel routine, which is done executing on a mode switch, so its status is disposed .

As to answer your question "Doesn't the kernel process have anything to save?"; there isn't really a kernel process running, it's just a shim that houses kernel memory, routines and some other magic.

share|improve this answer
Switching between kernel mode and user mode is called "mode switching." "Context switching" is changing from one process to another. People often use "context switch" when they refer to a change in mode, but this is not correct. – Patrick S. Jan 8 '13 at 0:46
@PatrickS.: You are wrong when you state that this is not correct, the task context has an equivalent relation to the process status. What happens when you "context switch" is that you either load or save the context (and thus the process status), which is exactly what happens when you do a "mode switch". Therefore, the statement "a context switch happens when the processor does a mode switch" which I've expanded further at the bottom of my answer is therefore true. – Tom Wijsman Jan 8 '13 at 0:51
Sorry, but I'm not wrong. Mode switching is changing modes. Context switching is changing processes. If change in mode does not require a change in the process being executed, there is no context switch required. – Patrick S. Jan 8 '13 at 0:56
@PatrickS.: Context switching is changing contexts, not processes; full process switches are irrelevant. – Tom Wijsman Jan 8 '13 at 1:16
Changing contexts in order to.... ? – Patrick S. Jan 8 '13 at 1:35

I’ve never been fond of the “kernel process” terminology, but that’s just my personal issue.  I believe that the answer is that all processes are user processes at their base, and the “kernel process” is activated only when a kernel entry point is called from user mode.  (I suspect that your professor might say that I haven’t worded that exactly correctly, but I believe that I’m close enough to give you a reasonably accurate idea of the relationship.) 

It’s like what happens in an ordinary program: If you have a function hypot and it calls sqrt, the system has to save hypot’s local variables and the return point (program counter / instruction pointer from which sqrt was called).  But when sqrt returns and hypot resumes, sqrt’s stack frame can be discarded.

share|improve this answer

This sounds like it's in the context of pre-emptive multitasking.

The OS/kernel is always running, so there is nothing to be saved. Other programs are running too (on top of the OS), but they each get a fraction of a second at a time, in turn. As each program gets its turn, the stack for that program needs to be loaded so processing can occur, then the state of all the registers need to be saved back to RAM from the processor so the next program can load.

share|improve this answer
No, the OS/kernel is not "always running". Nor are other programs (threads). Most threads spend most of their time waiting. When they're waiting, they're not running. Most of the OS/kernel is a bunch of routines that get called from application code - when such a call is not in progress, those routines aren't "running". Some of the OS/kernel is run in response to interrupts, but once an interrupt is handled, the interrupt service routine isn't running either. – Jamie Hanrahan Dec 6 '15 at 15:59

The professor is partially correct.

The data that is being saved when switching from user mode to kernel mode is the state of the CPU, not the process. User mode and kernel mode are CPU-related, not process-related. When the kernel executes some function on behalf of the user, the state of the CPU is saved beforehand. That is, the state of the CPU when it was executing the user mode code. Then, the kernel performs its action based on the user mode code's request. Finally, the kernel restores the state of the CPU back to the previously-saved state. The kernel may return the CPU back to user mode, depending on the kernel in question.

The kernel process has nothing to save, because it was not running beforehand. It (the kernel process) does a job on behalf of the user mode process, and then it is finished. The CPU can return to user mode, as far as the kernel is concerned.

Operating system components typically run in kernel mode, also called Ring 0 on x86 processors. User mode contains applications and is sometimes referred to as Ring 3.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.