Unformatted text preview:

ThreadsMethods to Monitor Multiple File DescriptorsWithout ThreadsWith Threadsprocessfd.cprocessid.c Analysismonitorfd.c - topmonitorfd.cPOSIX vs Solaris 2 (1)POSIX vs Solaris 2 (2) – Old BookThread Management Functions – New BookPOSIX.THR ThreadsSun Solaris 2 ThreadsThread Managementpthread_createpthread_exit/pthread_joinPTHREAD ExamplePTHREAD Example Analysiscopy_file – Topcopy_file – Middlecopy_file – BottomMulti-FD Copier – Upper TopMulti-FD Copier – Lower TopMulti-FD Copier – Upper BottomMulti-FD Copier – Lower BottomBad Copier – Upper TopBad Copier – Lower TopBad Copier – Upper BottomBad Copier – Lower BottomUser-Level Thread AdvantagesUser-Level Threads - DisadvantagesUser-Level ThreadsKernel-Level ThreadsKernel-Level Thread DiagramHybrid ThreadsHybrid Thread DiagramPOSIX:THR ThreadsSolaris 2 TerminologySolaris 2.3 Service TimesPOSIX:THR Thread AttributesPOSIX.1c Attribute ObjectsAttribute Object – ExampleThreads• Threads are lightweight processes• In a context switch, they change the contents of the CPU registers but do not change memory• Threads can simplify the programming of problems such as monitoring inputs from multiple file descriptors• They also provide a capability to overlap I/O with processing• Typical thread packages contain a runtime system to manage threads in a transparent way• A thread package contains calls for thread creation and destruction, mutual exclusion, and condition variables•POSIX.THR and Sun Solaris 2 standard libraries have such callsMethods to Monitor Multiple File Descriptors• Have a separate process monitor each file descriptor• Use the select command• Use the poll command• Use POSIX asynchronous I/O• Create a thread to monitor each file descriptorWithout Threadsprocess_fd();Calling Processprocess_fd(void) {}Called FunctionThread of executionWith Threadspthread_create();Creating Processprocess_fd(void) {}Created ThreadThread CreationThread of executionprocessfd.c#include <stdio.h>#include "restart.h"#define BUFSIZE 1024void docommand(char *cmd, int cmdsize);void *processfd(void *arg) { /* process commands read from file descriptor */char buf[BUFSIZE];int fd;ssize_t nbytes;fd = *((int *)(arg));for ( ; ; ) { if ((nbytes = r_read(fd, buf, BUFSIZE)) <= 0)break; docommand(buf, nbytes); }return NULL; }processid.c Analysis• Processid.c monitors only one file descriptor for an input• The input is a command for executionmonitorfd.c - top#include <pthread.h>#include <stdio.h>#include <stdlib.h>#include <string.h>void *processfd(void *arg);void monitorfd(int fd[], int numfds) { /* create threads to monitor fds */int error, i;pthread_t *tid;if ((tid = (pthread_t *)calloc(numfds, sizeof(pthread_t))) == NULL) {perror("Failed to allocate space for thread IDs");return; }monitorfd.cfor (i = 0; i < numfds; i++) /* create a thread for each file descriptor */if (error = pthread_create(tid + i, NULL, processfd, (fd + i))) {fprintf(stderr, "Failed to create thread %d: %s\n",i, strerror(error));tid[i] = pthread_self();}for (i = 0; i < numfds; i++) {if (pthread_equal(pthread_self(), tid[i]))continue;if (error = pthread_join(tid[i], NULL))fprintf(stderr, "Failed to join thread %d: %s\n", i, strerror(error));}free(tid);return;}POSIX vs Solaris 2 (1)• Most thread functions return 0 if successful and nonzero error code if unsuccessful• pthread_create (thr_create) – Creates a thread• pthread_exit (thr_exit) – Causes the calling thread to terminate without causing the entire process to exit• pthread_kill (thr_kil) – sends a signal to a specified thread• pthread_join (thr_join) – causes the calling thread to wait for the specified thread to exit• pthread_self (thr_self) – returns the caller’s identityPOSIX vs Solaris 2 (2) – Old Bookcond_initcond_destroycond_waitcond_timewaitcond_signalcond_broadcastpthread_cond_initpthread _cond_destroypthread _cond_waitpthread _cond_timewaitpthread _cond_signalpthread_cond_broadcastCondition variablesmutex_initmutex_destroymutex_lockmutex_trylockmutex_unlockpthread_mutex_initpthread_mutex_destroypthread_mutex_lockpthread_mutex_trylockpthread_mutex_unlockMutual exclusionthr_createthr_exitthr_killthr_jointhr_selfpthread_createpthread_exitpthread_killpthread_joinpthread_selfThread ManagementSolaris 2POSIXDescriptionThread Management Functions –New Bookterminate another threadpthread_cancelfind out own thread IDpthread_selfwait for a threadpthread_joinsend a signal to a threadpthread_killtest two thread ID’s for equalitypthread_equalset thread to release resourcespthread_detachcreate a threadpthread_createdescriptionPOSIX functionPOSIX.THR ThreadsPOSIX:THR uses attribute objects to represent thread properties• Properties such as stack size or scheduling policy are set for a thread attribute object• Several threads can be associated with the same attribute object• If a property of an object changes, the change is reflected in all threads associated with the object•POSIX:THR threads offer a more robust method of thread cancellation and termination (than Solaris)Sun Solaris 2 Threads• Solaris threads explicitly set properties of threads and other primitives• Therefore, some calls have long lists of parameters for setting properties• Solaris offers more control over how threads are mapped to processor resources (than POSIX)Thread Management• A thread has an ID, stack, execution priority, and starting address for execution (and perhaps scheduling and usage information)• POSIX threads are referenced by an ID of type pthread_t• A thread determines its ID by calling pthread_self• Threads for a process share the entire address space for that process• Threads are dynamic if they can be created at any time during execution•POSIX:THR creates threads dynamically with pthread_create (creates thread and places it in the ready queue)pthread_createSYNOPSIS#include <pthread.h>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_joinpthread_exit/pthread_joinSYNOPSIS#include <pthread.h>void pthread_exit(void *value_ptr);int pthread_join(pthread_t thread, void **value_ptr);POSIX.THR• pthread_exit terminates the calling


View Full Document

Chico CSCI 372 - Threads

Download Threads
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Threads and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Threads 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?