1 / 13

Professor Jinhua Guo CIS427 Winter 2003

Socket Programming - Advanced Techniques Introduction to PThreads. Professor Jinhua Guo CIS427 Winter 2003. Blocking. If you don't want a socket to be blocking, you have to make a call to fcntl(): #include <unistd.h> #include <fcntl.h> . .

megara
Télécharger la présentation

Professor Jinhua Guo CIS427 Winter 2003

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. Socket Programming - Advanced Techniques • Introduction to PThreads Professor Jinhua Guo CIS427 Winter 2003

  2. Blocking If you don't want a socket to be blocking, you have to make a call to fcntl(): #include <unistd.h> #include <fcntl.h> . . sockfd = socket(AF_INET, SOCK_STREAM, 0); fcntl(sockfd, F_SETFL, O_NONBLOCK); . . If you try to read from a non-blocking socket there's no data there, --it will return -1 and errno will be set to EWOULDBLOCK.

  3. select()--Synchronous I/O Multiplexing select() gives you the power to monitor several sockets at the same time. It'll tell you which ones are ready for reading, which are ready for writing, and which sockets have raised exceptions, if you really want to know that. #include <sys/time.h> #include <sys/types.h> #include <unistd.h> int select(int numfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);

  4. Macros operate on the fd_set • FD_ZERO(fd_set *set) -- clears a file descriptor set • FD_SET(int fd, fd_set *set) -- adds fd to the set • FD_CLR(int fd, fd_set *set) -- removes fd from the set • FD_ISSET(int fd, fd_set *set) -- tests to see if fd is in the set

  5. What are Pthreads? • Historically, hardware vendors have implemented their own proprietary versions of threads. Not portable. • Pthread is a standardized thread programming interface specified by the IEEE POSIX (portable operating systems interface) in 1995. • Pthreads are defined as a set of C language programming types and procedure calls, implemented with a pthread.h header/include file and a thread library.

  6. Why Pthread ? • To realize potential program performance gains. • When compared to the cost of creating and managing a process, a thread can be created with much less operating system overhead. Managing threads requires fewer system resources than managing processes. • All threads within a process share the same address space. Inter-thread communication is more efficient and in many cases, easier to use than inter-process communication.

  7. Why Pthread ? • Threaded applications offer potential performance gains and practical advantages over non-threaded applications in several other ways: • Overlapping CPU work with I/O • Priority/real-time scheduling: tasks which are more important can be scheduled to supersede or interrupt lower priority tasks. • Asynchronous event handling: tasks which service events of indeterminate frequency and duration can be interleaved. For example, a web server can both transfer data from previous requests and manage the arrival of new requests.

  8. Include files and libraries • .h file #include <pthread.h> #include <semaphore.h> //for semaphore only • Compile and Linking $gcc  foo.c  -o foo  -lpthread –lrt (for semaphore)

  9. The Pthreads API • Thread management: The first class of functions work directly on threads - creating, terminating, joining, etc. • Semaphores: provide for create, destroy, wait, and post on semaphores. • Mutexes: provide for creating, destroying, locking and unlocking mutexes. • Condition variables: include functions to create, destroy, wait and signal based upon specified variable values.

  10. Thread Creation pthread_create (tid, attr, start_routine, arg) • It returns the new thread ID via the tid argument. • The attr parameter is used to set thread attributes, NULL for the default values. • The start_routine is the C routine that the thread will execute once it is created. • A single argument may be passed to start_routine via arg. It must be passed by reference as a pointer cast of type void.

  11. Thread Termination and Join pthread_exit (value) ; • This Function is used by a thread  to terminate.  The return value is passed as a pointer. pthread_join (tid, value_ptr); • The pthread_join() subroutine blocks the calling thread until the specified threadid thread terminates. • Return 0 on success, and negative on failure.  The returned value is a pointer returned by reference.  If you do not care about the return value, you can pass NULL for the second argument.

  12. Example Code - Pthread Creation and Termination #include <pthread.h> #include <stdio.h> void *PrintHello(void * id) { printf(“Thread%d: Hello World!\n", id); pthread_exit(NULL); } int main (int argc, char *argv[]) { pthread_t thread0, thread1; pthread_create(&thread0, NULL, PrintHello, (void *) 0); pthread_create(&thread1, NULL, PrintHello, (void *) 1); pthread_exit(NULL); }

  13. References • POSIX thread programming http://www.llnl.gov/computing/tutorials/workshops/workshop/index.html • "Multithreaded, Parallel, and Distributed Programming" by Gregory R. Andrews. • Introduction to PThreads http://phoenix.liunet.edu/%7Emdevi/pthread/Main.htm • Getting Started With POSIX Threads http://dis.cs.umass.edu/%7Ewagner/threads_html/tutorial.html

More Related