[PDF] Programming With POSIX Threads !EXCLUSIVE! Download

1 view
Skip to first unread message

Robert Trimble

unread,
Jan 25, 2024, 6:51:21 PM1/25/24
to eroogerplig

The POSIX thread libraries are a standards based thread API for C/C++.It allows one to spawn a new concurrent process flow. It is most effectiveon multi-processor or multi-core systems where the process flow can be scheduled to run on another processor thus gaining speed through parallel or distributed processing.Threads require less overhead than "forking" or spawning a new process because the system does not initialize a new system virtual memory space and environment forthe process. While most effective on a multiprocessor system, gains arealso found on uniprocessor systems which exploit latency in I/O and othersystem functions which may halt process execution. (One thread may executewhile another is waiting for I/O or some other system latency.)Parallel programming technologies such as MPI and PVM are used in a distributedcomputing environment while threads are limited to a single computer system.All threads within a process share the same address space.A thread is spawned by defining a function and it's arguments which willbe processed in the thread.The purpose of using the POSIX thread library in your software is to execute software faster.

[PDF] Programming with POSIX threads download


Download Filehttps://t.co/DONAeEO1CD



A condition variable is a variable of type pthread_cond_t and isused with the appropriate functions for waiting and later, process continuation.The condition variable mechanism allows threads to suspend execution and relinquish the processor until some condition is true.A condition variable must always be associated with a mutexto avoid a race condition created by one thread preparing to wait and another threadwhich may signal the condition before the first thread actually waits on itresulting in a deadlock.The thread will be perpetually waiting for a signal that is never sent.Any mutex can be used, there is no explicit link between the mutex and the condition variable.

This book offers an in-depth description ofthe IEEE operating system interface standard, POSIXAE (PortableOperating System Interface) threads, commonly called Pthreads.Written for experienced C programmers, but assuming no previousknowledge of threads, the book explains basic concepts such asasynchronous programming, the lifecycle of a thread, andsynchronization. You then move to more advanced topics such asattributes objects, thread-specific data, and realtime scheduling.An entire chapter is devoted to "real code," with a look atbarriers, read/write locks, the work queue manager, and how toutilize existing libraries. In addition, the book tackles one ofthe thorniest problems faced by thread programmers-debugging-withvaluable suggestions on how to avoid code errors and performanceproblems from the outset.

At the very bottom of the Wikipedia page you reference in the "External Links" section, you will find a link to the current Posix specification of pthreads.h, which includes a history of changes. There have been a few, but the basic principles are intact. So the books you mention are probably still good learning materials. (I still have a well-thumbed copy of Programming with POSIX Threads on my bookshelf.)

In computing, POSIX Threads, commonly known as pthreads, is an execution model that exists independently from a programming language, as well as a parallel execution model. It allows a program to control multiple different flows of work that overlap in time. Each flow of work is referred to as a thread, and creation and control over these flows is achieved by making calls to the POSIX Threads API. POSIX Threads is an API defined by the Institute of Electrical and Electronics Engineers (IEEE) standard POSIX.1c, Threads extensions (IEEE Std 1003.1c-1995).

Implementations of the API are available on many Unix-like POSIX-conformant operating systems such as FreeBSD, NetBSD, OpenBSD, Linux, macOS, Android,[1] Solaris, Redox, and AUTOSAR Adaptive, typically bundled as a library libpthread. DR-DOS and Microsoft Windows implementations also exist: within the SFU/SUA subsystem which provides a native implementation of a number of POSIX APIs, and also within third-party packages such as pthreads-w32,[2] which implements pthreads on top of existing Windows API.

The POSIX semaphore API works with POSIX threads but is not part of threads standard, having been defined in the POSIX.1b, Real-time extensions (IEEE Std 1003.1b-1993) standard. Consequently, the semaphore procedures are prefixed by sem_ instead of pthread_.

This program creates five threads, each executing the function perform_work that prints the unique number of this thread to standard output. If a programmer wanted the threads to communicate with each other, this would require defining a variable outside of the scope of any of the functions, making it a global variable. This program can be compiled using the gcc compiler with the following command:

