+ All Categories
Home > Documents > 1 UNIX Systems Programming Interprocess communication.

1 UNIX Systems Programming Interprocess communication.

Date post: 26-Dec-2015
Category:
Upload: kelley-edwards
View: 221 times
Download: 1 times
Share this document with a friend
Popular Tags:
62
UNIX Systems UNIX Systems Programming Programming Interprocess communication
Transcript
Page 1: 1 UNIX Systems Programming Interprocess communication.

1

UNIX Systems ProgrammingUNIX Systems ProgrammingInterprocess communication

Page 2: 1 UNIX Systems Programming Interprocess communication.

2

OverviewOverview

1.1. PipesPipes

2.2. Co-processesCo-processes

3.3. FIFOsFIFOs

4.4. Message queuesMessage queues

5.5. Semaphores & Shared memory Semaphores & Shared memory

continued

Page 3: 1 UNIX Systems Programming Interprocess communication.

3

1. Pipes1. Pipes A form of interprocess communicationA form of interprocess communication Between processes that have a common Between processes that have a common

ancestorancestor Typical use:Typical use:

– Pipe created by a processPipe created by a process– Process calls fork()Process calls fork()– Pipe used between parent and childPipe used between parent and child

Page 4: 1 UNIX Systems Programming Interprocess communication.

4

Differences between Differences between versionsversions All systems support half-duplexAll systems support half-duplex

– Data flows in only one directionData flows in only one direction Many newer systems support full duplexMany newer systems support full duplex

– Data flows in two directionsData flows in two directions For portability, assume only half-duplexFor portability, assume only half-duplex

Page 5: 1 UNIX Systems Programming Interprocess communication.

5

Differences between Differences between versionsversions All systems support half-duplexAll systems support half-duplex

– Data flows in only one directionData flows in only one direction Many newer systems support full duplexMany newer systems support full duplex

– Data flows in two directionsData flows in two directions For portability, assume only half-duplexFor portability, assume only half-duplex

Page 6: 1 UNIX Systems Programming Interprocess communication.

6

Creating a pipeCreating a pipe

#include <unistd.h>#include <unistd.h>

int pipe(int filedes[2]);int pipe(int filedes[2]); Returns 0 if ok, -1 on errorReturns 0 if ok, -1 on error Returns two file descriptorsReturns two file descriptors

– filedes[0] is open for readingfiledes[0] is open for reading– filedes[1] is open for writingfiledes[1] is open for writing– Output of filedes[1] is input to filedes[0]Output of filedes[1] is input to filedes[0]

Page 7: 1 UNIX Systems Programming Interprocess communication.

7

After the pipe() callAfter the pipe() call

00 stdinstdin

11 stdoutstdout

22 stderrstderr

33

44

55 3 4

Filedes 0 1

Page 8: 1 UNIX Systems Programming Interprocess communication.

8

The process then calls fork()The process then calls fork() 00 stdinstdin

11 stdoutstdout

22 stderrstderr

33

44

55

00 stdinstdin

11 stdoutstdout

22 stderrstderr

33

44

55

Parent Child

Page 9: 1 UNIX Systems Programming Interprocess communication.

9

And then ….And then ….

We close the read end in one processWe close the read end in one process And close the write end in the other process And close the write end in the other process To get ….To get ….

Page 10: 1 UNIX Systems Programming Interprocess communication.

10

Parent writing to childParent writing to child 00 stdinstdin

11 stdoutstdout

22 stderrstderr

33 XX

44

55

00 stdinstdin

11 stdoutstdout

22 stderrstderr

33

44 XX

55

Parent Child

Page 11: 1 UNIX Systems Programming Interprocess communication.

11

Child writing to parentChild writing to parent 00 stdinstdin

11 stdoutstdout

22 stderrstderr

33

44 XX

55

00 stdinstdin

11 stdoutstdout

22 stderrstderr

33 XX

44

55

Parent Child

Page 12: 1 UNIX Systems Programming Interprocess communication.

12

After one end of the pipe is closed …After one end of the pipe is closed …

