Can someone describe the process of pre-emption in UNIX?


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Can someone describe the process of pre-emption in UNIX?
# 1  
Old 07-21-2015
Can someone describe the process of pre-emption in UNIX?

I never understood what exactly it is that preempts a process in a preemptive multitasking system. For example, round-robin, SRTF, or preemptive priority-based algorithms. I understand how the algorithm works, but what exactly triggers the preemption? I know in the case of round-robin scheduling, there is a clock interrupt that preempts a process once its time slot runs out. How exactly does this work? As for SRTF and priority-based preemption, the only time a task would be preempted would be if another task enters the queue that has a higher priority and/or a shorter remaining execution time. So essentially, preemption only occurs in response to a fork() system call. So I would assume that fork() somehow triggers preemption, but I've looked at the algorithm for fork() as given in The Design of the Unix Operating System and it doesn't mention preemption. What is it that triggers a context switch, and how does this process interrupt mechanism work?
# 2  
Old 07-21-2015
A timer. Hardware interrupts are happening all the time which gives a vector for the kernel to regain control without programs cooperatively giving it up. Keyboard or network (as just two examples) input being available cause interrupts so we can handle the data before buffers overflow. The timer does this too at regular intervals every few hz (Linux i386 once defaulted to 1000, MIPS to 100). I imagine that's when the scheduler decides who goes next. It's not just when fork() is called. we'd never regain control like that.

when there is no work to do, calling the interrupt handler alone keeps the CPU a little busy and wastes some power so modern kernels have dynamic ticks.

So the timer interrupt causes this context switch, how does it work depends on the arch I suppose. I know in MIPS there was a jump table of exception handlers created and stored at the beginning of memory. When the timer ticked, the Interrupt exception handler is called. That in turn checked a status register to know which interrupt. Then you'd exit that into the next program that deserved attention.
This User Gave Thanks to neutronscott For This Post:
# 3  
Old 07-23-2015
So does Unix do any preemption with fork(), or is it just round-robin scheduling with timer interrupts?
# 4  
Old 07-23-2015
fork() pre-empts -- just like any and every other system call pre-empts. The calling process is forced to stop during any system call, while the kernel executes instead. This does not mean the new process is guaranteed to start before the parent restarts, however. UNIX gives no guarantees of scheduling order unless you specifically ask for it.

UNIX scheduling is more complicated than round robin. You can set priorities. The 'nice' command is related to this, it lets you run a process at reduced priority.

How the scheduler itself is built varies because there's way more than one kind of UNIX, but it's often credit-based -- i.e. processes which "behave" are run preferentially before ones which "misbehave". A polite process pre-empts by itself, from waiting on I/O or calling sleep(), etc. An impolite job, such as a CPU-intensive physics simulation, almost never pre-empts by itself and gets demerits when the kernel must force pre-emption instead.

This allows the scheduler to automatically prioritize interactive processes -- where waits might be noticed -- above batch jobs, which care much less.

Last edited by Corona688; 07-23-2015 at 12:26 PM..
These 3 Users Gave Thanks to Corona688 For This Post:
# 5  
Old 07-23-2015
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

please describe me some simple command

Hi everyone I'm new here and I don't know some command of unix, please help by describe me how it work, I study unix command by myself and can't search exactly means so...Thanks :D sqlplus -s /nolog @${SQLFILE} ${file_type} >> ${OUTPUT_FILE} date "+%Y%m%d%H%M%S" $/usr/bin/echo "INFO : $1"... (2 Replies)
Discussion started by: zound617
2 Replies

2. Shell Programming and Scripting

Shell Script to Kill Process(number of process) Unix/Solaris

Hi Experts, we do have a shell script for Unix Solaris, which will kill all the process manullay, it used to work in my previous env, but now it is throwing this error.. could some one please help me to resolve it This is how we execute the script (and this is the requirement) ... (2 Replies)
Discussion started by: jonnyvic
2 Replies

3. Solaris

can someone describe

What is openboot prompt is this some kind of shell , and where is located :p (2 Replies)
Discussion started by: solaris_user
2 Replies

4. UNIX for Advanced & Expert Users

DEscribe this command

Hi all. can you please tell me (root) NOPASSWD: ALL what this command means (1 Reply)
Discussion started by: mindtee_abhi
1 Replies

5. Shell Programming and Scripting

special question, hard to describe in few words...

dear coders, i need some inspiration again... there is something what i always wanted to know... how to code following: value 1: __________ value 2: __________ important: when my "script" starts the display has to be cleared and two lines are shown (see above), the cursor has to be... (2 Replies)
Discussion started by: pseudocoder
2 Replies

6. Shell Programming and Scripting

I'm not sure how I would describe this

Some software, when you install it, prompts you for variables, such as the username for the software, or the password you are setting for the software's root account. I would like to know if it is possible to do such a thing, and if yes, how? basically, I would like my installer script to prompt... (3 Replies)
Discussion started by: Bakes
3 Replies

7. Solaris

Describe the parameters of iostat,vmstat and mpstat

Hi, I am trying to take the Statistics of the machine during load.Can someone explian the parameters of iostat: tty sd1 sd2 sd3 sd4 cpu tin tout kps tps serv kps tps serv kps tps serv kps tps serv us sy wt id vmstat: kthr ... (1 Reply)
Discussion started by: grrajeish
1 Replies

8. UNIX for Dummies Questions & Answers

Describe what this line of code would do

I am confused about what this line of code would do. I understand all the pieces but not the final outcome. Anyone that could help? find / -type f -xdev -mtime 0 -size 100000 -exec ls -lht {} /; 2>/dev/null (4 Replies)
Discussion started by: NickNine
4 Replies

9. UNIX for Dummies Questions & Answers

describe unix commands in english

the following unix command ls | grep'?cw1' | wc -l converting it to english is it going to be like list the result of the search '?cw1' in number of lines is that correct ? (4 Replies)
Discussion started by: props
4 Replies
Login or Register to Ask a Question