140 likes | 347 Views
POSIX Threads. Kevin Sawicki Rachel Strong. Overview. IEEE Standard Thread Basics Thread Safety Thread Characteristics POSIX comparison to Java. IEEE Standard 1003. Internationally known as ISO/IEC 9945 Developed jointly by IEEE and The Open Group
E N D
POSIX Threads Kevin Sawicki Rachel Strong
Overview • IEEE Standard • Thread Basics • Thread Safety • Thread Characteristics • POSIX comparison to Java
IEEE Standard 1003 • Internationally known as ISO/IEC 9945 • Developed jointly by IEEE and The Open Group • POSIX replaced original name of IEEE-IX • Stands for Portable Operating System Interface • Name created by Richard Stallman • Incredibly long documents • Base Definitions over 500 pages • System Interfaces over 1750 pages
POSIX Breadth • Defines a lot more than just threads: • Error Numbers • I/O Streams • Realtime • Sockets • Tracing • Outlines C Header files • Examples and code snippets are in C • Outlines concept of command interpreter or “shell”
Thread Basics • IEEE Thread Definition: “A single flow of control within a process. Each thread has its own thread ID, scheduling priority and policy, errno value, thread-specific key/value bindings, and the required system resources to support a flow of control. Anything whose address may be determined by a thread, including but not limited to static variables, storage obtained via malloc( ), directly addressable storage obtained through implementation-defined functions, and automatic variables, are accessible to all threads in the same process.”
Thread Contents • Each POSIX thread must maintain the following: • Thread ID (pthread_t) • Unique and reusable within the same process • Thread attributes (pthread_attr_t) • Scheduling information • Stack information • Cancellability State • Start routine • Arguments to start routine
Thread Safety • Definition: • “A function that may be safely invoked concurrently by multiple threads.” • Standard outlines all functions that must be thread-safe • Defined as functions that lock on static storage or shared objects
Mutually Exclusive locks • Defined data structure (pthread_mutex_t) • Configurable to multiple modes: • Normal Relocking causes deadlock • Error Check Error codes on incorrect calls • Recursive Semaphore type behavior • Default All incorrect calls cause undefined behavior • Obtained two ways: • Via successful return from locking functions • Via return from wait functions • Released two ways: • Via success return from unlocking functions • Via blocking call to wait functions
Conditional Variables • Defined data structure (pthread_cond_t) • Used to signal between threads on specific conditions • Wait and notification calls utilize conditionals • Must be used in conjunction with a mutex • Created through function call • Destroyed via function call
pthread_create pthread_cond_wait pthread_signal pthread_broadcast pthread_exit pthread_join pthread_wait mutex and semaphore new and start no conditional waits, another object could notify when the condition was met notify notifyAll leave run method join wait synchronize POSIX vs. Java
Real Time Java • RealtimeThread • Larger priority range • Control over scheduling algorithms • NoHeapRealtimeThread • Can preempt the garbage collector • More predictable and better control
Conclusions • Java threads encourage a better program structure than POSIX threads. • Although with the addition of Real Time thread libraries to Java provides more control and it can approach C++ in some thread performance tests, for programs that need precise control and has hard deadlines Java Real Time threads are not the best choice.
More Information • IEEE Standards available through: • http://ieeexplore.ieee.org • Freely accessible from RIT network • Select Standards and enter “1003” • The Open Group: • http://www.opengroup.org/onlinepubs/000095399/toc.htm • Man pages