Mind map
POSIX Threads
Map content
POSIX Threads
Tecnológico de Costa Rica, Carrera: Ingeniería en Computación, Sede: Cartago, Curso: Principios de sistemas operativos, Profesor: Esteban Arias Méndez, Estudiante: Jason David Espinoza Siles, Carné: 201223941, II semestre, 2016, Libro: Begining Linux Programming: capítulo 12. Reporte #6
What Is a Thread?
- Multiple strands of execution in a single program are called threads. A more precise definition is that a thread is a sequence of control within a process. All the programs you have seen so far have executed as a single process, although, like many other operating systems, Linux is quite capable of running multiple processes simultaneously. Indeed, all processes have at least one thread of execution. All the processes that you have seen so far in this book have had just one thread of execution. It’s important to be clear about the difference between the fork system call and the creation of new threads. When a process executes a fork call, a new copy of the process is created with its own variables and its own PID. This new process is scheduled independently, and (in general) executes almost independently of the process that created it. When we create a new thread in a process, in contrast, the new thread of execution gets its own stack (and hence local variables) but shares global vari
Advantages and Drawbacks of Threads
- The overhead cost of creating a new thread is significantly less than that of creating a new process (though Linux is particularly efficient at creating new processes compared with many other operating systems).
- Sometimes it is very useful to make a program appear to do two things at once.
- The performance of an application that mixes input, calculation, and output may be improved by running these as three separate threads.
- Now that multi-cored CPUs are common even in desktop and laptop machines, using multiple threads inside a process can, if the application is suitable, enable a single process to better utilize the hardware resources available.
- In general, switching between threads requires the operating system to do much less work than switching between processes.
Simultaneous Execution
- SE PUEDE HACER QUE LOS HILOS SE EJECUTEN SIMULTÁNEAMENTE, PERO ESTO TRAE EL PROBLEMA DE QUE ESTÉN SINCRONIZADOS EN CASO DE SER NECESARIO
Synchronization
- We look at two basic methods here: semaphores, which act as gatekeepers around a piece of code, and mutexes, which act as a mutual exclusion (hence the name mutex) device to protect sections of code. These methods are similar; indeed, one can be implemented in terms of the other. However, there are some cases where the semantics of the problem suggest that one is more expressive than the other. For example, con- trolling access to some shared memory, which only one thread can access at a time, would most naturally involve a mutex. However, controlling access to a set of identical objects as a whole, such as giving one telephone line out of a set of five available lines to a thread, suits a counting semaphore better. Which one you choose depends on personal preference and the most appropriate mechanism for your program.
- Synchronization with Semaphores
- A semaphore is a special type of variable that can be incremented or decremented, but crucial access to the variable is guaranteed to be atomic, even in a multithreaded program. This means that if two (or more) threads in a program attempt to change the value of a semaphore, the system guarantees that all the operations will in fact take place in sequence. With normal variables the result of conflicting operations from different threads within the same program is undefined.
- Synchronization with Mutexes
- The other way of synchronizing access in multithreaded programs is with mutexes (short for mutual exclusions), which act by allowing the programmer to “lock” an object so that only one thread can access it. To control access to a critical section of code you lock a mutex before entering the code section and then unlock it when you have finished.
Thread Attributes
- detachedstate : This attribute allows you to avoid the need for threads to rejoin. As with most of these _set functions, it takes a pointer to the attribute and a flag to determine the state required.
- schedpolicy : This controls how threads are scheduled. The options are SCHED_OTHER , SCHED_RP , and SCHED_FIFO . By default, the attribute is SCHED_OTHER . The other two types of scheduling are available only to processes running with superuser permissions, because they both have real-time scheduling but with slightly different behavior.
- schedparam : This is a partner to schedpolicy and allows control over the scheduling of threads running with schedule policy SCHED_OTHER . We take a look at an example of this a bit later in the chapter.
- inheritsched : This attribute takes two possible values: PTHREAD_EXPLICIT_SCHED and PTHREAD_INHERIT_SCHED . By default, the value is PTHREAD_EXPLICIT_SCHED , which means scheduling is explicitly set by the attributes.
- scope : This attribute controls how the scheduling of a thread is calculated. Because Linux currently supports only the value PTHREAD_SCOPE_SYSTEM , we will not look at this further here.
- stacksize : This attribute controls the thread creation stack size, set in bytes.
Thread Attributes Scheduling
- Let’s take a look at a second thread attribute you might want to change: scheduling. Changing the sched- uling attribute is very similar to setting the detached state, but there are two more functions that you can use to find the available priority levels, sched_get_priority_max and sched_get_priority_min .
Canceling a Thread
- Given a thread identifier, you can request that it be canceled. On the receiving end of the cancel request, things are slightly more complicated, but not much. A thread can set its cancel state using pthread_setcancelstate .
- The first parameter is either PTHREAD_CANCEL_ENABLE , which allows it to receive cancel requests, or PTHREAD_CANCEL_DISABLE , which causes them to be ignored. The oldstate pointer allows the previous state to be retrieved. If you are not interested, you can simply pass NULL . If cancel requests are accepted, there is a second level of control the thread can take, the cancel type, which is set with pthread_setcanceltype .
- Given a thread identifier, you can request that it be canceled. On the receiving end of the cancel request, things are slightly more complicated, but not much. A thread can set its cancel state using pthread_setcancelstate .
Threads in Abundance
- Up until now, we have always had the normal thread of execution of a program create just one other thread. However, we don’t want you to think that you can create only one extra thread.
Information
- Description
- Mind Map on POSIX Threads, created by Jason Espinoza Siles on 05/10/2016.
- Created by
- Jason Espinoza Siles
- Views
- 0
- Rating