27
ch06: UNIX Special Files Ju, Hong Taek Computer Network Lab. Keimyung University [email protected] Rm: 1228, Tel: 580-5234

ch06: UNIX Special Files

  • Upload
    chelsey

  • View
    41

  • Download
    0

Embed Size (px)

DESCRIPTION

ch06: UNIX Special Files. Ju, Hong Taek Computer Network Lab. Keimyung University [email protected] 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 - PowerPoint PPT Presentation

Citation preview

Page 1: ch06: UNIX Special Files

ch06: UNIX Special Files

Ju, Hong TaekComputer Network Lab.

Keimyung [email protected]

Rm: 1228, Tel: 580-5234

Page 2: ch06: UNIX Special Files

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 independe

nce

Page 3: ch06: UNIX Special Files

6.1 Pipes The capacity to communicate is essential for processes t

hat 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]);

Page 4: ch06: UNIX Special Files

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

Page 5: ch06: UNIX Special Files
Page 6: ch06: UNIX Special Files

6.2 Pipelines Redirection allow programs that are

written as filters to be used generallyex) ls –l | sort –n +4

sort

ls

pipe

[0] [2]

[1]

[0] [1]

[2]

Page 7: ch06: UNIX Special Files
Page 8: ch06: UNIX Special Files

parent

child

pipe

[0] [1]

[4]

[3] [4]

[2]

[2]

[3]

[1][0]

After the fork

Page 9: ch06: UNIX Special Files

parent

child

pipe

[0]

[1] [4]

[3] [4]

[2]

[2]

[3]

[1]

[0]

After both dup2

Page 10: ch06: UNIX Special Files

parent

child

pipe

[0]

[1]

[2]

[2]

[1]

[0]

After both close

Page 11: ch06: UNIX Special Files

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)

Page 12: ch06: UNIX Special Files
Page 13: ch06: UNIX Special Files
Page 14: ch06: UNIX Special Files
Page 15: ch06: UNIX Special Files
Page 16: ch06: UNIX Special Files
Page 17: ch06: UNIX Special Files
Page 18: ch06: UNIX Special Files

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

Page 19: ch06: UNIX Special Files
Page 20: ch06: UNIX Special Files
Page 21: ch06: UNIX Special Files
Page 22: ch06: UNIX Special Files
Page 23: ch06: UNIX Special Files

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?

Page 24: ch06: UNIX Special Files

6.5 Terminal Control Many special files represent devices with characteristics t

hat are platform dependent However, terminal control was thought to be essential on

all systems The stty command reports or sets terminal I/O characteristic

s 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);

Page 25: ch06: UNIX Special Files
Page 26: ch06: UNIX Special Files
Page 27: ch06: UNIX Special Files