Search code examples
csemaphorevxworks

Assigning one semaphore to other


Can I assign one sempahore to other semaphore, will both behave as the same, for eg.(this vxworks code)

   mySemId = semCreate(...)
       tempSemId = mySemId;
    semTake(tempSemId,...);
   ...
   semGive(mySemId).

will the above code snippet work in locking the resource.

Secondly, if this works is there a coding standard or practice that does not allow this kind of processing.


Solution

  • vxWorks semaphore IDs are copyable, and the copy is "just as good" as the original, so the code sample shown here is not wrong. It does not seem a particularly good idea, though. It's hard to tell from this snippet what the entire code looks like, and in a larger block of code, I think it would be difficult to tell which semaphore(s) are being Taken and which are being Given.

    (This is not a C language question: vxWorks semaphores are mainly usable from C, but are not defined by the language.)