S -1 Pipes. S -2 Inter-Process Communication (IPC) Chapter 12.1-12.3 Data exchange techniques between processes: –message passing: files, pipes, sockets.

Slides:



Advertisements
Similar presentations
Socket Programming Application Programming Interface.
Advertisements

Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Quick Overview. 2 ISO/OSI Reference Model Application Application Presentation Presentation Session Session Transport Transport Network Network Data Link.
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
1 Socket Interfaces Professor Jinhua Guo CIS527 Fall 2003.
CS 311 – Lecture 18 Outline IPC via Sockets – Server side socket() bind() accept() listen() – Client side connect() Lecture 181CS Operating Systems.
Client Server Model The client machine (or the client process) makes the request for some resource or service, and the server machine (the server process)
Interprocess Communication. Process Concepts Last class.
Socket programming in C. Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
CS1652 September 13th, 2012 The slides are adapted from the publisher’s material All material copyright J.F Kurose and K.W. Ross, All Rights.
Sockets and intro to IO multiplexing. Goals We are going to study sockets programming as means to introduce IO multiplexing problem. We will revisit socket.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
Agenda  Terminal Handling in Unix File Descriptors Opening/Assigning & Closing Sockets Types of Sockets – Internal(Local) vs. Network(Internet) Programming.
Elementary TCP Sockets
Socket Programming. Introduction Sockets are a protocol independent method of creating a connection between processes. Sockets can be either – Connection.
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
9/12/2015B.R1 Socket Abstraction and Interprocess Communication B.Ramamurthy CSE421.
Server Sockets: A server socket listens on a given port Many different clients may be connecting to that port Ideally, you would like a separate file descriptor.
CS162B: IPv4 Socketing Jacob T. Chan. Socketing in the Real World  Most computer games are multiplayer in nature or have multiplayer components  DotA,
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.
The Socket Interface Chapter 22. Introduction This chapter reviews one example of an Application Program Interface (API) which is the interface between.
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.
Page 1 Jan 5, 1998 CMPN 369 CPSC441 Sockets Module Sockets Application Programming Interface (API)
Agenda  Redirection: Purpose Redirection Facts How to redirecting stdin, stdout, stderr in a program  Pipes: Using Pipes Named Pipes.
Socket Programming Lec 2 Rishi Kant. Review of Socket programming Decide which type of socket – stream or datagram. Based on type create socket using.
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets.
TELE202 Lecture 15 Socket programming 1 Lecturer Dr Z. Huang Overview ¥Last Lecture »TCP/UDP (2) »Source: chapter 17 ¥This Lecture »Socket programming.
An Introductory 4.4BSD Interprocess Communication Tutorial Stuart Sechrest.
CE Operating Systems Lecture 13 Linux/Unix interprocess communication.
Introduction to Socket
Socket Programming Lab 1 1CS Computer Networks.
Operating Systems Yasir Kiani. 13-Sep Agenda for Today Review of previous lecture Interprocess communication (IPC) and process synchronization UNIX/Linux.
Sockets Socket = abstraction of the port concept: –Application programs request that the operating system create a socket when one is needed –O.S. returns.
Interprocess Communication Anonymous Pipes Named Pipes (FIFOs) popen() / pclose()
2: Application Layer1 Chapter 2: Application layer r 2.1 Principles of network applications r 2.2 Web and HTTP r 2.3 FTP r 2.4 Electronic Mail  SMTP,
4061 Session 13 (2/27). Today Pipes and FIFOs Today’s Objectives Understand the concept of IPC Understand the purpose of anonymous and named pipes Describe.
Intro to Socket Programming CS 360. Page 2 CS 360, WSU Vancouver Two views: Server vs. Client Servers LISTEN for a connection and respond when one is.
CSCI 330 UNIX and Network Programming
Socket Programming. Computer Science, FSU2 Interprocess Communication Within a single system – Pipes, FIFOs – Message Queues – Semaphores, Shared Memory.
Inter-Process Communication 9.1 Unix Sockets You may regard a socket as being a communication endpoint. –For two processes to communicate, both must create.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
Advanced UNIX programming Fall 2002 Instructor: Ashok Srinivasan Lecture 28 Acknowledgements: The syllabus and power point presentations are modified versions.
Socket programming in C. Socket programming with TCP Client must contact server server process must first be running server must have created socket (door)
Socket Programming in C CS587x Lecture 3 Department of Computer Science Iowa State University.
1 Socket Interface. 2 Client-Server Architecture The client is the one who speaks first Typical client-server situations  Client and server on the same.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
Socket Abstraction and Interprocess Communication
Lecture 5 Systems Programming: Unix Processes: Orphans and Zombies
Sockets and Beginning Network Programming
CS 1652 Jack Lange University of Pittsburgh
Interprocess Communication
Socket Abstraction and Interprocess Communication
TCP Sockets Programming
Programming Assignment # 2 – Supplementary Discussion
Socket Abstraction and Interprocess Communication
Socket Abstraction and Inter-process Communication
IPC Prof. Ikjun Yeom TA – Hoyoun
Chapter 2 Application Layer
Socket Abstraction and Interprocess Communication
Interprocess Communication
Socket Abstraction and Interprocess Communication
Chapter 3 Socket API © Bobby Hoggard, Department of Computer Science, East Carolina University These slides may not be used or duplicated without permission.
Socket Abstraction and Interprocess Communication
Socket Abstraction and Interprocess Communication
Socket Abstraction and Inter-process Communication
Socket Abstraction and Inter-process Communication
Presentation transcript:

