![]() |
Software > OpenVMS Systems > Documentation > 82final > 5841 ![]() HP OpenVMS Systems Documentation |
![]() |
HP OpenVMS Programming Concepts Manual
4.8 Synchronizing Programs by Specifying a Time for Program ExecutionYou can synchronize timed program execution in the following ways:
4.8.1 Obtaining the System TimeThe process control procedures that allow you to synchronize timed program execution require you to supply a system time value. You can use either system services or RTL routines for obtaining and reading time. They are summarized in Table 4-10. With these routines, you can determine the system time, convert it to an external time, and pass a time back to the system. The system services use the operating system's default date format. With the RTL routines, you can use the default format or specify your own date format. However, if you are just using the time and date for program synchronization, using the operating system's default format is probably sufficient. When using the RTL routines to change date/time formats, initialization routines are required. Refer to the HP OpenVMS RTL Library (LIB$) Manual for more information. See Chapter 27 for a further discussion of using timing operations with the operating system.
4.8.1.1 Executing a Program at a Specified TimeTo execute a program at a specified time, use LIB$SPAWN to create a process that executes a command procedure containing two commands---the DCL command WAIT and the command that invokes the desired program. Because you do not want the parent process to remain in hibernation until the process executes, execute the process concurrently. You can also use the SYS$CREPRC system service to execute a program at a specified time. However, because a process created by SYS$CREPRC hibernates rather than terminates after executing the desired program, HP recommends you use the LIB$SPAWN routine unless you need a detached process. Example 4-14 executes a program at a specified delta time. The parent program prompts the user for a delta time, equates the delta time to the symbol EXECUTE_TIME, and then creates a subprocess to execute the command procedure LATER.COM. LATER.COM uses the symbol EXECUTE_TIME as the parameter for the WAIT command. (You might also allow the user to enter an absolute time and have your program change it to a delta time by subtracting the current time from the specified time. Chapter 27 discusses time manipulation.)
LATER.COM
4.8.1.2 Executing a Program at Timed IntervalsTo execute a program at timed intervals, you can use either LIB$SPAWN or SYS$CREPRC. Using LIB$SPAWN, you can create a subprocess that executes a command procedure containing three commands: the DCL command WAIT, the command that invokes the desired program, and a GOTO command that directs control back to the WAIT command. Because you do not want the parent process to remain in hibernation until the subprocess executes, execute the subprocess concurrently. See Section 4.8.1.1 for an example of LIB$SPAWN. Using SYS$CREPRC, create a detached process to execute a program at timed intervals as follows:
Example 4-15 executes a program at timed intervals. The program creates a subprocess that immediately hibernates. (The identification number of the created subprocess is returned to the parent process so that it can be passed to SYS$SCHDWK.) The system wakes up the subprocess at 6:00 a.m. on the 23rd (month and year default to system month and year) and every 10 minutes thereafter.
4.8.2 Placing Entries in the System Timer QueueWhen you use the system timer queue, you use the timer expiration to signal when a routine is to be executed. It allows the caller to request a timer that will activate sometime in the future. The timer is requested for the calling kernel thread. When the timer activates, the event is reported to that thread. It does not affect any other thread in the process. For the actual signal, you can use an event flag or AST. With this method, you do not need a separate process to control program execution. However, you do use up your process's quotas for ASTs and timer queue requests. Use the system service SYS$SETIMR to place a request in the system timer queue. The format of this service is as follows:
Specify the absolute or delta time at which you want the program to begin execution using the daytim argument. Use the SYS$BINTIM system service to convert an ASCII time to the binary system format required for this argument. Once the system has reached this time, the timer expires. To signal timer expiration, set an event flag in the efn argument or specify an AST routine to be executed in the astadr argument. Refer to Section 6.8 and Chapter 8 for more information about using event flags and ASTs.
How Timer Requests Are Identified
The reqidt argument identifies each system time request uniquely. Then, if you need to cancel a request, you can refer to each request separately. To cancel a timer request, use the SYS$CANTIM system service. 4.9 Controlling Kernel Threads and Process ExecutionYou can control kernel threads and process execution in the following ways:
4.9.1 Process Hibernation and SuspensionThere are two ways to halt the execution of a kernel thread or process temporarily:
The kernel thread can continue execution normally only after a corresponding Wake from Hibernation (SYS$WAKE) system service (if it is hibernating), or after a Resume Process (SYS$RESUME) system service, if it is suspended. Suspending or hibernating a kernel thread puts it into a dormant state; the thread is not deleted. A process in hibernation can control itself; a process in suspension requires another process to control it. Table 4-11 compares hibernating and suspended processes.
1If a process is suspended in kernel mode (a hard suspension), it cannot receive any ASTs. If a process is suspended at supervisor mode (a soft suspension), it can receive executive or kernel mode ASTs. See the description of SYS$SUSPND in the HP OpenVMS System Services Reference Manual: GETUTC--Z. Table 4-12 summarizes the system services and routines that can place a process in or remove a process from hibernation or suspension.
4.9.1.1 Using Process HibernationThe hibernate/wake mechanism provides an efficient way to prepare an image for execution and then to place it in a wait state until it is needed. If you create a subprocess that must execute the same function repeatedly and must execute immediately when it is needed, you could use the SYS$HIBER and SYS$WAKE system services, as shown in the following example:
The Schedule Wakeup (SYS$SCHDWK) system service, a variation of the SYS$WAKE system service, schedules a wakeup for a hibernating process at a fixed time or at an elapsed (delta) time interval. Using the SYS$SCHDWK service, a process can schedule a wakeup for itself before issuing a SYS$HIBER call. For an example of how to use the SYS$SCHDWK system service, see Chapter 27. Hibernating processes can be interrupted by asynchronous system traps (ASTs), as long as AST delivery is enabled. The process can call SYS$WAKE on its own behalf in the AST service routine, and continue execution following the execution of the AST service routine. For a description of ASTs and how to use them, see Chapter 8. 4.9.1.2 Using Alternative Methods of HibernationYou can use two additional methods to cause a process to hibernate:
When you use the SYS$CREPRC system service, the creating process can control when to wake the created process. When you use the RUN command, its qualifiers control when to wake the process. If you use the /INTERVAL qualifier and the image to be executed does not call the SYS$HIBER system service, the image is placed in a state of hibernation whenever it issues a return instruction (RET). Each time the image is awakened, it begins executing at its entry point. If the image does call SYS$HIBER, each time it is awakened it begins executing at either the point following the call to SYS$HIBER or at its entry point (if it last issued a RET instruction). If wakeup requests are scheduled at time intervals, the image can be terminated with the Delete Process (SYS$DELPRC) or Force Exit (SYS$FORCEX) system service, or from the command level with the STOP command. The SYS$DELPRC and SYS$FORCEX system services are described in Section 4.9.3.4 and in Section 4.9.4. The RUN and STOP commands are described in the HP OpenVMS DCL Dictionary. These methods allow you to write programs that can be executed once, on request, or cyclically. If an image is executed more than once in this manner, normal image activation and termination services are not performed on the second and subsequent calls to the image. Note that the program must ensure both the integrity of data areas that are modified during its execution and the status of opened files.
|