Posix Threads
The Portable Operating System Interface for Unix (POSIX) is a set of standards for application programming interfaces (API) that allows the creation of portable applications on the Unix operating system. POSIX 1.c defines thread implementation and the functions to which application programmers should have access to build portable multithreaded applications. Although the specifications are intended for Unix-based operating systems, a Windows-based implementation of the same specification is also available. The POSIX method has been used as a paradigm for multiple implementations, which may provide developers with a different interface but the same functionality.Crucial issuesto note from a programming point of view as mentioned byBuyya et al.is listed below:
- A thread identifies a logical sequence of instructions. is of instructions to execute
- A thread can be created, terminated, or joined.
- A thread has a state that determines its current condition, whether it is executing, stopped, terminated, waiting for I/O, etc.
- The sequence of states that the thread undergoes is partly determined by the operating system scheduler and partly by the application developers.
- Threads share the memory of the process, and since they are executed concurrently, they need synchronization structures.
- Different synchronization abstractions are provided to solve different synchronization problems.
POSIX 1.c specification is provided for the C programming language. The thread.h header file, which is included typical C implementations, exposes all accessible functions and data structures. The POSIX thread libraries are a standard thread API for C/C++.
It allows the creation of a new concurrent process flow. It works well on multi-processor or multi-core systems, where the process flow may be scheduled to execute on another processor, increasing speed through parallel or distributed processing. Threads require less overhead than "forking" or creating a new process because the system does not initialize a new system virtual memory space and environment for the process. While multiprocessor systems are the most successful, improvements are also discovered on uniprocessor systems that exploit delays in I/O and other system operations that may stall process execution. One thread may be running while another waits for I/O or other system delays. Parallel programming technologies such as Message Passing Interface (MPI) and Parallel Virtual Machine (PVM) are used in a distributed computing environment while threads are limited to a single computer system. All threads within a process share the same address space. A thread is formed by specifying a function and the parameters that will be handled by the thread. The goal of utilizing the POSIX thread library in your software is to make it run quicker.
Thread Creation and Termination with C
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *print_message_function(void *ptr);
main() {
pthread_t thread1, thread2;
char *message1 = "Thread 1";
char *message2 = "Thread 2";
int iret1, iret2;
/* Create independent threads each of which will execute the 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 the 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);
printf("Thread 1 returns: %d\n",iret1);
printf("Thread 2 returns: %d\n",iret2);
exit(0);
}
void *print_message_function(void *ptr)
{
char *message;
message= (char *) ptr;
printf("%s\n", message);
}
Results:
Thread 1
Thread 2
Thread 1 returns: 0
Thread 2 returns: 0
Comments
Post a Comment