_os_sleep issue

_os_sleep issue

Post by Jer » Wed, 04 Nov 2009 23:21:02


Hi All,
I am making a project for college where I must create pipes, send
messages, after x seconds print the message and after y seconds send
the message to a new process on a different pipe. Each process is send
and receiving the messages successfully but when I try to write a
startup and enter the timing's some messages are not send. I am trying
to use:

TimeToDelay = 600;
TimeToPrint = 325;
{
//When I want to print the message I am entering:
_os_sleep(&TimeToPrint,&dummysignal);

//Then when I want to send it I simple delay the process by using:
_os_sleep(&TimeToDelay,&dummysignal);

There is 5 processes and the startup must fork them. Is there
something wrong with my sleep syntax or what is the probable cause of
the issue?

Regards,
Jer
 
 
 

_os_sleep issue

Post by Ian Goldb » Thu, 05 Nov 2009 18:56:06

It's hard to say much from the small fragment of code you gave, but
here are a few general comments.

1. _Always_ check the return value whenever you call an _os_*()
function. If you get an error you need to handle that error.

2. Remember that _os_sleep() wakes early if the process gets a signal.
(If you don't have a signal handler the process will be terminated, so
you must have a signal handler.) Always put _os_sleep() in a loop so
that the full sleep time is completed even if it gets woken by a
signal.

3. Consider using alarms rather than timed sleeps.

4. Pipes show up in the filing system with the path /pipe/<name>. You
can use shell functions like dir -e to examine what pipes have been
created, and look at their contents with dump. A pipe will continue to
exist until all the data has been read from it, so if you have pipes
lying around it means the reader hasn't completed its task.

Please let us know how you get on. By sharing your answer you
encourage other people to make suggestions.


> //Then when I want to send it I simple delay the process by usin>:
> _os_sleep(&TimeToDelay,&dummysignal>; >>
> There is 5 processes and the startup must fork them. Is the>e
> something wrong with my sleep syntax or what is the probable cause >f
> the issu>? >>
> Regard>,
> Jer

 
 
 

_os_sleep issue

Post by Ric Yeates » Fri, 06 Nov 2009 01:50:04

Related to #2 below. _os_sleep() will update the ticks value with the
number of ticks remaining to sleep. So, if the sleep is not
interrupted early by a signal, the updated value will be 0. If this
updated value is used, you will end up sleeping forever (until a
signal, of course).

For example, this code might not behave as expected:
ticks = 100;
_os_sleep(&ticks, &sig);
printf("first printf\n");
_os_sleep(&ticks, &sig);
printf("second printf\n");

The second printf() won't happen because ticks was updated to 0 by the
first _os_sleep(). Always initialize ticks before each sleep.

The sleep loop that was mentioned below looks something like this:
ticks = 100;
while (ticks)
_os_sleep(&ticks, &sig);

NOTE: I omitted error checking from all my code to make it easier to
read.
NOTE ALSO: _os_sleep() only returns an error if there's a catastrophic
problem. Getting a signal during the sleep will still result in
SUCCESS being returned.

Ric



> > _os_sleep(&TimeToPrint,&dummysignal>; >>> > > //Then when I want to send it I simple delay the process by usin>:> > > _os_sleep(&TimeToDelay,&dummysignal>; >>> > > There is 5 processes and the startup must fork them. Is the>e> > > something wrong with my sleep syntax or what is the probable cause >f> > > the issu>? >>> > > Regard>,> > > Jer- Hide quoted text>- >>
> - Show quoted text -