Questions tagged [context-switch]

A context switch is the process of storing and restoring the state (context) of a process.

In computing, a context switch is the process of storing and restoring the state (context) of a process so that execution can be resumed from the same point at a later time. This enables multiple processes to share a single CPU and is an essential feature of a multitasking operating system. What constitutes the context is determined by the processor and the operating system.

313 questions
11
votes
3 answers

Cannot avoid context-switches on a process launched alone on a CPU

I am investigating how run a process on a dedicated CPU in order to avoid context-switches. On my Ubuntu, I isolated two CPUs using the kernel parameters "isolcpus=3,7" and "irqaffinity=0-2,4-6". I am sure that it is correctly taken into account: $…
10
votes
6 answers

Can my thread help the OS decide when to context switch it out?

I am working on a threaded application on Linux in C++ which attempts to be real time, doing an action on a heartbeat, or as close to it as possible. In practice, I find the OS is swapping out my thread and causing delays of up to a tenth of a…
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
10
votes
3 answers

What is the difference between scheduler and dispatcher in context of process scheduling

I am currently pursuing an undergraduate level course in Operating Systems. I'm somewhat confused about the functions of dispatcher and scheduler in process scheduling. Based on what I've learnt, the medium term scheduler selects the process for…
Backspace
  • 292
  • 1
  • 4
  • 17
9
votes
2 answers

Creating a custom JSON response object with Zend Action Helper ContextSwitch

I normally append an encoded json object to the response body, however I now have a situation that warrants using the ContextSwitch action helper. I have a Zend_Form that requires three different response contexts: html - Render the form as normal…
gawpertron
  • 1,867
  • 3
  • 21
  • 37
9
votes
3 answers

How expensive is a context switch? Is it better to implement a manual task switch than to rely on OS threads?

Imagine I have two (three, four, whatever) tasks that have to run in parallel. Now, the easy way to do this would be to create separate threads and forget about it. But on a plain old single-core CPU that would mean a lot of context switching - and…
Vilx-
  • 104,512
  • 87
  • 279
  • 422
9
votes
1 answer

Is kernel/sched.c/context_switch() guaranteed to be invoked every time a process is switched in?

I want to alter the Linux kernel so that every time the current PID changes - i.e., a new process is switched in - some diagnostic code is executed (detailed explanation below, if curious). I did some digging around, and it seems that every time the…
Reiik
  • 135
  • 4
8
votes
4 answers

Writing a syscall to count context switches of a process

I have to do a system call to count the voluntary & involuntary context switches of a process. I already know the steps to add a new system call to a linux kernel but i have no clue of where i should start for the context-switch function. Any idea?
Nausikaa
  • 123
  • 1
  • 2
  • 10
8
votes
2 answers

Is processor cache flushed during context switch in multicore?

Recently, I discussed why there is a volatile mark at seq in Java Actors demo @volatile private var seq = 0L private def nextSeq: Long = { val next = seq seq += 1 next } One answer was that threads can be migrated and variables lost (other…
Val
  • 1
  • 8
  • 40
  • 64
8
votes
2 answers

What are some good use cases for calling 'yield' in a thread?

Many languages that support multi-threading provide an action that allows a thread to offer a context switch to another threads. For example Haskell's yield. However, the documentation doesn't say what is the actual use case. When it's appropriate…
Petr
  • 62,528
  • 13
  • 153
  • 317
8
votes
2 answers

Prevent context-switching in timed section of code (or measure then subtract time not actually spent in thread)

I have a multi-threaded application, and in a certain section of code I use a Stopwatch to measure the time of an operation: MatchCollection matches = regex.Matches(text); //lazy evaluation Int32 matchCount; //inside this bracket program should not…
David S.
  • 5,965
  • 2
  • 40
  • 77
7
votes
4 answers

Storing and retrieving process control block

When a process is in execution, the contents of the PCB (which is in kernel memory space?) are loaded onto the CPU registers, and status registers, kernel stack pointers, user stack pointers, etc. When there is a context switch to another process,…
7
votes
3 answers

System call without context switching?

I was just reading up on how linux works in my OS-book when I came across this.. [...] the kernel is created as a single, monolitic binary. The main reason is to improve performance. Because all kernel code and data structures are kept in a single…
user1130005
  • 392
  • 1
  • 4
  • 15
7
votes
2 answers

Simulating context switches in JavaScript?

I've been working on implementing a pretty complex system in JavaScript that needs to simulate, among other things, multithreaded processes. In a real multithreaded process (such as a kernel thread) it's possible to switch between threads by…
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
7
votes
1 answer

Context Switches on Sleeping/Waiting Threads

I'm trying to understand how operating systems handle context switching in different models to better understand why NIO performance is better in cases of large peaks in the number of requests. Apart from the fact that there may be a limit to the…
7
votes
2 answers

Reduce Context Switches Between Threads With Same Priority

I am writing an application that use a third-party library to perform heavy computations. This library implements parallelism internally and spawn given number threads. I want to run several (dynamic count) instances of this library and therefore…
ronag
  • 49,529
  • 25
  • 126
  • 221
1
2
3
20 21