18
CS345 Operating Systems Φροντιστήριο Άσκησης 2

CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets

Embed Size (px)

Citation preview

Page 1: CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets

CS345Operating Systems

Φροντιστήριο Άσκησης 2

Page 2: CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets

Inter-process communication

• Exchange data among processes

• Methods– Signal– Pipe – Sockets

Page 3: CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange 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– Sockets

Page 4: CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets

Pipes

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

Page 5: CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets

Named pipes

• Special file that is used to transfer data between unrelated processes

• Client writes to it and echo server reads from it

Page 6: CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets

The mkfifo()system call

• Creates a named pipe– with name pathname– mode specifies the permissions

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

int mkfifo(const char *pathname, mode_t mode);

Page 7: CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets

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_t read(int fd, void *buf, size_t count);ssize_t write(int fd, const void *buf, size_t count);

Page 8: CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets

Sockets

• endpoint of communication link between two programs running on the network

• inter-process communication flow across a computer network

Page 9: CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets

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.

Page 10: CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets

Socket Functions (1/5)

• 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

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

int socket(int domain, int type, int protocol);

Page 11: CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets

Socket Functions (2/5)

• 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(struct sockaddr_in) )

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

int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);

struct sockaddr_in {sa_family_t sin_family ; //= AF_INET in_port_t sin_port ; //into network byte order struct in_addr sin_addr ;};

struct in_addr { u_int32_t s_addr;};

Page 12: CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets

Socket Functions (3/5)

• Listen for connections on a socket– sockfd: file descriptor that refers to a socket– backlog: number of allowed connections

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

int listen(int sockfd, int backlog);

Page 13: CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets

Socket Functions (4/5)

• Accept a connection on a socket– Arguments same as bind function– addr: client’s information

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

int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);

Page 14: CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets

Socket Functions (5/5)

• Initiate a connection on a socket– Called by the client

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

int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);

Page 15: CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets

send/recv Functions

• The message is found in buf and has length len

• flag: 0, by default:

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

ssize_t send(int sockfd, const void *buf, size_t len, int flags);ssize_t recv(int sockfd, void *buf, size_t len, int flags);

Page 16: CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets

Useful functions

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)

#include <arpa/inet.h> Uint16_t htons(uint16_t hostshort);

#include <arpa/inet.h> int inet_pton(int af, const char *src, void *dst);

#include <netdb.h>struct hostent *gethostbyname(const char *name);

Page 17: CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets

Server example

struct sockaddr_in server_addr, client_addr;

sock = socket(AF_INET, SOCK_STREAM, 0);

server_addr = …bind(sock, (struct sockaddr *)&server_addr,

sizeof(struct sockaddr));

listen(sock, 5);

while(1){connected = accept(sock, (struct sockaddr *)&client_addr,&(sizeof(struct sockaddr_in)));//recv and send operations

}close(sock);

Page 18: CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets

Client example

struct sockaddr_in server_addr;

sock = socket(AF_INET, SOCK_STREAM, 0);

server_addr = …

connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr));

while(1){//send and recv operations

}

close(sock);