Reading from a empty pipe whose write end Reading from a empty pipe whose write end has been closed returns 0 (indicating EOF)has been closed returns 0 (indicating EOF)

Writing to a pipe whose read end has been Writing to a pipe whose read end has been closed generates a SIGPIPE signalclosed generates a SIGPIPE signal– If we ignore the signal or catch and return, If we ignore the signal or catch and return,

handler returns -1, and errno set to EPIPEhandler returns -1, and errno set to EPIPE

Page 13: 1 UNIX Systems Programming Interprocess communication.

13

Example …Example …

#include <unistd.h>#include <unistd.h>#include <stdio.h>#include <stdio.h>int main(void){int main(void){

int n; // to keep track of num bytes readint n; // to keep track of num bytes read int fd[2]; // to hold fds of both ends of pipeint fd[2]; // to hold fds of both ends of pipe pid_t pid; // pid of child processpid_t pid; // pid of child process char line[80]; // buffer to hold text read/writtenchar line[80]; // buffer to hold text read/written … …

Page 14: 1 UNIX Systems Programming Interprocess communication.

14

Continued …Continued …

if (pipe(fd) < 0) if (pipe(fd) < 0) // create the pipe // create the pipe perror("pipe error");perror("pipe error");

if ((pid = fork()) < 0) { if ((pid = fork()) < 0) { // fork off a child // fork off a child perror("fork error");perror("fork error"); } else if (pid > 0) { } else if (pid > 0) { // parent process // parent process close(fd[0]); close(fd[0]); // close read end// close read end write(fd[1], "hello world\n", 12); // write to it write(fd[1], "hello world\n", 12); // write to it }…}…

Page 15: 1 UNIX Systems Programming Interprocess communication.

15

continuedcontinued

} else { } else { // child process// child process

close(fd[1]); close(fd[1]); // close write end// close write end

n = read(fd[0], line, 80); // read from pipen = read(fd[0], line, 80); // read from pipe

write(1, line, n); write(1, line, n); // echo to screen// echo to screen

}}

exit(0);exit(0);

}}

Page 16: 1 UNIX Systems Programming Interprocess communication.

16

dup() and dup2dup() and dup2

#include <unistd.h>#include <unistd.h>int dup(int filedes);int dup(int filedes);int dup2(int filedes, int filedes2);int dup2(int filedes, int filedes2);

Both will duplicate an existing file descriptorBoth will duplicate an existing file descriptor dup() returns lowest available file descriptor, now dup() returns lowest available file descriptor, now

referring to whatever filedes refers toreferring to whatever filedes refers to dup2() - filedes2 (if open) will be closed and then dup2() - filedes2 (if open) will be closed and then

set to refer to whatever filedes refers toset to refer to whatever filedes refers to

Page 17: 1 UNIX Systems Programming Interprocess communication.

17

Redirection Redirection

See See command_pipe.Ccommand_pipe.C

Page 18: 1 UNIX Systems Programming Interprocess communication.

18

popen and pclosepopen and pclose

#include <stdio.h>#include <stdio.h>FILE *popen(const char *cmdstring, const char *type);FILE *popen(const char *cmdstring, const char *type);

Returns file pointer if OK, NULL on errorReturns file pointer if OK, NULL on error

int pclose(FILE *fp);int pclose(FILE *fp);Returns: termination status of cmdstring, or -1 on errorReturns: termination status of cmdstring, or -1 on error

Handle the “dirty work” of creating pipe, forking child, Handle the “dirty work” of creating pipe, forking child, closing unused ends, executing shell to run program, closing unused ends, executing shell to run program, waiting for command to terminatewaiting for command to terminate

popen runs cmdstring with output directed to fp returned popen runs cmdstring with output directed to fp returned by callby call

Page 19: 1 UNIX Systems Programming Interprocess communication.

19

exampleexample

Page 20: 1 UNIX Systems Programming Interprocess communication.

20

FIFOsFIFOs

Page 21: 1 UNIX Systems Programming Interprocess communication.

21

Message QueuesMessage Queues

