More Network Programming
|
|
- Susanna Ursula Howard
- 5 years ago
- Views:
Transcription
1 More Network Programming
2 More Network Programming HTTP push server Request framing and server push concepts Demo Useful API s select/poll and advanced sockets tidbits threads HTTP push server code Components Flow charts Code walk-through (code is online) 10/11/06 UIUC - CS/ECE 438, Fall
3 HTTP Request Framing Characteristics ASCII-based (human readable) Framed by text lines First line is command Remaining lines are additional data Blank line ends request frame 10/11/06 UIUC - CS/ECE 438, Fall
4 HTTP Request Framing Characteristics ASCII-based (human readable) Framed by text lines First line is command Remaining lines are additional data Blank line ends request frame GET /surf/too/much.html HTTP/1.0 Date: 28 February :25:53 CST Host: <blank line> 10/11/06 UIUC - CS/ECE 438, Fall
5 HTTP Server Push Idea Connection remains open Server pushes down new data as needed Termination Any time by server Stop loading (or reload) by client Components Header indicating multiple parts New part replaces old part New part sent any time Wrappers for each part 10/11/06 UIUC - CS/ECE 438, Fall
6 HTTP Server Push 10/11/06 UIUC - CS/ECE 438, Fall
7 HTTP Server Push HTTP/ OK Content-type: multipart/x-mixed-replace;\ boundary=---never_in_document never_in_document--- 10/11/06 UIUC - CS/ECE 438, Fall
8 HTTP Server Push HTTP/ OK Content-type: multipart/x-mixed-replace;\ boundary=---never_in_document never_in_document--- the data component 10/11/06 UIUC - CS/ECE 438, Fall
9 HTTP Server Push HTTP/ OK Content-type: multipart/x-mixed-replace;\ boundary=---never_in_document never_in_document--- the data component Content-type: text/html (actual data) ---never_in_document--- 10/11/06 UIUC - CS/ECE 438, Fall
10 HTTP Push Server Demonstration Server running on port 5012 File of interest: test.html Driven by unrelated process (not the server) Changes every 2 seconds Alternates between 13 files (a through m) Demonstration Server detects file changes Pushes update after every change 10/11/06 UIUC - CS/ECE 438, Fall
11 More Network Programming Useful Application Programming Interfaces Topics More advanced sockets Unix file functionality Multithreaded programming (Posix Threads) Specific APIs select/poll and advanced sockets Threads Attributes Creation Thread-specific data Minor synchronization 10/11/06 UIUC - CS/ECE 438, Fall
12 Select and Poll Building timeouts with select/poll Similar functions Notes Parameters Set of file descriptors Set of events for each descriptor Timeout length Return value Set of file descriptors Events for each descriptor Select is somewhat simpler Poll supports more events 10/11/06 UIUC - CS/ECE 438, Fall
13 Select and Poll: Prototypes Select: Wait for readable/writable file descriptors #include <sys/time.h> int select (int num_fds, fd_set* read_set, fd_set* write_set, fd_set* except_set, struct timeval* timeout); Poll: Poll file descriptors for events #include <poll.h> int poll (struct pollfd* pfds, nfds_t nfds, int timeout); 10/11/06 UIUC - CS/ECE 438, Fall
14 Select int select (int num_fds, fd_set* read_set, fd_set* write_set, fd_set* except_set, struct timeval* timeout); Wait for readable/writable file descriptors. Return: Number of descriptors ready -1 on error, sets errno Parameters: num_fds: number of file descriptors to check, numbered from 0 read_set, write_set, except_set: Sets (bit vectors) of file descriptors to check for the specific condition timeout: Time to wait for a descriptor to become ready 10/11/06 UIUC - CS/ECE 438, Fall
15 File Descriptor Sets Bit vectors Often 1024 bits (FD_SETSIZE) Only first num_fds checked Macros to create and check sets fds_set myset; void FD_ZERO (&myset); /* clear all bits */ void FD_SET (n, &myset); /* set bits n to 1 */ void FD_CLEAR (n, &myset); /* clear bit n */ int FD_ISSET (n, &myset); /* is bit n set? */ 10/11/06 UIUC - CS/ECE 438, Fall
16 File Descriptor Sets Three conditions to check for Readable: Data available for reading Writable: Buffer space available for writing Exception: Out-of-band data available (TCP) 10/11/06 UIUC - CS/ECE 438, Fall
17 Timeout Structure struct timeval { long tv_sec; /* seconds */ long tv_usec; /* microseconds */ }; 10/11/06 UIUC - CS/ECE 438, Fall
18 Select High-resolution sleep function All descriptor sets NULL Positive timeout Wait until descriptor(s) become ready At least one descriptor in set timeout NULL Wait until descriptor(s) become ready or timeout occurs At least one descriptor in set Positive timeout Check descriptors immediately (poll) At least one descriptor in set 0 timeout 10/11/06 UIUC - CS/ECE 438, Fall
19 Select: Example fd_set my_read; FD_ZERO(&my_read); FD_SET(0, &my_read); if (select(1, &my_read, NULL, NULL) == 1) { ASSERT(FD_ISSET(0, &my_read); /* data ready on stdin */ 10/11/06 UIUC - CS/ECE 438, Fall
20 Poll #include <poll.h> int poll (struct pollfd* pfds, nfds_t nfds, int timeout); Poll file descriptors for events. Return: Number of descriptors with events -1 on error, sets errno Parameters: pfds: nfds: An array of descriptor structures. File descriptors, desired events and returned events Length of the pfds array timeout: Timeout value in milliseconds 10/11/06 UIUC - CS/ECE 438, Fall
21 Descriptors Structure struct pollfd { int fd; /* file descriptor */ short events; /* queried event bit mask */ short revents; /* returned event mask */ Note: Any structure with fd < 0 is skipped 10/11/06 UIUC - CS/ECE 438, Fall
22 Event Flags POLLIN: data available for reading POLLOUT: Buffer space available for writing POLLERR: Descriptor has error to report POLLHUP: Descriptor hung up (connection closed) POLLVAL: Descriptor invalid 10/11/06 UIUC - CS/ECE 438, Fall
23 Poll High-resolution sleep function 0 nfds Positive timeout Wait until descriptor(s) become ready nfds > 0 timeout INFTIM or -1 Wait until descriptor(s) become ready or timeout occurs nfds > 0 Positive timeout Check descriptors immediately (poll) nfds > 0 0 timeout 10/11/06 UIUC - CS/ECE 438, Fall
24 Poll: Example struct pollfd my_pfds[1]; my_pfds[0].fd = 0; my_pfds[0].events = POLLIN; if (poll(&my_pfds, 1, INFTIM) == 1) { ASSERT (my_pfds[0].revents & POLLIN); /* data ready on stdin */ 10/11/06 UIUC - CS/ECE 438, Fall
25 Advanced Sockets signal (SIGPIPE, SIG_IGN); Call at start of main in server Allows you to ignore broken pipe signals which are degenerated when you write to a socket that has already been closed on the other side Default handler exits (terminates process) 10/11/06 UIUC - CS/ECE 438, Fall
26 Advanced Sockets int yes = 1; setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, (char *) &yes, sizeof (yes)); Call just before bind Allows bind to succeed despite the existence of existing connections in the requested TCP port Connections in limbo (e.g. lost final ACK) will cause bind to fail 10/11/06 UIUC - CS/ECE 438, Fall
27 Posix Threads (pthreads) When coding Include <pthread.h> first in all source files When compiling Use compiler flag D_REENTRANT When linking Link library -lpthread 10/11/06 UIUC - CS/ECE 438, Fall
28 pthreads Attributes Attributes Data structure pthread_attr_t Set of choices for a thread Passed in thread creation routine Choices Inherit scheduling specifics from parent thread? Compete at process or thread level? Scheduling policy Thread priority Detached state Join operation required at termination? Only useful if thread returns a value 10/11/06 UIUC - CS/ECE 438, Fall
29 pthreads Attributes Initialize an attributes structure to the default values int pthread_attr_init (pthread_attr_t* attr); Set the detached state value in an attributes structure int pthread_attr_setdetachedstate (pthread_attr_t* attr, int value); Value PTHREAD_CREATE_DETACHED PTHREAD_CREATE_JOINABLE 10/11/06 UIUC - CS/ECE 438, Fall
30 pthread Error Handling pthreads functions do not follow the usual Unix conventions Similarity: Returns 0 on success Differences: Return error code on failure Does not set errno What about errno? Each thread has its own Define _REENTRANT (-D_REENTRANT switch to compiler) when using pthreads 10/11/06 UIUC - CS/ECE 438, Fall
31 pthread Creation int pthread_create (pthread_t* tid, pthread_attr_t* attr, void*(child_main), void* arg); Spawn a new posix thread Parameters: tid: attr: Unique thread identifier returned from call Attributes structure used to define new thread Use NULL for default values child_main: arg: Main routine for child thread Takes a pointer (void*), returns a pointer (void*) Argument passed to child thread 10/11/06 UIUC - CS/ECE 438, Fall
32 pthread Cleanup Problem A thread can be terminated by an external source (i.e. by other threads) Deallocation an other cleanup must still be performed Solution Push and pop cleanup routines Defer external cancellation until after perfroming cleanup to avoid race conditions 10/11/06 UIUC - CS/ECE 438, Fall
33 pthread Cleanup { pthread_cleanup_push (my_cleanup, heap_data); { } } pthread_setcancel_type(pthread_cancel_deferred, &old); pthread_cleanup_pop(1); 10/11/06 UIUC - CS/ECE 438, Fall
34 pthread Cleanup Pitfalls your code... if (i_need_a_cleanup) { pthread_cleanup_push (my_cleanup, my_arg); } /* some code outside the cleanup logic */ if (i_need_a_cleanup) { pthread_cleanup_pop (1); } 10/11/06 UIUC - CS/ECE 438, Fall
35 pthread Cleanup Pitfalls what your code becomes... if (i_need_a_cleanup) { { /* code to push cleanup */ /* PUSH translation */ } /* THIS CODE IS NOW IN THE CLEANUP LOGIC! */ /* some code outside the cleanup logic */ if (i_need_a_cleanup) { } /* code to exec cleanup */ }/* POP translation */ 10/11/06 UIUC - CS/ECE 438, Fall
36 Unix Files #include <sys/stat.h> int stat(const char* name, struct stat* buf); Get information about a file Returns: 0 on success -1 on error, sets errno Parameters: name: Path to file. Absolute paths begin with /, relative paths do not buf: Statistics structure off_t st_size: Size in bytes time_t st_mtime: Date of last modification. Seconds since January 1, /11/06 UIUC - CS/ECE 438, Fall
37 Unix File Operations Open (and/or create) a file for reading, writing or both int open (const char* name, in flags); Read data from one buffer to file descriptor size_t read (int fd, void* buf, size_t cnt); Write data from file descriptor into buffer size_t write (int fd, void* buf, size_t cnt); Close a file int close(int fd); 10/11/06 UIUC - CS/ECE 438, Fall
38 File: Open #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int open (const char* name, int flags); Open (and/or create) a file for reading, writing or both Returns: New file descriptor on success ( 0) -1 on error, sets errno Parameters: name: Path to file, absolute or relative flags: O_RDONLY: read only, O_WRONLY: write only, O_RDWR: read and write, O_CREAT: create file if it doesn t exist, O_EXCL: prevent creation if it already exists 10/11/06 UIUC - CS/ECE 438, Fall
39 File: Read #include <unistd.h> size_t read (int fd, void* buf, size_t cnt); Read data from one buffer to file descriptor Return: Number of bytes read on success -1 on error, sets errno -1 on signal interrupt, sets errno to EINTR Parameters: fd: file descriptor buf: buffer to read data from cnt: length of buffer Can return < cnt for sockets without reaching end of files Returns 0 on end of file 10/11/06 UIUC - CS/ECE 438, Fall
40 File: Write #include <unistd.h> size_t write (int fd, void* buf, size_t cnt); Write data from file descriptor into buffer Return: Number of bytes written on success -1 on error, sets errno -1 on signal interrupt, sets errno to EINTR Parameters: fd: file descriptor buf: buffer to write data to cnt: length of buffer Can return < cnt for sockets without reaching end of files Returns 0 on end of file 10/11/06 UIUC - CS/ECE 438, Fall
41 File: Close #include <unistd.h> int close(int fd); Close a file Return: 0 on success -1 on error, sets errno Parameters: fd: file descriptor 10/11/06 UIUC - CS/ECE 438, Fall
42 Multithreading-Safe Functions Functions in original libraries Use static storage Not compatible with multiple threads Reentrant versions Dubbed <name>_r Pass state back and forth to routine Example: Tokenizing a string using state local to the current thread char* strtok_r (char* s, const char* delim, char** last_p); 10/11/06 UIUC - CS/ECE 438, Fall
43 Multithreading-Safe Functions char* strtok_r (char* s, const char* delim, char** last_p); Tokenizing a string using state local to the current thread Used to split up words in English text, for example, or commands and arguments given in a command line Parameters: s: The string on the first call, NULL afterwards delim: Characters that separate the tokens last_p: Storage for a pointer to the end of the last token 10/11/06 UIUC - CS/ECE 438, Fall
44 Example Server similar to last MP Push server Client-server connection remains open Server pushes new data Use pthreads Main thread Accepts new client connections Spawns child thread for each client Child threads Parses client requests Constructs response Checks for file modification Pushes file when necessary 10/11/06 UIUC - CS/ECE 438, Fall
45 Example: Server Thread Flow Chart 10/11/06 UIUC - CS/ECE 438, Fall
46 Example: Server Thread Flow Chart Start 10/11/06 UIUC - CS/ECE 438, Fall
47 Example: Server Thread Flow Chart Start Set up server TCP socket 10/11/06 UIUC - CS/ECE 438, Fall
48 Example: Server Thread Flow Chart Start Set up server TCP socket Initialize thread attributes structure 10/11/06 UIUC - CS/ECE 438, Fall
49 Example: Server Thread Flow Chart Start Set up server TCP socket Initialize thread attributes structure Create a thread specific data structure 10/11/06 UIUC - CS/ECE 438, Fall
50 Example: Server Thread Flow Chart Start Set up server TCP socket Initialize thread attributes structure Wait for a connection Create a thread specific data structure 10/11/06 UIUC - CS/ECE 438, Fall
51 Example: Server Thread Flow Chart Start Set up server TCP socket Initialize thread attributes structure Spawn a child thread to handle new connection Wait for a connection Create a thread specific data structure 10/11/06 UIUC - CS/ECE 438, Fall
52 Example: Server Thread Flow Chart Start Set up server TCP socket Initialize thread attributes structure Spawn a child thread to handle new connection Wait for a connection Create a thread specific data structure 10/11/06 UIUC - CS/ECE 438, Fall
53 Example: Client Thread Flow Chart 10/11/06 UIUC - CS/ECE 438, Fall
54 Example: Client Thread Flow Chart Start 10/11/06 UIUC - CS/ECE 438, Fall
55 Example: Client Thread Flow Chart Start Push thread cleanup function 10/11/06 UIUC - CS/ECE 438, Fall
56 Example: Client Thread Flow Chart Start Push thread cleanup function n Wait for 1 second or until client send request Need to send? n Got client request? 10/11/06 UIUC - CS/ECE 438, Fall
57 Example: Client Thread Flow Chart Start Push thread cleanup function n Wait for 1 second or until client send request Need to send? Store file name and reset send time n y Got client request? y Request valid? 10/11/06 UIUC - CS/ECE 438, Fall
58 Example: Client Thread Flow Chart Start Push thread cleanup function n Wait for 1 second or until client send request Send file to client y Need to send? n Got client request? y Success in send? y Store file name and reset send time y Request valid? 10/11/06 UIUC - CS/ECE 438, Fall
59 Example: Client Thread Flow Chart Start Push thread cleanup function n Wait for 1 second or until client send request Send file to client y Need to send? n Got client request? y Success in send? Done y Store file name and reset send time y Pop and execute thread cleanup function Request valid? n 10/11/06 UIUC - CS/ECE 438, Fall
60 Example: Client Thread Flow Chart Start Push thread cleanup function n Wait for 1 second or until client send request Send file to client y Need to send? n Got client request? y Success in send? n Done y Store file name and reset send time y Pop and execute thread cleanup function Request valid? n 10/11/06 UIUC - CS/ECE 438, Fall
61 Makefile CFLAGS = -g -D_REENTRANT Wall OFILES = pushy.o push_thr_code.o read_line.o \\ print_error.o all: pushy pushy: ${OFILES} gcc -g -o $@ ${OFILES} lpthread -lxnet %.o: %.c gcc -c ${CFLAGS} -O9 -o $@ $< clean: rm -f pushy query *.o *~ 10/11/06 UIUC - CS/ECE 438, Fall
62 set_up_server_socket static int set_up_server_socket (u_short port) { int fd; /* server socket file descriptor */ int yes = 1; /* used for setting socket options */ struct sockaddr_in addr; /* server socket address */ /* Create a TCP socket. */ if ((fd = socket (PF_INET, SOCK_STREAM, 0)) == -1) { perror ("set_up_server_socket/socket"); return -1; } /* Allow port reuse with the bind below. */ if (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, (char*)&yes, sizeof (yes)) == -1) { perror ("set_up_server_socket/setsockopt"); return -1; } 10/11/06 UIUC - CS/ECE 438, Fall
63 set_up_server_socket } /* Set up the address. */ bzero (&addr, sizeof (addr)); addr.sin_family = AF_INET; /* Internet address */ addr.sin_addr.s_addr = INADDR_ANY; /* fill in local IP address */ addr.sin_port = htons (port); /* port specified by caller*/ /* Bind the socket to the port. */ if (bind (fd, (struct sockaddr*)&addr, sizeof (addr)) == -1) { perror ("set_up_server_socket/bind"); return -1; } /* Listen for incoming connections (socket into passive state). */ if (listen (fd, BACKLOG) == -1) { perror ("set_up_server_socket/listen"); return -1; } /* The server socket is now ready. */ return fd; 10/11/06 UIUC - CS/ECE 438, Fall
64 wait_for_connections static void wait_for_connections (int fd){ pthread_attr_t attr; /* initial thread attributes */ thread_info_t* info; /* thread-specific connection information */ int len; /* value-result argument to accept */ pthread_t thread_id; /* child thread identifier */ /* Signal a bug for invalid descriptors. */ ASSERT (fd > 0); /* Initialize the POSIX threads attribute structure. */ if (pthread_attr_init (&attr)!= 0) { fputs ("failed to initialize pthread attributes\n", stderr); return; } /* The main thread never joins with the children. */ if (pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED)!= 0) { fputs ("failed to set detached state attribute\n", stderr); return; } 10/11/06 UIUC - CS/ECE 438, Fall
65 wait_for_connections /* Use an infinite loop to wait for connections. For each connection, create a structure with the thread-specific data, then spawn a child thread and pass it the data. The child is responsible for deallocating the memory before it terminates. */ while (1) { /* Create a thread information structure and initialize fields that can be filled in before a client contacts the server. */ if ((info = calloc (1, sizeof (*info))) == NULL) { perror ("wait_for_connections/calloc"); return; } info->fname = NULL; info->last_sent = (time_t)0; 10/11/06 UIUC - CS/ECE 438, Fall
66 wait_for_connections } } /* Wait for a client to contact the server. */ len = sizeof (info->addr); if ((info->fd = accept (fd, (struct sockaddr*)&info->addr, &len)) == -1) { perror ("accept"); return; } /* Create a thread to handle the client. */ if (pthread_create (&thread_id, &attr, (void* (*) (void*))client_thread, info)!= 0) { fputs ("failed to create thread\n", stderr); } /* The child does not exist, the main thread must clean up. */ close (info->fd); free (info); return; 10/11/06 UIUC - CS/ECE 438, Fall
67 client_thread void client_thread (thread_info_t* info) { /* Check argument. */ ASSERT (info!= NULL); } /* Free the thread info block whenever the thread terminates. Note that pushing this cleanup function races with external termination. If external termination wins, the memory is never released. */ pthread_cleanup_push ((void (*)(void*))release_thread_info, info); /* Loop between waiting for a request and sending a new copy of the current file of interest. */ while (read_client_request (info) == 0 && send_file_to_client (info) == 0); /* Defer cancellations to avoid re-entering deallocation routine (release_thread_info) in the middle, then pop (and execute) the deallocation routine.*/ pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, NULL); pthread_cleanup_pop (1); 10/11/06 UIUC - CS/ECE 438, Fall
68 client_has_data static int client_has_data (int fd) { fd_set read_set; struct timeval timeout; /* Check argument. */ ASSERT (fd > 0); /* Set timeout for select. */ timeout.tv_sec = CHECK_PERIOD; timeout.tv_usec = 0; /* Set read mask for select. */ FD_ZERO (&read_set); FD_SET (fd, &read_set); /* Call select. Possible return values are {-1, 0, 1}. */ if (select (fd + 1, &read_set, NULL, NULL, &timeout) < 1) { /* We can't check errno in a thread--assume nothing bad has happened. */ return 0; } /* Select returned 1 file descriptor ready for reading. */ return 1; } 10/11/06 UIUC - CS/ECE 438, Fall
UNIT III - APPLICATION DEVELOPMENT. TCP Echo Server
UNIT III - APPLICATION DEVELOPMENT TCP Echo Server TCP Echo Client Posix Signal handling Server with multiple clients boundary conditions: Server process Crashes, Server host Crashes, Server Crashes and
More informationAny of the descriptors in the set {1, 4} have an exception condition pending
Page 1 of 6 6.3 select Function This function allows the process to instruct the kernel to wait for any one of multiple events to occur and to wake up the process only when one or more of these events
More informationpthreads CS449 Fall 2017
pthreads CS449 Fall 2017 POSIX Portable Operating System Interface Standard interface between OS and program UNIX-derived OSes mostly follow POSIX Linux, macos, Android, etc. Windows requires separate
More informationISA 563: Fundamentals of Systems Programming
ISA 563: Fundamentals of Systems Programming Advanced IO April 9, 2012 Non-blocking IO Data processing can be much faster than data access Waiting for IO to finish can be time consuming, and may not even
More informationPOSIX threads CS 241. February 17, Copyright University of Illinois CS 241 Staff
POSIX threads CS 241 February 17, 2012 Copyright University of Illinois CS 241 Staff 1 Recall: Why threads over processes? Creating a new process can be expensive Time A call into the operating system
More informationECE 650 Systems Programming & Engineering. Spring 2018
ECE 650 Systems Programming & Engineering Spring 2018 Inter-process Communication (IPC) Tyler Bletsch Duke University Slides are adapted from Brian Rogers (Duke) Recall Process vs. Thread A process is
More informationCSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song
CSCE 313 Introduction to Computer Systems Instructor: Dezhen Song UNIX I/O Files and File Representation Basic operations: Reading / Writing Caching: File Open / Close Multiplexing: Select / Poll File
More informationCSE 333 SECTION 9. Threads
CSE 333 SECTION 9 Threads HW4 How s HW4 going? Any Questions? Threads Sequential execution of a program. Contained within a process. Multiple threads can exist within the same process. Every process starts
More informationUNIX System Calls. Sys Calls versus Library Func
UNIX System Calls Entry points to the kernel Provide services to the processes One feature that cannot be changed Definitions are in C For most system calls a function with the same name exists in the
More informationSocket programming in C
Socket programming in C Sven Gestegård Robertz September 2017 Abstract A socket is an endpoint of a communication channel or connection, and can be either local or over the network.
More informationCSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files
CSC209 Review CSC209: Software tools Unix files and directories permissions utilities/commands Shell programming quoting wild cards files ... and systems programming C basic syntax functions arrays structs
More informationCSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files. Compiler vs.
CSC209 Review CSC209: Software tools Unix files and directories permissions utilities/commands Shell programming quoting wild cards files... and systems programming C basic syntax functions arrays structs
More informationΕργαστήριο 9 I/O Multiplexing
Εργαστήριο 9 I/O Multiplexing Στοεργαστήριοθαμελετηθούν: Server High Level View I/O Multiplexing Solutions for Concurrency nonblocking I/O Use alarm and signal handler to interrupt slow system calls. Use
More informationCSE 333 SECTION 3. POSIX I/O Functions
CSE 333 SECTION 3 POSIX I/O Functions Administrivia Questions (?) HW1 Due Tonight HW2 Due Thursday, July 19 th Midterm on Monday, July 23 th 10:50-11:50 in TBD (And regular exercises in between) POSIX
More informationCISC2200 Threads Spring 2015
CISC2200 Threads Spring 2015 Process We learn the concept of process A program in execution A process owns some resources A process executes a program => execution state, PC, We learn that bash creates
More informationOverview. Administrative. * HW 2 Grades. * HW 3 Due. Topics: * What are Threads? * Motivating Example : Async. Read() * POSIX Threads
Overview Administrative * HW 2 Grades * HW 3 Due Topics: * What are Threads? * Motivating Example : Async. Read() * POSIX Threads * Basic Thread Management * User vs. Kernel Threads * Thread Attributes
More informationI/O OPERATIONS. UNIX Programming 2014 Fall by Euiseong Seo
I/O OPERATIONS UNIX Programming 2014 Fall by Euiseong Seo Files Files that contain a stream of bytes are called regular files Regular files can be any of followings ASCII text Data Executable code Shell
More informationNETWORK AND SYSTEM PROGRAMMING. I/O Multiplexing: select and poll function
NETWORK AND SYSTEM PROGRAMMING LAB 15 I/O Multiplexing: select and poll function 15.1 objectives What is a Concurrent server Use of Select System call Use of Poll System call 15.2 What is concurrent server?
More informationCSE 333 SECTION 3. POSIX I/O Functions
CSE 333 SECTION 3 POSIX I/O Functions Administrivia Questions (?) HW1 Due Tonight Exercise 7 due Monday (out later today) POSIX Portable Operating System Interface Family of standards specified by the
More informationCSE 410: Systems Programming
CSE 410: Systems Programming Input and Output Ethan Blanton Department of Computer Science and Engineering University at Buffalo I/O Kernel Services We have seen some text I/O using the C Standard Library.
More informationLecture 3. Introduction to Unix Systems Programming: Unix File I/O System Calls
Lecture 3 Introduction to Unix Systems Programming: Unix File I/O System Calls 1 Unix File I/O 2 Unix System Calls System calls are low level functions the operating system makes available to applications
More informationI/O OPERATIONS. UNIX Programming 2014 Fall by Euiseong Seo
I/O OPERATIONS UNIX Programming 2014 Fall by Euiseong Seo Files Files that contain a stream of bytes are called regular files Regular files can be any of followings ASCII text Data Executable code Shell
More informationNetwork programming(ii) Lenuta Alboaie
Network programming(ii) Lenuta Alboaie adria@info.uaic.ro 1 Content let s remember: iterative TCP client/server UDP client/server model I/O primitives Advanced programming aspects in Internet socket API
More informationCS 3013 Operating Systems WPI, A Term Assigned: Friday, August 31, 2007 Due: Monday, September 17, 2007
CS 3013 Operating Systems WPI, A Term 2007 Craig E. Wills Project 2 (30 pts) Assigned: Friday, August 31, 2007 Due: Monday, September 17, 2007 Introduction This assignment is intended to help you learn
More informationCSci 4061 Introduction to Operating Systems. (Threads-POSIX)
CSci 4061 Introduction to Operating Systems (Threads-POSIX) How do I program them? General Thread Operations Create/Fork Allocate memory for stack, perform bookkeeping Parent thread creates child threads
More informationNETWORK PROGRAMMING. Ipv4 and Ipv6 interoperability
NETWORK PROGRAMMING UNIT V ADVANCED SOCKETS Ipv4 and Ipv6 interoperability threaded servers thread creation and termination TCP echo server using threads Mutexes condition variables raw sockets raw socket
More informationLSN 13 Linux Concurrency Mechanisms
LSN 13 Linux Concurrency Mechanisms ECT362 Operating Systems Department of Engineering Technology LSN 13 Creating Processes fork() system call Returns PID of the child process created The new process is
More informationIntroduction to Network Programming in C/C++
Introduction to Network Programming in C/C++ 10.09.2009 Varun SINGH, Based on the slides by Joerg Ott and Carsten Bormann 1 Starting Point IDE Unix/Linux available in the department
More informationSection 3: File I/O, JSON, Generics. Meghan Cowan
Section 3: File I/O, JSON, Generics Meghan Cowan POSIX Family of standards specified by the IEEE Maintains compatibility across variants of Unix-like OS Defines API and standards for basic I/O: file, terminal
More informationThreads. lightweight processes
Threads lightweight processes 1 Motivation Processes are expensive to create. It takes quite a bit of time to switch between processes Communication between processes must be done through an external kernel
More informationNetwork Programming TDC 561
Network Programming TDC 561 Lecture # 4: Server Design (II) - Concurrent Servers Dr. Ehab S. Al-Shaer School of Computer Science & Telecommunication DePaul University Chicago, IL 1 Unix Signals A signal
More informationProcesses. Processes (cont d)
Processes UNIX process creation image-file arg1 arg2 Shell command line example ls -l Equivalent to /bin/ls -l Why? How do you find out where the image file is? Background processes ls -l & Execute a process
More informationCSC209 Review. Yeah! We made it!
CSC209 Review Yeah! We made it! 1 CSC209: Software tools Unix files and directories permissions utilities/commands Shell programming quoting wild cards files 2 ... and C programming... C basic syntax functions
More informationOperating Systems. Lecture 06. System Calls (Exec, Open, Read, Write) Inter-process Communication in Unix/Linux (PIPE), Use of PIPE on command line
Operating Systems Lecture 06 System Calls (Exec, Open, Read, Write) Inter-process Communication in Unix/Linux (PIPE), Use of PIPE on command line March 04, 2013 exec() Typically the exec system call is
More informationProject 3. Reliable Data Transfer over UDP. NTU CSIE Computer Networks 2011 Spring
Project 3 Reliable Data Transfer over UDP NTU CSIE Computer Networks 2011 Spring Project Goal In Project 3, students are asked to understand and implement reliable data transfer mechanism over UDP. UDP
More informationCSE 124 Discussion Section Sockets Programming 10/10/17
CSE 124 Discussion Section Sockets Programming 10/10/17 Topics What s a socket? Creating a socket Connecting a socket Sending data Receiving data Resolving URLs to IPs Advanced socket options Live code
More informationLecture 4 Threads. (chapter 4)
Bilkent University Department of Computer Engineering CS342 Operating Systems Lecture 4 Threads (chapter 4) Dr. İbrahim Körpeoğlu http://www.cs.bilkent.edu.tr/~korpe 1 References The slides here are adapted/modified
More informationConcurrent Programming. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University
Concurrent Programming Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu Echo Server Revisited int main (int argc, char *argv[]) {... listenfd = socket(af_inet,
More informationMultithreaded Programming
Multithreaded Programming The slides do not contain all the information and cannot be treated as a study material for Operating System. Please refer the text book for exams. September 4, 2014 Topics Overview
More information10. I/O System Library
10. I/O System Library Header File #include // Found in C:\Nburn\include General File Descriptor Functions close --- Close open file descriptors read --- Read data from a file descriptor ReadWithTimeout
More informationCSC209H Lecture 10. Dan Zingaro. March 18, 2015
CSC209H Lecture 10 Dan Zingaro March 18, 2015 Creating a Client To create a client that can connect to a server, call the following, in order: socket: create a communication endpoint This is the same as
More informationChapter 10. Threads. OS Processes: Control and Resources. A process as managed by the operating system groups together:
SFWR ENG 3BB4 Software Design 3 Concurrent System Design 2 SFWR ENG 3BB4 Software Design 3 Concurrent System Design 10.11 13 OS Processes: Control and Resources Chapter 10 Threads A process as managed
More informationPOSIX Shared Memory. Linux/UNIX IPC Programming. Outline. Michael Kerrisk, man7.org c 2017 November 2017
Linux/UNIX IPC Programming POSIX Shared Memory Michael Kerrisk, man7.org c 2017 mtk@man7.org November 2017 Outline 10 POSIX Shared Memory 10-1 10.1 Overview 10-3 10.2 Creating and opening shared memory
More informationCSC 271 Software I: Utilities and Internals
CSC 271 Software I: Utilities and Internals Lecture 13 : An Introduction to File I/O in Linux File Descriptors All system calls for I/O operations refer to open files using a file descriptor (a nonnegative
More informationPOSIX Threads. Paolo Burgio
POSIX Threads Paolo Burgio paolo.burgio@unimore.it The POSIX IEEE standard Specifies an operating system interface similar to most UNIX systems It extends the C language with primitives that allows the
More informationCPSC 341 OS & Networks. Threads. Dr. Yingwu Zhu
CPSC 341 OS & Networks Threads Dr. Yingwu Zhu Processes Recall that a process includes many things An address space (defining all the code and data pages) OS resources (e.g., open files) and accounting
More informationCS 3305 Intro to Threads. Lecture 6
CS 3305 Intro to Threads Lecture 6 Introduction Multiple applications run concurrently! This means that there are multiple processes running on a computer Introduction Applications often need to perform
More informationIntroduction to Network Programming using C/C++
Introduction to Network Programming using C/C++ Slides mostly prepared by Joerg Ott (TKK) and Olaf Bergmann (Uni Bremen TZI) 1 Would be giving brief introduction on... Parsing Command line Socket Related
More informationCS240: Programming in C
CS240: Programming in C Lecture 15: Unix interface: low-level interface Cristina Nita-Rotaru Lecture 15/Fall 2013 1 Streams Recap Higher-level interface, layered on top of the primitive file descriptor
More informationThreads. Threads (continued)
Threads A thread is an alternative model of program execution A process creates a thread through a system call Thread operates within process context Use of threads effectively splits the process state
More informationPreview. What are Pthreads? The Thread ID. The Thread ID. The thread Creation. The thread Creation 10/25/2017
Preview What are Pthreads? What is Pthreads The Thread ID The Thread Creation The thread Termination The pthread_join() function Mutex The pthread_cancel function The pthread_cleanup_push() function The
More informationNaked C Lecture 6. File Operations and System Calls
Naked C Lecture 6 File Operations and System Calls 20 August 2012 Libc and Linking Libc is the standard C library Provides most of the basic functionality that we've been using String functions, fork,
More informationCSCI4430 Data Communication and Computer Networks. Pthread Programming. ZHANG, Mi Jan. 26, 2017
CSCI4430 Data Communication and Computer Networks Pthread Programming ZHANG, Mi Jan. 26, 2017 Outline Introduction What is Multi-thread Programming Why to use Multi-thread Programming Basic Pthread Programming
More informationEPL372 Lab Exercise 2: Threads and pthreads. Εργαστήριο 2. Πέτρος Παναγή
EPL372 Lab Exercise 2: Threads and pthreads Εργαστήριο 2 Πέτρος Παναγή 1 Threads Vs Processes 2 Process A process is created by the operating system, and requires a fair amount of "overhead". Processes
More informationConcurrent Server Design Multiple- vs. Single-Thread
Concurrent Server Design Multiple- vs. Single-Thread Chuan-Ming Liu Computer Science and Information Engineering National Taipei University of Technology Fall 2007, TAIWAN NTUT, TAIWAN 1 Examples Using
More informationProcess Creation in UNIX
Process Creation in UNIX int fork() create a child process identical to parent Child process has a copy of the address space of the parent process On success: Both parent and child continue execution at
More informationIPv4 and ipv6 INTEROPERABILITY
IT2351-NPM/UNIT-4/ 1 IPv4 and ipv6 INTEROPERABILITY Till the time, IPv6 is established all over the world, there is a need for one to host dual stacks that is both IPv4 and IPv6 are running concurrently
More informationConcurrent Programming. Concurrent Programming with Processes and Threads
Concurrent Programming Concurrent Programming with Processes and Threads Review The fork system call creates a process Memory and resources are allocated The exec system call allow a program to execute
More informationECE 598 Advanced Operating Systems Lecture 23
ECE 598 Advanced Operating Systems Lecture 23 Vince Weaver http://web.eece.maine.edu/~vweaver vincent.weaver@maine.edu 21 April 2016 Don t forget HW#9 Midterm next Thursday Announcements 1 Process States
More informationLecture 7. Followup. Review. Communication Interface. Socket Communication. Client-Server Model. Socket Programming January 28, 2005
Followup symbolic link (soft link): pathname, can be across file systems, replacement of file will be active on all symbolic links, consumes at least an inode. hard link: pointers to an inode, only in
More informationThreads. studykorner.org
Threads Thread Subpart of a process Basic unit of CPU utilization Smallest set of programmed instructions, can be managed independently by OS No independent existence (process dependent) Light Weight Process
More informationI/O Models. Kartik Gopalan
I/O Models Kartik Gopalan Types of Concurrency True Concurrency (multiple processes or threads) Multi-processor machines Child processes/threads execute in parallel. Multi-process (forking) servers If
More informationOperating systems. Lecture 7
Operating systems. Lecture 7 Michał Goliński 2018-11-13 Introduction Recall Plan for today History of C/C++ Compiler on the command line Automating builds with make CPU protection rings system calls pointers
More informationCreating Threads. Programming Details. COMP750 Distributed Systems
Creating Threads Programming Details COMP750 Distributed Systems Thread and Process Creation Processes can be created on Unix systems in C or C++ using the fork() function. Threads can be created in C
More informationLecture files in /home/hwang/cs375/lecture05 on csserver.
Lecture 5 Lecture files in /home/hwang/cs375/lecture05 on csserver. cp -r /home/hwang/cs375/lecture05. scp -r user@csserver.evansville.edu:/home/hwang/cs375/lecture05. Project 1 posted, due next Thursday
More informationA Socket Example. Haris Andrianakis & Angelos Stavrou George Mason University
A Socket Example & George Mason University Everything is a file descriptor Most socket system calls operate on file descriptors Server - Quick view socket() bind() listen() accept() send(), recv() close()
More informationCS 326: Operating Systems. Process Execution. Lecture 5
CS 326: Operating Systems Process Execution Lecture 5 Today s Schedule Process Creation Threads Limited Direct Execution Basic Scheduling 2/5/18 CS 326: Operating Systems 2 Today s Schedule Process Creation
More informationSystems Programming. COSC Software Tools. Systems Programming. High-Level vs. Low-Level. High-Level vs. Low-Level.
Systems Programming COSC 2031 - Software Tools Systems Programming (K+R Ch. 7, G+A Ch. 12) The interfaces we use to work with the operating system In this case: Unix Programming at a lower-level Systems
More informationSystem Calls. Library Functions Vs. System Calls. Library Functions Vs. System Calls
System Calls Library Functions Vs. System Calls A library function: Ordinary function that resides in a library external to the calling program. A call to a library function is just like any other function
More informationTHREADS. Jo, Heeseung
THREADS Jo, Heeseung TODAY'S TOPICS Why threads? Threading issues 2 PROCESSES Heavy-weight A process includes many things: - An address space (all the code and data pages) - OS resources (e.g., open files)
More informationMaria Hybinette, UGA. ! One easy way to communicate is to use files. ! File descriptors. 3 Maria Hybinette, UGA. ! Simple example: who sort
Two Communicating Processes Hello Gunnar CSCI 6730/ 4730 Operating Systems Process Chat Maria A Hi Nice to Hear from you Process Chat Gunnar B Dup & Concept that we want to implement 2 On the path to communication
More informationECE 435 Network Engineering Lecture 2
ECE 435 Network Engineering Lecture 2 Vince Weaver http://web.eece.maine.edu/~vweaver vincent.weaver@maine.edu 6 September 2018 Announcements Homework 1 will be posted. Will be on website, will announce
More informationRedesde Computadores(RCOMP)
Redesde Computadores(RCOMP) Theoretical-Practical (TP) Lesson 07 2016/2017 Berkeley sockets API, C and Java. Basic functions/methods for TCP applications. TCP client and server. Asynchronous reception.
More informationProcesses often need to communicate. CSCB09: Software Tools and Systems Programming. Solution: Pipes. Recall: I/O mechanisms in C
2017-03-06 Processes often need to communicate CSCB09: Software Tools and Systems Programming E.g. consider a shell pipeline: ps wc l ps needs to send its output to wc E.g. the different worker processes
More informationCOMP/ELEC 429/556 Introduction to Computer Networks
COMP/ELEC 429/556 Introduction to Computer Networks Creating a Network Application Some slides used with permissions from Edward W. Knightly, T. S. Eugene Ng, Ion Stoica, Hui Zhang 1 How to Programmatically
More informationCONCURRENCY MODEL. UNIX Programming 2014 Fall by Euiseong Seo
CONCURRENCY MODEL UNIX Programming 2014 Fall by Euiseong Seo Echo Server Revisited int main (int argc, char *argv[]) {... listenfd = socket(af_inet, SOCK_STREAM, 0); bzero((char *)&saddr, sizeof(saddr));
More informationThread. Disclaimer: some slides are adopted from the book authors slides with permission 1
Thread Disclaimer: some slides are adopted from the book authors slides with permission 1 IPC Shared memory Recap share a memory region between processes read or write to the shared memory region fast
More informationpthreads Announcement Reminder: SMP1 due today Reminder: Please keep up with the reading assignments (see class webpage)
pthreads 1 Announcement Reminder: SMP1 due today Reminder: Please keep up with the reading assignments (see class webpage) 2 1 Thread Packages Kernel thread packages Implemented and supported at kernel
More informationProcesses and Threads
TDDI04 Concurrent Programming, Operating Systems, and Real-time Operating Systems Processes and Threads [SGG7] Chapters 3 and 4 Copyright Notice: The lecture notes are mainly based on Silberschatz s, Galvin
More informationAssignment 2 Group 5 Simon Gerber Systems Group Dept. Computer Science ETH Zurich - Switzerland
Assignment 2 Group 5 Simon Gerber Systems Group Dept. Computer Science ETH Zurich - Switzerland t Your task Write a simple file server Client has to be implemented in Java Server has to be implemented
More informationCSC209H Lecture 11. Dan Zingaro. March 25, 2015
CSC209H Lecture 11 Dan Zingaro March 25, 2015 Level- and Edge-Triggering (Kerrisk 63.1.1) When is an FD ready? Two answers: Level-triggered: when an operation will not block (e.g. read will not block),
More informationIntroduction to pthreads
CS 220: Introduction to Parallel Computing Introduction to pthreads Lecture 25 Threads In computing, a thread is the smallest schedulable unit of execution Your operating system has a scheduler that decides
More informationTutorial on Socket Programming
Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Hao Wang (Slides are mainly from Seyed Hossein Mortazavi, Monia Ghobadi, and Amin Tootoonchian, ) 1 Outline Client-server
More informationSignal Example 1. Signal Example 2
Signal Example 1 #include #include void ctrl_c_handler(int tmp) { printf("you typed CTL-C, but I don't want to die!\n"); int main(int argc, char* argv[]) { long i; signal(sigint, ctrl_c_handler);
More informationTypes (Protocols) Associated functions Styles We will look at using sockets in C Java sockets are conceptually quite similar
Socket Programming What is a socket? Using sockets Types (Protocols) Associated functions Styles We will look at using sockets in C Java sockets are conceptually quite similar - Advanced Data Communications:
More informationComputer Network Programming
Practical Programming Computer Network Programming Marwan Burelle & David Bouchet david.bouchet.epita@gmail.com 1 Quick Overview 1.IP and Protocol Stack 2.TCP Concepts 3.Client / Server Concepts 4.Socket
More informationNetwork Programming in C: The Berkeley Sockets API. Networked Systems 3 Laboratory Sessions
Network Programming in C: The Berkeley Sockets API Networked Systems 3 Laboratory Sessions The Berkeley Sockets API Widely used low-level C networking API First introduced in 4.3BSD Unix Now available
More informationThread Concept. Thread. No. 3. Multiple single-threaded Process. One single-threaded Process. Process vs. Thread. One multi-threaded Process
EECS 3221 Operating System Fundamentals What is thread? Thread Concept No. 3 Thread Difference between a process and a thread Prof. Hui Jiang Dept of Electrical Engineering and Computer Science, York University
More informationFile Descriptors and Piping
File Descriptors and Piping CSC209: Software Tools and Systems Programming Furkan Alaca & Paul Vrbik University of Toronto Mississauga https://mcs.utm.utoronto.ca/~209/ Week 8 Today s topics File Descriptors
More informationAsynchronous Events on Linux
Asynchronous Events on Linux Frederic.Rossi@Ericsson.CA Open System Lab Systems Research June 25, 2002 Ericsson Research Canada Introduction Linux performs well as a general purpose OS but doesn t satisfy
More informationCS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University
CS 333 Introduction to Operating Systems Class 3 Threads & Concurrency Jonathan Walpole Computer Science Portland State University 1 Process creation in UNIX All processes have a unique process id getpid(),
More informationPorts under 1024 are often considered special, and usually require special OS privileges to use.
1 2 Turns out that besides an IP address (used by the IP layer), there is another address that is used by TCP (stream sockets) and, coincidentally, by UDP (datagram sockets). It is the port number. It's
More informationMultiprocessors 2007/2008
Multiprocessors 2007/2008 Abstractions of parallel machines Johan Lukkien 1 Overview Problem context Abstraction Operating system support Language / middleware support 2 Parallel processing Scope: several
More informationRedes de Computadores (RCOMP)
Redes de Computadores (RCOMP) Theoretical-Practical (TP) Lesson 07 2017/2018 Berkeley sockets API, C and Java. Basic functions/methods for TCP applications. TCP client and server. Asynchronous reception.
More informationECE 435 Network Engineering Lecture 2
ECE 435 Network Engineering Lecture 2 Vince Weaver http://web.eece.maine.edu/~vweaver vincent.weaver@maine.edu 31 August 2017 Announcements Homework 1 will be posted. Will be on website, will announce
More informationNETWORK PROGRAMMING. Instructor: Junaid Tariq, Lecturer, Department of Computer Science
NETWORK PROGRAMMING CSC- 341 25 Instructor: Junaid Tariq, Lecturer, Department of Computer Science 26 9 Lecture Sockets as means for inter-process communication (IPC) application layer Client Process Socket
More informationSOCKET PROGRAMMING. What is a socket? Using sockets Types (Protocols) Associated functions Styles
LABORATORY SOCKET PROGRAMMING What is a socket? Using sockets Types (Protocols) Associated functions Styles 2 WHAT IS A SOCKET? An interface between application and network The application creates a socket
More informationMemory management. Johan Montelius KTH
Memory management Johan Montelius KTH 2017 1 / 22 C program # include int global = 42; int main ( int argc, char * argv []) { if( argc < 2) return -1; int n = atoi ( argv [1]); int on_stack
More informationCS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University
CS 333 Introduction to Operating Systems Class 3 Threads & Concurrency Jonathan Walpole Computer Science Portland State University 1 The Process Concept 2 The Process Concept Process a program in execution
More informationCSCI 4210 Operating Systems CSCI 6140 Computer Operating Systems Sample Exam 2 Questions (document version 1.0)
CSCI 4210 Operating Systems CSCI 6140 Computer Operating Systems Sample Exam 2 Questions (document version 1.0) Overview Exam 2 will be in class on Thursday, April 13, 2017 from 10:00-11:45AM (please arrive
More information