1 / 18

CS345 Operating Systems

CS345 Operating Systems. Φροντιστήριο Άσκησης 2. Inter-process communication. E xchange data among processes Methods Signal Pipe Sockets. Echo server. Sits and waits on client connections Echoing messages Version 1: same machine Named pipes Version 2: different machines S ockets.

miriam
Download Presentation

CS345 Operating Systems

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS345Operating Systems Φροντιστήριο Άσκησης 2

  2. Inter-process communication • Exchange data among processes • Methods • Signal • Pipe • Sockets

  3. Echo server • Sits and waits on client connections • Echoing messages • Version 1: same machine • Named pipes • Version 2: different machines • Sockets

  4. Pipes • Chain of processes arranged so that the output of each process is the input of the next

  5. Named pipes • Special file that is used to transfer data between unrelated processes • Client writes to it and echo server reads from it

  6. The mkfifo()system call #include <sys/types.h> #include <sys/stat.h> intmkfifo(const char *pathname, mode_tmode); • Creates a named pipe • with name pathname • mode specifies the permissions

  7. How do I use a named pipe? • Open it like a normal file • Use read() and write() • Close it like a normal file • The unlink() system call deletes a name and the file it refers to #include <unistd.h> ssize_tread(intfd, void *buf, size_t count); ssize_twrite(intfd, const void *buf, size_tcount);

  8. Sockets • endpoint of communication link between two programs running on the network • inter-process communication flow across a computer network

  9. Socket Types Stream sockets, also known as connection-oriented sockets, provides sequenced, reliable, two-way, connection-based byte streams. Datagram sockets, also known as connectionless sockets.

  10. Socket Functions (1/5) #include <sys/types.h> #include <sys/socket.h> intsocket(intdomain, inttype, intprotocol); • create an endpoint for communication • The domain argument specifies a communication domain (“AF_INET”, “AF_UNIX”, etc) • type specifies the communication semantics (“SOCK_STREAM”, “SOCK_DGRAM”, etc) • protocol set to 0

  11. Socket Functions (2/5) #include <sys/types.h> #include <sys/socket.h> int bind(intsockfd, conststructsockaddr *addr, socklen_taddrlen); structsockaddr_in { sa_family_tsin_family ; //= AF_INET in_port_tsin_port; //into network byte order structin_addrsin_addr;}; structin_addr { u_int32_t s_addr; }; assigns the address specified to by addr to the socket referred to by the file descriptor sockfd addrlen specifies the size, in bytes, of the address structure pointed to by addr( sizeof(structsockaddr_in) )

  12. Socket Functions (3/5) #include <sys/types.h> #include <sys/socket.h> intlisten(intsockfd, intbacklog); • Listen for connections on a socket • sockfd: file descriptor that refers to a socket • backlog:number of allowed connections

  13. Socket Functions (4/5) #include <sys/types.h> #include <sys/socket.h> intaccept(intsockfd, structsockaddr *addr, socklen_t *addrlen); • Accept a connection on a socket • Arguments same as bind function • addr: client’s information

  14. Socket Functions (5/5) #include <sys/types.h> #include <sys/socket.h> intconnect(intsockfd, conststructsockaddr *addr, socklen_taddrlen); • Initiate a connection on a socket • Called by the client

  15. send/recv Functions #include <sys/types.h> #include <sys/socket.h> ssize_tsend(intsockfd, const void *buf, size_tlen, intflags); ssize_trecv(intsockfd, void *buf, size_tlen, intflags); The message is found in buf and has length len flag: 0, by default:

  16. Useful functions #include <arpa/inet.h> Uint16_t htons(uint16_t hostshort); #include <arpa/inet.h> intinet_pton(intaf, const char *src, void *dst); #include <netdb.h> structhostent *gethostbyname(const char *name); Convert multi-byte integer types from host byte order to network byte order. Convert IPv4 and IPv6 addresses from text to binary form e.g.: inet_pton(AF_INET, server_ip, &addr.sin_addr) Return a structure with information for the host name (can be an IP address)

  17. Server example structsockaddr_inserver_addr, client_addr; sock = socket(AF_INET, SOCK_STREAM, 0); server_addr = … bind(sock, (structsockaddr *)&server_addr, sizeof(structsockaddr)); listen(sock, 5); while(1){ connected = accept(sock, (structsockaddr *)&client_addr,&(sizeof(structsockaddr_in))); //recv and send operations } close(sock);

  18. Client example structsockaddr_inserver_addr; sock = socket(AF_INET, SOCK_STREAM, 0); server_addr = … connect(sock, (structsockaddr *)&server_addr, sizeof(structsockaddr)); while(1){ //send and recv operations } close(sock);

More Related