Page 22: 1 UNIX Systems Programming Interprocess communication.

22

Semaphores andSemaphores and Shared Memory Shared Memory

Page 23: 1 UNIX Systems Programming Interprocess communication.

23

Directory StructureDirectory Structure A directory ‘file’ is a sequence of lines; each A directory ‘file’ is a sequence of lines; each

line holds an line holds an i-node numberi-node number and a file name. and a file name.

The data is stored as binary, so we cannot The data is stored as binary, so we cannot simply use simply use cat to view itto view it

Page 24: 1 UNIX Systems Programming Interprocess communication.

24

I-nodeI-node::– The administrative information about a file The administrative information about a file

is kept in a structure known as an is kept in a structure known as an inodeinode.. Inodes in a file system, in general, are structured Inodes in a file system, in general, are structured

as an array known as an as an array known as an inode tableinode table..

– An inode number, which is an index to the An inode number, which is an index to the inode table, inode table, uniquely identifiesuniquely identifies a file in a file a file in a file system.system.

Page 25: 1 UNIX Systems Programming Interprocess communication.

25

i-node and Data Blocksi-node and Data Blocks

Page 26: 1 UNIX Systems Programming Interprocess communication.

26

2. Links2. Links

2.12.1 What is a Link?What is a Link?2.22.2 Creating a LinkCreating a Link2.32.3 Seeing LinksSeeing Links2.42.4 Removing a LinkRemoving a Link2.52.5 Symbolic LinksSymbolic Links2.6 Implementation2.6 Implementation

Page 27: 1 UNIX Systems Programming Interprocess communication.

27

2.1. What is a Link?2.1. What is a Link?

A link is a pointer to a file.A link is a pointer to a file.

Useful for sharing files:Useful for sharing files:– a file can be shared by giving each person their a file can be shared by giving each person their

own link (pointer) to it.own link (pointer) to it.

Page 28: 1 UNIX Systems Programming Interprocess communication.

28

2.2. Creating a Link2.2. Creating a Link

ln existing-file new-pointerln existing-file new-pointer

Jenny types:Jenny types:ln draft /home/bob/letterln draft /home/bob/letter

/

home

bob jenny

memo planning

/home/bob/draftand/home/jenny/letter

Page 29: 1 UNIX Systems Programming Interprocess communication.

29

Changes to a file affects every link:Changes to a file affects every link:$ cat file_aThis is file A.$ ln file_a file_b$ cat file_bThis is file A.

$ vi file_b :

$ cat file_bThis is file B after the change.$ cat file_aThis is file B after the change.

Page 30: 1 UNIX Systems Programming Interprocess communication.

30

2.3. Seeing Links2.3. Seeing Links

Compare status information:Compare status information:$ ls -l file_a file_b file_c file_d

-rw-r--r-- 2 dkl 33 May 24 10:52 file_a-rw-r--r-- 2 dkl 33 May 24 10:52 file_b-rw-r--r-- 1 dkl 16 May 24 10:55 file_c-rw-r--r-- 1 dkl 33 May 24 10:57 file_d

Look at inode number:Look at inode number:$ $ ls -i file_a file_b file_c file_d

3534 file_a 3534 file_b 5800 file_c 7328 file_d

Page 31: 1 UNIX Systems Programming Interprocess communication.

31

2.4. Removing a Link2.4. Removing a Link

Deleting a link does not remove the file.Deleting a link does not remove the file.

Only when the file Only when the file andand every link is gone every link is gone will the file be removed.will the file be removed.

Page 32: 1 UNIX Systems Programming Interprocess communication.

32

2.5. Symbolic Links2.5. Symbolic Links

The links described so far are often called The links described so far are often called hard linkshard links– a hard link is a pointer to a file which must be on a hard link is a pointer to a file which must be on

the the samesame file system file system

A A symbolic linksymbolic link is an is an indirect pointerindirect pointer to a file to a file– it stores the pathname of the pointed-to fileit stores the pathname of the pointed-to file– it can link it can link acrossacross file systems file systems

