Personal tools

POSIX process group API reform proposal

From HaskellWiki

Jump to: navigation, search

Currently there is no way to find out the process group ID from a process ID. Also, process group IDs and process IDs share the same type, which prohibits static type-checking. (Note: The new API has been implemented in the new version of the unix library.)

Contents

1 Proposal

1.1 New type

It is proposed that System.Posix.Types have a new type ProcessGroupID where

newtype ProcessGroupID = ProcessGroupID CPid

1.2 New API

It is proposed that System.Posix.Process have these functions, (at least getProcessGroupIDOf)

-- wrappers for getpgrp() and getpgid(pid)
getProcessGroupID :: IO ProcessGroupID
getProcessGroupIDOf :: ProcessID -> IO ProcessGroupID
 
-- wrappers for setpgid(0,pgid) and setpgid(pid,pgid)
setProcessGroupID :: ProcessGroupID -> IO ()
setProcessGroupIDOf :: ProcessID -> ProcessGroupID -> IO ()
 
-- wrapper for setpgid(0,0) and setpgid(pid,0)
createProcessGroup :: IO ProcessGroupID
createProcessGroupFor :: ProcessID -> IO ProcessGroupID
 
-- a synonym of setProcessGroup
joinProcessGroup :: ProcessGroupID -> IO ()

2 Motivation

In POSIX, process groups and processes are two totally different concepts. It is unfortunate that the underlying type used in POSIX API is the same. Using different types for different concepts can help programmers avoid errors. For example, currently
setProcessGroupID pgid pid
will not raise a compile-time error, and it will under the proposed implementation.

Also, these is no way to query the process group of a process now. This is needed for a careful implementation to the proposed functionality mentioned in GHC Ticket #3994. The getpgid has been standardized in POSIX.1-2001, which provides the functionality.

3 Backward compatibility

Unless people use
CPid
explicitly to store process group IDs, all current codes using
ProcessGroupID
should be complied without modification. Two functions
setProcessGroupID
and
createProcessGroup
are backward incompatible with previous implantations. This proposal suggests announcing the proposed change and then really changes the interface in future versions.

4 Naming convention

The new API is trying to be more clear and consistent, while avoiding compatibility issues. The suffices Of or For are to indicate that the first argument is the process id of another process.