1 UNIX Systems Programming Interprocess communication.

Slides:



Advertisements
Similar presentations
Operating Systems Lecture 7.
Advertisements

Lab 9 CIS 370 Umass Dartmouth.  A pipe is typically used as a one-way communications channel which couples one related process to another.  UNIX deals.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
January 13, Csci 2111: Data and File Structures Week1, Lecture 2 Basic File Processing Operations.
Today’s topic: –File operations –I/O redirection –Inter-process communication through pipes.
Operating System Inter-Process Communication. IPC F How does one process communicate with another process? –semaphores -- signal notifies waiting process.
Exec function Exec function: - replaces the current process (its code, data, stack & heap segments) with a new program - the new program starts executing.
Files. System Calls for File System Accessing files –Open, read, write, lseek, close Creating files –Create, mknod.
6/24/2015B.RamamurthyPage 1 File System B. Ramamurthy.
CS Lecture 16 Outline Inter-process Communication (IPC) – Pipes – Signals Lecture 161CS Operating Systems 1.
7/15/2015B.RamamurthyPage 1 File System B. Ramamurthy.
CS 311 – Lecture 12 Outline File management system calls Stat() Directory Information  Opendir()  Readdir()  Closedir() Truncate() and remove() Lecture.
Today’s topic: –File operations –I/O redirection –Inter-process communication through pipes.
Today’s topic Inter-process communication with pipes.
POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O.
Shell (Part 1). Process r A process is an instance of an application running r If there are two instances of an application running then there are two.
1 THE UNIX FILE SYSTEM By Chokechai Chuensukanant ID COSC 513 Operating System.
The file structure and related utilities CS240 Computer Science II.
Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.
1 Homework Introduction to HW7 –Complexity similar to HW6 –Don’t wait until last minute to start on it File Access will be needed in HW8.
Slide 1 Interprocess communication. Slide 2 1. Pipes  A form of interprocess communication Between processes that have a common ancestor  Typical use:
SIMULATED UNIX FILE SYSTEM Implementation in C Tarek Youssef Bipanjit Sihra.
1 UNIX System Programming v Objectives –look at how to program with directories –briefly describe the UNIX file system Directories and File System.
Cli/Serv.: procs/51 Client/Server Distributed Systems v Objectives –look at how to program UNIX processes , Semester 1, Processes.
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
Shell (Part 2). Example r What if we want to support something like this: m ps –le | sort r One process should execute ps –le and another should execute.
Recitation 11: 11/18/02 Outline Robust I/O Chapter 11 Practice Problems Annie Luo Office Hours: Thursday 6:00 – 7:00 Wean.
System Commands and Interprocess Communication. chroot int chroot(const char *path); chroot changes the root directory to that specified in path. This.
Pipes A pipe is a simple, synchronized way of passing information between processes A pipe is a special file/buffer that stores a limited amount of data.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 8: Opening Files and Starting Processes.
Adv. UNIX: dirs/181 Advanced UNIX v Objectives –look at how to program with directories –briefly describe the UNIX file system Special.
Directory structure. Slide 2 Directory Structure  A directory ‘file’ is a sequence of lines; each line holds an i-node number and a file name.  The.
CS162B: Pipes Jacob T. Chan. Pipes  These allow output of one process to be the input of another process  One of the oldest and most basic forms of.
UNIX Files File organization and a few primitives.
Agenda  Redirection: Purpose Redirection Facts How to redirecting stdin, stdout, stderr in a program  Pipes: Using Pipes Named Pipes.
Shell (Addendum). Example r What if we want to support something like this: m ps –le | sort r One process should execute ps –le and another should execute.
Files and Directories File types stat functions for file information
Navigating Directories
Chapter 4 The File Structure. Contents u The Hierarchical File Structure u Directory and Ordinary Files u Directories u Access Permissions u Links.
Interprocess Communication Anonymous Pipes Named Pipes (FIFOs) popen() / pclose()
Today’s topic Access and manipulate meta data for files –File type, ownership, access permissions, access time, etc How to determine if a file is not there?
CSCI 330 UNIX and Network Programming Unit XII: Process & Pipe Part 1.
Adv. UNIX: FileStr/11 Advanced UNIX v Objectives –to supplement the “Introduction to UNIX” slides with extra information on files Special.
File Systems cs550 Operating Systems David Monismith.
Interprocess Communication
CSCI 330 UNIX and Network Programming
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
CS 241 Section Week #8 (10/29/09). Outline MP5 Overview Files & I/O UNIX File Systems inodes Directories Links.
S -1 Processes. S -2 wait and waitpid (11.2) Recall from a previous slide: pid_t wait( int *status ) wait() can: (a) block; (b) return with status; (c)
Dup, dup2 An existing file descriptor ( filedes ) is duplicated The new file descriptor returned by dup is guaranteed to be the lowest numered available.
CSC 271 – Software I: Utilities and Internals An Introduction to File I/O in Linux Credited to Dr. Robert Siegfried and Beginning Linux Programming by.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
Process Related System Calls By Neha Hulkoti & Kavya Bhat.
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Pipes and Fifos.
Files in UNIX u UNIX deals with two different classes of files:  Special Files  Regular Files u Regular files are just ordinary data files on disk -
File System Design David E. Culler CS162 – Operating Systems and Systems Programming Lecture 23 October 22, 2014 Reading: A&D a HW 4 out Proj 2 out.
Lecture 5 Systems Programming: Unix Processes: Orphans and Zombies
CS 3733 Operating Systems Topics: IPC and Unix Special Files
Directory Directory is a file containing list of entries,where each entry represents inode number and name of the file stored in that directory.
File redirection ls > out
Pipes A pipe provides a one-way flow of data example: who | sort| lpr
תרגול 8 – ק/פ ותקשורת תהליכים ב-Linux
File System B. Ramamurthy B.Ramamurthy 11/27/2018.
CSE 333 – Section 3 POSIX I/O Functions.
Unix programming Term: Unit-V I PPT Slides
Programming Assignment # 2 – Supplementary Discussion
CSE 333 – Section 3 POSIX I/O Functions.
IPC Prof. Ikjun Yeom TA – Hoyoun
Advanced UNIX progamming
CSE 333 – Section 3 POSIX I/O Functions.
Presentation transcript:

