{
    "mode": "info",
    "parameter": "IPC::ShareLite",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/info/IPC%3A%3AShareLite/json",
    "generated": "2026-07-05T02:02:51Z",
    "synopsis": "use IPC::ShareLite;\nmy $share = IPC::ShareLite->new(\n-key     => 1971,\n-create  => 'yes',\n-destroy => 'no'\n) or die $!;\n$share->store( \"This is stored in shared memory\" );\nmy $str = $share->fetch;",
    "sections": {
        "NAME": {
            "content": "IPC::ShareLite - Lightweight interface to shared memory\n",
            "subsections": []
        },
        "VERSION": {
            "content": "This document describes IPC::ShareLite version 0.17\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use IPC::ShareLite;\n\nmy $share = IPC::ShareLite->new(\n-key     => 1971,\n-create  => 'yes',\n-destroy => 'no'\n) or die $!;\n\n$share->store( \"This is stored in shared memory\" );\nmy $str = $share->fetch;\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "IPC::ShareLite provides a simple interface to shared memory, allowing\ndata to be efficiently communicated between processes. Your operating\nsystem must support SysV IPC (shared memory and semaphores) in order to\nuse this module.\n\nIPC::ShareLite provides an abstraction of the shared memory and\nsemaphore facilities of SysV IPC, allowing the storage of arbitrarily\nlarge data; the module automatically acquires and removes shared memory\nsegments as needed. Storage and retrieval of data is atomic, and\nlocking functions are provided for higher-level synchronization.\n\nIn many respects, this module is similar to IPC::Shareable. However,\nIPC::ShareLite does not provide a tied interface, does not\n(automatically) allow the storage of variables, and is written in C for\nadditional speed.\n\nConstruct an IPC::ShareLite object by calling its constructor:\n\nmy $share = IPC::ShareLite->new(\n-key     => 1971,\n-create  => 'yes',\n-destroy => 'no'\n) or die $!;\n\nOnce an instance has been created, data can be written to shared memory\nby calling the store() method:\n\n$share->store(\"This is going in shared memory\");\n\nRetrieve the data by calling the fetch() method:\n\nmy $str = $share->fetch();\n\nThe store() and fetch() methods are atomic; any processes attempting to\nread or write to the memory are blocked until these calls finish.\nHowever, in certain situations, you'll want to perform multiple\noperations atomically.  Advisory locking methods are available for this\npurpose.\n\nAn exclusive lock is obtained by calling the lock() method:\n\n$share->lock();\n\nHappily, the lock() method also accepts all of the flags recognized by\nthe flock() system call.  So, for example, you can obtain a shared lock\nlike this:\n\n$share->lock( LOCKSH );\n\nOr, you can make either type of lock non-blocking:\n\n$share->lock( LOCKEX|LOCKNB );\n\nRelease the lock by calling the unlock() method:\n\n$share->unlock;\n",
            "subsections": []
        },
        "METHODS": {
            "content": "\"new($key, $create, $destroy, $exclusive, $mode, $flags, $size)\"\nThis is the constructor for IPC::ShareLite.  It accepts both the\npositional and named parameter calling styles.\n\n$key is an integer value used to associate data between processes.  All\nprocesses wishing to communicate should use the same $key value.  $key\nmay also be specified as a four character string, in which case it will\nbe converted to an integer value automatically.  If $key is undefined,\nthe shared memory will not be accessible from other processes.\n\n$create specifies whether the shared memory segment should be created\nif it does not already exist.  Acceptable values are 1, 'yes', 0, or\n'no'.\n\n$destroy indicates whether the shared memory segments and semaphores\nshould be removed from the system once the object is destroyed.\nAcceptable values are 1, 'yes', 0, or 'no'.\n\nIf $exclusive is true, instantiation will fail if the shared memory\nsegment already exists. Acceptable values are 1, 'yes', 0, or 'no'.\n\n$mode specifies the permissions for the shared memory and semaphores.\nThe default value is 0666.\n\n$flags specifies the exact shared memory and semaphore flags to use.\nThe constants IPCCREAT, IPCEXCL, and IPCPRIVATE are available for\nimport.\n\n$size specifies the shared memory segment size, in bytes. The default\nsize is 65,536 bytes, which is fairly portable. Linux, as an example,\nsupports segment sizes of 4 megabytes.\n\nThe constructor croaks on error.\n\n\"store( $scalar )\"\nThis method stores $scalar into shared memory.  $scalar may be\narbitrarily long.  Shared memory segments are acquired and released\nautomatically as the data length changes.  The only limits on the\namount of data are the system-wide limits on shared memory pages\n(SHMALL) and segments (SHMMNI) as compiled into the kernel.\n\nThe method raises an exception on error.\n\nNote that unlike IPC::Shareable, this module does not automatically\nallow references to be stored. Serializing all data is expensive, and\nis not always necessary. If you need to store a reference, you should\nemploy the Storable module yourself. For example:\n\nuse Storable qw( freeze thaw );\n...\n$hash = { red => 1, white => 1, blue => 1 };\n$share->store( freeze( $hash ) );\n...\n$hash = thaw( $share->fetch );\n\n\"fetch\"\nThis method returns the data that was previously stored in shared\nmemory.  The empty string is returned if no data was previously stored.\n\nThe method raises an exception on error.\n\n\"lock( $type )\"\nObtains a lock on the shared memory.  $type specifies the type of lock\nto acquire.  If $type is not specified, an exclusive read/write lock is\nobtained.  Acceptable values for $type are the same as for the flock()\nsystem call.  The method returns true on success, and undef on error.\nFor non-blocking calls (see below), the method returns 0 if it would\nhave blocked.\n\nObtain an exclusive lock like this:\n\n$share->lock( LOCKEX ); # same as default\n\nOnly one process can hold an exclusive lock on the shared memory at a\ngiven time.\n\nObtain a shared lock this this:\n\n$share->lock( LOCKSH );\n\nMultiple processes can hold a shared lock at a given time.  If a\nprocess attempts to obtain an exclusive lock while one or more\nprocesses hold shared locks, it will be blocked until they have all\nfinished.\n\nEither of the locks may be specified as non-blocking:\n\n$share->lock( LOCKEX|LOCKNB );\n$share->lock( LOCKSH|LOCKNB );\n\nA non-blocking lock request will return 0 if it would have had to wait\nto obtain the lock.\n\nNote that these locks are advisory (just like flock), meaning that all\ncooperating processes must coordinate their accesses to shared memory\nusing these calls in order for locking to work.  See the flock() call\nfor details.\n\nLocks are inherited through forks, which means that two processes\nactually can possess an exclusive lock at the same time.  Don't do\nthat.\n\nThe constants LOCKEX, LOCKSH, LOCKNB, and LOCKUN are available for\nimport:\n\nuse IPC::ShareLite qw( :lock );\n\nOr, just use the flock constants available in the Fcntl module.\n\n\"unlock\"\nReleases any locks.  This is actually equivalent to:\n\n$share->lock( LOCKUN );\n\nThe method returns true on success and undef on error.\n\n\"version\"\nEach share has a version number that incrementents monotonically for\neach write to the share. When the share is initally created its version\nnumber will be 1.\n\nmy $numwrites = $share->version;\n\n\"key\"\nGet a share's key.\n\nmy $key = $share->key;\n\n\"create\"\nGet a share's create flag.\n\n\"exclusive\"\nGet a share's exclusive flag.\n\n\"flags\"\nGet a share's flag.\n\n\"mode\"\nGet a share's mode.\n\n\"size\"\nGet a share's segment size.\n\n\"numsegments\"\nGet the number of segments in a share. The memory usage of a share can\nbe approximated like this:\n\nmy $usage = $share->size * $share->numsegments;\n\n$usage will be the memory usage rounded up to the next segment\nboundary.\n\n\"destroy\"\nGet or set the share's destroy flag.\n",
            "subsections": []
        },
        "PERFORMANCE": {
            "content": "For a rough idea of the performance you can expect, here are some\nbenchmarks.  The tests were performed using the Benchmark module on a\nCyrix PR166+ running RedHat Linux 5.2 with the 2.0.36 kernel, perl\n5.00502 using perl's malloc, and the default shared memory segment\nsize.  Each test was run 5000 times.\n\nDATA SIZE (bytes)       TIME (seconds)  Op/Sec\n\nstore  16384                   2               2500\nfetch  16384                   2               2500\n\nstore  32768                   3               1666\nfetch  32768                   3               1666\n\nstore  65536                   6               833\nfetch  65536                   5               1000\n\nstore  131072                  12              416\nfetch  131072                  12              416\n\nstore  262144                  28              178\nfetch  262144                  27              185\n\nstore  524288                  63              79\nfetch  524288                  61              81\n\nMost of the time appears to be due to memory copying.  Suggestions for\nspeed improvements are welcome.\n",
            "subsections": []
        },
        "PORTABILITY": {
            "content": "The module should compile on any system with SysV IPC and an ANSI C\ncompiler, and should compile cleanly with the -pedantic and -Wall\nflags.\n\nThe module has been tested under Solaris, FreeBSD, and Linux.  Testing\non other platforms is needed.\n\nIf you encounter a compilation error due to the definition of the semun\nunion, edit the top of sharestuff.c and undefine the semun definition.\nAnd then please tell me about it.\n\nI've heard rumors that a SysV IPC interface has been constructed for\nWin32 systems.  Support for it may be added to this module.\n\nIPC::ShareLite does not understand the shared memory data format used\nby IPC::Shareable.\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Copyright 1998-2002, Maurice Aubrey <maurice@hevanet.com>.  All rights\nreserved.\n\nThis release by Andy Armstrong <andy@hexten.net>.\n\nThis module is free software; you may redistribute it and/or modify it\nunder the same terms as Perl itself.\n",
            "subsections": []
        },
        "CREDITS": {
            "content": "Special thanks to Benjamin Sugars for developing the IPC::Shareable\nmodule.\n\nSee the Changes file for other contributors.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "IPC::Shareable, ipc(2), shmget(2), semget(2), perl.\n\nperl v5.34.0                      2022-02-06               IPC::ShareLite(3pm)",
            "subsections": []
        }
    },
    "summary": "IPC::ShareLite - Lightweight interface to shared memory",
    "flags": [],
    "examples": [],
    "see_also": [
        {
            "name": "ipc",
            "section": "2",
            "url": "https://www.chedong.com/phpMan.php/man/ipc/2/json"
        },
        {
            "name": "shmget",
            "section": "2",
            "url": "https://www.chedong.com/phpMan.php/man/shmget/2/json"
        },
        {
            "name": "semget",
            "section": "2",
            "url": "https://www.chedong.com/phpMan.php/man/semget/2/json"
        },
        {
            "name": "ShareLite",
            "section": "3pm",
            "url": "https://www.chedong.com/phpMan.php/man/ShareLite/3pm/json"
        }
    ]
}