1 cfa37a7b 2004-04-10 devnull .TH IOPROC 3
3 b2cfc4e2 2003-09-30 devnull closeioproc,
6 b2cfc4e2 2003-09-30 devnull iointerrupt,
12 b2cfc4e2 2003-09-30 devnull iowrite \- slave I/O processes for threaded programs
13 b2cfc4e2 2003-09-30 devnull .SH SYNOPSIS
16 b2cfc4e2 2003-09-30 devnull .ift .sp 0.5
20 b2cfc4e2 2003-09-30 devnull .ta \w'Ioproc* 'u
21 b2cfc4e2 2003-09-30 devnull #include <u.h>
22 b2cfc4e2 2003-09-30 devnull #include <libc.h>
23 b2cfc4e2 2003-09-30 devnull #include <thread.h>
25 b2cfc4e2 2003-09-30 devnull typedef struct Ioproc Ioproc;
27 b2cfc4e2 2003-09-30 devnull Ioproc* ioproc(void);
29 b2cfc4e2 2003-09-30 devnull int ioopen(Ioproc *io, char *file, int omode);
30 b2cfc4e2 2003-09-30 devnull int ioclose(Ioproc *io, int fd);
31 b2cfc4e2 2003-09-30 devnull long ioread(Ioproc *io, int fd, void *a, long n);
32 b2cfc4e2 2003-09-30 devnull long ioreadn(Ioproc *io, int fd, void *a, long n);
33 b2cfc4e2 2003-09-30 devnull long iowrite(Ioproc *io, int fd, void *a, long n);
34 b2cfc4e2 2003-09-30 devnull int iodial(Ioproc *io, char *addr, char *local, char *dir, char *cdfp);
36 b2cfc4e2 2003-09-30 devnull void iointerrupt(Ioproc *io);
37 b2cfc4e2 2003-09-30 devnull void closeioproc(Ioproc *io);
39 b2cfc4e2 2003-09-30 devnull long iocall(Ioproc *io, long (*op)(va_list *arg), ...);
41 b2cfc4e2 2003-09-30 devnull .SH DESCRIPTION
43 b2cfc4e2 2003-09-30 devnull These routines provide access to I/O in slave procs.
44 b2cfc4e2 2003-09-30 devnull Since the I/O itself is done in a slave proc, other threads
45 b2cfc4e2 2003-09-30 devnull in the calling proc can run while the calling thread
46 b2cfc4e2 2003-09-30 devnull waits for the I/O to complete.
48 b2cfc4e2 2003-09-30 devnull .I Ioproc
49 b2cfc4e2 2003-09-30 devnull forks a new slave proc and returns a pointer to the
50 b2cfc4e2 2003-09-30 devnull .B Ioproc
51 b2cfc4e2 2003-09-30 devnull associated with it.
52 b2cfc4e2 2003-09-30 devnull .I Ioproc
54 b2cfc4e2 2003-09-30 devnull .I mallocz
56 b2cfc4e2 2003-09-30 devnull .IR proccreate ;
57 b2cfc4e2 2003-09-30 devnull if either fails, it calls
58 b2cfc4e2 2003-09-30 devnull .I sysfatal
59 b2cfc4e2 2003-09-30 devnull rather than return an error.
61 b2cfc4e2 2003-09-30 devnull .IR Ioopen ,
62 b2cfc4e2 2003-09-30 devnull .IR ioclose ,
63 b2cfc4e2 2003-09-30 devnull .IR ioread ,
64 b2cfc4e2 2003-09-30 devnull .IR ioreadn ,
65 b2cfc4e2 2003-09-30 devnull .IR iowrite ,
67 b2cfc4e2 2003-09-30 devnull .IR iodial
68 b2cfc4e2 2003-09-30 devnull are execute the
69 b2cfc4e2 2003-09-30 devnull similarly named library or system calls
71 bf8a59fa 2004-04-11 devnull .IR open (3),
72 bf8a59fa 2004-04-11 devnull .IR read (3),
74 bf8a59fa 2004-04-11 devnull .IR dial (3))
75 b2cfc4e2 2003-09-30 devnull in the slave process associated with
77 b2cfc4e2 2003-09-30 devnull It is an error to execute more than one call
78 b2cfc4e2 2003-09-30 devnull at a time in an I/O proc.
80 b2cfc4e2 2003-09-30 devnull .I Iointerrupt
81 b2cfc4e2 2003-09-30 devnull interrupts the call currently executing in the I/O proc.
82 b2cfc4e2 2003-09-30 devnull If no call is executing,
83 b2cfc4e2 2003-09-30 devnull .IR iointerrupt
84 b2cfc4e2 2003-09-30 devnull is a no-op.
86 b2cfc4e2 2003-09-30 devnull .I Closeioproc
87 b2cfc4e2 2003-09-30 devnull terminates the I/O proc and frees the associated
88 b2cfc4e2 2003-09-30 devnull .B Ioproc .
90 b2cfc4e2 2003-09-30 devnull .I Iocall
91 b2cfc4e2 2003-09-30 devnull is a primitive that may be used to implement
92 b2cfc4e2 2003-09-30 devnull more slave I/O routines.
93 b2cfc4e2 2003-09-30 devnull .I Iocall
94 b2cfc4e2 2003-09-30 devnull arranges for
96 b2cfc4e2 2003-09-30 devnull to be called in
97 b2cfc4e2 2003-09-30 devnull .IR io 's
98 b2cfc4e2 2003-09-30 devnull proc, with
100 b2cfc4e2 2003-09-30 devnull set to the variable parameter list,
101 b2cfc4e2 2003-09-30 devnull returning the value that
103 b2cfc4e2 2003-09-30 devnull returns.
104 b2cfc4e2 2003-09-30 devnull .SH EXAMPLE
105 b2cfc4e2 2003-09-30 devnull Relay messages between two file descriptors,
106 b2cfc4e2 2003-09-30 devnull counting the total number of bytes seen:
109 b2cfc4e2 2003-09-30 devnull .ta +\w'xxxx'u +\w'xxxx'u +\w'xxxx'u
110 b2cfc4e2 2003-09-30 devnull int tot;
113 b2cfc4e2 2003-09-30 devnull relaythread(void *v)
115 b2cfc4e2 2003-09-30 devnull int *fd, n;
116 b2cfc4e2 2003-09-30 devnull char buf[1024];
117 b2cfc4e2 2003-09-30 devnull Ioproc *io;
120 b2cfc4e2 2003-09-30 devnull io = ioproc();
121 b2cfc4e2 2003-09-30 devnull while((n = ioread(io, fd[0], buf, sizeof buf)) > 0){
122 b2cfc4e2 2003-09-30 devnull if(iowrite(io, fd[1], buf, n) != n)
123 b2cfc4e2 2003-09-30 devnull sysfatal("iowrite: %r");
124 b2cfc4e2 2003-09-30 devnull tot += n;
126 b2cfc4e2 2003-09-30 devnull closeioproc(io);
130 b2cfc4e2 2003-09-30 devnull relay(int fd0, int fd1)
132 b2cfc4e2 2003-09-30 devnull int fd[4];
134 b2cfc4e2 2003-09-30 devnull fd[0] = fd[3] = fd0;
135 b2cfc4e2 2003-09-30 devnull fd[1] = fd[2] = fd1;
136 b2cfc4e2 2003-09-30 devnull threadcreate(relaythread, fd, 8192);
137 b2cfc4e2 2003-09-30 devnull threadcreate(relaythread, fd+2, 8192);
141 b2cfc4e2 2003-09-30 devnull If the two
142 b2cfc4e2 2003-09-30 devnull .I relaythread
143 b2cfc4e2 2003-09-30 devnull instances were running in different procs, the
144 b2cfc4e2 2003-09-30 devnull common access to
146 b2cfc4e2 2003-09-30 devnull would be unsafe.
149 b2cfc4e2 2003-09-30 devnull Implement
150 b2cfc4e2 2003-09-30 devnull .IR ioread :
153 b2cfc4e2 2003-09-30 devnull static long
154 b2cfc4e2 2003-09-30 devnull _ioread(va_list *arg)
157 b2cfc4e2 2003-09-30 devnull void *a;
160 b2cfc4e2 2003-09-30 devnull fd = va_arg(*arg, int);
161 b2cfc4e2 2003-09-30 devnull a = va_arg(*arg, void*);
162 b2cfc4e2 2003-09-30 devnull n = va_arg(*arg, long);
163 b2cfc4e2 2003-09-30 devnull return read(fd, a, n);
167 b2cfc4e2 2003-09-30 devnull ioread(Ioproc *io, int fd, void *a, long n)
169 b2cfc4e2 2003-09-30 devnull return iocall(io, _ioread, fd, a, n);
172 b2cfc4e2 2003-09-30 devnull .SH SOURCE
173 b5fdffee 2004-04-19 devnull .B /usr/local/plan9/src/libthread/io*.c
174 b2cfc4e2 2003-09-30 devnull .SH SEE ALSO
175 bf8a59fa 2004-04-11 devnull .IR dial (3),
176 bf8a59fa 2004-04-11 devnull .IR open (3),
177 bf8a59fa 2004-04-11 devnull .IR read (3),
178 bf8a59fa 2004-04-11 devnull .IR thread (3)