270 likes | 394 Views
ch06: UNIX Special Files. Ju, Hong Taek Computer Network Lab. Keimyung University juht@kmu.ac.kr Rm: 1228, Tel: 580-5234. Objectives. Learn about interprocess communication Experiment with client-server interactions Explore pipes and redirections Use device control to set parameters
E N D
ch06: UNIX Special Files Ju, Hong Taek Computer Network Lab. Keimyung University juht@kmu.ac.kr Rm: 1228, Tel: 580-5234
Objectives • Learn about interprocess communication • Experiment with client-server interactions • Explore pipes and redirections • Use device control to set parameters • Understand how UNIX achieves device independence
6.1 Pipes • The capacity to communicate is essential for processes that cooperate to solve a problem • Pipe is a the simplest UNIX interprocess communication mechanism • Pipe system call • Creates a communication buffer that caller can access through the file descriptors fildes[0], and fildes[1] • The data written to fildes[1] can be read from fildes[0] on a first-in-first-out basis • Returns 0, if successful • Returns -1 and sets errno, if unsuccessful #include <unistd.h> int pipe(int fildes[2]);
A pipe has no external or permanent name • Used only by the process that created it and by descendants • Unidirectional communication buffer • When a process calls read on a pipe • Returns immediately if the pipe is not empty • Blocks until something is written if the pipe is empty • Return 0 if no process has the pipe open for writing
6.2 Pipelines • Redirection allow programs that are written as filters to be used generally ex) ls –l | sort –n +4 [2] sort [1] [0] pipe [1] ls [0] [2]
[0] [1] [2] parent [4] [3] pipe [3] [4] child [2] [0] [1] After the fork
[1] [2] parent [4] [0] [3] pipe [3] [1] [4] child [2] [0] After both dup2
[1] [2] parent [0] pipe [1] child [2] [0] After both close
6.3 FIFO • Pipes are temporarily • Disappear when no process has them open • FIFO • Named pipe: persist special file • Creates a new FIFO special file • Return 0 if successful • Return -1 if unsuccessful and set errno • The unlink function is used to removes FIFO #include <sys/sat.h> int mkfifo(const char *path, not_t mode)
6.4 Pipes and the Client-Server Model • The client-server model is a standard pattern for process interaction • Client: requests a service • Server: reply a message for the request. • Two named pipes are used • A request pipe • A response pipe
when multiple clients make request. Can the server replies be ready to the right client? Is it possible for a client to block another client request?
6.5 Terminal Control • Many special files represent devices with characteristics that are platform dependent • However, terminal control was thought to be essential on all systems • The stty command reports or sets terminal I/O characteristics • Speed, size (row, column), echo, • Retrieve the attributes by tcgetattr • Set the parameters by tcsetattr #include <termios.h> int tcgetattr( int fildes, struct termios *termios_p); int tcsetattr( int fildes, int optional_actions, const struct termios *termio_p);