Project 2 Hints. pthread_create SYNOPSIS #include int pthread_create(pthread_t *tid, const pthread_attr_t *attr, void *(*start_routine)(void *), void.

Slides:



Advertisements
Similar presentations
Sockets: Network IPC Internet Socket UNIX Domain Socket.
Advertisements

Today’s topic: Basic TCP API –Socket –Bind –Listen –Connect –Accept –Read –Write –Close.
Socket Programming Application Programming Interface.
Critical Sections and Semaphores A critical section is code that contains access to shared resources that can accessed by multiple processes. Critical.
Networks: TCP/IP Socket Calls1 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.
Client-Server Communications Servers provide services to clients over a network Most LANs have file servers to manage common disk space This makes it easier.
Socket Programming: a Primer Socket to me!. Feb. 23, 2001EE122, UCB2 Why does one need sockets? application network protocol sockets network.
1 Socket Interfaces Professor Jinhua Guo CIS527 Fall 2003.
Tutorial 8 Socket Programming
Programming with Berkeley Sockets Presented by Chris GauthierDickey Written by Daniel Stutzbach (I think!) for CIS 432/532 Useful References: ● man pages.
Threads Threads are lightweight processes
CS 311 – Lecture 18 Outline IPC via Sockets – Server side socket() bind() accept() listen() – Client side connect() Lecture 181CS Operating Systems.
Introduction to Socket Programming April What is a socket? An interface between application and network –The application creates a socket –The socket.
Socket Addresses. Domains Internet domains –familiar with these Unix domains –for processes communicating on the same hosts –not sure of widespread use.
Introduction to Project 1 Web Client and Server Jan 2006.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
CPSC 441 TUTORIAL – JANUARY 18, 2012 TA: MARYAM ELAHI INTRODUCTION TO SOCKET PROGRAMMING WITH C.
UNIX Socket Programming CS 6378
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
1 Networking (Stack and Sockets API). 2 Topic Overview Introduction –Protocol Models –Linux Kernel Support TCP/IP Sockets –Usage –Attributes –Example.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
Operating Systems Chapter 9 Distributed Communication.
Assignment 3 A Client/Server Application: Chatroom.
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.
Sockets CIS 370 Lab 10 UMass Dartmouth. Introduction 4 Sockets provide a simple programming interface which is consistent for processes on the same machine.
Sirak Kaewjamnong Computer Network Systems
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.
The Application Layer Application Services (Telnet, FTP, , WWW) Reliable Stream Transport (TCP) Connectionless Packet Delivery Service (IP) Unreliable.
 Wind River Systems, Inc Chapter - 13 Network Programming.
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.
University of Calgary – CPSC 441.  A socket is an interface between the application and the network (the lower levels of the protocol stack)  The application.
Introduction to Socket
Socket Programming Lab 1 1CS Computer Networks.
1 Sockets Programming Socket to me!. 2 Network Application Programming Interface (API) The services provided by the operating system that provide the.
Sockets Socket = abstraction of the port concept: –Application programs request that the operating system create a socket when one is needed –O.S. returns.
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,
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
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.
Introduction to Sockets
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
Read() recv() connection establishment Server (connection-oriented protocol) blocks until connection from client Client socket() bind() listen() accept()
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
回到第一頁 Client/sever model n Client asks (request) – server provides (response) n Typically: single server - multiple clients n The server does not need.
In unistd.h : int gethostname(char * name, int maxlen) Purpose : Find the computer's name.
Socket Programming. Computer Science, FSU2 Interprocess Communication Within a single system – Pipes, FIFOs – Message Queues – Semaphores, Shared Memory.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
Lecture 15 Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
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.
Socket Programming(1/2). Outline  1. Introduction to Network Programming  2. Network Architecture – Client/Server Model  3. TCP Socket Programming.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
Case Study: Pthread Synchronization Dr. Yingwu Zhu.
Assignment 3 A Client/Server Application: Chatroom
Week 13 - Friday CS222.
Socket Programming in C
Transport layer API: Socket Programming
Socket Programming in C
Socket Programming.
Socket Programming(1/2)
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Internet Networking recitation #8
in unistd.h: int gethostname(char * name, int maxlen)
Sockets.
Today’s topic: Basic TCP API
Presentation transcript:

Project 2 Hints

pthread_create SYNOPSIS #include int pthread_create(pthread_t *tid, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg); POSIX:THR tid points to thread ID attr points to attributes of thread (NULL implies default attributes) start routine points to function thread calls when it begins execution start routine returns a pointer to void which is treated as exit status by pthread_join

