Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Semaphore example (Windows Regina)

47 views
Skip to first unread message

Arthur T.

unread,
Dec 5, 2008, 8:54:03 PM12/5/08
to
Earlier, I had asked for any examples of using semaphores. None were
forthcoming. As suggested, I e-mailed Mr. McPhee. He sent me very useful
sample code, but didn't specifically say I could publicly post his samples.

Using his samples, though, I was able to determine what I was doing
wrong. So that the next person to search will find something, here is the
code I came up with. They don't do anything particularly useful except to
prove that the semaphore code works correctly.

SemServ.rex:

/*REXX exec to play around with semaphores before doing it for real */

/* Standard entry code */
arg temp1
do while temp1 \= ''
parse var temp1 temp2 temp1
if right(temp2,1) = ')' then
temp2 = translate(temp2,'= ','()')
if pos('=',temp2) = 0 then temp2 = temp2 || '= ""'
interpret temp2
end
drop temp1
drop temp2
if debug = 'TRACE' then trace ?i
else if debug \= "DEBUG" then trace i
/* */

call rxfuncadd 'sysloadfuncs','RexxUtil','sysloadfuncs'
call sysloadfuncs

numCli = 10 /* number of clients to start */

mutexHandle = syscreatemutexsem('semtest')
if mutexhandle = '' then
do
say 'Error creating Mutex semaphore'
exit 8
end

do i = 1 to numcli
eventHandle.i = syscreateeventsem(i)
if eventhandle = '' then
do
say 'Error creating Event semaphore' i
exit 8
end

"start /B semcli" i
end

do i = numcli to 1 by -1 /* wait for them in reverse order of starting */
x = syswaiteventsem(eventhandle.i,15000)
if x <> 0 then say 'Event' i 'did not finish.'
end

say 'End of Serv'
exit 0


SemCli.rex:

/*REXX exec to play around with semaphores before doing it for real */

/* Standard modified entry code */
arg cli# temp1
do while temp1 \= ''
parse var temp1 temp2 temp1
if right(temp2,1) = ')' then
temp2 = translate(temp2,'= ','()')
if pos('=',temp2) = 0 then temp2 = temp2 || '= ""'
interpret temp2
end
drop temp1
drop temp2
if debug = 'TRACE' then trace ?i
else if debug \= "DEBUG" then trace i
/* */

call rxfuncadd 'sysloadfuncs','RexxUtil','sysloadfuncs'
call sysloadfuncs

datafile = 'f:\semtest.dat'

mutexHandle = sysopenmutexsem('semtest')
if mutexhandle = '' then
do
say 'Error Opening Mutex semaphore in client #' cli#
exit 8
end

eventHandle = sysopeneventsem(cli#)
if eventhandle = '' then
do
say 'Error creating Event semaphore in client #' cli#
exit 8
end

n = random(1,10)
x = sleep(n) /* simulate doing 1 to 10 seconds worth of work */

/*
deliberately choose a wait time low enough that at least one
will fail, usually. This IS for testing, after all.
*/
x = sysrequestmutexsem(mutexhandle, 1000)
if x = 0 then
do
do i = 1 to 5
call lineout datafile, right(i,3) 'From client' right(cli#,3)
end
call stream datafile, 'c', 'close'
x = sleep(1)
x = sysreleasemutexsem(mutexhandle)
end
else
do
say 'Could not get exclusive use in client #' cli#
end

x = sysposteventsem(eventhandle)
say 'End of Client #' cli#
exit 0


--
Arthur T. - ar23hur "at" intergate "dot" com
Looking for a z/OS (IBM mainframe) systems programmer position

0 new messages