getpid() or something similar

John Meacham john@repetae.net
Wed, 21 May 2003 19:57:40 -0700


Heres a couple of useful functions for creating a temporary file.
getUniqueName creates a unique filename and atomicWrite uses it to
atomically update a file on the filesystem by writing to a temporary
file and renaming it over the target.
        John


getUniqueName :: IO String
getUniqueName = do
    id <- getProcessID
    u <- newUnique
    n <- liftM nodeName getSystemID 
    t <- epochTime
    return $ n ++ "." ++ show id ++ "." ++ show t ++ "." ++ show (hashUnique u)

atomicWrite :: String -> (Handle -> IO a) -> IO a
atomicWrite fn action = do
    n <- getUniqueName
    let tn = fn ++ "." ++ n
    v <- E.bracket (openFile tn WriteMode) hClose action 
    rename tn fn
    return v


On Thu, May 22, 2003 at 01:02:57AM +0200, Peter Simons wrote:
> Please pardon me if it turns out I'm just blind, but is there any
> function I can use to get some "unique" information to use for
> temporary file names and the like? A function that will tell me my
> process ID, for instance? 
> 
> I looked through Hugs and GHC, yet couldn't find anything but
> Data.Unique, and that looks like big-time overkill for the purpose.
> 
> Any pointers are appreciated!

-- 
---------------------------------------------------------------------------
John Meacham - California Institute of Technology, Alum. - john@foo.net
---------------------------------------------------------------------------