pthread_exit/pthread_join SYNOPSIS #include void pthread_exit(void *value_ptr); int pthread_join(pthread_t thread, void **value_ptr); POSIX.THR pthread_exit terminates the calling thread The value_ptr parameter is available to a successful pthread_join However, the pthread_exit value_ptr parameter points to data that exists after the thread exits, so it cannot be allocated as an automatic local variable

PTHREAD Example #include #include #include #include #include #include void main(void) { pthread_t copy_tid; int myarg[2]; int error; void *copy_file(void *arg); if ((myarg[0] = open("my.in", O_RDONLY)) == -1) perror("Could not open my.in"); else if ((myarg[1] = open("my.out", O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) == -1) perror("Could not open my.out"); else if (error=pthread_create(&copy_tid, NULL, copy_file, (void *)myarg)) fprintf(stderr,"Thread creation was not successful: %s\n", strerror(error)); }

Solaris 2.3 Service Times OperationMicroseconds Unbound thread create Bound thread create fork() Unbound thread synchronize Bound thread synchronize Between process synchronize

POSIX.SEM Semaphore Variables Semaphore variable is of type sem_t Atomic operations for initializing, incrementing and decrementing value Unnamed semaphores – Can be used by a single process or by children of a process that created it Named semaphores – Can be used by all processes Unnamed semaphores are similar in operation to pipes, and named semaphores are similar to named pipes

POSIX.SEM Semaphore Declaration #include sem_t sem; sem is a semaphore variable POSIX.SEM does not specify underlying type of sem_t One possibility is for it to act like a file descriptor that points to a local table and the table entries point to entries in a system file table

Semaphore Operations SYNOPSIS #include int sem_init (sem_t *sem, int pshared, unsigned int value); int sem_destroy (sem_t *sem); int sem_wait (sem_t *sem); int sem_try (sem_t *sem); int sem_post (sem_t *sem); int sem_getvalue (sem_t *sem, int *sval); POSIX.SEM All semaphore functions return –1 and set errno on error It is uncertain what semaphore functions return on success, but usually 0 _POSIX_SEMAPHORES may be defined but system may NOT support POSIX.SEM semaphores POSIX.SEM semaphores are counting semaphores

sem_init Initializes semaphore to value parameter If the value of pshared is non-zero, the semaphore can be used between processes (the process that initializes it and by children of that process) If the value of pshared is zero, the semaphore can only be used by threads of a single process Think of sem as referring to a semaphore rather than being the semaphore itself

sem_destroy Destroys a previously initialized semaphore If sem_destroy attempts to destroy a semaphore that is being used by another process, it may return –1 and set errno to EBUSY – Unfortunately, the specifications do not require that the system detect this

sem_wait and sem_trywait sem_wait is a standard semaphore wait operation If the semaphore value is 0, sem_wait blocks until it can successfully decrement value or when interrupted such as by SIGINT sem_trywait is similar to sem_wait except instead of blocking on 0, it returns –1 and sets errno to EAGAIN

sem_post sem_post increments the semaphore value and is the classical semaphore signal operation sem_post must be async_signal_safe and may be invoked from a signal handler

Unnamed Semaphore Example #include … void main(); { … if (sem_init(&my_lock, 1, 1) { perror(“could not initialize my_lock semaphore); … for (i = 1; i < n; ++i) if (childpid = fork()) break; … if (sem_wait (&my_lock) == – 1) { perror (“semaphore invalid); exit (1); } Critical Section if (sem_post (&my_lock) == – 1) { perror (“semaphore done”); exit(1); } … }

Connection-Oriented Protocol Server waits for a connection request from a client Once the connection is established, communication takes place using handles (file descriptors) The server address is not included in the user message Connection-oriented protocol has setup overhead

Single Port Strategy Simplest client-server communication takes place over a single communication port If client and server are on the same machine, the single port can be a FIFO On a network, port can be socket connection When server starts up, it opens FIFO (or Socket) and waits for client requests When client needs service, it opens FIFO (or Socket) and writes its request Server then performs the service Server Client Requests

UICI Implementation u_opensocket bind listen u_listenaccept u_connectsocket connect u_readread u_writewrite

Server Actions Socket Bind Listen Accept

socket #include int socket(int domain, int type, int protocol); domain – selects protocol family to be used AF_UNIX – Used on a single UNIX system AF_INET – Used on internet and between remote hosts type – SOCK_STREAM – Reliable 2-way connection oriented typically implemented with TCP SOCK_DGRAM – Connectionless communication using unreliable messages of fixed length, typically implemented with UDP Protocol – Specifies protocol to be used – There is usually only one type so it is usually 0

