A time slice is the time that the CPU allocates to each program, and each thread is assigned a time period, called its time slice, that is, the time the process is allowed to run, so that the individual programs are simultaneously on the surface. If the process is still running at the end of the time slice, the CPU is stripped and assigned to another process. If the process blocks or ends before the end of the time slice, the CPU switches immediately. Without wasting CPU resources. On the macro level: we can open multiple applications at the same time, with each program running in parallel. But on the micro: because there is only one CPU, one can only handle part of the program requirements at a time, how to deal with fairness, one way is to introduce a time slice, each program is executed in turn.
Example
Edit
You enter two documents simultaneously: A.txt and B.txt;
After you enter a word in a, enter a word in B and enter it in turn until it is complete. Overall it seems that you are writing two articles at the same time, you can say I write a side write B. But when specific to a word, it is along the way of time, AB alternating. And the time you enter each word, we can call it a time slice.
Operating system
Edit
Embedded operating system can be divided into real-time operating system and timeshare operating system two categories.
Realtime
Real-time operating system is a real-time, can support the real-time control system work operating system. The first task of the real-time operating system is to dispatch all available resources to complete real-time control tasks, and secondly, to improve the efficiency of computer system, and its important feature is to satisfy the correct response to important events within the stipulated time by task scheduling. The real-time operating system has obvious difference from the timeshare operating system. Specifically, for the time-sharing operating system, the implementation of the software is not strict in timing, time delay or timing errors, generally do not cause catastrophic consequences. For real-time operating systems, the main task is to deal with the event in real time, although the event may arrive at unpredictable times, but the software must respond in a strict time frame when the event occurs randomly (system response time). Even if the system is under peak load, this should be the case, and the timeout of the system time response means a fatal failure. In addition, the important feature of the real-time operating system is the system's certainty that the system can make accurate estimates of the best and worst cases of operation.
Timeshare
Time-sharing operating system is divided into the length of the CPU is basically the same time interval, that is, "time slice", through the management of the operating system, these time slices in turn assigned to each user to use. If a job is not finished before the time slice ends, the job is suspended. Give up the CPU and wait for the next round to continue. The CPU is then assigned to another job to use. Because the computer processing speed, as long as the time interval is appropriate, then a user job from the time allocated to it to obtain the next CPU time slice, the middle has "pause"; But the user is not aware of it, as if the entire system by it "exclusive".
Rotation scheduling
Edit
Time-Slice rotation Scheduling is one of the oldest, simplest, fairest and most widely used algorithms. Each process is assigned a time period, called its time slice, which is the time that the process is allowed to run. If the process is still running at the end of the time slice, the CPU is stripped and assigned to another process. If the process blocks or ends before the end of the time slice, the CPU switches immediately. What the scheduler has to do is maintain a list of ready processes that are moved to the end of the queue when the process runs out of its time slice.
The only interesting point in time-slice rotation scheduling is the length of the time slice. It takes time to switch from one process to another-saving and loading register values and memory images, updating various tables and queues, and so on. If the process switch-sometimes called context switch -takes 5 milliseconds and then assumes that the time slice is set to 20 milliseconds, the CPU will spend 5 milliseconds to process switching after 20 milliseconds of useful work. 20% of CPU time is wasted on administrative overhead.
To improve CPU efficiency, we can set the time slice to 500 milliseconds. The time wasted was only 1%. But in a time- sharing system , what happens if 10 interactive users press ENTER almost simultaneously? Assuming that all other processes are using their time slices, the last unfortunate process has to wait 0.5 seconds to get the chance to run. Most users cannot tolerate a short command that takes 0.5 seconds to respond. The same problem can occur on a personal computer that supports multi-channel programs .
Typically, the length of time that all processes in a system are allocated is not equal, although the initial time slices are essentially equal (in Linux systems, the initial time slices are not equal, but half of the respective parent processes), and the system is "asleep" and "running" through the measurement process The duration of the state to calculate the interactivity of each process, the interactivity and the superposition of the static priority (nice value) for each process preset is the dynamic priority, and the dynamic priority scaling is the length of time slices to be allocated to that process. In general, in order to achieve faster response times, interactive processes (that is, tend to IO-consuming) are allocated to a time slice that is longer than the interactive (processor-intensive) process.
The conclusion can be summed up as follows: The time slices are set too short to cause excessive process switching , which reduces CPU efficiency, and is too long to cause a poor response to short interaction requests. Setting a time slice to 100 milliseconds is usually a reasonable tradeoff.
Instance
Edit
The time slice for Windows 95 is 20ms
The time slice of Linux is 5ms-800ms
CPU time slice and tick rotation scheduling