Page 33: 1 UNIX Systems Programming Interprocess communication.

33

Jenny types:Jenny types:ln ln -s-s shared /home/dkl/project shared /home/dkl/project

/

home

dkl jenny

memo planning

/home/jenny/sharedand/home/dkl/project

separatefile system

Page 34: 1 UNIX Systems Programming Interprocess communication.

34

Symbolic links are listed differently:Symbolic links are listed differently:

$ ln -s pics /home/mh/img

$ ls -lF pics /home/mh/img

drw-r--r-- 1 dkl staff 981 May 24 10:55 pics

lrwxrwxrxw 1 dkl staff 4 May 24 10:57/home/mh/img --> pics

Page 35: 1 UNIX Systems Programming Interprocess communication.

35

?

abcabcabc

update newdelete new

XYXYabc

newbob newbob bobnew

2.6 Link Creation, Update & Removal2.6 Link Creation, Update & Removal

continued

abc

cp bob new

abc abc

ln bob new ln -s bob new

bob

abc

new

XY

Page 36: 1 UNIX Systems Programming Interprocess communication.

36

2.7 2.7 link() and and unlink()unlink()#include <unistd.h>int link( const char *oldpath, const char *newpath );

Meaning of:Meaning of: link( “abc”, “xyz” )

:

:

120207135

“fred.html”“abc”“bookmark.c”

207 “xyz”

continued

Page 37: 1 UNIX Systems Programming Interprocess communication.

37

unlink() clears the clears the directory recorddirectory record – usually means that the i-node number is set to 0usually means that the i-node number is set to 0

The i-node is only deleted when the The i-node is only deleted when the lastlast link to it is removed; the data block for the link to it is removed; the data block for the file is also deleted (reclaimed) & no process file is also deleted (reclaimed) & no process have the file openedhave the file opened

Page 38: 1 UNIX Systems Programming Interprocess communication.

38

Example: unlinkExample: unlink#include <stdio.h>#include <sys/stat.h>#include <sys/types.h>#include <fcntl.h>

