ipc.tex 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. %
  2. % $Id$
  3. % This file is part of the FPC documentation.
  4. % Copyright (C) 1998, by Michael Van Canneyt
  5. %
  6. % The FPC documentation is free text; you can redistribute it and/or
  7. % modify it under the terms of the GNU Library General Public License as
  8. % published by the Free Software Foundation; either version 2 of the
  9. % License, or (at your option) any later version.
  10. %
  11. % The FPC Documentation is distributed in the hope that it will be useful,
  12. % but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. % Library General Public License for more details.
  15. %
  16. % You should have received a copy of the GNU Library General Public
  17. % License along with the FPC documentation; see the file COPYING.LIB. If not,
  18. % write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. % Boston, MA 02111-1307, USA.
  20. %
  21. \chapter{The IPC unit.}
  22. \label{ch:ipcunit}
  23. \FPCexampledir{ipcex}
  24. This chapter describes the IPC unit for Free Pascal. It was written for
  25. \linux by Micha\"el Van Canneyt. It gives all the functionality of system V
  26. Inter-Process Communication: shared memory, semaphores and messages.
  27. It works only on the \linux operating system.
  28. The chapter is divided in 2 sections:
  29. \begin{itemize}
  30. \item The first section lists types, constants and variables from the
  31. interface part of the unit.
  32. \item The second section describes the functions defined in the unit.
  33. \end{itemize}
  34. \section {Types, Constants and variables : }
  35. \subsection{Variables}
  36. \begin{verbatim}
  37. Var
  38. IPCerror : longint;
  39. \end{verbatim}
  40. The \var{IPCerror} variable is used to report errors, by all calls.
  41. \subsection{Constants}
  42. Many constants here are provided for completeness only, and should under
  43. normal circumstances not be used by the programmer.
  44. \begin{verbatim}
  45. Const
  46. IPC_CREAT = 1 shl 9; { create if key is nonexistent }
  47. IPC_EXCL = 2 shl 9; { fail if key exists }
  48. IPC_NOWAIT = 4 shl 9; { return error on wait }
  49. \end{verbatim}
  50. These constants are used in the various \var{xxxget} calls.
  51. \begin{verbatim}
  52. IPC_RMID = 0; { remove resource }
  53. IPC_SET = 1; { set ipc_perm options }
  54. IPC_STAT = 2; { get ipc_perm options }
  55. IPC_INFO = 3; { see ipcs }
  56. \end{verbatim}
  57. These constants can be passed to the various \var{xxxctl} calls.
  58. \begin{verbatim}
  59. const
  60. MSG_NOERROR = 1 shl 12;
  61. MSG_EXCEPT = 2 shl 12;
  62. MSGMNI = 128;
  63. MSGMAX = 4056;
  64. MSGMNB = 16384;
  65. \end{verbatim}
  66. These constants are used in the messaging system, they are not for use by
  67. the programmer.
  68. \begin{verbatim}
  69. const
  70. SEM_UNDO = $1000;
  71. GETPID = 11;
  72. GETVAL = 12;
  73. GETALL = 13;
  74. GETNCNT = 14;
  75. GETZCNT = 15;
  76. SETVAL = 16;
  77. SETALL = 17;
  78. \end{verbatim}
  79. These constants call be specified in the \seef{semop} call.
  80. \begin{verbatim}
  81. SEMMNI = 128;
  82. SEMMSL = 32;
  83. SEMMNS = (SEMMNI * SEMMSL);
  84. SEMOPM = 32;
  85. SEMVMX = 32767;
  86. \end{verbatim}
  87. These constanst are used internally by the semaphore system, they should not
  88. be used by the programmer.
  89. \begin{verbatim}
  90. const
  91. SHM_R = 4 shl 6;
  92. SHM_W = 2 shl 6;
  93. SHM_RDONLY = 1 shl 12;
  94. SHM_RND = 2 shl 12;
  95. SHM_REMAP = 4 shl 12;
  96. SHM_LOCK = 11;
  97. SHM_UNLOCK = 12;
  98. \end{verbatim}
  99. These constants are used in the \seef{shmctl} call.
  100. \subsection{Types}
  101. The following two types are provided because they are needed. One they they
  102. should be defined in the system unit, however.
  103. \begin{verbatim}
  104. Type
  105. PULong = ^Cardinal;
  106. PWord = ^Word;
  107. \end{verbatim}
  108. \begin{verbatim}
  109. Type
  110. TKey = Longint;
  111. \end{verbatim}
  112. \var{TKey} is the type returned by the \seef{ftok} key generating function.
  113. \begin{verbatim}
  114. type
  115. PIPC_Perm = ^TIPC_Perm;
  116. TIPC_Perm = record
  117. key : TKey;
  118. uid,
  119. gid,
  120. cuid,
  121. cgid,
  122. mode,
  123. seq : Word;
  124. end;
  125. \end{verbatim}
  126. The \var{TIPC\_Perm} structure is used in all IPC systems to specify the
  127. permissions.
  128. \begin{verbatim}
  129. Type
  130. PSHMid_DS = ^TSHMid_ds;
  131. TSHMid_ds = record
  132. shm_perm : TIPC_Perm;
  133. shm_segsz : longint;
  134. shm_atime : longint;
  135. shm_dtime : longint;
  136. shm_ctime : longint;
  137. shm_cpid : word;
  138. shm_lpid : word;
  139. shm_nattch : integer;
  140. shm_npages : word;
  141. shm_pages : Pointer;
  142. attaches : pointer;
  143. end;
  144. \end{verbatim}
  145. The \var{TSHMid\_ds} strucure is used in the \seef{shmctl} call to set or
  146. retrieve settings concerning shared memory.
  147. \begin{verbatim}
  148. type
  149. PSHMinfo = ^TSHMinfo;
  150. TSHMinfo = record
  151. shmmax : longint;
  152. shmmin : longint;
  153. shmmni : longint;
  154. shmseg : longint;
  155. shmall : longint;
  156. end;
  157. \end{verbatim}
  158. The \var{TSHMinfo} record is used by the shared memory system, and should
  159. not be accessed by the programer directly.
  160. \begin{verbatim}
  161. type
  162. PMSG = ^TMSG;
  163. TMSG = record
  164. msg_next : PMSG;
  165. msg_type : Longint;
  166. msg_spot : PChar;
  167. msg_stime : Longint;
  168. msg_ts : Integer;
  169. end;
  170. \end{verbatim}
  171. The \var{TMSG} record is used in the handling of message queues. There
  172. should be few cases where the programmer needs to access this data.
  173. \begin{verbatim}
  174. type
  175. PMSQid_ds = ^TMSQid_ds;
  176. TMSQid_ds = record
  177. msg_perm : TIPC_perm;
  178. msg_first : PMsg;
  179. msg_last : PMsg;
  180. msg_stime : Longint;
  181. msg_rtime : Longint;
  182. msg_ctime : Longint;
  183. wwait : Pointer;
  184. rwait : pointer;
  185. msg_cbytes : word;
  186. msg_qnum : word;
  187. msg_qbytes : word;
  188. msg_lspid : word;
  189. msg_lrpid : word;
  190. end;
  191. \end{verbatim}
  192. The \var{TMSQid\_ds} record is returned by the \seef{msgctl} call, and
  193. contains all data about a message queue.
  194. \begin{verbatim}
  195. PMSGbuf = ^TMSGbuf;
  196. TMSGbuf = record
  197. mtype : longint;
  198. mtext : array[0..0] of char;
  199. end;
  200. \end{verbatim}
  201. The \var{TMSGbuf} record is a record containing the data of a record. you
  202. should never use this record directly, instead you should make your own
  203. record that follows the structure of the \var{TMSGbuf} record, but that has
  204. a size that is big enough to accomodate your messages. The \var{mtype} field
  205. should always be present, and should always be filled.
  206. \begin{verbatim}
  207. Type
  208. PMSGinfo = ^TMSGinfo;
  209. TMSGinfo = record
  210. msgpool : Longint;
  211. msgmap : Longint;
  212. msgmax : Longint;
  213. msgmnb : Longint;
  214. msgmni : Longint;
  215. msgssz : Longint;
  216. msgtql : Longint;
  217. msgseg : Word;
  218. end;
  219. \end{verbatim}
  220. The \var{TMSGinfo} record is used internally by the message queue system,
  221. and should not be used by the programmer directly.
  222. \begin{verbatim}
  223. Type
  224. PSEMid_ds = ^PSEMid_ds;
  225. TSEMid_ds = record
  226. sem_perm : tipc_perm;
  227. sem_otime : longint;
  228. sem_ctime : longint;
  229. sem_base : pointer;
  230. sem_pending : pointer;
  231. sem_pending_last : pointer;
  232. undo : pointer;
  233. sem_nsems : word;
  234. end;
  235. \end{verbatim}
  236. The \var{TSEMid\_ds} structure is returned by the \seef{semctl} call, and
  237. contains all data concerning a semahore.
  238. \begin{verbatim}
  239. Type
  240. PSEMbuf = ^TSEMbuf;
  241. TSEMbuf = record
  242. sem_num : word;
  243. sem_op : integer;
  244. sem_flg : integer;
  245. end;
  246. \end{verbatim}
  247. The \var{TSEMbuf} record us use in the \seef{semop} call, and is used to
  248. specify which operations you want to do.
  249. \begin{verbatim}
  250. Type
  251. PSEMinfo = ^TSEMinfo;
  252. TSEMinfo = record
  253. semmap : longint;
  254. semmni : longint;
  255. semmns : longint;
  256. semmnu : longint;
  257. semmsl : longint;
  258. semopm : longint;
  259. semume : longint;
  260. semusz : longint;
  261. semvmx : longint;
  262. semaem : longint;
  263. end;
  264. \end{verbatim}
  265. The \var{TSEMinfo} record is used internally by the semaphore system, and
  266. should not be used directly.
  267. \begin{verbatim}
  268. Type
  269. PSEMun = ^TSEMun;
  270. TSEMun = record
  271. case longint of
  272. 0 : ( val : longint );
  273. 1 : ( buf : PSEMid_ds );
  274. 2 : ( arr : PWord );
  275. 3 : ( padbuf : PSeminfo );
  276. 4 : ( padpad : pointer );
  277. end;
  278. \end{verbatim}
  279. The \var{TSEMun} variant record (actually a C union) is used in the
  280. \seef{semctl} call.
  281. \section{Functions and procedures}
  282. \begin{function}{ftok}
  283. \Declaration
  284. Function ftok (Path : String; ID : char) : TKey;
  285. \Description
  286. \var{ftok} returns a key that can be used in a \seef{semget},\seef{shmget}
  287. or \seef{msgget} call to access a new or existing IPC resource.
  288. \var{Path} is the name of a file in the file system, \var{ID} is a
  289. character of your choice. The ftok call does the same as it's C couterpart,
  290. so a pascal program and a C program will access the same resource if
  291. they use the same \var{Path} and \var{ID}
  292. \Errors
  293. \var{ftok} returns -1 if the file in \var{Path} doesn't exist.
  294. \SeeAlso
  295. \seef{semget},\seef{shmget},\seef{msgget}
  296. \end{function}
  297. For an example, see \seef{msgctl}, \seef{semctl}, \seef{shmctl}.
  298. \begin{function}{msgget}
  299. \Declaration
  300. Function msgget(key: TKey; msgflg:longint):longint;
  301. \Description
  302. \var{msgget} returns the ID of the message queue described by \var{key}.
  303. Depending on the flags in \var{msgflg}, a new queue is created.
  304. \var{msgflg} can have one or more of the following values (combined by ORs):
  305. \begin{description}
  306. \item[IPC\_CREAT] The queue is created if it doesn't already exist.
  307. \item[IPC\_EXCL] If used in combination with \var{IPC\_CREAT}, causes the
  308. call to fail if the queue already exists. It cannot be used by itself.
  309. \end{description}
  310. Optionally, the flags can be \var{OR}ed with a permission mode, which is the
  311. same mode that can be used in the file system.
  312. \Errors
  313. On error, -1 is returned, and \var{IPCError} is set.
  314. \SeeAlso
  315. \seef{ftok},\seef{msgsnd}, \seef{msgrcv}, \seef{msgctl}, \seem{semget}{2}
  316. \end{function}
  317. For an example, see \seef{msgctl}.
  318. \begin{function}{msgsnd}
  319. \Declaration
  320. Function msgsnd(msqid:longint; msgp: PMSGBuf; msgsz: longint; msgflg:longint): Boolean;
  321. \Description
  322. \var{msgsend} sends a message to a message queue with ID \var{msqid}.
  323. \var{msgp} is a pointer to a message buffer, that should be based on the
  324. \var{TMsgBuf} type. \var{msgsiz} is the size of the message (NOT of the
  325. message buffer record !)
  326. The \var{msgflg} can have a combination of the following values (ORed
  327. together):
  328. \begin{description}
  329. \item [0] No special meaning. The message will be written to the queue.
  330. If the queue is full, then the process is blocked.
  331. \item [IPC\_NOWAIT] If the queue is full, then no message is written,
  332. and the call returns immediatly.
  333. \end{description}
  334. The function returns \var{True} if the message was sent successfully,
  335. \var{False} otherwise.
  336. \Errors
  337. In case of error, the call returns \var{False}, and \var{IPCerror} is set.
  338. \SeeAlso
  339. \seef{msgget}, \seef{msgrcv}, seef{msgctl}
  340. \end{function}
  341. For an example, see \seef{msgctl}.
  342. \begin{function}{msgrcv}
  343. \Declaration
  344. Function msgrcv(msqid:longint; msgp: PMSGBuf; msgsz: longint; msgtyp:longint; msgflg:longint): Boolean;
  345. \Description
  346. \var{msgrcv} retrieves a message of type \var{msgtyp} from the message
  347. queue with ID \var{msqid}. \var{msgtyp} corresponds to the \var{mtype}
  348. field of the \var{TMSGbuf} record. The message is stored in the \var{MSGbuf}
  349. structure pointed to by \var{msgp}.
  350. The \var{msgflg} parameter can be used to control the behaviour of the
  351. \var{msgrcv} call. It consists of an ORed combination of the following
  352. flags:
  353. \begin{description}
  354. \item [0] No special meaning.
  355. \item [IPC\_NOWAIT] if no messages are available, then the call returns
  356. immediatly, with the \var{ENOMSG} error.
  357. \item [MSG\_NOERROR] If the message size is wrong (too large),
  358. no error is generated, instead the message is truncated.
  359. Normally, in such cases, the call returns an error (E2BIG)
  360. \end{description}
  361. The function returns \var{True} if the message was received correctly,
  362. \var{False} otherwise.
  363. \Errors
  364. In case of error, \var{False} is returned, and \var{IPCerror} is set.
  365. \SeeAlso
  366. \seef{msgget}, \seef{msgsnd}, \seef{msgctl}
  367. \end{function}
  368. For an example, see \seef{msgctl}.
  369. \begin{function}{msgctl}
  370. \Declaration
  371. Function msgctl(msqid:longint; cmd: longint; buf: PMSQid\_ds): Boolean;
  372. \Description
  373. \var{msgctl} performs various operations on the message queue with id
  374. \var{ID}. Which operation is performed, depends on the \var{cmd}
  375. parameter, which can have one of the following values:
  376. \begin{description}
  377. \item[IPC\_STAT] In this case, the \var{msgctl} call fills the
  378. \var{TMSQid\_ds} structure with information about the message queue.
  379. \item[IPC\_SET] in this case, the \var{msgctl} call sets the permissions
  380. of the queue as specified in the \var{ipc\_perm} record inside \var{buf}.
  381. \item[IPC\_RMID] If this is specified, the message queue will be removed
  382. from the system.
  383. \end{description}
  384. \var{buf} contains the data that are needed by the call. It can be
  385. \var{Nil} in case the message queue should be removed.
  386. The function returns \var{True} if successfull, \var{False} otherwise.
  387. \Errors
  388. On error, \var{False} is returned, and \var{IPCerror} is set accordingly.
  389. \SeeAlso
  390. \seef{msgget}, \seef{msgsnd}, \seef{msgrcv}
  391. \end{function}
  392. \FPCexample{msgtool}
  393. \begin{function}{semget}
  394. \Declaration
  395. Function semget(key:Tkey; nsems:longint; semflg:longint): longint;
  396. \Description
  397. \var{msgget} returns the ID of the semaphore set described by \var{key}.
  398. Depending on the flags in \var{semflg}, a new queue is created.
  399. \var{semflg} can have one or more of the following values (combined by ORs):
  400. \begin{description}
  401. \item[IPC\_CREAT] The queue is created if it doesn't already exist.
  402. \item[IPC\_EXCL] If used in combination with \var{IPC\_CREAT}, causes the
  403. call to fail if the set already exists. It cannot be used by itself.
  404. \end{description}
  405. Optionally, the flags can be \var{OR}ed with a permission mode, which is the
  406. same mode that can be used in the file system.
  407. if a new set of semaphores is created, then there will be \var{nsems}
  408. semaphores in it.
  409. \Errors
  410. On error, -1 is returned, and \var{IPCError} is set.
  411. \SeeAlso
  412. \seef{ftok}, \seef{semop}, \seef{semctl}
  413. \end{function}
  414. \begin{function}{semop}
  415. \Declaration
  416. Function semop(semid:longint; sops: pointer; nsops: cardinal): Boolean;
  417. \Description
  418. \var{semop} performs a set of operations on a message queue.
  419. \var{sops} points to an array of type \var{TSEMbuf}. The array should
  420. contain \var{nsops} elements.
  421. The fields of the \var{TSEMbuf} structure
  422. \begin{verbatim}
  423. TSEMbuf = record
  424. sem_num : word;
  425. sem_op : integer;
  426. sem_flg : integer;
  427. \end{verbatim}
  428. should be filled as follows:
  429. \begin{description}
  430. \item[sem\_num] The number of the semaphore in the set on which the
  431. operation must be performed.
  432. \item[sem\_op] The operation to be performed. The operation depends on the
  433. sign of \var{sem\_op}
  434. \begin{enumerate}
  435. \item A positive number is simply added to the current value of the
  436. semaphore.
  437. \item If 0 (zero) is specified, then the process is suspended until the
  438. specified semaphore reaches zero.
  439. \item If a negative number is specified, it is substracted from the
  440. current value of the semaphore. If the value would become negative
  441. then the process is suspended until the value becomes big enough, unless
  442. \var{IPC\_NOWAIT} is specified in the \var{sem\_flg}.
  443. \end{enumerate}
  444. \item[sem\_flg] Optional flags: if \var{IPC\_NOWAIT} is specified, then the
  445. calling process will never be suspended.
  446. \end{description}
  447. The function returns \var{True} if the operations were successful,
  448. \var{False} otherwise.
  449. \Errors
  450. In case of error, \var{False} is returned, and \var{IPCerror} is set.
  451. \SeeAlso
  452. \seef{semget}, \seef{semctl}
  453. \end{function}
  454. \begin{function}{semctl}
  455. \Declaration
  456. Function semctl(semid:longint; semnum:longint; cmd:longint; var arg: tsemun): longint;
  457. \Description
  458. \var{semctl} performs various operations on the semaphore \var{semnum} w
  459. ith semaphore set id \var{ID}.
  460. The \var{arg} parameter supplies the data needed for each call. This is
  461. a variant record that should be filled differently, according to the
  462. command:
  463. \begin{verbatim}
  464. Type
  465. TSEMun = record
  466. case longint of
  467. 0 : ( val : longint );
  468. 1 : ( buf : PSEMid_ds );
  469. 2 : ( arr : PWord );
  470. 3 : ( padbuf : PSeminfo );
  471. 4 : ( padpad : pointer );
  472. end;
  473. \end{verbatim}
  474. Which operation is performed, depends on the \var{cmd}
  475. parameter, which can have one of the following values:
  476. \begin{description}
  477. \item[IPC\_STAT] In this case, the arg record should have it's \var{buf}
  478. field set to the address of a \var{TSEMid\_ds} record.
  479. The \var{semctl} call fills this \var{TSEMid\_ds} structure with information
  480. about the semaphore set.
  481. \item[IPC\_SET] In this case, the \var{arg} record should have it's \var{buf}
  482. field set to the address of a \var{TSEMid\_ds} record.
  483. The \var{semctl} call sets the permissions of the queue as specified in
  484. the \var{ipc\_perm} record.
  485. \item[IPC\_RMID] If this is specified, the semaphore set is removed from
  486. from the system.
  487. \item[GETALL] In this case, the \var{arr} field of \var{arg} should point
  488. to a memory area where the values of the semaphores will be stored.
  489. The size of this memory area is \var{SizeOf(Word)* Number of semaphores
  490. in the set}.
  491. This call will then fill the memory array with all the values of the
  492. semaphores.
  493. \item[GETNCNT] This will fill the \var{val} field of the \var{arg} union
  494. with the bumber of processes waiting for resources.
  495. \item[GETPID] \var{semctl} returns the process ID of the process that
  496. performed the last \seef{semop} call.
  497. \item[GETVAL] \var{semctl} returns the value of the semaphore with number
  498. \var{semnum}.
  499. \item[GETZCNT] \var{semctl} returns the number of processes waiting for
  500. semaphores that reach value zero.
  501. \item[SETALL] In this case, the \var{arr} field of \var{arg} should point
  502. to a memory area where the values of the semaphores will be retrieved from.
  503. The size of this memory area is \var{SizeOf(Word)* Number of semaphores
  504. in the set}.
  505. This call will then set the values of the semaphores from the memory array.
  506. \item[SETVAL] This will set the value of semaphore \var{semnum} to the value
  507. in the \var{val} field of the \var{arg} parameter.
  508. \end{description}
  509. The function returns -1 on error.
  510. \Errors
  511. The function returns -1 on error, and \var{IPCerror} is set accordingly.
  512. \SeeAlso
  513. \seef{semget}, \seef{semop}
  514. \end{function}
  515. \FPCexample{semtool}
  516. \begin{function}{shmget}
  517. \Declaration
  518. Function shmget(key: Tkey; Size:longint; flag:longint):longint;
  519. \Description
  520. \var{shmget} returns the ID of a shared memory block, described by \var{key}.
  521. Depending on the flags in \var{flag}, a new memory block is created.
  522. \var{flag} can have one or more of the following values (combined by ORs):
  523. \begin{description}
  524. \item[IPC\_CREAT] The queue is created if it doesn't already exist.
  525. \item[IPC\_EXCL] If used in combination with \var{IPC\_CREAT}, causes the
  526. call to fail if the queue already exists. It cannot be used by itself.
  527. \end{description}
  528. Optionally, the flags can be \var{OR}ed with a permission mode, which is the
  529. same mode that can be used in the file system.
  530. if a new memory block is created, then it will have size \var{Size}
  531. semaphores in it.
  532. \Errors
  533. On error, -1 is returned, and \var{IPCError} is set.
  534. \SeeAlso
  535. \end{function}
  536. \begin{function}{shmat}
  537. \Declaration
  538. Function shmat (shmid:longint; shmaddr:pchar; shmflg:longint):pchar;
  539. \Description
  540. \var{shmat} attaches a shared memory block with identified \var{shmid}
  541. to the current process. The function returns a pointer to the shared memory
  542. block.
  543. If \var{shmaddr} is \var{Nil}, then the system chooses a free unmapped
  544. memory region, as high up in memory space as possible.
  545. If \var{shmaddr} is non-nil, and \var{SHM\_RND} is in \var{shmflg}, then
  546. the returned address is \var{shmaddr}, rounded down to \var{SHMLBA}.
  547. If \var{SHM\_RND} is not specified, then \var{shmaddr} must be a
  548. page-aligned address.
  549. The parameter \var{shmflg} can be used to control the behaviour of the
  550. \var{shmat} call. It consists of a ORed combination of the following
  551. costants:
  552. \begin{description}
  553. \item[SHM\_RND] The suggested address in \var{shmaddr} is rounded down to
  554. \var{SHMLBA}.
  555. \item[SHM\_RDONLY] the shared memory is attached for read access only.
  556. Otherwise the memory is attached for read-write. The process then needs
  557. read-write permissions to access the shared memory.
  558. \end{description}
  559. \Errors
  560. If an error occurs, -1 is returned, and \var{IPCerror} is set.
  561. \SeeAlso
  562. \seef{shmget}, \seef{shmdt}, \seef{shmctl}
  563. \end{function}
  564. For an example, see \seef{shmctl}.
  565. \begin{function}{shmdt}
  566. \Declaration
  567. Function shmdt (shmaddr:pchar):boolean;
  568. \Description
  569. \var{shmdt} detaches the shared memory at address \var{shmaddr}. This shared
  570. memory block is unavailable to the current process, until it is attached
  571. again by a call to \seef{shmat}.
  572. The function returns \var{True} if the memory block was detached
  573. successfully, \var{False} otherwise.
  574. \Errors
  575. On error, False is returned, and IPCerror is set.
  576. \SeeAlso
  577. \seef{shmget}, \seef{shmat}, \seef{shmctl}
  578. \end{function}
  579. \begin{function}{shmctl}
  580. \Declaration
  581. Function shmctl(shmid:longint; cmd:longint; buf: pshmid\_ds): Boolean;
  582. \Description
  583. \var{shmctl} performs various operations on the shared memory block
  584. identified by identifier \var{shmid}.
  585. The \var{buf} parameter points to a \var{TSHMid\_ds} record. The
  586. \var{cmd} parameter is used to pass which operation is to be performed.
  587. It can have one of the following values :
  588. \begin{description}
  589. \item[IPC\_STAT] \var{shmctl} fills the \var{TSHMid\_ds} record that
  590. \var{buf} points to with the available information about the shared memory
  591. block.
  592. \item[IPC\_SET] applies the values in the \var{ipc\_perm} record that
  593. \var{buf} points to, to the shared memory block.
  594. \item[IPC\_RMID] the shared memory block is destroyed (after all processes
  595. to which the block is attached, have detached from it).
  596. \end{description}
  597. If successful, the function returns \var{True}, \var{False} otherwise.
  598. \Errors
  599. If an error occurs, the function returns \var{False}, and \var{IPCerror}
  600. is set.
  601. \SeeAlso
  602. \seef{shmget}, \seef{shmat}, \seef{shmdt}
  603. \end{function}
  604. \FPCexample{shmtool}