S -1 Pipes

S -2 Inter-Process Communication (IPC) Chapter Data exchange techniques between processes: –message passing: files, pipes, sockets –shared-memory model (not the default … not mentioned in Wang, but we’ll still cover in this, a few weeks) Limitations of files for inter-process data exchange: –slow! Limitations of pipes: –two processes must be running on the same machine –two processes communicating must be “related” Sockets overcome these limitations (we’ll cover sockets in the next lecture)

S -3 File Descriptors Revisited Section Used by low-level I/O –open(), close(), read(), write() declared as an integer int fd ; Not the same as a "file stream", FILE *fp streams and file descriptors are related (see following slides)

S -4 Pipes and File Descriptors A fork’d child inherits file descriptors from its parent It’s possible to alter these using fclose() and fopen() : fclose( stdin ); FILE *fp = fopen( “/tmp/junk”, “r” ); One could exchange two entries in the fd table by closing and reopening both streams, but there’s a more efficient way, using dup() or dup2() (…see next slide)

S -5 dup() and dup2() (12.2) newFD = dup( oldFD ); if( newFD < 0 ) { perror(“dup”); exit(1); } or, to force the newFD to have a specific number: returnCode = dup2( oldFD, newFD ); if(returnCode < 0) { perror(“dup2”); exit(1);} In both cases, oldFD and newFD now refer to the same file For dup2(), if newFD is open, it is first automatically closed Note that dup() and dup2() refer to fd’s and not streams –A useful system call to convert a stream to a fd is int fileno( FILE *fp );

S -6 pipe() (12.2) The pipe() system call creates an internal system buffer and two file descriptors: one for reading and one for writing With a pipe, typically want the stdout of one process to be connected to the stdin of another process … this is where dup2() becomes useful (see next slide and figure 12-2 for examples) Usage: int fd[2]; pipe( fd ); /* fd[0] for reading; fd[1] for writing */

S -7 pipe() / dup2() example /* equivalent to “sort < file1 | uniq” */ int fd[2]; FILE *fp = fopen( “file1”, “r” ); dup2( fileno(fp), fileno(stdin) ); fclose( fp ); pipe( fd ); if( fork() == 0 ) { dup2( fd[1], fileno(stdout) ); close( fd[0] ); close( fd[1] ); execl( “/usr/bin/sort”, “sort”, (char *) 0 ); exit( 2 ); } else { dup2( fd[0], fileno(stdin) ); close( fd[0] ); close( fd[1] ); execl( “/usr/bin/uniq”, “uniq”, (char *) 0 ); exit( 3 ); }

S -8 popen() and pclose() (12.1) popen() simplifies the sequence of: –generating a pipe –forking a child process –duplicating file descriptors –passing command execution via an exec() Usage: FILE *popen( const char *command, const char *type ); Example: FILE *pipeFP; pipeFP = popen( “/usr/bin/ls *.c”, “r” );

S -9 Sockets

S -10 What are sockets? (12.5) Sockets are an extension of pipes, with the advantages that the processes don’t need to be related, or even on the same machine A socket is like the end point of a pipe -- in fact, the UNIX kernel implements pipes as a pair of sockets Two (or more) sockets must be connected before they can be used to transfer data Two main categories of socket types … we’ll talk about both: –the UNIX domain: both processes on same machine –the INET domain: processes on different machines Three main types of sockets: SOCK_STREAM, SOCK_DGRAM, and SOCK_RAW … we’ll only talk about SOCK_STREAM

S -11 Connection-Oriented Paradigm Create a socket socket() Assign a name to the socket bind() Establish a queue for connections listen() Extract a connection from the queue accept() SERVER read() write() CLIENT Create a socket socket() Initiate a connection connect() write() read() established

S -12 Example: server.c FILE “ server.c ” … highlights: socket( AF_UNIX, SOCK_STREAM, 0 ); serv_adr.sun_family = AF_UNIX; strcpy( serv_adr.sun_path, NAME ); bind( orig_sock, &serv_adr, size ); listen( orig_sock, 1 ); accept( orig_sock, &clnt_adr, &clnt_len ); read( new_sock, buf, sizeof(buf) ); close( sd ); unlink( the_file );

S -13 Example: client.c FILE “ client.c ” … highlights: socket( AF_UNIX, SOCK_STREAM, 0 ); serv_adr.sun_family = AF_UNIX; strcpy( serv_adr.sun_path, NAME ); connect(orig_sock, &serv_adr, size ); write( new_sock, buf, sizeof(buf) ); close( sd ); Note: server.c and client.c need to be linked with the libsocket.a library (ie: gcc -lsocket )

S -14 The INET domain The main difference is the bind() command … in the UNIX domain, the socket name is a filename, but in the INET domain, the socket name is a machine name and port number: static struct sockaddr_in serv_adr; memset( &serv_adr, 0, sizeof(serv_adr) ); serv_adr.sin_family = AF_INET; serv_adr.sin_addr.s_addr = htonl(INADDR_ANY); serv_adr.sin_port = htons( 6789 ); Need to open socket with AF_INET instead of AF_UNIX Also need to include and

S -15 The INET domain (cont.) The client needs to know the machine name and port of the server struct hostent *host; host = gethostbyname( “eddie.cdf” ); Note: need to link with libnsl.a to resolve gethostbyname() see course website for: –server.c, client.c UNIX domain example –server2.c, client2.c, INET domain example