Knowledge (XXG)

Context switch

Source đź“ť

54:. In a traditional CPU, each process - a program in execution - utilizes the various CPU registers to store data and hold the current state of the running process. However, in a multitasking operating system, the operating system switches between processes or threads to allow the execution of multiple processes simultaneously. For every switch, the operating system must save the state of the currently running process, followed by loading the next process state, which will run on the CPU. This sequence of operations that stores the state of the running process and loads the following running process is called a context switch. 207:
When an interrupt occurs, the hardware automatically switches a part of the context (at least enough to allow the handler to return to the interrupted code). The handler may save additional context, depending on details of the particular hardware and software designs. Often only a minimal part of the
280:
Since the operating system has effectively suspended the execution of one process, it can then switch context by choosing a process from the ready queue and restoring its PCB. In doing so, the program counter from the PCB is loaded, and thus execution can continue in the chosen process. Process and
327:
routine to first save the general-purpose user registers of A onto A's kernel stack, then it saves A's current kernel register values into the PCB of A, restores kernel registers from the PCB of process B, and switches context, that is, changes kernel stack pointer to point to the kernel stack of
212:
does not spawn or schedule a special process to handle interrupts, but instead the handler executes in the (often partial) context established at the beginning of interrupt handling. Once interrupt servicing is complete, the context in effect before the interrupt occurred is restored so that the
82:
Context switches are usually computationally intensive, and much of the design of operating systems is to optimize the use of context switches. Switching from one process to another requires a certain amount of time for doing the administration – saving and loading registers and
83:
memory maps, updating various tables and lists, etc. What is actually involved in a context switch depends on the architectures, operating systems, and the number of resources shared (threads that belong to the same process share many resources compared to unrelated non-cooperating processes).
322:
Suppose a process A is running and a timer interrupt occurs. The user regsiters—program counter, stack pointer, and status register—of process A are then implicitly saved by the CPU onto the kernel stack of A. Then, the hardware switches to kernel mode and jumps into interrupt handler for the
57:
The precise meaning of the phrase "context switch" varies. In a multitasking context, it refers to the process of storing the system state for one task, so that task can be paused and another task resumed. A context switch can also occur as the result of an
309:
is incremented. A and B are read from memory and are stored in registers R1, R2 respectively. In this case, B+1 is calculated and written in R1 as the final answer. This operation as there are sequential reads and writes and there's no waits for
169:
system, the scheduler may also switch out processes that are still runnable. To prevent other processes from being starved of CPU time, pre-emptive schedulers often configure a timer interrupt to fire when a process exceeds its
435:
Associated performance issues, e.g., software context switching can be selective and store only those registers that need storing, whereas hardware context switching stores nearly all registers whether they are required or
110:(unless the old process shares the memory with the new) are loaded from the PCB for the new process. To avoid incorrect address translation in the case of the previous and current processes using different memory, the 328:
process B. The operating system then returns from interrupt. The hardware then loads user registers from B's kernel stack, switches to user mode, and starts running process B from B's program counter.
482:
Context switching lies at the heart of the process juggling act. It consists of stopping the current computation, saving enough information so it may be restarted later, and restarting another process.
157:
scheme, one process must be switched out of the CPU so another process can run. This context switch can be triggered by the process making itself unrunnable, such as by waiting for an
822: 114:(TLB) must be flushed. This negatively affects performance because every memory reference to the TLB will be a miss because it is empty after most context switches. 94:(PCB) stored in the PCB table in the kernel stack to retrieve information about the state of the new process. CPU state information including the registers, 769: 344:
between multiple tasks. Switching between threads of a single process can be faster than between two separate processes because threads share the same
597: 190:
until the read is over; it can issue the request (to the I/O device) and continue with some other task. When the read is over, the CPU can be
1098: 1069: 693: 367: 601: 512: 162: 401:
As with other tasks performed in hardware, one would expect this to be rather fast; however, mainstream operating systems, including
475: 195: 233:
is not by itself a context switch. However, depending on the operating system, a context switch may also take place at this time.
873: 817: 125:, and is often very lightweight, saving and restoring minimal context. In extreme cases, such as switching between goroutines in 792: 762: 985: 883: 429: 46:
at a later point, and then restoring a different, previously saved, state. This allows multiple processes to share a single
248:, plus any other operating system specific data that may be necessary. This is usually stored in a data structure called a 1263: 812: 797: 51: 1284: 858: 843: 802: 111: 679: 382:
and its successors, have hardware support for context switches, by making use of a special data segment designated the
1024: 971: 724: 653: 565: 386:(TSS). A task switch can be explicitly triggered with a CALL or JMP instruction targeted at a TSS descriptor in the 1039: 878: 755: 395: 370:
can be faster than switching between two processes in an operating system with private per-process address spaces.
1074: 893: 853: 848: 807: 241:
The state of the currently executing process must be saved so it can be restored when rescheduled for execution.
209: 126: 1117: 1004: 868: 730: 166: 863: 387: 66:, freeing up CPU time for other tasks. Some operating systems also require a context switch to move between 47: 1251: 1190: 1079: 1059: 1008: 966: 337: 262: 154: 1034: 1000: 902: 838: 446: 250: 91: 43: 359:. The time from when a hardware interrupt is generated to when the interrupt is serviced is called the 293:
The details vary depending on the architecture and operating system, but these are common scenarios.
1231: 1205: 736: 531: 302: 1200: 1152: 1029: 103: 35: 1137: 1044: 383: 270: 269:), or there may be some specific operating system-defined data structure for this information. A 95: 39: 20: 413:
Hardware context switching does not save all the registers (only general-purpose registers, not
378:
Context switching can be performed primarily by software or hardware. Some processors, like the
588: 186:
driven. This means that if the CPU requests data from a disk, for example, it does not need to
1246: 1195: 1127: 1084: 925: 689: 685: 546: 508: 471: 402: 360: 301:
Considering a general arithmetic addition operation A = B+1. The instruction is stored in the
281:
thread priority can influence which process is chosen from the ready queue (i.e., it may be a
200: 208:
context is changed in order to minimize the amount of time spent handling the interrupt. The
1226: 778: 637: 425: 398:(IDT). When a task switch occurs the CPU can automatically load the new state from the TSS. 244:
The process state includes all the registers that the process may be using, especially the
174:. This interrupt ensures that the scheduler will gain control to perform a context switch. 1170: 1132: 1103: 432:
and giving the OS the opportunity to save and restore the floating-point state as needed).
306: 245: 99: 74:
tasks. The process of context switching can have a negative impact on system performance.
204:
is installed, and it is the interrupt handler that handles the interrupt from the disk.
1256: 1180: 1142: 414: 345: 282: 1278: 1122: 961: 915: 273:
to the PCB is added to a queue of processes that are ready to run, often called the
19:
This article is about computer task switching. For the term in human cognition, see
1049: 390:. It can occur implicitly when an interrupt or exception is triggered if there's a 158: 122: 87: 63: 1175: 1157: 940: 930: 920: 636:
D.L. Sims. "Multiple and single address spaces: towards a middle ground". 1993.
466:
Douglas Comer; Timmothy V. Fossum (1988). "4 Scheduling and Context Switching".
379: 226: 118: 71: 641: 742: 311: 266: 171: 134: 107: 1112: 1019: 945: 910: 391: 341: 222: 187: 183: 130: 67: 59: 27: 718: 657: 355:. The time to switch between two threads of the same process is called the 470:. Vol. I: The XINU Approach (PC Edition). Prentice Hall. p. 67. 1241: 559: 1236: 1165: 935: 623: 336:
Context switching itself has a cost in performance, due to running the
747: 619: 323:
operating system to take over. Then the operating system calls the
198:) and presented with the read. For interrupts, a program called an 1185: 406: 351:
The time to switch between two separate processes is called the
751: 493:
Or sequentially so rapidly that they appear to be simultaneous.
1221: 409:, do not use this feature. This is mainly due to two reasons: 213:
interrupted process can resume execution in its proper state.
194:(by a hardware in this case, which sends interrupt request to 314:
used, hence no context switch/wait takes place in this case.
50:(CPU), and is an essential feature of a multiprogramming or 145:
There are three potential triggers for a context switch:
117:
Furthermore, analogous context switching happens between
90:, context switching involves loading the corresponding 558:
Arpaci-Dusseau, Remzi; Arpaci-Dusseau, Andrea (2015).
133:
yield, which is only marginally more expensive than a
503:
Tanenbaum, Andrew S.; Bos, Herbert (March 20, 2014).
428:, resulting in a fault when executing floating-point 1214: 1151: 1097: 1058: 993: 984: 954: 901: 892: 831: 785: 737:
General Architecture and Design -Interrupt Handling
340:, TLB flushes, and indirectly due to sharing the 16:Switch between processes or tasks on a computer 265:in kernel memory (as opposed to the user-mode 102:as well as memory management information like 763: 681:Understanding the Linux Kernel, Third Edition 528:IA-64 Linux Kernel: Design and Implementation 8: 678:Bovet, Daniel Pierre; Cesati, Marco (2006). 733:from the Microsoft Developer Network (MSDN) 990: 898: 770: 756: 748: 261:The PCB might be stored on a per-process 34:is the process of storing the state of a 727:by The Linux Information Project (LINFO) 42:, so that it can be restored and resume 598:Federated Computing Research Conference 458: 348:maps, so a TLB flush is not necessary. 229:, a context switch is not necessary; a 590:Quantifying The Cost of Context Switch 421:bit is automatically turned on in the 129:, a context switch is equivalent to a 62:, such as when a task needs to access 368:single address space operating system 366:Switching between two processes in a 7: 743:OS Context Switching - Computerphile 561:Operating Systems: Three Easy Pieces 221:When the system transitions between 587:Chuanpeng Li; Chen Ding; Kai Shen. 318:Context switch caused by interrupt 14: 618:Ulrich Drepper (9 October 2014). 568:from the original on 25 July 2016 874:Object-oriented operating system 607:from the original on 2017-08-13. 620:"Memory part 3: Virtual Memory" 884:Supercomputer operating system 600:, San Diego, 13-14 June 2007. 217:User and kernel mode switching 1: 52:multitasking operating system 859:Just enough operating system 844:Distributed operating system 532:4.7 Switching Address Spaces 165:operation to complete. On a 112:translation lookaside buffer 972:User space and kernel space 656:. Linfo.org. Archived from 654:"Context Switch definition" 153:Most commonly, within some 1301: 879:Real-time operating system 642:10.1109/IWOOOS.1993.324906 545:, 5.6 The Context Switch, 396:interrupt descriptor table 18: 1075:Multilevel feedback queue 1070:Fixed-priority preemptive 854:Hobbyist operating system 849:Embedded operating system 725:Context Switch Definition 507:(4th ed.). Pearson. 417:registers — although the 353:process switching latency 182:Modern architectures are 1118:General protection fault 869:Network operating system 823:User features comparison 505:Modern Operating Systems 357:thread switching latency 297:No context switch needed 167:pre-emptive multitasking 864:Mobile operating system 468:Operating System Design 388:global descriptor table 48:central processing unit 967:Loadable kernel module 1035:Process control block 1001:Computer multitasking 839:Disk operating system 447:Latency (engineering) 374:Hardware vs. software 251:process control block 92:process control block 1206:Virtual tape library 798:Forensic engineering 303:instruction register 86:For example, in the 1285:Process (computing) 1215:Supporting concepts 1201:Virtual file system 104:segmentation tables 1138:Segmentation fault 986:Process management 384:task state segment 178:Interrupt handling 21:Human multitasking 1272: 1271: 1128:Memory protection 1099:Memory management 1093: 1092: 1085:Shortest job next 980: 979: 779:Operating systems 719:Context Switching 695:978-0-596-00565-8 543:Operating Systems 361:interrupt latency 201:interrupt handler 1292: 1227:Computer network 991: 899: 772: 765: 758: 749: 731:Context Switches 706: 705: 703: 702: 675: 669: 668: 666: 665: 650: 644: 634: 628: 627: 615: 609: 608: 606: 595: 584: 578: 577: 575: 573: 555: 549: 540: 534: 525: 519: 518: 500: 494: 491: 485: 484: 463: 426:control register 424: 420: 326: 1300: 1299: 1295: 1294: 1293: 1291: 1290: 1289: 1275: 1274: 1273: 1268: 1210: 1171:Defragmentation 1156: 1147: 1133:Protection ring 1102: 1089: 1061: 1054: 976: 950: 888: 827: 781: 776: 715: 710: 709: 700: 698: 696: 688:. p. 104. 677: 676: 672: 663: 661: 652: 651: 647: 635: 631: 617: 616: 612: 604: 593: 586: 585: 581: 571: 569: 557: 556: 552: 541: 537: 526: 522: 515: 502: 501: 497: 492: 488: 478: 465: 464: 460: 455: 443: 422: 418: 376: 334: 324: 320: 307:program counter 299: 291: 246:program counter 239: 231:mode transition 219: 180: 163:synchronization 151: 143: 141:Switching cases 100:program counter 80: 24: 17: 12: 11: 5: 1298: 1296: 1288: 1287: 1277: 1276: 1270: 1269: 1267: 1266: 1261: 1260: 1259: 1257:User interface 1254: 1244: 1239: 1234: 1229: 1224: 1218: 1216: 1212: 1211: 1209: 1208: 1203: 1198: 1193: 1188: 1183: 1181:File attribute 1178: 1173: 1168: 1162: 1160: 1149: 1148: 1146: 1145: 1143:Virtual memory 1140: 1135: 1130: 1125: 1120: 1115: 1109: 1107: 1095: 1094: 1091: 1090: 1088: 1087: 1082: 1077: 1072: 1066: 1064: 1056: 1055: 1053: 1052: 1047: 1042: 1037: 1032: 1027: 1022: 1017: 1015:Context switch 1012: 997: 995: 988: 982: 981: 978: 977: 975: 974: 969: 964: 958: 956: 952: 951: 949: 948: 943: 938: 933: 928: 923: 918: 913: 907: 905: 896: 890: 889: 887: 886: 881: 876: 871: 866: 861: 856: 851: 846: 841: 835: 833: 829: 828: 826: 825: 820: 815: 810: 805: 800: 795: 789: 787: 783: 782: 777: 775: 774: 767: 760: 752: 746: 745: 740: 739:at FreeBSD.org 734: 728: 722: 714: 713:External links 711: 708: 707: 694: 686:O'Reilly Media 670: 645: 629: 610: 579: 550: 535: 520: 514:978-0133591620 513: 495: 486: 476: 457: 456: 454: 451: 450: 449: 442: 439: 438: 437: 433: 415:floating point 375: 372: 346:virtual memory 338:task scheduler 333: 330: 319: 316: 312:function calls 298: 295: 290: 287: 283:priority queue 238: 235: 218: 215: 179: 176: 150: 147: 142: 139: 79: 76: 32:context switch 15: 13: 10: 9: 6: 4: 3: 2: 1297: 1286: 1283: 1282: 1280: 1265: 1262: 1258: 1255: 1253: 1250: 1249: 1248: 1245: 1243: 1240: 1238: 1235: 1233: 1230: 1228: 1225: 1223: 1220: 1219: 1217: 1213: 1207: 1204: 1202: 1199: 1197: 1194: 1192: 1189: 1187: 1184: 1182: 1179: 1177: 1174: 1172: 1169: 1167: 1164: 1163: 1161: 1159: 1154: 1150: 1144: 1141: 1139: 1136: 1134: 1131: 1129: 1126: 1124: 1123:Memory paging 1121: 1119: 1116: 1114: 1111: 1110: 1108: 1105: 1100: 1096: 1086: 1083: 1081: 1078: 1076: 1073: 1071: 1068: 1067: 1065: 1063: 1057: 1051: 1048: 1046: 1043: 1041: 1038: 1036: 1033: 1031: 1028: 1026: 1023: 1021: 1018: 1016: 1013: 1010: 1006: 1002: 999: 998: 996: 992: 989: 987: 983: 973: 970: 968: 965: 963: 962:Device driver 960: 959: 957: 953: 947: 944: 942: 939: 937: 934: 932: 929: 927: 924: 922: 919: 917: 914: 912: 909: 908: 906: 904: 903:Architectures 900: 897: 895: 891: 885: 882: 880: 877: 875: 872: 870: 867: 865: 862: 860: 857: 855: 852: 850: 847: 845: 842: 840: 837: 836: 834: 830: 824: 821: 819: 816: 814: 811: 809: 806: 804: 801: 799: 796: 794: 791: 790: 788: 784: 780: 773: 768: 766: 761: 759: 754: 753: 750: 744: 741: 738: 735: 732: 729: 726: 723: 720: 717: 716: 712: 697: 691: 687: 683: 682: 674: 671: 660:on 2010-02-18 659: 655: 649: 646: 643: 639: 633: 630: 625: 621: 614: 611: 603: 599: 592: 591: 583: 580: 567: 563: 562: 554: 551: 548: 544: 539: 536: 533: 529: 524: 521: 516: 510: 506: 499: 496: 490: 487: 483: 479: 477:0-13-638180-4 473: 469: 462: 459: 452: 448: 445: 444: 440: 434: 431: 427: 416: 412: 411: 410: 408: 404: 399: 397: 393: 389: 385: 381: 373: 371: 369: 364: 362: 358: 354: 349: 347: 343: 339: 331: 329: 317: 315: 313: 308: 304: 296: 294: 288: 286: 284: 278: 276: 272: 268: 264: 259: 257: 253: 252: 247: 242: 236: 234: 232: 228: 224: 216: 214: 211: 205: 203: 202: 197: 193: 189: 185: 177: 175: 173: 168: 164: 160: 156: 148: 146: 140: 138: 136: 132: 128: 124: 123:green threads 120: 115: 113: 109: 105: 101: 97: 96:stack pointer 93: 89: 84: 77: 75: 73: 69: 65: 61: 55: 53: 49: 45: 41: 37: 33: 29: 22: 1158:file systems 1050:Time-sharing 1014: 721:at OSDev.org 699:. Retrieved 680: 673: 662:. Retrieved 658:the original 648: 632: 613: 589: 582: 570:. Retrieved 560: 553: 542: 538: 527: 523: 504: 498: 489: 481: 467: 461: 430:instructions 400: 377: 365: 356: 352: 350: 335: 321: 300: 292: 279: 274: 260: 255: 249: 243: 240: 230: 220: 206: 199: 191: 181: 152: 149:Multitasking 144: 119:user threads 116: 88:Linux kernel 85: 81: 64:disk storage 56: 31: 25: 1176:Device file 1166:Boot loader 1080:Round-robin 1005:Cooperative 941:Rump kernel 931:Multikernel 921:Microkernel 818:Usage share 380:Intel 80386 332:Performance 275:ready queue 256:switchframe 227:kernel mode 192:interrupted 108:page tables 72:kernel mode 1106:protection 1062:algorithms 1060:Scheduling 1009:Preemptive 955:Components 926:Monolithic 793:Comparison 701:2009-11-23 664:2013-09-08 453:References 267:call stack 172:time slice 155:scheduling 135:subroutine 121:, notably 1196:Partition 1113:Bus error 1040:Real-time 1020:Interrupt 946:Unikernel 911:Exokernel 392:task gate 342:CPU cache 254:(PCB) or 223:user mode 188:busy-wait 184:interrupt 131:coroutine 68:user mode 60:interrupt 44:execution 28:computing 1279:Category 1242:Live USB 1104:resource 994:Concepts 832:Variants 813:Timeline 602:Archived 566:Archived 441:See also 325:switch() 305:and the 289:Examples 1237:Live CD 1191:Journal 1155:access, 1153:Storage 1030:Process 936:vkernel 803:History 786:General 624:LWN.net 572:25 July 403:Windows 394:in the 36:process 1045:Thread 916:Hybrid 894:Kernel 692:  596:. ACM 547:p. 118 511:  474:  271:handle 210:kernel 137:call. 98:, and 40:thread 1247:Shell 1186:Inode 605:(PDF) 594:(PDF) 407:Linux 263:stack 237:Steps 808:List 690:ISBN 574:2016 509:ISBN 472:ISBN 436:not. 405:and 225:and 106:and 78:Cost 70:and 30:, a 1264:PXE 1252:CLI 1232:HAL 1222:API 1025:IPC 638:doi 423:CR0 285:). 196:PIC 161:or 159:I/O 38:or 26:In 1281:: 1007:, 684:. 622:. 564:. 530:, 480:. 419:TS 363:. 277:. 258:. 127:Go 1101:, 1011:) 1003:( 771:e 764:t 757:v 704:. 667:. 640:: 626:. 576:. 517:. 23:.

Index

Human multitasking
computing
process
thread
execution
central processing unit
multitasking operating system
interrupt
disk storage
user mode
kernel mode
Linux kernel
process control block
stack pointer
program counter
segmentation tables
page tables
translation lookaside buffer
user threads
green threads
Go
coroutine
subroutine
scheduling
I/O
synchronization
pre-emptive multitasking
time slice
interrupt
busy-wait

Text is available under the Creative Commons Attribution-ShareAlike License. Additional terms may apply.

↑