ipc.tex 21 KB

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