*BSD News Article 23682


Return to BSD News archive

Path: sserve!newshost.anu.edu.au!munnari.oz.au!constellation!osuunx.ucc.okstate.edu!moe.ksu.ksu.edu!crcnis1.unl.edu!wupost!howland.reston.ans.net!agate!library.ucla.edu!csulb.edu!nic.csu.net!osiris!olson
Newsgroups: comp.unix.bsd
Subject: How to cause preemption under Unix???
Message-ID: <1993Nov10.200248.7409@nic.csu.net>
From: olson@osiris (Brian Olson)
Date: 10 Nov 93 20:02:47 PST
Nntp-Posting-Host: osiris.cs.csufresno.edu
X-Newsreader: TIN [version 1.1 PL8]
Lines: 47

friends and gurus, 

A Unix programming question:

Does anyone know of a way (system call, etc.) that a running 
process may voluntarily give-up the CPU?  That is, is there a way
for the active process to cause a context switch to be performed,
while still leaving itself in the ready queue?

Why would I want to do such a thing in a preemptive environment, you
ask?  Well suppose the process was waiting around for some flag to be
raised, or timeout trying.  It would be nice to implement timeouts in 
this fashion, like so:

Process A:                                      Process B:  
-------------                                   ---------------------
flag = FALSE;                                   ...
while (!flag)                                   ...
   {                                            ...
   if ((startTime - clock) < waitTimeout)       flag=TRUE; 
       SignalPreemption();                      ...
   else                                         ...
        break;	                                ...
   }
if (!flag)
	return TIMEOUT_ERROR;

..
..

**Note that flag is Shared Memory (and both processes have it attached).
Also for various other reasons semaphores and signals are not adequate.


Basically, I want to avoid the inefficiency of the "spin-loop",
constantly checking for the condition or the timeout.  If Unix gives
me a time slice, and after checking, I have nothing to do, I should be
able to give it back immediately.  Right?


Thanks for any answers or comments,
indebted to the net, as usual.

-Brian
olson@osiris.engr.csufresno.edu