ipc.tex 21 KB

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