DOCUMENT ID: 1284-02 SYNOPSIS: Description of Application Threads OS RELEASE: 2.4, SunOS 5.4 PRODUCT: Solaris x86 KEYWORDS: application threads lwp library structure DESCRIPTION: Application Threads and how they are handled by the kernel. SOLUTION: ====================================================================== Application Threads Thread -a sequence of instructions within a program. ) Threads ) | | ( ( \ / \ / \ / \ / | | LWP Threads * Each thread has its own stack and some threads have local storage. * The threads share most of the process address space with other threads. * The threads can execute independently (concurrently). * Threads are invisible outside of the process. Application threads are handled by the thread library and described by the "thread_t" structure. Threads should be viewed as an execution resource that may be applied to a task. Much of the state of the process is shared by all of the threads (that is open files, current directory, and signal handlers). Since application threads share most of the process data, care must be taken to synchronize access to shared data. The threads are visible to the operating system, so there is no system protection between application threads. Application threads do not break the UNIX programming model. Application threads do not require kernel resources. This means they are light-weight and can be created quickly. Synchronization between threads does not require a trap into the kernel. LWP (Light Weight Process) * Application threads can be multiplexed onto the LWPs * The LWP can be considered a virtual CPU. * Each LWP is associated with a kernel thread. * A system thread such as the "page daemon" has a kernel thread but no LWP. It's described by the klwp_t structure. A demo program that uses threads --------------------------------- #define _REENTRANT /* basic 3-lines for threads */ #include#include void *hello(void *); int a,b; main() { thread_t tid; thr_create(NULL,NULL,hello,NULL,NULL/*THR_DETACHED*/, &tid); /* Parent for loop */ for(a=0;a<1000;a++) printf("Parent %i\n",a); thr_exit(0); printf("this is 2a test of threads\n"); } void *hello(void *arg) { /*child for loop */ for(b=0;b<1000;b++) printf("Thread %i\n",b); } ---------------------------------- to compile do "cc file -lpthread" pthread is the thread Library need to use threads. Thread are an alternative for Forking a new process. They act like seperate unix process, but share the same PID at the application that started and share it's cpu cycles, and memory space. If the parent and child for loops changed the same variable the results might be unpredictable. DATE APPROVED: 04/11/95