slide 1 interprocess communication. slide 2 1. pipes a form of interprocess communication between...

64
Slide Slide 1 Interprocess communication

Upload: julia-dean

Post on 29-Dec-2015

223 views

Category:

Documents


0 download

TRANSCRIPT

Slide Slide 11

Interprocess communication

Slide Slide 22

1. Pipes

A form of interprocess communication Between processes that have a common ancestor

Typical use: Pipe created by a process Process calls fork() Pipe used between parent and child

Slide Slide 33

Differences between versions

All systems support half-duplex Data flows in only one direction

Many newer systems support full duplex Data flows in two directions

For portability, assume only half-duplex

Slide Slide 44

Creating a pipe

#include <unistd.h> int pipe(int filedes[2]); Returns 0 if ok, -1 on error Returns two file descriptors

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

Slide Slide 55

After the pipe() call

0 stdin

1 stdout

2 stderr

3

4

5 3 4

Filedes 0 1

Slide Slide 66

The process then calls fork()

0 stdin

1 stdout

2 stderr

3

4

5

0 stdin

1 stdout

2 stderr

3

4

5

Parent Child

Slide Slide 77

And then ….

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

Slide Slide 88

Parent writing to child

0 stdin

1 stdout

2 stderr

3 X

4

5

0 stdin

1 stdout

2 stderr

3

4 X

5

Parent Child

Slide Slide 99

Child writing to parent

0 stdin

1 stdout

2 stderr

3

4 X

5

0 stdin

1 stdout

2 stderr

3 X

4

5

Parent Child

Slide Slide 1010

After one end of the pipe is closed …

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

Writing to a pipe whose read end has been closed generates a SIGPIPE signal If we ignore the signal or catch and return, handler returns -1,

and errno set to EPIPE

Slide Slide 1111

Example …

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

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

Slide Slide 1212

Continued …

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

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

Slide Slide 1313

continued

} else { // child process

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

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

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

}

exit(0);

}

Slide Slide 1414

dup() and dup2

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

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

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

whatever filedes refers to

Slide Slide 1515

DUP

Duplicate a file descriptor (system call)int dup( int fd );duplicates fd as the lowest unallocated descriptor

• Commonly used to redirect stdin/stdout • Example: redirect stdin to “foo”

int fd;fd = open(“foo”, O_RDONLY, 0);close(0);dup(fd);close(fd);

Slide Slide 1616

DUP2

For convenience…dup2( int fd1, int fd2 );

use fd2(new) to duplicate fd1 (old) closes fd2 if it was in use • Example: redirect stdin to “foo”

fd = open(“foo”, O_RDONLY, 0);

dup2(fd,0);

close(fd);

Slide Slide 1717

Pipes and Standard I/O

int pid, p[2];if (pipe(p) == -1)

exit(1);pid = fork();if (pid == 0) {

close(p[1]);dup2(p[0],0);close(p[0]);... read from stdin ...

}else {

close(p[0]);dup2(p[1],1);close(p[1]);... write to stdout ...wait(&status);

}

Slide Slide 1818

Pipes and Exec()int pid, p[2];if (pipe(p) == -1)

exit(1);pid = fork();if (pid == 0) {

close(p[1]);dup2(p[0],0);close(p[0]);execl(...);

}else {

close(p[0]);dup2(p[1],1);close(p[1]);... write to stdout ...wait(&status);

}

Slide Slide 1919

‘ls | more’ example

When command shells interprets ‘ls | more’, it:

