CREATESEM
(Statement)
Creates a semaphore.
CREATESEM (<Arithmetic expression>)
This function creates a semaphore and obtains a semaphore ID.
If there is no semaphore ID, other semaphore related commands cannot be used. Therefore, be sure to execute this CREATESEM command prior to using a semaphore.
Designate a task queuing (execution waiting) system using an argument. Determine the execution order if plural tasks have the same semaphore.
The following two types of queuing systems are available.
Argument
|
|
---|---|
0
|
First-come sequence
|
Other than 0
|
Priority order of tasks
|
First-come sequence is the order in which TAKESEM commands are executed.
Task priority is designated with an option (PRIORITY) of the RUN command.
As soon as CREATESEM is executed, the system status changes to the one where a semaphore is present. Therefore, TAKESEM can be executed even if GIVESEM is not executed.
Up to 32 semaphores can be created.
Notes on using a CREATESEM instruction:
-
Phenomena which occur due to wrong usageIf one of the following actions is executed when a program which already has a semaphore ID created by a CREATESEM instruction is being executed or suspended and in the wait status, the program with the semaphore ID will be left in the wait status without being able to obtain a semaphore.
-
Rewrite a semaphore ID storage variable using CREATESEM.or,
-
Purposely rewrite the variable from the program pendant.
-
ExampleStart up pro1 and instantaneously stop using the STOP key during execution of pro2. After that, if the system is restarted, pro3 will wait for a semaphore indefinitely since the semaphore ID stored in i1 will be changed.PROGRAM PRO1i1 = CREATESEM(0)RUN PRO2RUN PRO3ENDPROGRAM PRO2TAKESEM i1···GIVESEM i1ENDPROGRAM PRO3TAKESEM i1···GIVESEM i1END
-
Countermeasure when an infinite wait occurs.To get out of this status, run a calling program (ipro1 in this example) after stopping the program (pro3 in this example) that is in an infinite wait status.
-
Observe the following for proper use of this statement.Do not rewrite the ID of a semaphore that is in a wait status. Especially observe the following two points.
-
Create a semaphore that uses the same variable number only once as long as it is not clearly deleted.
-
Do not use a variable which controls a semaphore ID for other purposes.
DEFINT Li1, Li2, Li3 = 1
|
|
Li1 = CREATESEM(Li3)
|
'Creates a semaphore with the queuing system
'designated in Li3 and the semaphore
'ID obtained in Li1.
|
Li2 = CREATESEM(Li3)
|
'Creates a semaphore with the queuing system
'designated in Li3 and the semaphore
'ID obtained in Li2.
|
TAKESEM Li1
|
'Obtains the semaphore designated in Li1.
|
TALESEM Li2, 100
|
'Obtains the semaphore designated in Li1.
'However, a timeout occurs after 100 ms.
|
RUN samp1
|
|
GIVESEM Li1
|
'Releases one task from the wait status
'which has the semaphore designated in Li1.
|
FLUSHSEM Li2
|
'Releases all tasks from the wait status
'which have the semaphore designated in Li2.
|
DELETESEM Li1
|
'Deletes the semaphore with the semaphore
'ID designated in Li1.
|
DELETESEM Li2
|
'Deletes the semaphore with the semaphore
'ID designated in Li2.
|