Windows does not support the pthreads standard natively, therefore the Pthreads4w project seeks to provide a portable and open-source wrapper implementation. It can also be used to port Unix software (which uses pthreads) with little or no modification to the Windows platform.[4] Pthreads4w version 3.0.0[5] or later, released under the Apache Public License v2.0, is compatible with 64-bit or 32-bit Windows systems. Version 2.11.0,[6] released under the LGPLv3 license, is also 64-bit or 32-bit compatible.

I'm looking for a comprehensive pthread tutorial. I considered buying Programming with posix threads but this book seems is bit dated. Other online tutorials like this are very basic. Can someone please suggest a good and complete pthread tutorial.

David Butenhof's Programming with POSIX Threads was published 10 years ago, in 1997. At the time, it was the definitive work onthe POSIX thread API, and multi-threaded programming in general. Ten years is a long time in computing so how does it faretoday?

This tutorial is an attempt to help you become familiar with multi-threadedprogramming with the POSIX threads (pthreads) library, and attempts to showhow its features can be used in "real-life" programs. It explains thedifferent tools defined by the library, shows how to use them, and then givesan example of using them to solve programming problems. There is an implicitassumption that the user has some theoretical familiarity with paralellprogramming (or multi-processing) concepts. Users without such backgroundmight find the concepts harder to grasp. A seperate tutorial will be preparedto explain the theoreticl background and terms to those who are familiar onlywith normal "serial" programming.

I would assume that users which are familiar with asynchronous programmingmodels, such as those used in windowing environments (X, Motif), will find iteasier to grasp the concepts of multi-threaded programming.

When talking about POSIX threads, one cannot avoid the question "Which draftof the POSIX threads standard shall be used?". As this threads standard has beenrevised over a period of several years, one will find that implementationsadhering to different drafts of the standard have a different set of functions,different default values, and different nuances. Since this tutorial waswritten using a Linux system with the kernel-level LinuxThreads library, v0.5,programmers with access to other systems, using different versions of pthreads,should refer to their system's manuals in case of incompatibilities. Also, sincesome of the example programs are using blocking system calls, they won'twork with user-level threading libraries (refer to ourparallel programming theory tutorial formore information).
Having said that,i'd try to check the example programs on other systems as well (Solaris 2.5comes to mind), to make it more "cross-platform".

A thread is a semi-process, that has its own stack, and executes a givenpiece of code. Unlike a real process, the thread normally shares its memorywith other threads (where as for processes we usually have a different memoryarea for each one of them). A Thread Group is a set of threads all executinginside the same process. They all share the same memory, and thus can accessthe same global variables, same heap memory, same set of file descriptors,etc. All these threads execute in parallel (i.e. using time slices, or ifthe system has several processors, then really in parallel).

A few notes should be mentioned about this program:

  1. Note that the main program is also a thread, so it executes the do_loop() function in parallel to the thread it creates.
  2. pthread_create() gets 4 parameters. The first parameter is used by pthread_create() to supply the program with information about the thread. The second parameter is used to set some attributes for the new thread. In our case we supplied a NULL pointer to tell pthread_create() to use the default values. The third parameter is the name of the function that the thread will start executing. The forth parameter is an argument to pass to this function. Note the cast to a 'void*'. It is not required by ANSI-C syntax, but is placed here for clarification.
  3. The delay loop inside the function is used only to demonstrate that the threads are executing in parallel. Use a larger delay value if your CPU runs too fast, and you see all the printouts of one thread before the other.
  4. The call to pthread_exit() Causes the current thread to exit and free any thread-specific resources it is taking. There is no need to use this call at the end of the thread's top function, since when it returns, the thread would exit automatically anyway. This function is useful if we want to exit a thread in the middle of its execution.

In order to compile a multi-threaded program using gcc,we need to link it with the pthreads library. Assuming you have this libraryalready installed on your system, here is how to compile our first program:

gcc pthread_create.c -o pthread_create -lpthread

The source code for this program may be found in thepthread_create.c file.

For instance, consider the case where two threads try to update two variables.One tries to set both to 0, and the other tries to set both to 1. If boththreads would try to do that at the same time, we might get with a situationwhere one variable contains 1, and one contains 0. This is because acontext-switch (we already know what this is by now, right?) might occur afterthe first tread zeroed out the first variable, then the second thread wouldset both variables to 1, and when the first thread resumes operation, it willzero out the second variable, thus getting the first variable set to '1',and the second set to '0'.

dd2b598166
Reply all
Reply to author
Forward
0 new messages