1. Invokes the pipe( ) system call; let us assume that pipe( ) returns the file descriptors 3 (the pipe's read channel ) and 4 (the write channel ).

2. Invokes the fork( ) system call twice.

3. Invokes the close( ) system call twice to release file descriptors 3 and 4.

Slide Slide 2020

‘ls | more’ example

The first child process, which must execute the ls program, performs the following operations:

1. Invokes dup2(4,1) to copy file descriptor 4 to file descriptor 1. From now on, file descriptor 1 refers to the pipe's write channel.

2. Invokes the close( ) system call twice to release file descriptors 3 and 4.

3. Invokes the execve( ) system call to execute the /bin/ls program. By default, such a program writes its output to the file having file descriptor 1 (the standard output), that is, it writes into the pipe.

Slide Slide 2121

‘ls | more’ example

The second child process must execute the more program; therefore, it performs the following operations:

1. Invokes dup2(3,0) to copy file descriptor 3 to file descriptor 0. From now on, file descriptor 0 refers to the pipe's read channel.

2. Invokes the close( ) system call twice to release file descriptors 3 and 4.

3. Invokes the execve( ) system call to execute /bin/more. By default, that program reads its input from the file having file descriptor (the standard input); that is, it reads from the pipe.

Slide Slide 2222

popen and pclose

#include <stdio.h>FILE *popen(const char *cmdstring, const char *type); The popen( ) function receives two parameters:

the cmdstring pathname of an executable file a type string specifying the direction of the data transfer (r or w).

It returns the pointer to a FILE data structure (fp). popen runs cmdstring with output or input directed to fp based

on value of type parameter. Handle the “dirty work” of creating pipe, forking child, closing

unused ends, executing shell to run program, waiting for command to terminate

int pclose(FILE *fp); The pclose( ) function, which receives the file pointer returned by popen(

) as its parameter, simply invokes the wait4( ) system call and waits for the termination of the process created by popen( ).

Slide Slide 2323

Other techniques for IPC

Socket programming We have seen this before in internet engineering. It is mainly used for IPC between two network processes.

Semaphores Seen before.

Shared memory shmget() for Create/Access Shared Memory shmat() for Accessing Shared Memory shmctl() for controlling shared memory shmdt() for deleting shared memory

Messages msgget( KEY, IPC_CREAT|IPC_EXCL…) for Create/access: msgctl( id, IPC_RMID ) for Control: Send/receive

– msgsnd( id, buf, text_size, flags )– msgrcv( id, buf, max_size, flags )

FIFO mkfifo( ) specifically to create a FIFO. Once created, a FIFO can be accessed through the usual open( ), read( ), write( ), and close( ) system

calls.

Directory structure

Slide Slide 2525

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

holds an i-node number and a file name.

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

Slide Slide 2626

I-node: The administrative information about a file is kept

in a structure known as an inode.– Inodes in a file system, in general, are structured as an

array known as an inode table. An inode number, which is an index to the inode

table, uniquely identifies a file in a file system.

Slide Slide 2727

i-node and Data Blocks

Slide Slide 2828

2. Links

2.1 What is a Link?2.2 Creating a Link2.3 Seeing Links2.4 Removing a Link2.5 Symbolic Links2.6 Implementation

Slide Slide 2929

2.1. What is a Link?

A link is a pointer to a file.

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

(pointer) to it.

Slide Slide 3030

2.2. Creating a Link

ln existing-file new-pointer

Jenny types:ln draft /home/bob/letter

/

home

bob jenny

memo planning

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

Slide Slide 3131

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.

Slide Slide 3232

2.3. Seeing Links

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:$ ls -i file_a file_b file_c file_d

3534 file_a 3534 file_b 5800 file_c 7328 file_d

Slide Slide 3333

2.4. Removing a Link

Deleting a link does not remove the file.

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

Slide Slide 3434

2.5. Symbolic Links

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

same file system

A symbolic link is an indirect pointer to a file it stores the pathname of the pointed-to file it can link across file systems

Slide Slide 3535

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

/

home

dkl jenny

memo planning

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

separatefile system

Slide Slide 3636

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

Slide Slide 3737

?

abcabcabc

update newdelete new

XYXYabc

newbob newbob bobnew

2.6 Link Creation, Update & Removal

continued

abc

cp bob new

abc abc

ln bob new ln -s bob new

bob

abc

new

XY

Slide Slide 3838

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

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

:

:

120207135

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

207 “xyz”

continued

Slide Slide 3939

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

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

Slide Slide 4040Example: 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);

}

Slide Slide 4141symlink()

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

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

Symbolic links are interpreted at run-time.

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

If newpath exists it will not be overwritten.

Slide Slide 4242

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.

Slide Slide 4343

3. Subdirectory Creation

“mkdir uga” causes: the creation of a uga directory file and an i-node

for it an i-node number and name are added to the parent

directory file

:

:

120207135

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

201 “uga”

Slide Slide 4444

4. “.” and “..”

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

Example:

dkl

book memos

continued

Slide Slide 4545

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

Slide Slide 4646

5. 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 mode: return 0 if ok, -1 on error

continued

Slide Slide 4747

“.” and “..” entries are added automatically

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

e.g. 0755

Slide Slide 4848

6. rmdir() #include <unistd.h>

int rmdir(char *pathname);

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

Will delay until other processes have stopped using the directory.

Slide Slide 4949

7. 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

Slide Slide 5050

dirent and DIR

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 FILE) when a directory is first opened, the stream points to the first

entry in the directory

Slide Slide 5151

Example: 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.

Slide Slide 5252

/* 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 */

Slide Slide 5353

8. chdir()

#include <unistd.h>

int chdir( char *pathname );

int fchdir( int fd );

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

Slide Slide 5454

Example: cd to /tmp

Part of to_tmp.c:

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

Slide Slide 5555

Directory Change is Local

The directory change is limited to within the program.

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

Slide Slide 5656

9. getcwd() #include <unistd.h>

char *getcwd(char *buf, int size);

Store the cwd of the calling process in buf;return buf if ok, NULL on error.

buf must be big enough for the pathname string (size specifies the length of buf).

Slide Slide 5757

Example#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 ): :

Slide Slide 5858

10. Walking over Directories

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

Apply a user-defined function to every visited file.

Slide Slide 5959

Function 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

Slide Slide 6060

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

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

Slide Slide 6161

MyFunc Details

The file argument is the pathname relative to the start directory it will be passed to MyFunc() automatically by ftw() as it

visits each file

sbuf argument is a pointer to the stat information for the file being examined.

continued

Slide Slide 6262

The flag argument will be set to one of the following for the item being examined: FTW_F Item is a regular file. FTW_D Item is a directory. FTW_NS Could not get stat info for item. FTW_DNR Directory cannot be read.

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

Slide Slide 6363

Example: shower.c#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <ftw.h>

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

void main(){ ftw(“.”, shower, 1); }

continued

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

Slide Slide 6464

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

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