1 UNIX Systems Programming Interprocess communication

2 Overview 1. Pipes 2. Co-processes 3. FIFOs 4. Message queues 5. Semaphores & Shared memory continued

3 1. Pipes v A form of interprocess communication v Between processes that have a common ancestor v Typical use: –Pipe created by a process –Process calls fork() –Pipe used between parent and child

4 Differences between versions v All systems support half-duplex –Data flows in only one direction v Many newer systems support full duplex –Data flows in two directions v For portability, assume only half-duplex

5 Differences between versions v All systems support half-duplex –Data flows in only one direction v Many newer systems support full duplex –Data flows in two directions v For portability, assume only half-duplex

6 Creating a pipe #include #include int pipe(int filedes[2]); int pipe(int filedes[2]); v Returns 0 if ok, -1 on error v 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]

7 After the pipe() call 0 stdin stdin 1 stdout stdout 2 stderr stderr Filedes 0 1

8 The process then calls fork() 0stdin 1stdout 2stderr stdin 1stdout 2stderr Parent Child

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

10 Parent writing to child 0stdin 1stdout 2stderr 3 X 4 5 0stdin 1stdout 2stderr 3 4 X 5 Parent Child

11 Child writing to parent 0stdin 1stdout 2stderr 3 4 X 5 0stdin 1stdout 2stderr 3 X 4 5 Parent Child

12 After one end of the pipe is closed … v Reading from a empty pipe whose write end has been closed returns 0 (indicating EOF) v 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

13 Example … #include #include int main(void){ int n; // to keep track of num bytes read int n; // to keep track of num bytes read int fd[2]; // to hold fds of both ends of pipe int fd[2]; // to hold fds of both ends of pipe pid_t pid; // pid of child process pid_t pid; // pid of child process char line[80]; // buffer to hold text read/written char line[80]; // buffer to hold text read/written …

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

