unix-2.4.2.0: POSIX functionality

Portability | non-portable (requires POSIX)
Stability | provisional
Maintainer | libraries@haskell.org

System.Posix.Process

Contents

- Processes
  - Forking and executing
  - Exiting
  - Process environment
  - Process groups
  - Sessions
  - Process times
  - Scheduling priority
  - Process status

Description

POSIX process support. See also the System.Cmd and System.Process modules in the process package.

Synopsis

- forkProcess :: IO () -> IO ProcessID
- executeFile :: FilePath -> Bool -> [String] -> Maybe [(String, String)] -> IO a
- exitImmediately :: ExitCode -> IO ()
- getProcessID :: IO ProcessID
- getParentProcessID :: IO ProcessID
- getProcessGroupID :: IO ProcessGroupID
- createProcessGroup :: ProcessID -> IO ProcessGroupID
- joinProcessGroup :: ProcessGroupID -> IO ()
- setProcessGroupID :: ProcessID -> ProcessGroupID -> IO ()
- createSession :: IO ProcessGroupID
- data ProcessTimes = ProcessTimes {
  - elapsedTime :: ClockTick
  - userTime :: ClockTick
  - systemTime :: ClockTick
  - childUserTime :: ClockTick
  - childSystemTime :: ClockTick
  }
- getProcessTimes :: IO ProcessTimes
- nice :: Int -> IO ()
- getProcessPriority :: ProcessID -> IO Int
- getProcessGroupPriority :: ProcessGroupID -> IO Int
- getUserPriority :: UserID -> IO Int
- setProcessPriority :: ProcessID -> Int -> IO ()
- setProcessGroupPriority :: ProcessGroupID -> Int -> IO ()
- setUserPriority :: UserID -> Int -> IO ()
- data ProcessStatus
  - = Exited ExitCode
  - | Terminated Signal
  - | Stopped Signal
- getProcessStatus :: Bool -> Bool -> ProcessID -> IO (Maybe ProcessStatus)
- getAnyProcessStatus :: Bool -> Bool -> IO (Maybe (ProcessID, ProcessStatus))
- getGroupProcessStatus :: Bool -> Bool -> ProcessGroupID -> IO (Maybe (ProcessID, ProcessStatus))

# Processes

## Forking and executing

forkProcess :: IO () -> IO ProcessID

`forkProcess` corresponds to the POSIX `fork` system call. The `IO` action passed as an argument is executed in the child process; no other threads will be copied to the child process. On success, `forkProcess` returns the child's `ProcessID` to the parent process; in case of an error, an exception is thrown.

`forkProcess` comes with a giant warning: since any other running threads are not copied into the child process, it's easy to go wrong: e.g. by accessing some shared resource that was held by another thread in the parent.

GHC note: `forkProcess` is not currently supported when using multiple processors (`+RTS -N`), although it is supported with `-threaded` as long as only one processor is being used.

executeFile

Arguments

:: FilePath | Command
-> Bool | Search PATH?
-> [String] | Arguments
-> Maybe [(String, String)] | Environment
-> IO a |

``executeFile` cmd args env` calls one of the `execv*` family, depending on whether or not the current PATH is to be searched for the command, and whether or not an environment is provided to supersede the process's current environment. The basename (leading directory names suppressed) of the command is passed to `execv*` as `arg[0]`; the argument list passed to `executeFile` therefore begins with `arg[1]`.

## Exiting

exitImmediately :: ExitCode -> IO ()

``exitImmediately` status` calls `_exit` to terminate the process with the indicated exit `status`. The operation never returns.

## Process environment

getProcessID :: IO ProcessID

`getProcessID` calls `getpid` to obtain the `ProcessID` for the current process.

getParentProcessID :: IO ProcessID

`getProcessID` calls `getppid` to obtain the `ProcessID` for the parent of the current process.

getProcessGroupID :: IO ProcessGroupID

`getProcessGroupID` calls `getpgrp` to obtain the `ProcessGroupID` for the current process.

## Process groups

createProcessGroup :: ProcessID -> IO ProcessGroupID

``createProcessGroup` pid` calls `setpgid` to make process `pid` a new process group leader.

joinProcessGroup :: ProcessGroupID -> IO ()

``joinProcessGroup` pgid` calls `setpgid` to set the `ProcessGroupID` of the current process to `pgid`.

setProcessGroupID :: ProcessID -> ProcessGroupID -> IO ()

``setProcessGroupID` pid pgid` calls `setpgid` to set the `ProcessGroupID` for process `pid` to `pgid`.

## Sessions

createSession :: IO ProcessGroupID

`createSession` calls `setsid` to create a new session with the current process as session leader.

## Process times

data ProcessTimes

Constructors

ProcessTimes |

Fields

elapsedTime :: ClockTick
userTime :: ClockTick
systemTime :: ClockTick
childUserTime :: ClockTick
childSystemTime :: ClockTick

getProcessTimes :: IO ProcessTimes

`getProcessTimes` calls `times` to obtain time-accounting information for the current process and its children.

## Scheduling priority

nice :: Int -> IO ()

getProcessPriority :: ProcessID -> IO Int

getProcessGroupPriority :: ProcessGroupID -> IO Int

getUserPriority :: UserID -> IO Int

setProcessPriority :: ProcessID -> Int -> IO ()

setProcessGroupPriority :: ProcessGroupID -> Int -> IO ()

setUserPriority :: UserID -> Int -> IO ()

## Process status

data ProcessStatus

Constructors

Exited ExitCode |
Terminated Signal |
Stopped Signal |

Instances

Eq ProcessStatus |
Ord ProcessStatus |
Show ProcessStatus |

getProcessStatus :: Bool -> Bool -> ProcessID -> IO (Maybe ProcessStatus)

``getProcessStatus` blk stopped pid` calls `waitpid`, returning ``Just` tc`, the `ProcessStatus` for process `pid` if it is available, `Nothing` otherwise. If `blk` is `False`, then `WNOHANG` is set in the options for `waitpid`, otherwise not. If `stopped` is `True`, then `WUNTRACED` is set in the options for `waitpid`, otherwise not.

getAnyProcessStatus :: Bool -> Bool -> IO (Maybe (ProcessID, ProcessStatus))

``getAnyProcessStatus` blk stopped` calls `waitpid`, returning ``Just` (pid, tc)`, the `ProcessID` and `ProcessStatus` for any child process if one is available, `Nothing` otherwise. If `blk` is `False`, then `WNOHANG` is set in the options for `waitpid`, otherwise not. If `stopped` is `True`, then `WUNTRACED` is set in the options for `waitpid`, otherwise not.

getGroupProcessStatus :: Bool -> Bool -> ProcessGroupID -> IO (Maybe (ProcessID, ProcessStatus))

``getGroupProcessStatus` blk stopped pgid` calls `waitpid`, returning ``Just` (pid, tc)`, the `ProcessID` and `ProcessStatus` for any process in group `pgid` if one is available, `Nothing` otherwise. If `blk` is `False`, then `WNOHANG` is set in the options for `waitpid`, otherwise not. If `stopped` is `True`, then `WUNTRACED` is set in the options for `waitpid`, otherwise not.