1

It seems like both involves swapping processes in and out.

In paging, you swap out a page of an existing process and replace it with another page from disk (virtual memory I believe), but for context switching, you also swap out a process by first saving it in a PCB, and then restoring another PCB into memory.

What are the differences between the two? Here is my current guess/understanding:

  1. Context switching is with regards to the whole process, so possibly all the pages are swapped out at one go.

  2. Process control block or PCB will therefore contain the pages (if not, how are the two related?).

closed as off-topic by Michael Homer, Jeff Schaller, shirish, Romeo Ninov, Rui F Ribeiro Jun 3 '18 at 21:10

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Requests for learning materials (tutorials, how-tos etc.) are off topic. The only exception is questions about where to find official documentation (e.g. POSIX specifications). See the Help Center and our Community Meta for more information." – Michael Homer, Romeo Ninov, Rui F Ribeiro
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    I think the answer is "everything", but I also think this is probably off-topic here since it's not about Unix but a general systems question. I'm not sure if Super User or one of the other sites takes this kind of question or not. – Michael Homer Jun 1 '18 at 22:10
2

I think there's more differences than commonalities.

Context switching basically refers to whatever CPU context the program code needs to run. That includes stuff like values of the CPU registers, flags, the instruction pointer and stack pointer, priority level, virtual memory setup (page tables) etc.

It doesn't need to include the actual memory of the process, and usually doesn't since swapping out a process's memory in full would be slow. Provided that the system can store multiple programs in memory at the same time, and that the programs have a way of knowing which part of the memory is theirs to use, there's no need to swap out the whole memory of a process when switching to another.

(According to Wikipedia, they used to swap out whole processes in the 1960s, though.)

Paging is the process of moving blocks ("pages") of memory between main memory and some secondary storage, usually in a way that's invisible to the running process.

On a modern-ish system, context switching between processes will include reloading page tables, but doesn't need to involve paging at all, since usually everything fits in memory. On the other hand, paging can happen even within a single process, if one uses enough memory to require swap.

So really, context switching and paging are rather orthogonal, they're just both concepts of multitasking virtual memory OS.

1

Context switching is not related to paging, its what allows you to run two different programs at the same time. The context is what user-space or kernel-space code is running and the switching is moving from user-space to kernel-space or visa-versa.

Paging only occurs when you have swap space to put memory pages that you want to swap out to that space in order to use the memory page for something else. If there is no swap space, then there is no paging, however there will be context switching.

When context is switched out of user-space to kernel-space code (and then possibly to some other user-space code of a different app), no paging is done necessarily. Ie. the original processes memory is not automatically swapped out. So it will be still using memory while the second process is running.

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