在书上看到一段这样的话:
在kernel 2.6中时间片的计算是分散的,具体的计算时间可以用task_timeslice(),也可以用其他方法。
a.进程创建时,将父进程的时间片分一半给子进程,同时父进程的时间片减半。
b.进程用完时间片以后,需要重新计算时间片,并将进程插入到相应的运行队列。
c.进程退出时,根据first_timeslice的值来决定是否将子进程的时间片返还给父进程。
疑问:对于a,子进程的时间片是从父进程那里分得的,也就是说其实父进程也是有时间去完成子进程的任务,那干脆就不用创建子进程咯。子进程分父进程的时间片,那又何来创建进程可以增加机器的并行性呢?
对于b,这个“重新计算时间片”是不是摆脱了子进程从父进程那里分得时间片的限制,父子进程的时间片是否还有着某种关系呢?
对于多核的机器,子进程和父进程的时间片划分又是怎么样的呢?多核间如何协调时间片的长短?
a. Multiple processes will inevitably occupy more resources. Why do the masters come up with such a design? Because different processes can perform different types of tasks, for example, one is accessing the disk, and the other is processing the TCP connection, which is independent of each other, multi-process is best at this area.
b. You must get rid of the parent process. Just reason about it. The OS will not be so stupid. Resources are valuable only when they are used rationally (I want to complain about the people who put protective film on mobile phones again).
c. How to divide time slices? Different OSs have different algorithms, some are as fair as possible, and some are prioritized. The algorithm that ensures the task is completed as quickly as possible is a good algorithm. In addition, only multi-core can achieve true parallelism. In other words, even if the two processes have the same type, they are not afraid of using the CPU at the same time. Anyway, there are more than two cores for them to use separately and at the same time.