1
0

ioproc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. .TH IOPROC 2
  2. .SH NAME
  3. closeioproc,
  4. iocall,
  5. ioclose,
  6. iointerrupt,
  7. iodial,
  8. ioopen,
  9. ioproc,
  10. ioread,
  11. ioreadn,
  12. iowrite \- slave I/O processes for threaded programs
  13. .SH SYNOPSIS
  14. .PP
  15. .de XX
  16. .ift .sp 0.5
  17. .ifn .sp
  18. ..
  19. .EX
  20. .ta \w'Ioproc* 'u
  21. #include <u.h>
  22. #include <libc.h>
  23. #include <thread.h>
  24. .sp
  25. typedef struct Ioproc Ioproc;
  26. .sp
  27. Ioproc* ioproc(void);
  28. .XX
  29. int ioopen(Ioproc *io, char *file, int omode);
  30. int ioclose(Ioproc *io, int fd);
  31. long ioread(Ioproc *io, int fd, void *a, long n);
  32. long ioreadn(Ioproc *io, int fd, void *a, long n);
  33. long iowrite(Ioproc *io, int fd, void *a, long n);
  34. int iodial(Ioproc *io, char *addr, char *local, char *dir, char *cdfp);
  35. .XX
  36. void iointerrupt(Ioproc *io);
  37. void closeioproc(Ioproc *io);
  38. .XX
  39. long iocall(Ioproc *io, long (*op)(va_list *arg), ...);
  40. .EE
  41. .SH DESCRIPTION
  42. .PP
  43. These routines provide access to I/O in slave procs.
  44. Since the I/O itself is done in a slave proc, other threads
  45. in the calling proc can run while the calling thread
  46. waits for the I/O to complete.
  47. .PP
  48. .I Ioproc
  49. forks a new slave proc and returns a pointer to the
  50. .B Ioproc
  51. associated with it.
  52. .I Ioproc
  53. uses
  54. .I mallocz
  55. and
  56. .IR proccreate ;
  57. if either fails, it calls
  58. .I sysfatal
  59. rather than return an error.
  60. .PP
  61. .IR Ioopen ,
  62. .IR ioclose ,
  63. .IR ioread ,
  64. .IR ioreadn ,
  65. .IR iowrite ,
  66. and
  67. .IR iodial
  68. are execute the
  69. similarly named library or system calls
  70. (see
  71. .IR open (2),
  72. .IR read (2),
  73. and
  74. .IR dial (2))
  75. in the slave process associated with
  76. .IR io .
  77. It is an error to execute more than one call
  78. at a time in an I/O proc.
  79. .PP
  80. .I Iointerrupt
  81. interrupts the call currently executing in the I/O proc.
  82. If no call is executing,
  83. .IR iointerrupt
  84. is a no-op.
  85. .PP
  86. .I Closeioproc
  87. terminates the I/O proc and frees the associated
  88. .B Ioproc .
  89. .PP
  90. .I Iocall
  91. is a primitive that may be used to implement
  92. more slave I/O routines.
  93. .I Iocall
  94. arranges for
  95. .I op
  96. to be called in
  97. .IR io 's
  98. proc, with
  99. .I arg
  100. set to the variable parameter list,
  101. returning the value that
  102. .I op
  103. returns.
  104. .SH EXAMPLE
  105. Relay messages between two file descriptors,
  106. counting the total number of bytes seen:
  107. .IP
  108. .EX
  109. .ta +\w'xxxx'u +\w'xxxx'u +\w'xxxx'u
  110. int tot;
  111. void
  112. relaythread(void *v)
  113. {
  114. int *fd, n;
  115. char buf[1024];
  116. Ioproc *io;
  117. fd = v;
  118. io = ioproc();
  119. while((n = ioread(io, fd[0], buf, sizeof buf)) > 0){
  120. if(iowrite(io, fd[1], buf, n) != n)
  121. sysfatal("iowrite: %r");
  122. tot += n;
  123. }
  124. closeioproc(io);
  125. }
  126. void
  127. relay(int fd0, int fd1)
  128. {
  129. int fd[4];
  130. fd[0] = fd[3] = fd0;
  131. fd[1] = fd[2] = fd1;
  132. threadcreate(relaythread, fd, 8192);
  133. threadcreate(relaythread, fd+2, 8192);
  134. }
  135. .EE
  136. .LP
  137. If the two
  138. .I relaythread
  139. instances were running in different procs, the
  140. common access to
  141. .I tot
  142. would be unsafe.
  143. .EE
  144. .PP
  145. Implement
  146. .IR ioread :
  147. .IP
  148. .EX
  149. static long
  150. _ioread(va_list *arg)
  151. {
  152. int fd;
  153. void *a;
  154. long n;
  155. fd = va_arg(*arg, int);
  156. a = va_arg(*arg, void*);
  157. n = va_arg(*arg, long);
  158. return read(fd, a, n);
  159. }
  160. long
  161. ioread(Ioproc *io, int fd, void *a, long n)
  162. {
  163. return iocall(io, _ioread, fd, a, n);
  164. }
  165. .EE
  166. .SH SOURCE
  167. .B /sys/src/libthread/io*.c
  168. .SH SEE ALSO
  169. .IR dial (2),
  170. .IR open (2),
  171. .IR read (2),
  172. .IR thread (2)