System processes are managed by the Task class.
Tasks, also known as processes, form the basis of process execution in an operating system. By creating a task object, it is possible to execute a program from within the host system.
To execute a compiled program, set the Location field to point to the executable file before initialising the task. Arguments can be passed to the executable by setting the Parameters field. After initialising the task, use the Activate() action to run the executable. If the program executes successfully, the task object can be removed and this will not impact the running program.
The task object that represents the active process can be acquired from CurrentTask().
The Task class consists of the following fields:
Access | Name | Type | Comment | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Actions | APTR | Used to gain direct access to a task's actions. | |||||||||||||||||||||||
This field provides direct access to the actions of a task, and is intended for use with the active task object returned from CurrentTask(). Hooking into the action table allows the running executable to 'blend-in' with Parasol's object oriented design. The Actions field points to a lookup table of ActionEntry items. Hooking into an action involves writing its if (!AccessObject(CurrentTask(), 5000, &task)) { task->getPtr(FID_Actions, &actions); actions[AC::Seek] = PROGRAM_Seek; ReleaseObject(task); } | |||||||||||||||||||||||||
Args | STRING | Command line arguments (string format). | |||||||||||||||||||||||
This field allows command line arguments to be set using a single string, whereby each value is separated by whitespace. The string will be disassembled and the arguments will be available to read from the Parameters field. If an argument needs to include whitespace, use double-quotes to encapsulate the value. | |||||||||||||||||||||||||
ErrorCallback | FUNCTION | This callback returns incoming data from STDERR. | |||||||||||||||||||||||
The ErrorCallback field can be set with a function reference that will be called when an active process sends data via STDERR. The callback must follow the prototype The information read from STDERR will be returned in the Data pointer and the byte-length of the data will be indicated by the | |||||||||||||||||||||||||
ExitCallback | FUNCTION | The callback is activated when the process is terminated. | |||||||||||||||||||||||
The ExitCallback field can be set with a function reference that will be called when the executed process is terminated. The callback must follow the prototype Please keep in mind that if the | |||||||||||||||||||||||||
Flags | TSF | Optional flags. | |||||||||||||||||||||||
| |||||||||||||||||||||||||
InputCallback | FUNCTION | This callback returns incoming data from STDIN. | |||||||||||||||||||||||
The InputCallback field is available for use only when the task object represents the current process. The referenced function will be called when process receives data from STDIN. The callback must follow the prototype The information read from STDOUT will be returned in the A status of | |||||||||||||||||||||||||
LaunchPath | STRING | Launched executables will start in the path specified here. | |||||||||||||||||||||||
Use the LaunchPath field to specify the folder that a launched executable will start in when the task object is activated. This will override all other path options, such as the | |||||||||||||||||||||||||
Location | STRING | Location of an executable file to launch. | |||||||||||||||||||||||
When a task object is activated, the Location field will be checked for a valid filename. If the path is valid, the executable code will be loaded from this source. The source must be in an executable format recognised by the native platform. Leading spaces will be ignored by the string parser. The Location string can be enclosed with quotes, in which case only the quoted portion of the string will be used as the source path. | |||||||||||||||||||||||||
Name | STRING | Name of the task. | |||||||||||||||||||||||
This field specifies the name of the task or program that has been initialised. It is up to the developer of the program to set the Name which will appear in this field. If there is no name for the task then the system may assign a randomly generated name. | |||||||||||||||||||||||||
OutputCallback | FUNCTION | This callback returns incoming data from STDOUT. | |||||||||||||||||||||||
The OutputCallback field can be set with a function reference that will be called when an active process sends data via STDOUT. The callback must follow the prototype The information read from STDOUT will be returned in the | |||||||||||||||||||||||||
Parameters | pf::vector<std::string> | Command line arguments (list format). | |||||||||||||||||||||||
Command line arguments for a program can be defined here as a vector list, whereby each argument is an independent string. To illustrate, the following command-line string: 1> YourProgram PREFS MyPrefs -file "documents:readme.txt" Would be represented as follows: pf::vector<std::string> Args = { "PREFS", "MyPrefs", "-file", "documents:readme.txt" }; | |||||||||||||||||||||||||
Path | STRING | The current working folder of the active process. | |||||||||||||||||||||||
The Path specifies the 'working folder' that determines where files are loaded from when an absolute path is not otherwise specified for file access. Initially the working folder is usually set to the folder of the parent process, such as that of a terminal shell. The working folder can be changed at any time by updating the Path with a new folder location. If changing to the new folder fails for any reason, the working folder will remain unchanged and the path value will not be updated. | |||||||||||||||||||||||||
Priority | INT | The task priority in relation to other tasks is be defined here. | |||||||||||||||||||||||
Set the Priority field to change the priority of the process associated with the task. The default value for all processes is zero. High positive values will give the process more CPU time while negative values will yield CPU time to other active processes. Note that depending on the platform, there may be limits as to whether one process can change the priority level of a foreign process. Other factors such as the scheduler used by the host system should be considered in the effect of prioritisation. | |||||||||||||||||||||||||
ProcessID | INT | Reflects the process ID when an executable is launched. | |||||||||||||||||||||||
If a task object launches an executable file via Activate(), the ProcessID will be set to the 'pid' that was assigned to the new process by the host system. At all other times the ProcessID is set to zero. | |||||||||||||||||||||||||
ProcessPath | STRING | The path of the executable that is associated with the task. | |||||||||||||||||||||||
The ProcessPath is set to the path of the executable file that is associated with the task (not including the executable file name). This value is managed internally and cannot be altered. In Microsoft Windows it is not always possible to determine the origins of an executable, in which case the ProcessPath is set to the working folder in use at the time the process was launched. | |||||||||||||||||||||||||
ReturnCode | INT | The task's return code can be retrieved following execution. | |||||||||||||||||||||||
Once a process has completed execution then its return code can be read from this field. If process is still running, the error code | |||||||||||||||||||||||||
TimeOut | DOUBLE | Limits the amount of time to wait for a launched process to return. | |||||||||||||||||||||||
This field can be set in conjunction with the |
The following actions are currently supported:
Name | Comment | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
Activate | Activating a task object will execute it. | |||||||||
ERR acActivate(*Object) Activating a task results in the execution of the file referenced in the Location field. On successful execution, the ProcessID will refer to the ID of the executed process. This ID is compatible with the hosting platform's unique process numbers. If the The process' return code can be read from the ReturnCode field after the process has completed its execution. In Microsoft Windows, output can be redirected to a file if the redirection symbol is used to direct output in one of the task arguments. For instance When running a DOS program in Microsoft Windows, the Error Codes
| ||||||||||
GetKey | Retrieves custom key values. | |||||||||
ERR acGetKey(*Object, CSTRING Key, STRING Value, LONG Size)
| ||||||||||
SetKey | Variable fields are supported for the general storage of program variables. | |||||||||
ERR acSetKey(*Object, CSTRING Key, CSTRING Value)
| ||||||||||
Write | Send raw data to a launched process' stdin descriptor. | |||||||||
ERR acWrite(*Object, APTR Buffer, LONG Length, LONG Result)
If a process is successfully launched with the |
The following methods are currently supported:
Name | Comment | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
AddArgument | Adds a new argument to the Parameters field. | ||||||||||||||
ERR task::AddArgument(OBJECTPTR Object, CSTRING Argument)
This method will add a new argument to the end of the Parameters field array. If the string is surrounded by quotes, they will be removed automatically. Error Codes
| |||||||||||||||
Expunge | Forces a Task to expunge unused code. | ||||||||||||||
ERR task::Expunge(OBJECTPTR Object) The Expunge() method releases all loaded libraries that are no longer in use by the active process. Error Codes
| |||||||||||||||
GetEnv | Retrieves environment variables for the active process. | ||||||||||||||
ERR task::GetEnv(OBJECTPTR Object, CSTRING Name, CSTRING * Value)
On platforms that support environment variables, GetEnv() returns the value of the environment variable matching the In Windows, it is possible to look up registry keys if the string starts with one of the following (in all other cases, the system's environment variables are queried): \HKEY_LOCAL_MACHINE\ \HKEY_CURRENT_USER\ \HKEY_CLASSES_ROOT\ \HKEY_USERS\ Here is a valid example for reading the 'Parasol' key value Caution: If your programming language uses backslash as an escape character (true for Fluid developers), remember to use double-backslashes as the key value separator in your Name string. Error Codes
| |||||||||||||||
Quit | Sends a quit message to a task. | ||||||||||||||
ERR task::Quit(OBJECTPTR Object) The Quit() method can be used as a convenient way of sending a task a quit message. This will normally result in the destruction of the task, so long as it is still functioning correctly and has been coded to respond to the Error Codes
| |||||||||||||||
SetEnv | Sets environment variables for the active process. | ||||||||||||||
ERR task::SetEnv(OBJECTPTR Object, CSTRING Name, CSTRING Value)
On platforms that support environment variables, SetEnv() is used for defining values for named variables. A In Windows, it is possible to set registry keys if the string starts with one of the following (in all other cases, the system's environment variables are queried): \HKEY_LOCAL_MACHINE\ \HKEY_CURRENT_USER\ \HKEY_CLASSES_ROOT\ \HKEY_USERS\ When setting a registry key, the function will always set the Value as a string type unless the key already exists. If the existing key value is a number such as Error Codes
|
Task flags
Name | Description |
---|---|
TSF::ATTACHED | Forces new task to be attached to the parent (child will close when parent closes). |
TSF::DETACHED | Forces new task to be detached from the parent. |
TSF::FOREIGN | Set this flag when using the task object to execute a foreign process - that is an executable that does not use the Parasol API. |
TSF::LOG_ALL | Additional debug messages will be printed during normal usage of the task class when this flag is set. |
TSF::PIPE | Enable the output pipe to the launched process so that it can read data. |
TSF::PRIVILEGED | During a normal execution process, any privileges of the parent process will be dropped so that the child process runs unprivileged. This behaviour can be reversed if he PRIVILEGED flag is set, in which case the child process has the same privileges as the parent. |
TSF::QUIET | Setting this flag will divert all process output to /dev/null or the nearest equivalent for non-Unix systems. |
TSF::RESET_PATH | If set, the executed process will start in its own folder rather than the folder of the parent process. |
TSF::SHELL | Enables shell mode. On Unix systems, this means that a shell (usually BASH) will be used to launch the process. |
TSF::WAIT | This flag will cause the parent process to halt when the task is activated. Control is returned to the parent process once the child process terminates. |
Class Info | |
---|---|
ID | ID_TASK |
Category | System |
Include | modules/task.h |
Version | 1 |