If processes can share data (global variables for example), we sometimes need a way of coordinating the actions of a set of processes as they try to manipulate this shared data (remember critical sections, precedence and race conditions from 315? - if not - review now).
Different operating systems and languages provide different mechanisms for coordinating the activities of such processes. Again from 315 you probably remember the topics of:
XINU provides sempahores for process synchronization. The operations are as follows:
semnum = screate(initial_value);
wait(semnum);
signal(semnum);
The choice of the subroutine names wait and signal are, in my opinion very unfortunate. These names are really stolen from monitors - but as you hopefully remember, wait and signal in monitors are very different operations than are the semaphore operations P() and V(). P() and V() are stateful in that ach time you do a V() even if no one does a P(), the value of the semaphore changes (it is incremented). The monitor operations wait() and signal() are stateless in that if there is no process blocked on wait(), it doesn't matter how many times you perform signal() - it will have no effect.
So - calling the sempahore operations in XINU wait() and signal() confuses their sematics with the monitor operations of that name. To set the record straight, wait() and signal() in XINU are sempahore operations and they are stateful. They are not monitor operations.
Wait() is the same as P(). and signal() is the same as V() - and they should have been named that way.
If I wanted in my previous coding example (on the Process Creation page) to make sure the output was ABABABAB... (alternating A's and B's) then how would I change the XINU program?
#include <conf.h>
#define P wait
#define V signal
main()
{
int goa, gob;
int prA(), prB();
goa = screate(1);
gob = screate(0);
resume( create (prA, 12000, 20, "pra", 2, goa, gob ) );
resume( create (prB, 12000, 20, "prb", 2, goa, gob ) );
}
prA(int sema, semb)
{
while(1)
{
P(sema);
putc(CONSOLE, 'A');
V(semb);
}
}
prB(int sema, semb)
{
while(1)
{
P(semb);
putc(CONSOLE, 'B');
V(sema);
}
}