background preloader

Multi-processing and multi-threading

Facebook Twitter

POSIX Threads Programming. Table of Contents In shared memory multiprocessor architectures, threads can be used to implement parallelism.

POSIX Threads Programming

Historically, hardware vendors have implemented their own proprietary versions of threads, making portability a concern for software developers. For UNIX systems, a standardized C language threads programming interface has been specified by the IEEE POSIX 1003.1c standard. Implementations that adhere to this standard are referred to as POSIX threads, or Pthreads. The tutorial begins with an introduction to concepts, motivations, and design considerations for using Pthreads. Level/Prerequisites: This tutorial is ideal for those who are new to parallel programming with pthreads.

What is a Thread? Technically, a thread is defined as an independent stream of instructions that can be scheduled to run as such by the operating system. What are Pthreads? Historically, hardware vendors have implemented their own proprietary versions of threads. Why Pthreads? POSIX Threads. Thread Basics: Thread operations include thread creation, termination, synchronization (joins,blocking), scheduling, data management and process interaction.

POSIX Threads

A thread does not maintain a list of created threads, nor does it know the thread that created it. All threads within a process share the same address space. Threads in the same process share: Process instructions Most data open files (descriptors) signals and signal handlers current working directory User and group id Each thread has a unique: Thread ID set of registers, stack pointer stack for local variables, return addresses signal mask priority Return value: errno pthread functions return "0" if OK. Thread Creation and Termination: Example: pthread1.c. Introduction to Priority Inversion. Introduction to Priority Inversion When tasks share resources, as they often do, strange things can and will happen.

Introduction to Priority Inversion

Priority inversions can be particularly difficult to anticipate. Here's an introduction to priority inversions and a pair of techniques you can use to avoid them. Most commercial real-time operating systems (RTOSes) employ a priority-based preemptive scheduler. These systems assign each task a unique priority level. Because tasks share resources, events outside the scheduler's control can prevent the highest priority ready task from running when it should. Resource sharing Tasks need to share resources to communicate and process data. Synchronization (computer science) Critical section. In concurrent programming, a critical section is a part of a multi-process program that may not be concurrently executed by more than one of the program's processes.

Critical section

[a] In other words, it is a piece of a program that requires mutual exclusion of access.[1] Typically, the critical section accesses a shared resource, such as a data structure, a peripheral device, or a network connection, that does not allow multiple concurrent accesses.[2] A critical section may consist of multiple discontiguous parts of the program's code. For example, one part of a program might read from a file that another part wishes to modify. These parts together form a single critical section, since simultaneous readings and modifications may interfere with each other.[1] How critical sections are implemented varies among operating systems.

The simplest method is to prevent any change of processor control inside the critical section. This brute-force approach can be improved upon by using semaphores. See also[edit]

Pthread conditional variable

Threads-semaphores. Threads. [fbsd] Re: system scope vs. process scope. John Baldwin jhb at freebsd.org Tue Aug 8 18:02:00 UTC 2006.

[fbsd] Re: system scope vs. process scope

System scope vs. process scope. John Baldwin john at baldwin.cx Fri Aug 4 15:35:58 UTC 2006 On Friday 04 August 2006 10:06, Jeremie Le Hen wrote: > Hi, > > occasionally, I saw environnement variables LIBPTHREAD_SYSTEM_SCOPE and > LIBPTHREAD_PROCESS_SCOPE mentionned hither and thither.

system scope vs. process scope

I grep(1)'ed > through the source tree for some documentation, but I wasn't able to > find any. Difference between mutex and semaphore. Wait(2) - Linux manual page. WAIT(2) Linux Programmer's Manual WAIT(2) NAME top wait, waitpid, waitid - wait for process to change state SYNOPSIS top #include <sys/types.h> #include <sys/wait.h> pid_t wait(int *status); pid_t waitpid(pid_t pid, int *status, int options); int waitid(idtype_t idtype, id_t id, siginfo_t *infop, int options); /* This is the glibc and POSIX interface; see NOTES for information on the raw system call. */ Feature Test Macro Requirements for glibc (see feature_test_macros(7)): waitid(): _SVID_SOURCE || _XOPEN_SOURCE >= 500 || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L DESCRIPTION top All of these system calls are used to wait for state changes in a child of the calling process, and obtain information about the child whose state has changed.

wait(2) - Linux manual page

RETURN VALUE top ERRORS top ECHILD (for wait()) The calling process does not have any unwaited- for children. CONFORMING TO top SVr4, 4.3BSD, POSIX.1-2001. C - fork() child and parent processes.