ipc.tex 21 KB

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