Unformatted text preview:

ThreadsMethods to Monitor Multiple File DescriptorsWithout ThreadsWith ThreadsPOSIX 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 AnalysisUser-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 executionPOSIX 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 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#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <string.h>#include <pthread.h>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));}PTHREAD ExamplePTHREAD Example Analysis• copy_tid holds the ID of the created thread• copy_file is the name of the function the thread executes• myarg is a pointer to the parameter value to be passed to the thread function (contains file descriptors for my.in and my.out)User-Level Thread Advantages• Run on top of the existing operating system• Compete among themselves for the resources allocated to a process• Threads are scheduled by a run-time thread system that is part of the process code• Each library/system call is enclosed by a “jacket” –jacket code calls the runtime system• read and sleep could be a problem because they cause the process to block – the potentially blocking code is replaced in the jacket by non-blocking code• If the code does not block, do the call right away – if the code does block, add it


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?