int main(void){

if( open( "tempfile", O_RDWR ) < 0 ) {

perror( "open error“ ); exit( 1 ); }

if( unlink( "tempfile“ ) < 0 ) {

perror( "unlink error“ ); exit( 1 ); }

printf( "file unlinked\n“ ); exit(0);

}

Page 39: 1 UNIX Systems Programming Interprocess communication.

39

symlink()symlink()#include <unistd.h>int symlink(const char *oldpath, const char *newpath);

Creates a symbolic link named Creates a symbolic link named newpathnewpath which which contains the string contains the string oldpath.oldpath.

Symbolic links are interpreted at run-time.Symbolic links are interpreted at run-time.

Dangling link – may point to an non-existing file.Dangling link – may point to an non-existing file.

If If newpathnewpath exists it will not be overwritten. exists it will not be overwritten.

Page 40: 1 UNIX Systems Programming Interprocess communication.

40

readlink()readlink()#include <unistd.h>int readlink( const char *path, char *buf,

size_t bufsiz ); Read value of a symbolic link (does not follow the link).

– Places the contents of the symbolic link path in the buffer buf, which has size bufsiz.

– Does not append a NULL character to buf.

Return value– The count of characters placed in the buffer if it

succeeds. -1 if an error occurs.

Page 41: 1 UNIX Systems Programming Interprocess communication.

41

3. Subdirectory Creation3. Subdirectory Creation “mkdir uga” causes:causes:

– the creation of a the creation of a ugauga directory file and an i- directory file and an i-node for itnode for it

– an i-node number and name are added to the an i-node number and name are added to the parent directory fileparent directory file

:

:

120207135

“fred.html”“abc”“bookmark.c”

201 “uga”

Page 42: 1 UNIX Systems Programming Interprocess communication.

42

4. “4. “..” and “” and “....””

““..” and “” and “....” are stored as ordinary file names with ” are stored as ordinary file names with i-node numbers pointing to the correct directory i-node numbers pointing to the correct directory files.files.

Example:Example: dkl

book memos

continued

Page 43: 1 UNIX Systems Programming Interprocess communication.

43

In more detail:In more detail:

123247260

“.”“..”“book”

401 “memos”

Directory ben

260123566

“.”“..”“chap1”

567 “chap2”

Directory book

“chap3”590

401123800

“.”“..”“kh”

810077

“kd”

Directory memos

“mw”590

Page 44: 1 UNIX Systems Programming Interprocess communication.

44

5. 5. mkdir()mkdir()

#include <sys/types.h>#include <fcntl.h>#include <unistd.h>

int mkdir(char *pathname, mode_t mode);

Creates a new directory with the specified Creates a new directory with the specified mode: return 0 if ok, -1 on error: return 0 if ok, -1 on error

continued

Page 45: 1 UNIX Systems Programming Interprocess communication.

45

““..” and “” and “....” entries are added ” entries are added automaticallyautomatically

modemode must include execute permissions so must include execute permissions so the user(s) can use the user(s) can use cd..

e.g.e.g. 07550755

Page 46: 1 UNIX Systems Programming Interprocess communication.

46

6. 6. rmdir()rmdir()

#include <unistd.h>int rmdir(char *pathname);

Delete an empty directory;Delete an empty directory;return 0 if ok, -1 on error.return 0 if ok, -1 on error.

Will delay until other processes have stopped Will delay until other processes have stopped using the directory.using the directory.

Page 47: 1 UNIX Systems Programming Interprocess communication.

47

7. Reading Directories7. Reading Directories

#include <sys/types.h>#include <dirent.h>

DIR *opendir(char *pathname);

struct dirent *readdir(DIR *dp);

int closedir(DIR *dp);

returns apointer if ok, NULL on error

returns apointer if ok, NULL at end or on error

Page 48: 1 UNIX Systems Programming Interprocess communication.

48

direntdirent and and DIRDIR

struct dirent {long d_ino; /* i-node number */char d_name[NAME_MAX+1]; /* fname */off_t d_off; /* offset to next rec */unsigned short d_reclen; /* record length */

}

DIR is a directory stream (similar to is a directory stream (similar to FILE))– when a directory is first opened, the stream points to when a directory is first opened, the stream points to

the first entry in the directorythe first entry in the directory

Page 49: 1 UNIX Systems Programming Interprocess communication.

49

Example: listdir.cExample: listdir.c

#include <stdio.h>#include <dirent.h>

int main(){ DIR *dp;

struct dirent *dir;

if( (dp = opendir(“.”)) == NULL ) {

fprintf( stderr, “Cannot open dir\n” ); exit(1); }

continued

List the contents of the current directory.

Page 50: 1 UNIX Systems Programming Interprocess communication.

50

/* read entries */ while( (dir = readdir(dp)) != NULL )

{ /* ignore empty records */ if( dir->d_ino != 0 ) printf( “%s\n”, dir->d_name );

}closedir( dp );

return 0;} /* end main */

Page 51: 1 UNIX Systems Programming Interprocess communication.

51

8. 8. chdir()chdir()

#include <unistd.h>

int chdir( char *pathname );

int fchdir( int fd );

Change the current working directory (Change the current working directory (cwdcwd) ) of the calling process; return 0 if ok, -1 on of the calling process; return 0 if ok, -1 on error.error.

Page 52: 1 UNIX Systems Programming Interprocess communication.

52

Example: cd to /tmpExample: cd to /tmp

Part of Part of to_tmp.cc::

:if( chdir(“/tmp” ) < 0 printf( “chdir error\n”) ;else printf( “In /tmp\n” );

Page 53: 1 UNIX Systems Programming Interprocess communication.

53

Directory Change is LocalDirectory Change is Local

The directory change is limited to within the The directory change is limited to within the program.program.

e.g.e.g.$ pwd$ pwd/usr/lib/usr/lib$ to_tmp$ to_tmp /* from last slide */ /* from last slide */In /tmpIn /tmp$ pwd$ pwd/usr/lib/usr/lib

Page 54: 1 UNIX Systems Programming Interprocess communication.

54

9. 9. getcwd()getcwd()

#include <unistd.h>char *getcwd(char *buf, int size);

Store the Store the cwdcwd of the calling process in of the calling process in bufbuf;;return return bufbuf if ok, if ok, NULLNULL on error. on error.

bufbuf must be big enough for the pathname must be big enough for the pathname string (string (sizesize specifies the length of specifies the length of bufbuf).).

Page 55: 1 UNIX Systems Programming Interprocess communication.

55

ExampleExample

#include <stdio.h>#include <unistd.h>#include <dirent.h> /* for NAME_MAX */

int main(){

char name[NAME_MAX+1];

if( getcwd( name, NAME_MAX+1 ) == NULL ) printf( “getcwd error\n” ); else printf( “cwd = %s\n”, name ): :

Page 56: 1 UNIX Systems Programming Interprocess communication.

56

10. Walking over 10. Walking over DirectoriesDirectories

'Visit' every file in a specified directory 'Visit' every file in a specified directory andand all of all of its subdirectoriesits subdirectories– visit means get the name of the file visit means get the name of the file

Apply a user-defined function to every visited file.Apply a user-defined function to every visited file.

Page 57: 1 UNIX Systems Programming Interprocess communication.

57

Function PrototypesFunction Prototypes

#include <ftw.h>

/* ftw means file tree walk, starting at directory */int ftw( char *directory, MyFunc *fp,

int depth );

/* apply MyFunc() to each visited file */typedef int MyFunc( const char *file,

struct stat *sbuf, int flag );

continued

Page 58: 1 UNIX Systems Programming Interprocess communication.

58

depthdepth is the maximum number of is the maximum number of directories that can be open at once. Safest directories that can be open at once. Safest value is 1, although it slows down value is 1, although it slows down ftw()ftw()..

Result of Result of ftw()ftw(): 0 for a successful visit of : 0 for a successful visit of everyevery file, -1 on error. file, -1 on error.

Page 59: 1 UNIX Systems Programming Interprocess communication.

59

MyFuncMyFunc Details Details

The The filefile argument is the pathname argument is the pathname relative to the start directoryrelative to the start directory– it will be passed to it will be passed to MyFunc()MyFunc() automatically automatically

by by ftw()ftw() as it visits each file as it visits each file

sbufsbuf argument is a pointer to the argument is a pointer to the statstat information for the file being examined.information for the file being examined.

continued

Page 60: 1 UNIX Systems Programming Interprocess communication.

60

The The flagflag argument will be set to one of the argument will be set to one of the following for the item being examined:following for the item being examined:– FTW_FFTW_F Item is a regular file.Item is a regular file.– FTW_DFTW_D Item is a directory.Item is a directory.– FTW_NSFTW_NS Could not get stat info for item.Could not get stat info for item.– FTW_DNRFTW_DNR Directory cannot be read.Directory cannot be read.

If the If the MyFuncMyFunc function returns a non-zero value function returns a non-zero value then the then the ftw()ftw() walk will terminate. walk will terminate.

Page 61: 1 UNIX Systems Programming Interprocess communication.

61

Example: shower.cExample: shower.c

#include <stdio.h>#include <stdio.h>#include <sys/types.h>#include <sys/types.h>#include <sys/stat.h>#include <sys/stat.h>#include <ftw.h>#include <ftw.h>

int shower(const char *file,int shower(const char *file, const struct stat *sbuf, const struct stat *sbuf, int flag); int flag);

void main()void main(){ { ftwftw(“.”, (“.”, showershower, 1); , 1); }}

continued

Print the names of all the filesfound below the current directory.

Page 62: 1 UNIX Systems Programming Interprocess communication.

62

int int showershower(const char *file,(const char *file, const struct stat *sbuf, const struct stat *sbuf, int flag) int flag)

{{ if (flag == FTW_F) /* is a file */ if (flag == FTW_F) /* is a file */ printf("Found: %s\n", file); printf("Found: %s\n", file); return 0; return 0;}}


Recommended