1. Field of the Invention
The present invention relates to a method of managing semaphores that are used for executing exclusive control of resources in a multi-tasking operating system.
2. Description of the Related Art
In a multi-tasking operating system, semaphores are largely used for implementing exclusive control of resources. A task that needs to exert an exclusive control over the resource first creates a semaphore by a “Create semaphore” command. During semaphore creation, a pointer to the semaphore structure, an index of the semaphore structure array, or a handle provided by the operating system for operating the semaphore (hereinafter, “semaphore handle”) is returned to the processor executing the task. The processor executing the task acquires the semaphore by an “Acquire semaphore” command, with the semaphore handle specified in the command, and gains exclusive access to the resource. After the resource has been used, the processor releases the resource by a “Release resource” command, with the semaphore handle specified in the command.
The term “task” refers to a program created by a user for realizing an intended function. The user needs to know the correlation between the semaphore handle and the resource for implementing exclusive control over the resource using the commands for operating the semaphore (Create semaphore, Acquire semaphore, and Release semaphore).
However, the semaphore handle being a numerical value makes it difficult for establishing a correlation between the semaphore handle and the resource intuitively, thereby making the management of the correlation between the semaphore handle and the resource a difficult task.
As a remedy to this problem, a resource management method is disclosed in Japanese Patent Laid-Open Publication No. H5-81048 in which the resources are managed by assigning them names (that is, representing the resources by symbols), thus implementing smooth program (task) creation and diversion and good resource management.
Specifically, the task notifies a resource management module of the symbol of the resource in use. An identifier managing unit of the resource management module acquires the symbol from a task, and searches an identifier management table containing resource symbols and identifiers (semaphore handles) in a correlated form, finds an unassigned semaphore handle, registers the semaphore handle in association with the acquired symbol, and sets the semaphore handle as “in use”. The identifier managing unit also notifies the semaphore handle to the task via the identifier assigning unit. Using the semaphore handle, the task issues a system call to the multi-tasking operating system, and uses the resource. After the resource has been used, the task notifies the resource management module of the semaphore handle. The identifier managing unit accepts the semaphore handle via the identifier assigning unit of the resource management module, deletes the symbol associated with the notified semaphore handle in the identifier management table, and sets the semaphore handle as “not in use”.
In the technology disclosed in Japanese Patent Laid-Open Publication No. H5-81048, an identifier management table is provided, which stores resource names represented by symbols, and semaphore handles in correlation. The task notifies the symbol to the resource management module that manages the identifier management table, and in response, is notified of the semaphore handle assigned to the symbol, using which the task operates the resource. Thus, the user creating the task can do so with the symbol alone and does not need to know the correlation between the symbol representing the resource and the semaphore handle.
In the conventional technology disclosed in Japanese Patent Laid-Open Publication No. H5-81048, the user does not have to know the correlation between the symbol and the semaphore number. However, to use the semaphore using the semaphore handle returned by the resource management module, the user must manage the semaphore handle within the task.
Further, in the conventional technology, first the task has to acquire the semaphore handle associated with the symbol, and then acquire the semaphore using the semaphore handle. That is, the task needs to issue two commands, namely, “Acquire semaphore handle” and “Acquire semaphore”, thus increasing the number of steps.
Further, for using the semaphore, the task creates a semaphore structure corresponding to the semaphore by “Create semaphore” command, and acquires the semaphore handle of the semaphore corresponding to the created semaphore structure. However, the acquired semaphore handle has to be notified to all the tasks using the created semaphore.
It is an object of the present invention to at least solve the problems in the conventional technology.
According to an aspect of the present invention, a method of semaphore management, implemented in a multi-tasking operating system that performs exclusive control of resources using semaphores, the method includes receiving from a task, a semaphore operation command for exclusive control of a resource, where the semaphore operation command includes a resource symbol specifying the resource to be controlled; managing a symbol-handle table, including searching and retrieving from the symbol-handle table, a semaphore handle associated with the resource symbol included in the semaphore operation command, where the symbol-handle table stores a resource symbol identifying a resource in association with a semaphore handle identifying the semaphore controlling the resource; acquiring a semaphore based on semaphore information of the semaphore corresponding to retrieved semaphore handle, if the semaphore operation command is an “Acquire semaphore” command, and assigning the resource controlled by acquired semaphore to a task that issued the “Acquire semaphore” command, where the semaphore information specifies a usage status of the semaphore, and is registered in a semaphore structure corresponding to the semaphore; and releasing a semaphore based on the semaphore information of the semaphore corresponding to the retrieved semaphore handle, if the semaphore operation command is a “Release semaphore” command, and releasing the resource controlled by released semaphore from a task that issued the “Release semaphore” command.
According to another aspect of the present invention, a computer-readable recording medium that records thereon a computer program for semaphore management, implemented in a multi-tasking operating system that performs exclusive control of resources using semaphores, the computer program including instructions which, when executed, cause a computer to execute the above method.
The above and other objects, features, advantages and technical and industrial significance of this invention will be better understood by reading the following detailed description of presently preferred embodiments of the invention, when considered in connection with the accompanying drawings.
Exemplary embodiments of the present invention are explained in detail below, with reference to the accompanying drawings. The invention is not limited by the embodiments described herein.
An embodiment of the present invention is explained next with reference to
The task 5 represents the programs created by a user, and includes various commands for realizing intended functions. The commands output by the task 5 are semaphore operation commands for operating the semaphores that exert exclusive control over the resources 7. The semaphore operation commands are commands for using the resources 7. The semaphore operation commands include Acquire semaphore (P command in
The resources 7 may be memory, printer, etc., used by the operating system 2, and each of the resources 7 is assigned a symbol. A symbol easily recognized by the user can be assigned to the resource 7. In
The semaphore managing unit 3 includes semaphore structures 6 (represented by 6-1 through 6-n), a symbol-handle association table 4, and a semaphore operating unit 8. The semaphore structures 6 are arranged in a predetermined sequence (as a semaphore structure array), and each semaphore structure 6 is associated with the resource 7 it has exclusive control over. The semaphore managing unit 3 recognizes each semaphore structure 6 by a distinctive index or pointer to the semaphore structure array.
In
The semaphore structure 6 contains semaphore information related to the usage status of the semaphore in question. The semaphore information includes a flag 61 that contains status or attribute of the semaphore, a counter 62 that contains the current value of the semaphore, and a pending task queue 63 that contains a process ID of the task 5 waiting to use the semaphore.
The symbol-handle association table 4 contains the semaphore handles of the semaphores associated with the symbols of the resources 7 the semaphores have exclusive control over. When the task 5 issues a semaphore operation command, the semaphore operating unit 8 acquires or releases the semaphore based on the symbol-handle association table 4 and the semaphore structure 6.
The semaphore operating unit 8 includes an association table managing unit 81 that manages the symbol-handle association table 4, a semaphore creating unit 82 that creates the semaphore structure 6 corresponding to the semaphore, a semaphore acquisition processing unit 83 that implements a semaphore acquisition process based on the semaphore structure 6, and a semaphore release processing unit 84 that implements a release process of the semaphore based on the semaphore structure 6.
The functioning of the multi-tasking system 1 implementing the semaphore managing method according to the present invention is explained next with reference to
Upon receiving the Acquire semaphore command, the association table managing unit 81 of the semaphore operating unit 8 retrieves from the symbol-handle association table 4, the semaphore handle corresponding to the symbol included in the Acquire semaphore command (step S101).
If the symbol-handle association table 4 contains the semaphore handle corresponding to the symbol included in the Acquire semaphore command (“Yes” at step S102), the association table managing unit 81 sends an Acquired semaphore notification to the semaphore acquisition processing unit 83, including the retrieved semaphore handle therein.
Upon receiving the Acquired semaphore notification, the semaphore acquisition processing unit 83 implements the semaphore acquisition process using the semaphore handle included in the Acquired semaphore notification (step S103). Specifically, the semaphore acquisition processing unit 83 checks the count value in the counter 62 of the semaphore structure 6 corresponding to the semaphore handle included in the Acquired semaphore notification, to determine whether the semaphore can be acquired. The count value indicates the current value of the semaphore corresponding to the semaphore structure 6. The semaphore acquisition processing unit 83 determines that the semaphore cannot be acquired if the count value is 0, and the semaphore can be acquired if the count value is not 0. If it is determined that the semaphore can be acquired, the semaphore acquisition processing unit 83 decrements the count value of the counter 62 of the semaphore structure 6 corresponding to the semaphore handle included in the Acquired semaphore notification, and acquires the semaphore.
Upon acquiring the semaphore (“Yes” at step S104), the semaphore acquisition processing unit 83 notifies the task 5 that the semaphore has been acquired. After this notification, the task 5 implements the intended process by using the resource 7 exclusively controlled by the acquired semaphore (step S105).
If it is determined that the semaphore cannot be acquired (“No” at step S104), the semaphore acquisition processing unit 83 sets the process ID of the task 5 that output the Acquire semaphore command, into the pending task queue 63 of the semaphore structure 6 corresponding to the semaphore handle (step S106).
When another task 5 releases the awaited semaphore (step S107), the semaphore acquisition processing unit 83 implements the semaphore acquisition process and acquires the semaphore, and notifies the task 5 that the semaphore has been acquired, and the task 5 executes the intended process using the resource 7 exclusively controlled by the acquired semaphore (steps S103 through S105). While acquiring the semaphore from the task 5 awaiting the semaphore, the semaphore acquisition processing unit 83 decrements the value of the counter 62 and deletes the process ID of the task 5 from the pending task queue 63.
If the semaphore handle cannot be retrieved from the symbol-handle association table 4, that is, if the symbol-handle association table 4 does not include an entry of the symbol included in the Acquire semaphore command and its corresponding semaphore (“No” at step S102), the association table managing unit 81 sends a Create semaphore command to the semaphore creating unit 82.
Upon receiving the Create semaphore command, the semaphore creating unit 82 adds a new semaphore structure 6 to the semaphore structure array to create a semaphore (step S108). Next, the semaphore creating unit 82 sends the association table managing unit 81 a creation completion notification, including in it the semaphore handle provided by the operating system 2 during semaphore creation.
Upon receiving the creation completion notification, the association table managing unit 81 sets the semaphore handle included in the creation completion notification into the symbol-handle association table 4, in association with the symbol included in the Acquire semaphore command (step S109). The association table managing unit 81 notifies the Acquire semaphore command to the semaphore acquisition processing unit 83, including therein the semaphore handle present in the creation completion notification, that is, the semaphore handle set in the symbol-handle association table 4.
The semaphore acquisition processing unit 83 implements the semaphore acquisition process, and notifies the task 5 that the semaphore has been acquired, and the task 5 executes the intended process using the resource 7 exclusively controlled by the acquired semaphore (steps S103 through S105).
The functioning of the Release semaphore command is explained next with reference to the flow chart in
Upon receiving the Release semaphore command, the association table managing unit 81 of the semaphore operating unit 8 retrieves from the symbol-handle association table 4, the semaphore handle associated with the symbol included in the Release semaphore command (step S201). The association table managing unit 81 sends a Release semaphore instruction to the semaphore release processing unit 84, including in the instruction the retrieved semaphore handle.
Upon receiving the Release semaphore instruction, the semaphore release processing unit 84 implements a semaphore release process using the semaphore handle included in the Release semaphore instruction (step S202). Specifically, the semaphore release processing unit 84 increments the count value of the counter 62 of the semaphore structure 6 corresponding to the semaphore handle.
The semaphore release processing unit 84 notifies the task 5 that the semaphore has been released. The task 5 recognizes this step as the end of the semaphore release process, and executes the next command (step S203).
Thus, according to the present embodiment, the symbol-handle association table 4 contains, in an associated form, the symbol that functions as an identifier for the resource 7, and the semaphore handle that functions as an identifier for the semaphore that controls the resource 7. The association table managing unit 81 retrieves from the symbol-handle association table 4 the semaphore handle corresponding to the symbol included in the semaphore operating command issued by the task 5. If the semaphore operating command is Acquire semaphore, the semaphore acquisition processing unit 83 acquires the semaphore corresponding to the retrieved semaphore handle and assigns the resource 7 controlled by the acquired semaphore to the task 5 that issued the Acquire semaphore command. If the semaphore operating command is Release semaphore, the semaphore release processing unit 84 releases the semaphore corresponding to the retrieved semaphore handle and releases the resource 7 controlled by the semaphore from the task 5 that issued the Release semaphore command. Consequently, implementation of exclusive control using semaphores can be realized by operating the semaphores using the symbols assigned to the resource 7, obviating the need for managing the semaphore handles within the task 5.
Further, by operating the semaphores using the symbols assigned to the resource 7 and not having to manage the semaphore handles within the task 5, the need for notifying the semaphore handle to all the tasks 5 using the same semaphore is obviated.
Further, according to the present embodiment, if the semaphore handle associated with the symbol included in the semaphore operation command issued by the task 5 is not set in the symbol-handle association table 4, the semaphore creating unit 82 generates a new semaphore structure 6 to create a semaphore, and the association table managing unit 81 associates the semaphore with the symbol included in the semaphore operation command. The semaphore creating unit 82 sets the semaphore handle of the created semaphore in the symbol-handle association table 4, and the semaphore acquisition processing unit 83 acquires the semaphore. Consequently, semaphore creation and semaphore acquisition are carried out by a single command, reducing the number of steps of the task 5.
Further, for exerting exclusive control using semaphore, semaphores can be operated using the symbol of resource 7 and without having to manage the semaphore handle within the task 5. Consequently user is spared the additional labor when creating the task 5. Additionally, errors relating to associating the semaphores with the symbols are avoided, improving the quality of the program.
In the present embodiment, when the semaphore handle can not be retrieved from the symbol-handle association table 4 during the Acquire semaphore command, the semaphore is created unconditionally. As shown in a flowchart in
The functions of the association table managing unit 81, the semaphore creating unit 82, the semaphore acquisition processing unit 83, and the semaphore release processing unit 84 of the semaphore operating unit 8 according to the present embodiment can also be realized by providing a program that can be executed by a microprocessor such as a central processing unit (CPU), etc., operated by the operating system 2.
According to the present invention, implementation of exclusive control using semaphores can be realized by operating the semaphores using the symbols assigned to the resource, obviating the need for managing the semaphore handles within the task.
Although the invention has been described with respect to a specific embodiment for a complete and clear disclosure, the appended claims are not to be thus limited but are to be construed as embodying all modifications and alternative constructions that may occur to one skilled in the art that fairly fall within the basic teaching herein set forth.
Number | Date | Country | Kind |
---|---|---|---|
2005-375386 | Dec 2005 | JP | national |