delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/08/13/09:55:37

From: i96csm AT river DOT tay DOT ac DOT uk
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Volatile
Message-ID: <1997Aug13.095730.2@river.tay.ac.uk>
Date: 13 Aug 97 09:57:30 +0100
References: <Pine DOT D-G DOT 3 DOT 91 DOT 970811124422 DOT 3813H-100000 AT dg1>
Organization: University of Abertay Dundee
Lines: 108
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

In article <Pine DOT D-G DOT 3 DOT 91 DOT 970811124422 DOT 3813H-100000 AT dg1>, "Art S. Kagel" <kagel AT ns1 DOT bloomberg DOT com> writes:
> On 7 Aug 1997, Paul Derbyshire wrote:
> 
>> 
>> Andrew Crabtree (andrewc AT typhoon DOT rose DOT hp DOT com) writes:
>> >>   I know it sounds crazy, but what does the 'voltaile' with a
>> >> variable/function ?
>> 
>> What the heck is a "semaphore"???
> 
> From "UNIX Network Programming" by W. Richard Stevens (p137):
> 
> 	Semaphores are a synchronization primitive.  As a form of IPC
> 	they ... are intended to let multiple processes synchronize
> 	their operations. ...  Consider a semaphore as an integer
> 	valued variable that is a resource counter.  The value of the
> 	variable at any point in time is the number of resource units 
> 	available.

  
> 
> A binary semaphore is a semaphore whose valid values are 0 and 1 which
> is normally used to track and protect usage of a single resource.
> Mutex semaphores are a newer high speed form of semaphore which is
> part of the POSIX light weight thread primitives.  Get an read
> Stevens' book, it is the BEST reference on UNIX Interprocess
> Communication available.

Also, a semaphore is checked AND modified at the SAME time, to stop a program
checking it, yielding to another program, which then checks and graps the
semaphore like this


   prog A               prog B

   check semaphore
        |
        +--- (yield) -->--+ 
                          |

                        check semaphore
                          |
                          |
                        raise semaphore
                          |
                          |
         +--<-(yield)-----+
         |
         |
    raise semaphore

Both progs. think that they have raised the semaphore, and have access to the
resource, which means that the resource will get confuse. Eg. what happens if
two programs try to print at once?

If a program cannot raise the semaphore, it is normally (but not always)
blocked by the O/S, and then restarted when the other program lowers the
semaphore.


In answer to Q2,
the keyword volatile tells the compiler that the variable can be changed at
any time, normally by an interrupt or directly by an PC device card.
The compiler MUST NOT store a copy of the variable in a CPU register.

for example

extern volatile int* isReady=(volatile int*)0x1000; 
/* isReady points to device's status register */
 
void get_data(char * buff)
{
   while (!(*isReady)) {}; /* Loop until ready; */

/*
....
read the data

*/


}

If it wasn't for the volatile keywords the program will read the
value of isReady into a CPU register when the loop is encountered, on each
call the function.
This means that is the device was not ready when the function is called
it checks the copy of the register (not the actual value), and will
not notice any change.

This is intended as sample code only, I don't know what is at address 0x1000
on a PC, and if protected mode code will even let you access it like this.



I hope this clears matters up a bit
  


         
                 

> 
> Art S. Kagel, kagel AT bloomberg DOT com
> 



- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019