bind SYNOPSIS #include int bind (int s, const struct sockaddr *address, size_t address_len); s – File descriptor returned by the socket call address_len – Number of bytes returned in the *address structure *address – Contains family name and protocol-specific info struct sockaddr_in { short sin_family; u_short sin_port; struct in_addr sin_addr; char sin_zero[8] };

bind (continued) Bind associates a socket endpoint or handle with a specific network connection Internet domain protocols specify the physical connection by a port number UNIX domain protocols specify the connection by a pathname

struct sockaddr Contains family name and protocol specific information

struct sockaddr_in The internet domain uses struct sockaddr_in for struct sockaddr sin_family – AF_INET sin_port – is the port number using the network byte ordering sin_addr – INADDR_ANY allows communication from any host sin_zero – an array that fills out the structure so that it has the same size as struct sockaddr

listen SYNOPSIS #include int listen (int s, int backlog); s – File descriptor returned by socket backlog – number of pending client requests allowed

sin_port field Represents the port using network byte ordering Machines that use a different byte ordering must do a conversion The macro host to network short (htons) can be used to convert port numbers htons should be used even when not necessary to maintain portability

u_open socket Implementation int u_open(u_port_t port) { int sock; struct sockaddr_in server; if (( u_ignore_sigpipe() != 0) || ((sock = socket(AF_INET, SOCK_STREAM,0)) < 0) return –1; server.sin_family = AF_INET; server.sin_addr.s_addr = INADDR_ANY; server.sin_port = htons((short)port); if((bind(sock,(struct sockaddr *)&server,sizeof(server)) <0) || (listen(sock, MAXBACKLOG) < 0) return –1; return sock; }

accept SYNOPSIS #include int accept(int s, struct sockaddr *address, int *address_len); Parameters are similar to bind, except that accept fills in *address with info about the client making the connection sin_addr holds Internet address of client *address_len – contains number of bytes of the buffer actually filled in by the accept call accept returns file descriptor for communicating with client In parent-server model, server forks a child to handle the request and resumes monitoring the file descriptor Convert sin_addr to a name by calling gethostbyaddr

gethostbyaddr SYNOPSIS #include struct hostent *gethostbyaddr(const void *addr, size_t len, int type); struct hostent includes a field h_name that is a pointer to the official host name On error, gethostbyaddr returns NULL and sets the external integer h_error

u_listen socket Implementation int u_listen(int fd, char *hostn) { struct sockaddr_in net_client; int len = sizeof(struct sockaddr); int retval; struct hostent *hostptr; while ( ((retval = accept(fd, (struct sockaddr *)(&net_client), &len)) == -1) && (errno == EINTR) ); if (retval == -1) return retval; hostptr = gethostbyaddr((char *)&(net_client.sin_addr.s_addr), 4, AF_INET); if (hostptr == NULL) strcpy(hostn, "unknown"); else strcpy(hostn, (*hostptr).h_name); return retval; }

Client Actions Socket Connect

connect SYNOPSIS #include int connect (int s, struct sockaddr *address, size_t addres_len); Establishes a link between file descriptors and a well-known port on the remote server The sockaddr_in structure is filled in as it is with bind

gethostbyname SYNOPSIS #include struct hostent *gethostbyname(const char *name); The host name string is converted to an appropriate Internet address by calling gethostbyname struct hostent includes two members of interest: –h_addr_list is an array of pointers to network addresses used by this host – use the first one h_addr_list[0] –h_length is filled with the number of bytes in the address On error gethostbyname returns NULL and sets the external integer h_error – macros exist to determine error

u_connect socket Implementation (top) int u_connect(u_port_t port, char *hostn) { struct sockaddr_in server; struct hostent *hp; int sock; int retval; if ( (!(hp = gethostbyname(hostn)) || ((sock = socket(AF_INET, SOCK_STREAM, 0)) h_addr_list[0], hp->h_length); server.sin_port = htons((short)port); server.sin_family = AF_INET;

u_connect Implementation (bottom) while ( ((retval = connect(sock, (struct sockaddr *)&server, sizeof(server))) == -1) && (errno == EINTR) ); if (retval == -1) { close(sock); return -1; } return sock; } Client creates a socket and makes the connection request Client can be interrupted by a signal and loop reinitiates the call Program does not use strncpy for server.sin_addr since the source may have an embedded zero byte Once client and server establish a connection, they exchange information using read and write