15 continued } else { // child process } else { // child process close(fd[1]); // close write end close(fd[1]); // close write end n = read(fd[0], line, 80); // read from pipe n = read(fd[0], line, 80); // read from pipe write(1, line, n); // echo to screen write(1, line, n); // echo to screen } exit(0); exit(0);}

16 dup() and dup2 dup() and dup2 #include #include int dup(int filedes); int dup2(int filedes, int filedes2); v Both will duplicate an existing file descriptor v dup() returns lowest available file descriptor, now referring to whatever filedes refers to v dup2() - filedes2 (if open) will be closed and then set to refer to whatever filedes refers to

17 Redirection v See command_pipe.C command_pipe.C

18 popen and pclose #include #include FILE *popen(const char *cmdstring, const char *type); Returns file pointer if OK, NULL on error int pclose(FILE *fp); Returns: termination status of cmdstring, or -1 on error v Handle the “dirty work” of creating pipe, forking child, closing unused ends, executing shell to run program, waiting for command to terminate v popen runs cmdstring with output directed to fp returned by call

19 example

20 FIFOs

21 Message Queues

22 Semaphores and Shared Memory

23 Directory Structure v 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 to view it  The data is stored as binary, so we cannot simply use cat to view it

24 v I-node: –The administrative information about a file is kept in a structure known as an inode. u 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.

25 i-node and Data Blocks

26 2. Links 2.1What is a Link? 2.2Creating a Link 2.3Seeing Links 2.4Removing a Link 2.5Symbolic Links 2.6 Implementation

What is a Link? v A link is a pointer to a file. v Useful for sharing files: –a file can be shared by giving each person their own link (pointer) to it.

Creating a Link ln existing-file new-pointer v Jenny types: ln draft /home/bob/letter / home bobjenny memoplanning /home/bob/draft and /home/jenny/letter

29  Changes to a file affects every link:  Changes to a file affects every link: $ cat file_a This is file A. $ ln file_a file_b $ cat file_b This is file A. $ vi file_b : $ cat file_b This is file B after the change. $ cat file_a This is file B after the change.

Seeing Links v 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 v 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

Removing a Link v Deleting a link does not remove the file. v Only when the file and every link is gone will the file be removed.

Symbolic Links v 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 v 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

33 v Jenny types: ln -s shared /home/dkl/project / home dkljenny memoplanning /home/jenny/shared and /home/dkl/project separate file system

34 v 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

35 ? abc update newdelete new XY abc newbobnewbob new 2.6 Link Creation, Update & Removal continued abc cp bob new abc ln bob new ln -s bob new bob abc new XY

and unlink() 2.7 link() and unlink() #include int link( const char *oldpath, const char *newpath );  Meaning of:  Meaning of: link( “abc”, “xyz” ) : : “fred.html” “abc” “bookmark.c” 207 “xyz” continued

37  clears the directory record  unlink() clears the directory record –usually means that the i-node number is set to 0 v 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

38 Example: unlink #include 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); }

39symlink() #include int symlink(const char *oldpath, const char *newpath); v Creates a symbolic link named newpath which contains the string oldpath. v Symbolic links are interpreted at run-time. v Dangling link – may point to an non-existing file. v If newpath exists it will not be overwritten.

40 readlink() #include int readlink( const char *path, char *buf, size_t bufsiz ); v v 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. v v Return value – –The count of characters placed in the buffer if it succeeds. u u -1 if an error occurs.

41 3. Subdirectory Creation  causes:  “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 : : “fred.html” “abc” “bookmark.c” 201 “uga”

42 4. “. ” and “.. ”  “. ” and “.. ” are stored as ordinary file names with i-node numbers pointing to the correct directory files. v Example: dkl bookmemos continued

43 In more detail: “.” “..” “book” 401“memos” Directory ben “.” “..” “chap1” 567“chap2” Directory book “chap3” “.” “..” “kh” “kd” Directory memos “mw”590

44 5. mkdir()   #include #include #include int mkdir(char *pathname, mode_t mode);  Creates a new directory with the specified : return 0 if ok, -1 on error  Creates a new directory with the specified mode : return 0 if ok, -1 on error continued

45  “. ” and “.. ” entries are added automatically  mode must include execute permissions so the user(s) can use.  mode must include execute permissions so the user(s) can use cd. e.g.0755

46 6. rmdir()   #include int rmdir(char *pathname); v Delete an empty directory; return 0 if ok, -1 on error. v Will delay until other processes have stopped using the directory.

47 7. Reading Directories v v #include #include DIR *opendir(char *pathname); struct dirent *readdir(DIR *dp); int closedir(DIR *dp); returns a pointer if ok, NULL on error returns a pointer if ok, NULL at end or on error

48 dirent and DIR v v 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 */ }  is a directory stream (similar to )  DIR is a directory stream (similar to FILE ) –when a directory is first opened, the stream points to the first entry in the directory

49 Example: listdir.c #include #include 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.

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

51 8. chdir() #include int chdir( char *pathname ); int fchdir( int fd ); v Change the current working directory (cwd) of the calling process; return 0 if ok, -1 on error.

52 Example: cd to /tmp  Part of c :  Part of to_tmp.c : : if( chdir(“/tmp” ) < 0 printf( “chdir error\n”) ; else printf( “In /tmp\n” );

53 Directory Change is Local v The directory change is limited to within the program. v e.g. $ pwd /usr/lib $ to_tmp /* from last slide */ In /tmp $ pwd /usr/lib

54 9. getcwd()   #include 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 ).

55 Example #include #include #include /* 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 ): :

Walking over Directories v 'Visit' every file in a specified directory and all of its subdirectories –visit means get the name of the file v Apply a user-defined function to every visited file.

57 Function Prototypes v v #include /* 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

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

59 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

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

61 Example: shower.c #include #include #include #include int shower(const char *file, const struct stat *sbuf, int flag); void main() { ftw(“.”, shower, 1); } continued Print the names of all the files found below the current directory.

62 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; }