Processes contain information about program resources and program execution state, including:
Process ID, process group ID, user ID, and group ID
Environment
Working directory.
Program instructions
Registers
Stack
Heap
File descriptors
Signal actions
Shared libraries
Inter-process communication tools (such as message queues, pipes, semaphores, or shared memory).
UNIX Process
Threads
Threads use and exist within these process resources, yet are able to be scheduled by the operating system and run as independent entities largely because they duplicate only the bare essential resources that enable them to exist as executable code.
Cont.
This independent flow of control is accomplished because a thread maintains its own:
Stack pointer
Registers
Scheduling properties (such as policy or priority)
Set of pending and blocked signals
Thread specific data (thread ID)
UNIX Thread
UNIX Threads
In the UNIX environment a thread:
Exists within a process and uses the process resources
Has its own independent flow of control as long as its parent process exists and the OS supports it
Duplicates only the essential resources it needs to be independently schedulable
May share the process resources with other threads that act equally independently (and dependently)
Dies if the parent process dies - or something similar
Is "lightweight" because most of the overhead has already been accomplished through the creation of its process.
Pthreads
A thread library that provides the programmer an API for creating and managing threads
Pthreads refers to the POSIX standards defining an API for thread creation and synchronization
Pthreads
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
Creating threads
Initially, your main() program comprises a single, default thread. All other threads must be explicitly created by the programmer.
pthread_create() creates a new thread and makes it executable. This routine can be called any number of times from anywhere within your code.
Creating threads (cont.)
pthread_create() arguments:
thread: An opaque, unique identifier for the new thread returned by the subroutine.
attr: An opaque attribute object that may be used to set thread attributes. You can specify a thread attributes object, or NULL for the default values.
start_routine: the C routine that the thread will execute once it is created.
arg: A single argument that may be passed to start_routine . It must be passed by reference as a pointer cast of type void. NULL may be used if no argument is to be passed.
Cont.
Return value:
If successful, the pthread_create() routine shall return zero
other- wise, an error number shall be returned to indicate the error.
Thread attributes
Thread attributes include (define members of the struct pthread_attr_t):
detached state
scheduling policy
scheduling parameter
inheritsched attribute
Scope
guard size
stack address
stack size
Scheduling threads
After a thread has been created, how do you know when it will be scheduled to run by the operating system?
It is up to the implementation and/or operating system to decide where and when threads will execute.
Terminating threads
pthread_exit() is used to explicitly exit a thread.
If main() finishes before the threads it has created, and exits with pthread_exit() , the other threads will continue to execute. Otherwise, they will be automatically terminated when main() finishes.
Joining Threads
int pthread_join (pthread_t thread, void **value_ptr);
Description:
The pthread_join() function shall suspend execution of the calling thread until the target thread terminates.
Return value:
If successful, the pthread_join() function shall return zero; otherwise, an error number shall be returned to indicate the error.
main() { pthread_t thread1, thread2; char *message1 = "Thread 1"; char *message2 = "Thread 2"; int iret1, iret2; /* Create independent threads each of which will execute function */ iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1); iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2); /* Wait till threads are complete before main continues. Unless we */ /* wait we run the risk of executing an exit which will terminate */ /* the process and all threads before the threads have completed. */ pthread_join( thread1, NULL); pthread_join( thread2, NULL);
Defines an array of threads (note: threads are not yet created!) of opaque type pthread_t
Threads will be created using pthread_create() . The routine print_message_function() is the starting routine for the two newly created routines. Notice that an argument was sent to print_message_function() as an argument to pthread_create()
The two threads newly created will be joined using pthread_join()
0 comments
Post a comment