Slides Pratica Named Pipes e Select
-
Upload
andre-perdigao -
Category
Documents
-
view
223 -
download
0
Transcript of Slides Pratica Named Pipes e Select
-
8/3/2019 Slides Pratica Named Pipes e Select
1/6
Department of Informatics EngineeringFCTUCUniversity of Coimbra
Named Pipes (also known as FIFOs) Similar to pipes but allow communication between
unrelated processes. Each pipe has a name (string). The pipe is written persistently in the file system. For creating a named pipe, use the "mkfifo" command orcall mkfifo(const char* filename, mode_t mode);
Typically, like pipes, they are half-duplex Means that they must be open read-only or write-only They are opened like files, but they are not files You cannot fseekO a named pipe; writeO always appendsto the pipe, readO always returns data from the beginningof the pipe. After data is read from the named pipe, it's no longerthere. It's not a file, it's an object in the unix kernel!
-
8/3/2019 Slides Pratica Named Pipes e Select
2/6
Unrelated client/server program (np_server.c)#define PIPE NAME "np_cUent_server"( ... )int main (){ I I Creates the named pipe if it doesn't exist yeti f mk fi fo (P IP E_ NA ME , O _C RE AT IO _E XC LI 06 00 )< 0) && (errno!= EEXIST)) {p erro r("Ca nnot c reat e pi pe: ");exit(0);}I I Opens the pipe for readingint fd;if fd = open(PIPE_NAME, O_RDONLY)) < 0) {perror( "Cannot open pipe for reading: ");exit(0);}I I Do some workn umbe rs n;while (1) {
r e ad (f d , & n, s i ze of ( nu m be rs ) );p rint f("[S ERVE R] Rece ived (%d,% d), a ddin g it: %d\n " ,n .a, n. b , n.a+n .b) ;}retu rn 0 ;
~.: [2]
Unrelated client/server program (np_client.c)#define PIPE NAME "np_cUent_server"( ... )int main (){
I I Opens the pipe for writingint fd;if fd = open(PIPE_NAME, O_WRONLY)) < 0) {perror("Cannot open pipe for writing: ");exit(0);}I I Do some workwhile (1) {n umbe rs n;n.a = rand() % 100;n.b = rand() % 100;p rint f("[C LIEN T] Send ing (%d ,%d) for a ddin g\n", n. a, n.b );w ri te (f d, & n, s iz eo f( nu mb er s) );s l ee p (2 ) ;}retu rn 0 ;
}
-
8/3/2019 Slides Pratica Named Pipes e Select
3/6
Some interesting issues... If you get a SIGPIPEsignal, this means that you are trying to
read/write from a closed pipe
A named pipe is a connection between two processes. Aprocess blocks until the other party open the pipe ... Being it for reading or writing. It's possible to bypass this behavior (open it non-blocking_ O_NONBLOCK),but be very, very careful: if not properlyprogrammed, it can lead to busy waiting. If a named pipeis open non-blocking, EOF is indicated when readO returnso .
When designing a client/server multiple client application,this means that either the pipe is re-opened after eachclient disconnects, or the pipe is open read-write.
If opened "read-write", the server will not block until theother party connects (since, he itself is also anotherparty!)
Interesting Problem A printer daemon is connected to a physical printer There are 3 named-pipes which allow automaticformatted printing/printer/aa _double _sided
/printer/a3_single_sided
-
8/3/2019 Slides Pratica Named Pipes e Select
4/6
In te res ting Problem Pipes are blocking Pooling
/printer/aa _double _sided Pipes are blocking, so thisdoesn't work!!!
/printer/a3_single_sided
lID Multiplexing 1/0 Multiple}{~~=g:The ability to examine severalfile descriptors at the same time selectO and pselectO
int select(int n, I I ~ I I : Greatest fd plus oneFor reading activit1d_set* readfd,fd_set* writefd, For writing activityfd_set* exceptfd, for out-of-band activ tystruct timeval* timeout)\\Blocks until activity is detected or a timeout occurs.The fd_set variables are input/output. Upon return, they indicateif there was activity in a certain descriptor or not. 0
-
8/3/2019 Slides Pratica Named Pipes e Select
5/6
selectO Careful: n is the number of the highest filedescriptor added of one.
It's not the number of file descriptors
fd set A bit set representingfile descriptorsI I I I I I I I I I I I I I I I I FD_ZERO(fd_set* set)Cleans up the file descriptor set FD_SET(int fd, fd_set* set)Sets a bit in the file descriptor set FD_CLEAR(int fd, fd_set* set)Clears a bit in the file descriptor set FD_ISSET(int fd, fd_set* set)Tests if a file descriptor is set
Example (printerd.c)( ... )#define BUF SIZE#de fine NUM PRIN TERS 40963const char* PRINTER_NAME[] = {" pr in te rl ", " pr in te r2 ", " pr in te r3 "};II Th e pri nter f ile de scrip torsi n t p r i nt e r [N U M _P R I NT E R S] ;v oi d c re at e_ pr in te rs () {for (int i=0; i = O );}}
int main(int argc, char* argv[]) {create_printers();accept_requests();}.......................................................................................................................................
-
8/3/2019 Slides Pratica Named Pipes e Select
6/6
Example (printerd.c) (2)v oi d a cc ep t_ re qu es ts () {while (1) {fd set read set;FD=ZERO(&read_set);
for (int i=O; i 0 ) {for (int i=O; i 0) {buf[nj = '\0';p ri nt f( " %5 ", b uf );}} while (n > 0);
c l o se ( p ri n t er [ i j) ;p r in te r [i j = o p en ( P RI NT E R_ N AM E[ i j, O _ RD ON L YI O _N ON B LO CK ) ;}
}}}}............................................................................................................d