/usr/share/doc/python-svipc/examples/sem-demo.py is in python-svipc 0.16-2build4.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | #! /usr/bin/env python
import sys, os
from os import getpid, fork
from time import sleep, time
from svipc import sem_init, sem_take, sem_give, sem_cleanup
# create id
sid = 0x7dcb0000 | getpid()
# create a pool with one semaphore
sem_init(sid,nums=1)
# time unit for test
delay=0.5
class unit_test(object):
def __init__(self):
self.errmsg=''
if fork():
if not self.parent(): print 'OK'
else: print 'ERROR', self.errmsg
else:
self.child()
exit()
def parent(self): return 0
def child(self): pass
class ut1(unit_test):
def parent(self):
print '- wait for child forever:'
s = sem_take(sid, 0)
return s
def child(self):
sleep(delay)
sem_give(sid,0)
class ut2(unit_test):
def parent(self):
print '- wait for child with timeout:'
tstart=time()
s = sem_take(sid, 0, wait= 2 * delay)
if time()-tstart < delay:
self.errmsg='timeout too quickly.'
return -2
return s
def child(self):
sleep(delay)
sem_give(sid,0)
class ut3(unit_test):
def parent(self):
print '- wait for child with timeout, and timeout:'
tstart=time()
s = sem_take(sid, 0, wait=delay)
if time()-tstart < delay:
self.errmsg='did not wait long enough'
return -2
self.errmsg='wait did not time out.'
return not s
def child(self):
pass
class ut4(unit_test):
def parent(self):
print '- wait for child x10 times:'
s = sem_take(sid, 0, count=10)
return s
def child(self):
for i in range(10):
sem_give(sid,0)
class ut5(unit_test):
def parent(self):
print '- try take semaphore (available):'
sleep(delay)
s = sem_take(sid, 0, wait=0)
self.errmsg='sem was not available'
return s
def child(self):
sem_give(sid,0)
class ut6(unit_test):
def parent(self):
print '- try take semaphore (non-available):'
s = sem_take(sid, 0, wait=0)
self.errmsg='sem was available'
return not s
def child(self):
pass
ut1()
ut2()
ut3()
ut4()
ut5()
ut6()
sem_cleanup(sid)
|