SlideShare ist ein Scribd-Unternehmen logo
1 von 39
Downloaden Sie, um offline zu lesen
Team Emertxe
Linux Internals
Day 3
Threads
Threads
● Threads, like processes, are a mechanism to allow a program
to do more than one thing at a time
● As with processes, threads appear to run concurrently
● The Linux kernel schedules them asynchronously, interrupting
each thread from time to time to give others a chance to
execute
● Threads are a finer-grained unit of execution than processes
● That thread can create additional threads; all these threads
run the same program in the same process
● But each thread may be executing a different part of the
program at any given time
Threads
Single and Multi threaded Process
Threads are similar to handling multiple functions in parallel. Since they share
same code & data segments, care to be taken by programmer to avoid issues.
Single Threaded Process
code data files
registers
stack
thread
Multi Threaded Process
code data files
registers
stack
thread
registers
stack
thread
registers
stack
thread
Threads
Advantages
● Takes less time to create a new thread in an existing
process than to create a brand new process
● Switching between threads is faster than a normal
context switch
● Threads enhance efficiency in communication between
different executing programs
● No kernel involved
Threads
pthread API's
● GNU/Linux implements the POSIX standard thread API
(known as pthreads)
● All thread functions and data types are declared in the
header file <pthread.h>
● The pthread functions are not included in the standard C
library
● Instead, they are in libpthread, so you should add
-lpthread to the command line when you link your
program
Using libpthread is a very good example to understand differences between
functions, library functions and system calls
Threads
Creation
● The pthread_create function creates a new thread
Function Meaning
int pthread_create(
pthread_t *thread,
const pthread_attr_t *attr,
void *(*start_routine) (void *),
void *arg)
 A pointer to a pthread_t variable, in which the
thread ID of the new thread is stored
 A pointer to a thread attribute object. If you
pass NULL as the thread attribute, a thread will
be created with the default thread attributes
 A pointer to the thread function. This is an
ordinary function pointer, of this type: void* (*)
(void*)
 A thread argument value of type void *.
Whatever you pass is simply passed as the
argument to the thread function when thread
begins executing
Threads
Creation
● A call to pthread_create returns immediately, and the
original thread continues executing the instructions
following the call
● Meanwhile, the new thread begins executing the thread
function
● Linux schedules both threads asynchronously
● Programs must not rely on the relative order in which
instructions are executed in the two threads
Threads
Compilation
● Use the following command to compile the programs
using thread libraries
$ gcc -o <output_file> <input_file.c> -lpthread
Threads
Joining
● It is quite possible that output created by a thread needs to be
integrated for creating final result
● So the main program may need to wait for threads to
complete actions
● The pthread_join() function helps to achieve this purpose
Function Meaning
int pthread_join(
pthread_t thread,
void **value_ptr)
 Thread ID of the thread to wait
 Pointer to a void* variable that will receive
thread finished value
 If you don’t care about the thread return
value, pass NULL as the second argument.
Threads
Passing Data
● The thread argument provides a convenient method of
passing data to threads
● Because the type of the argument is void*, though, you
can’t pass a lot of data directly via the argument
● Instead, use the thread argument to pass a pointer to
some structure or array of data
● Define a structure for each thread function, which
contains the “parameters” that the thread function
expects
● Using the thread argument, it’s easy to reuse the same
thread function for many threads. All these threads
execute the same code, but on different data
Threads
Return Values
● If the second argument you pass to pthread_join is non-
null, the thread’s return value will be placed in the
location pointed to by that argument
● The thread return value, like the thread argument, is of
type void*
● If you want to pass back a single int or other small
number, you can do this easily by casting the value to
void* and then casting back to the appropriate type after
calling pthread_join
Threads
Attributes
● Thread attributes provide a mechanism for fine-tuning
the behaviour of individual threads
● Recall that pthread_create accepts an argument that is a
pointer to a thread attribute object
● If you pass a null pointer, the default thread attributes
are used to configure the new thread
● However, you may create and customize a thread
attribute object to specify other values for the attributes
Threads
Joinable and Detached
● A thread may be created as a joinable thread (the default)
or as a detached thread
● A joinable thread, like a process, is not automatically cleaned
up by GNU/Linux when it terminates
● Thread’s exit state hangs around in the system (kind of like a
zombie process) until another thread calls pthread_join to
obtain its return value. Only then are its resources released
● A detached thread, in contrast, is cleaned up automatically
when it terminates
●
Because a detached thread is immediately cleaned up,
another thread may not synchronize on its completion by
using pthread_join or obtain its return value
Synchronization
Synchronization
why?
● Programming with threads is very tricky because most
threaded programs are concurrent programs
● In particular, there’s no way to know when the system will
schedule one thread to run and when it will run another
● One thread might run for a very long time, or the system
might switch among threads very quickly
● Debugging a threaded program is difficult because you cannot
always easily reproduce the behavior that caused the problem.
● You might run the program once and have everything work
fine; the next time you run it, it might crash.
● There’s no way to make the system schedule the threads
exactly the same way it did before.
Synchronization
Race Condition
● The ultimate cause of most bugs involving threads is that the
threads are accessing the same data.
● So the powerful aspects of threads can become a danger.
● If one thread is only partway through updating a data
structure when another thread accesses the same data
structure, it’s a problem.
● These bugs are called race conditions; the threads are racing
one another to change the same data structure.
Synchronization
Race Condition – The Problem
● Now suppose that two threads happen to finish a job at about
the same time, but only one job remains in the queue.
● The first thread checks whether job_queue is null; finding
that it isn’t, the thread enters the loop and stores the pointer
to the job object in next_job.
● At this point, Linux happens to interrupt the first thread and
schedules the second.
● The second thread also checks job_queue and finding it non-
null, also assigns the same job pointer to next_job.
● By unfortunate coincidence, we now have two threads
executing the same job.
Synchronization
Race Condition – The Solution
● To eliminate race conditions, you need a way to make
operations atomic.
● An atomic operation is indivisible and uninterruptible; once
the operation starts, it will not be paused or interrupted until
it completes, and no other operation will take place mean
while.
● In this particular example, you want to check job_queue; if
it’s not empty, remove the first job, all as a single atomic
operation.
Synchronization
Mutexes
● The solution to the job queue race condition problem is to let
only one thread access the queue of jobs at a time.
● GNU/Linux provides mutexes, short for MUTual EXclusion
locks.
● A mutex is a special lock that only one thread may lock at a
time.
● If a thread locks a mutex and then a second thread also tries
to lock the same mutex, the second thread is blocked, or put
on hold.
● Only when the first thread unlocks the mutex is the second
thread unblocked—allowed to resume execution.
Synchronization
Mutex - Creation
● To create a mutex, create a variable of type
pthread_mutex_t and pass a pointer to it to
pthread_mutex_init.
● The second argument to pthread_mutex_init is a
pointer to a mutex attribute object, which specifies
attributes of the mutex.
Synchronization
Mutex – Locking & Blocking
● A thread may attempt to lock a mutex by calling
pthread_mutex_lock on it.
● If the mutex was unlocked, it becomes locked and the
function returns immediately.
● If the mutex was locked by another thread,
pthread_mutex_lock blocks execution and returns only
eventually when the mutex is unlocked by the other thread.
● More than one thread may be blocked on a locked mutex at
one time.
● When the mutex is unlocked, only one of the blocked threads
is unblocked and allowed to lock the mutex; the other threads
stay blocked.
Synchronization
Mutex – Unlocking
● A call to pthread_mutex_unlock unlocks a mutex.
● This function should always be called from the same
thread that locked the mutex.
Synchronization
Semaphores
● A semaphore is a counter that can be used to synchronize
multiple threads.
● As with a mutex, GNU/Linux guarantees that checking or
modifying the value of a semaphore can be done safely,
without creating a race condition.
● Each semaphore has a counter value, which is a non-negative
integer.
Synchronization
Semaphores - Operations
● A wait operation decrements the value of the
semaphore by 1. If the value is already zero, the
operation blocks until the value of the semaphore
becomes positive (due to the action of some other
thread). When the semaphore’s value becomes
positive, it is decremented by 1 and the wait
operation returns.
● A post operation increments the value of the
semaphore by 1. If the semaphore was previously
zero and other threads are blocked in a wait
operation on that semaphore, one of those threads is
unblocked and its wait operation completes (which
brings the semaphore’s value back to zero).
Synchronization
Semaphores - Variable
● A semaphore is represented by a sem_t variable.
● Before using it, you must initialize it using the
sem_init function, passing a pointer to the sem_t
variable.
● The second parameter should be zero, and the third
parameter is the semaphore’s initial value.
● If you no longer need a semaphore, it’s good to de-
allocate it with sem_destroy.
Synchronization
Semaphores – Wait & Post
● To wait on a semaphore, use sem_wait.
● To post to a semaphore, use sem_post.
Signals
Signals
● Signals are used to notify a process of a particular event
● Signals make the process aware that something has
happened in the system
● Target process should perform some pre-defined actions
to handle signals
● This is called ‘signal handling’
● Actions may range from 'self termination' to 'clean-up'
Signals
Virtual Machine Model
Signals
Identification
Each signal in Linux has got a name starting with 'SIG' and
a number associated with it.
For example Signal 'SIGSEGV' has got a number 11.
This is defined in /usr/include/bits/signum.h *
* The path might vary on different distributions
Signals
Origins
● The kernel sends signals to processes in response to
specific conditions. For instance, any of these Signals
may be sent to a process that attempts to perform an
illegal operation :
– SIGBUS (bus error),
– SIGSEGV (segmentation violation),
● A Process may also send a Signal to another Process.
● A Process may also send a Signal to itself
Signals
Handling
● When a process receives a signal, it processes
● Immediate handling
● For all possible signals, the system defines a default
disposition or action to take when a signal occurs
● There are four possible default dispositions:
– Exit: Forces process to exit
– Core: Forces process to exit and create a core file
– Stop: Stops the process
– Ignore: Ignores the signal
● Handling can be done, called ‘signal handling’
Signals
Set Behaviour
● The sigaction function can be used to set a signal disposition
whose prototype is:
int sigaction(int signum, const struct sigaction *act, struct
sigaction *oldact);
● The first parameter is the signal number.
● The next two parameters are pointers to sigaction structures
● The first of these contains the desired disposition for that
signal number, while the second receives the previous
disposition
Signals
Note
● A signal handler should perform the minimum work necessary
to respond to the signal, and then return control to the main
program (or terminate the program).
● In most cases, this consists simply of recording the fact that a
signal occurred.
● The main program then checks periodically whether a signal
has occurred and reacts accordingly.
Signals
vs Interrupt
● Signals can be described as soft-interrupts
● The concept of 'signals' and 'signals handling' is
analogous to that of the 'interrupt' handling done by a
microprocessor
● When a signal is sent to a process or thread, a signal
handler may be entered (depending on the current
disposition of the signal), which is similar to the
system entering an interrupt handler as the result of
receiving an interrupt.
Signals
Process Termination
● Normally, a process terminates in one of two ways. Either the
executing program calls the exit function, or the program’s
main function returns.
● Each process has an exit code: a number that the process
returns to its parent. The exit code is the argument passed to
the exit function, or the value returned from main.
● A process may also terminate abnormally, in response to a
signal. For instance, the SIGBUS, SIGSEGV, and SIGFPE signals
mentioned previously cause the process to terminate.
Stay connected
About us: Emertxe is India’s one of the top IT finishing schools & self learning kits
provider. Our primary focus is on Embedded with diversification focus on Java,
Oracle and Android areas
Branch Office: Corporate Headquarters:
Emertxe Information Technologies, Emertxe Information Technologies,
No-1, 9th Cross, 5th Main, 83, Farah Towers, 1st
Floor,
Jayamahal Extension, MG Road,
Bangalore, Karnataka 560046 Bangalore, Karnataka - 560001
T: +91 809 555 7333 (M), +91 80 41289576 (L)
E: training@emertxe.com
https://www.facebook.com/Emertxe https://twitter.com/EmertxeTweet https://www.slideshare.net/EmertxeSlides
Thank You

Weitere ähnliche Inhalte

Was ist angesagt?

Linux Kernel Development
Linux Kernel DevelopmentLinux Kernel Development
Linux Kernel DevelopmentPriyank Kapadia
 
linux device driver
linux device driverlinux device driver
linux device driverRahul Batra
 
Arm device tree and linux device drivers
Arm device tree and linux device driversArm device tree and linux device drivers
Arm device tree and linux device driversHoucheng Lin
 
Introduction Linux Device Drivers
Introduction Linux Device DriversIntroduction Linux Device Drivers
Introduction Linux Device DriversNEEVEE Technologies
 
The Linux Block Layer - Built for Fast Storage
The Linux Block Layer - Built for Fast StorageThe Linux Block Layer - Built for Fast Storage
The Linux Block Layer - Built for Fast StorageKernel TLV
 
Jagan Teki - U-boot from scratch
Jagan Teki - U-boot from scratchJagan Teki - U-boot from scratch
Jagan Teki - U-boot from scratchlinuxlab_conf
 

Was ist angesagt? (20)

Memory model
Memory modelMemory model
Memory model
 
Linux Systems: Getting started with setting up an Embedded platform
Linux Systems: Getting started with setting up an Embedded platformLinux Systems: Getting started with setting up an Embedded platform
Linux Systems: Getting started with setting up an Embedded platform
 
Linux Kernel Development
Linux Kernel DevelopmentLinux Kernel Development
Linux Kernel Development
 
Embedded C
Embedded CEmbedded C
Embedded C
 
C Programming - Refresher - Part II
C Programming - Refresher - Part II C Programming - Refresher - Part II
C Programming - Refresher - Part II
 
linux device driver
linux device driverlinux device driver
linux device driver
 
I2c drivers
I2c driversI2c drivers
I2c drivers
 
Embedded Linux on ARM
Embedded Linux on ARMEmbedded Linux on ARM
Embedded Linux on ARM
 
Arm device tree and linux device drivers
Arm device tree and linux device driversArm device tree and linux device drivers
Arm device tree and linux device drivers
 
Introduction Linux Device Drivers
Introduction Linux Device DriversIntroduction Linux Device Drivers
Introduction Linux Device Drivers
 
C Programming - Refresher - Part I
C Programming - Refresher - Part I C Programming - Refresher - Part I
C Programming - Refresher - Part I
 
Basic Linux Internals
Basic Linux InternalsBasic Linux Internals
Basic Linux Internals
 
Ixgbe internals
Ixgbe internalsIxgbe internals
Ixgbe internals
 
Advanced C - Part 2
Advanced C - Part 2Advanced C - Part 2
Advanced C - Part 2
 
Introduction to Embedded System
Introduction to Embedded SystemIntroduction to Embedded System
Introduction to Embedded System
 
BusyBox for Embedded Linux
BusyBox for Embedded LinuxBusyBox for Embedded Linux
BusyBox for Embedded Linux
 
I2c drivers
I2c driversI2c drivers
I2c drivers
 
The Linux Block Layer - Built for Fast Storage
The Linux Block Layer - Built for Fast StorageThe Linux Block Layer - Built for Fast Storage
The Linux Block Layer - Built for Fast Storage
 
Embedded Operating System - Linux
Embedded Operating System - LinuxEmbedded Operating System - Linux
Embedded Operating System - Linux
 
Jagan Teki - U-boot from scratch
Jagan Teki - U-boot from scratchJagan Teki - U-boot from scratch
Jagan Teki - U-boot from scratch
 

Andere mochten auch

Linux Internals - Kernel/Core
Linux Internals - Kernel/CoreLinux Internals - Kernel/Core
Linux Internals - Kernel/CoreShay Cohen
 
FCFS scheduling OS
FCFS scheduling OSFCFS scheduling OS
FCFS scheduling OSTotan Banik
 
Terminal Commands (Linux - ubuntu) (part-1)
Terminal Commands  (Linux - ubuntu) (part-1)Terminal Commands  (Linux - ubuntu) (part-1)
Terminal Commands (Linux - ubuntu) (part-1)raj upadhyay
 
fudcon-fedora-arm-iot-rajeshsola
fudcon-fedora-arm-iot-rajeshsolafudcon-fedora-arm-iot-rajeshsola
fudcon-fedora-arm-iot-rajeshsolaRajesh Sola
 
Vba Macros Interoperability
Vba Macros InteroperabilityVba Macros Interoperability
Vba Macros InteroperabilityRajesh Sola
 
aoa-adk-osidays-rajeshsola
aoa-adk-osidays-rajeshsolaaoa-adk-osidays-rajeshsola
aoa-adk-osidays-rajeshsolaRajesh Sola
 

Andere mochten auch (20)

Embedded Android : System Development - Part II (Linux device drivers)
Embedded Android : System Development - Part II (Linux device drivers)Embedded Android : System Development - Part II (Linux device drivers)
Embedded Android : System Development - Part II (Linux device drivers)
 
Linux Internals - Kernel/Core
Linux Internals - Kernel/CoreLinux Internals - Kernel/Core
Linux Internals - Kernel/Core
 
Linux systems - Getting started with setting up and embedded platform
Linux systems - Getting started with setting up and embedded platformLinux systems - Getting started with setting up and embedded platform
Linux systems - Getting started with setting up and embedded platform
 
Communication Protocols (UART, SPI,I2C)
Communication Protocols (UART, SPI,I2C)Communication Protocols (UART, SPI,I2C)
Communication Protocols (UART, SPI,I2C)
 
Linux Programming
Linux ProgrammingLinux Programming
Linux Programming
 
Embedded Android : System Development - Part I
Embedded Android : System Development - Part IEmbedded Android : System Development - Part I
Embedded Android : System Development - Part I
 
Data Structures & Algorithm design using C
Data Structures & Algorithm design using C Data Structures & Algorithm design using C
Data Structures & Algorithm design using C
 
Building careers in embedded
Building careers in embeddedBuilding careers in embedded
Building careers in embedded
 
Emertxe : Linux training portfolio
Emertxe : Linux training portfolioEmertxe : Linux training portfolio
Emertxe : Linux training portfolio
 
Linux programming - Getting self started
Linux programming - Getting self started Linux programming - Getting self started
Linux programming - Getting self started
 
Linux Internals - Interview essentials 4.0
Linux Internals - Interview essentials 4.0Linux Internals - Interview essentials 4.0
Linux Internals - Interview essentials 4.0
 
Embedded Android : System Development - Part IV
Embedded Android : System Development - Part IVEmbedded Android : System Development - Part IV
Embedded Android : System Development - Part IV
 
Embedded C - Optimization techniques
Embedded C - Optimization techniquesEmbedded C - Optimization techniques
Embedded C - Optimization techniques
 
FCFS scheduling OS
FCFS scheduling OSFCFS scheduling OS
FCFS scheduling OS
 
Ubuntu Terminal
Ubuntu TerminalUbuntu Terminal
Ubuntu Terminal
 
Terminal Commands (Linux - ubuntu) (part-1)
Terminal Commands  (Linux - ubuntu) (part-1)Terminal Commands  (Linux - ubuntu) (part-1)
Terminal Commands (Linux - ubuntu) (part-1)
 
Emertxe Certified Embedded Professional (ECEP) : Induction
Emertxe Certified Embedded Professional (ECEP) : InductionEmertxe Certified Embedded Professional (ECEP) : Induction
Emertxe Certified Embedded Professional (ECEP) : Induction
 
fudcon-fedora-arm-iot-rajeshsola
fudcon-fedora-arm-iot-rajeshsolafudcon-fedora-arm-iot-rajeshsola
fudcon-fedora-arm-iot-rajeshsola
 
Vba Macros Interoperability
Vba Macros InteroperabilityVba Macros Interoperability
Vba Macros Interoperability
 
aoa-adk-osidays-rajeshsola
aoa-adk-osidays-rajeshsolaaoa-adk-osidays-rajeshsola
aoa-adk-osidays-rajeshsola
 

Ähnlich wie Linux Internals - Part III

Multithreading programming in java
Multithreading programming in javaMultithreading programming in java
Multithreading programming in javaElizabeth alexander
 
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptxsandhyakiran10
 
Sync, async and multithreading
Sync, async and multithreadingSync, async and multithreading
Sync, async and multithreadingTuan Chau
 
Java Threads: Lightweight Processes
Java Threads: Lightweight ProcessesJava Threads: Lightweight Processes
Java Threads: Lightweight ProcessesIsuru Perera
 
Chap3 multi threaded programming
Chap3 multi threaded programmingChap3 multi threaded programming
Chap3 multi threaded programmingraksharao
 
Understanding Threads in operating system
Understanding Threads in operating systemUnderstanding Threads in operating system
Understanding Threads in operating systemHarrytoye2
 
Programming with Threads in Java
Programming with Threads in JavaProgramming with Threads in Java
Programming with Threads in Javakoji lin
 
Java Concurrency Starter Kit
Java Concurrency Starter KitJava Concurrency Starter Kit
Java Concurrency Starter KitMark Papis
 
Multithreaded_Programming_in_Python.pdf
Multithreaded_Programming_in_Python.pdfMultithreaded_Programming_in_Python.pdf
Multithreaded_Programming_in_Python.pdfgiridharsripathi
 
Lecture 23-24.pptx
Lecture 23-24.pptxLecture 23-24.pptx
Lecture 23-24.pptxtalha ijaz
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaKavitha713564
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaKavitha713564
 
Multithreaded Programming Part- II.pdf
Multithreaded Programming Part- II.pdfMultithreaded Programming Part- II.pdf
Multithreaded Programming Part- II.pdfHarika Pudugosula
 
Introduction to concurrent programming with akka actors
Introduction to concurrent programming with akka actorsIntroduction to concurrent programming with akka actors
Introduction to concurrent programming with akka actorsdatamantra
 

Ähnlich wie Linux Internals - Part III (20)

Multithreading programming in java
Multithreading programming in javaMultithreading programming in java
Multithreading programming in java
 
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx
 
Sync, async and multithreading
Sync, async and multithreadingSync, async and multithreading
Sync, async and multithreading
 
Java Threads: Lightweight Processes
Java Threads: Lightweight ProcessesJava Threads: Lightweight Processes
Java Threads: Lightweight Processes
 
Chap3 multi threaded programming
Chap3 multi threaded programmingChap3 multi threaded programming
Chap3 multi threaded programming
 
Md09 multithreading
Md09 multithreadingMd09 multithreading
Md09 multithreading
 
Understanding Threads in operating system
Understanding Threads in operating systemUnderstanding Threads in operating system
Understanding Threads in operating system
 
Threads
ThreadsThreads
Threads
 
Threads concept in java
Threads concept in javaThreads concept in java
Threads concept in java
 
Programming with Threads in Java
Programming with Threads in JavaProgramming with Threads in Java
Programming with Threads in Java
 
Java Concurrency Starter Kit
Java Concurrency Starter KitJava Concurrency Starter Kit
Java Concurrency Starter Kit
 
Multithreaded_Programming_in_Python.pdf
Multithreaded_Programming_in_Python.pdfMultithreaded_Programming_in_Python.pdf
Multithreaded_Programming_in_Python.pdf
 
Lecture 23-24.pptx
Lecture 23-24.pptxLecture 23-24.pptx
Lecture 23-24.pptx
 
Chap7 slides
Chap7 slidesChap7 slides
Chap7 slides
 
Java threading
Java threadingJava threading
Java threading
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Multithreaded Programming Part- II.pdf
Multithreaded Programming Part- II.pdfMultithreaded Programming Part- II.pdf
Multithreaded Programming Part- II.pdf
 
Introduction to concurrent programming with akka actors
Introduction to concurrent programming with akka actorsIntroduction to concurrent programming with akka actors
Introduction to concurrent programming with akka actors
 

Mehr von Emertxe Information Technologies Pvt Ltd

Mehr von Emertxe Information Technologies Pvt Ltd (20)

premium post (1).pdf
premium post (1).pdfpremium post (1).pdf
premium post (1).pdf
 
Career Transition (1).pdf
Career Transition (1).pdfCareer Transition (1).pdf
Career Transition (1).pdf
 
10_isxdigit.pdf
10_isxdigit.pdf10_isxdigit.pdf
10_isxdigit.pdf
 
01_student_record.pdf
01_student_record.pdf01_student_record.pdf
01_student_record.pdf
 
02_swap.pdf
02_swap.pdf02_swap.pdf
02_swap.pdf
 
01_sizeof.pdf
01_sizeof.pdf01_sizeof.pdf
01_sizeof.pdf
 
07_product_matrix.pdf
07_product_matrix.pdf07_product_matrix.pdf
07_product_matrix.pdf
 
06_sort_names.pdf
06_sort_names.pdf06_sort_names.pdf
06_sort_names.pdf
 
05_fragments.pdf
05_fragments.pdf05_fragments.pdf
05_fragments.pdf
 
04_magic_square.pdf
04_magic_square.pdf04_magic_square.pdf
04_magic_square.pdf
 
03_endianess.pdf
03_endianess.pdf03_endianess.pdf
03_endianess.pdf
 
02_variance.pdf
02_variance.pdf02_variance.pdf
02_variance.pdf
 
01_memory_manager.pdf
01_memory_manager.pdf01_memory_manager.pdf
01_memory_manager.pdf
 
09_nrps.pdf
09_nrps.pdf09_nrps.pdf
09_nrps.pdf
 
11_pangram.pdf
11_pangram.pdf11_pangram.pdf
11_pangram.pdf
 
10_combinations.pdf
10_combinations.pdf10_combinations.pdf
10_combinations.pdf
 
08_squeeze.pdf
08_squeeze.pdf08_squeeze.pdf
08_squeeze.pdf
 
07_strtok.pdf
07_strtok.pdf07_strtok.pdf
07_strtok.pdf
 
06_reverserec.pdf
06_reverserec.pdf06_reverserec.pdf
06_reverserec.pdf
 
05_reverseiter.pdf
05_reverseiter.pdf05_reverseiter.pdf
05_reverseiter.pdf
 

Kürzlich hochgeladen

Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 

Kürzlich hochgeladen (20)

Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 

Linux Internals - Part III

  • 3. Threads ● Threads, like processes, are a mechanism to allow a program to do more than one thing at a time ● As with processes, threads appear to run concurrently ● The Linux kernel schedules them asynchronously, interrupting each thread from time to time to give others a chance to execute ● Threads are a finer-grained unit of execution than processes ● That thread can create additional threads; all these threads run the same program in the same process ● But each thread may be executing a different part of the program at any given time
  • 4. Threads Single and Multi threaded Process Threads are similar to handling multiple functions in parallel. Since they share same code & data segments, care to be taken by programmer to avoid issues. Single Threaded Process code data files registers stack thread Multi Threaded Process code data files registers stack thread registers stack thread registers stack thread
  • 5. Threads Advantages ● Takes less time to create a new thread in an existing process than to create a brand new process ● Switching between threads is faster than a normal context switch ● Threads enhance efficiency in communication between different executing programs ● No kernel involved
  • 6. Threads pthread API's ● GNU/Linux implements the POSIX standard thread API (known as pthreads) ● All thread functions and data types are declared in the header file <pthread.h> ● The pthread functions are not included in the standard C library ● Instead, they are in libpthread, so you should add -lpthread to the command line when you link your program Using libpthread is a very good example to understand differences between functions, library functions and system calls
  • 7. Threads Creation ● The pthread_create function creates a new thread Function Meaning int pthread_create( pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg)  A pointer to a pthread_t variable, in which the thread ID of the new thread is stored  A pointer to a thread attribute object. If you pass NULL as the thread attribute, a thread will be created with the default thread attributes  A pointer to the thread function. This is an ordinary function pointer, of this type: void* (*) (void*)  A thread argument value of type void *. Whatever you pass is simply passed as the argument to the thread function when thread begins executing
  • 8. Threads Creation ● A call to pthread_create returns immediately, and the original thread continues executing the instructions following the call ● Meanwhile, the new thread begins executing the thread function ● Linux schedules both threads asynchronously ● Programs must not rely on the relative order in which instructions are executed in the two threads
  • 9. Threads Compilation ● Use the following command to compile the programs using thread libraries $ gcc -o <output_file> <input_file.c> -lpthread
  • 10. Threads Joining ● It is quite possible that output created by a thread needs to be integrated for creating final result ● So the main program may need to wait for threads to complete actions ● The pthread_join() function helps to achieve this purpose Function Meaning int pthread_join( pthread_t thread, void **value_ptr)  Thread ID of the thread to wait  Pointer to a void* variable that will receive thread finished value  If you don’t care about the thread return value, pass NULL as the second argument.
  • 11. Threads Passing Data ● The thread argument provides a convenient method of passing data to threads ● Because the type of the argument is void*, though, you can’t pass a lot of data directly via the argument ● Instead, use the thread argument to pass a pointer to some structure or array of data ● Define a structure for each thread function, which contains the “parameters” that the thread function expects ● Using the thread argument, it’s easy to reuse the same thread function for many threads. All these threads execute the same code, but on different data
  • 12. Threads Return Values ● If the second argument you pass to pthread_join is non- null, the thread’s return value will be placed in the location pointed to by that argument ● The thread return value, like the thread argument, is of type void* ● If you want to pass back a single int or other small number, you can do this easily by casting the value to void* and then casting back to the appropriate type after calling pthread_join
  • 13. Threads Attributes ● Thread attributes provide a mechanism for fine-tuning the behaviour of individual threads ● Recall that pthread_create accepts an argument that is a pointer to a thread attribute object ● If you pass a null pointer, the default thread attributes are used to configure the new thread ● However, you may create and customize a thread attribute object to specify other values for the attributes
  • 14. Threads Joinable and Detached ● A thread may be created as a joinable thread (the default) or as a detached thread ● A joinable thread, like a process, is not automatically cleaned up by GNU/Linux when it terminates ● Thread’s exit state hangs around in the system (kind of like a zombie process) until another thread calls pthread_join to obtain its return value. Only then are its resources released ● A detached thread, in contrast, is cleaned up automatically when it terminates ● Because a detached thread is immediately cleaned up, another thread may not synchronize on its completion by using pthread_join or obtain its return value
  • 16. Synchronization why? ● Programming with threads is very tricky because most threaded programs are concurrent programs ● In particular, there’s no way to know when the system will schedule one thread to run and when it will run another ● One thread might run for a very long time, or the system might switch among threads very quickly ● Debugging a threaded program is difficult because you cannot always easily reproduce the behavior that caused the problem. ● You might run the program once and have everything work fine; the next time you run it, it might crash. ● There’s no way to make the system schedule the threads exactly the same way it did before.
  • 17. Synchronization Race Condition ● The ultimate cause of most bugs involving threads is that the threads are accessing the same data. ● So the powerful aspects of threads can become a danger. ● If one thread is only partway through updating a data structure when another thread accesses the same data structure, it’s a problem. ● These bugs are called race conditions; the threads are racing one another to change the same data structure.
  • 18. Synchronization Race Condition – The Problem ● Now suppose that two threads happen to finish a job at about the same time, but only one job remains in the queue. ● The first thread checks whether job_queue is null; finding that it isn’t, the thread enters the loop and stores the pointer to the job object in next_job. ● At this point, Linux happens to interrupt the first thread and schedules the second. ● The second thread also checks job_queue and finding it non- null, also assigns the same job pointer to next_job. ● By unfortunate coincidence, we now have two threads executing the same job.
  • 19. Synchronization Race Condition – The Solution ● To eliminate race conditions, you need a way to make operations atomic. ● An atomic operation is indivisible and uninterruptible; once the operation starts, it will not be paused or interrupted until it completes, and no other operation will take place mean while. ● In this particular example, you want to check job_queue; if it’s not empty, remove the first job, all as a single atomic operation.
  • 20. Synchronization Mutexes ● The solution to the job queue race condition problem is to let only one thread access the queue of jobs at a time. ● GNU/Linux provides mutexes, short for MUTual EXclusion locks. ● A mutex is a special lock that only one thread may lock at a time. ● If a thread locks a mutex and then a second thread also tries to lock the same mutex, the second thread is blocked, or put on hold. ● Only when the first thread unlocks the mutex is the second thread unblocked—allowed to resume execution.
  • 21. Synchronization Mutex - Creation ● To create a mutex, create a variable of type pthread_mutex_t and pass a pointer to it to pthread_mutex_init. ● The second argument to pthread_mutex_init is a pointer to a mutex attribute object, which specifies attributes of the mutex.
  • 22. Synchronization Mutex – Locking & Blocking ● A thread may attempt to lock a mutex by calling pthread_mutex_lock on it. ● If the mutex was unlocked, it becomes locked and the function returns immediately. ● If the mutex was locked by another thread, pthread_mutex_lock blocks execution and returns only eventually when the mutex is unlocked by the other thread. ● More than one thread may be blocked on a locked mutex at one time. ● When the mutex is unlocked, only one of the blocked threads is unblocked and allowed to lock the mutex; the other threads stay blocked.
  • 23. Synchronization Mutex – Unlocking ● A call to pthread_mutex_unlock unlocks a mutex. ● This function should always be called from the same thread that locked the mutex.
  • 24. Synchronization Semaphores ● A semaphore is a counter that can be used to synchronize multiple threads. ● As with a mutex, GNU/Linux guarantees that checking or modifying the value of a semaphore can be done safely, without creating a race condition. ● Each semaphore has a counter value, which is a non-negative integer.
  • 25. Synchronization Semaphores - Operations ● A wait operation decrements the value of the semaphore by 1. If the value is already zero, the operation blocks until the value of the semaphore becomes positive (due to the action of some other thread). When the semaphore’s value becomes positive, it is decremented by 1 and the wait operation returns. ● A post operation increments the value of the semaphore by 1. If the semaphore was previously zero and other threads are blocked in a wait operation on that semaphore, one of those threads is unblocked and its wait operation completes (which brings the semaphore’s value back to zero).
  • 26. Synchronization Semaphores - Variable ● A semaphore is represented by a sem_t variable. ● Before using it, you must initialize it using the sem_init function, passing a pointer to the sem_t variable. ● The second parameter should be zero, and the third parameter is the semaphore’s initial value. ● If you no longer need a semaphore, it’s good to de- allocate it with sem_destroy.
  • 27. Synchronization Semaphores – Wait & Post ● To wait on a semaphore, use sem_wait. ● To post to a semaphore, use sem_post.
  • 29. Signals ● Signals are used to notify a process of a particular event ● Signals make the process aware that something has happened in the system ● Target process should perform some pre-defined actions to handle signals ● This is called ‘signal handling’ ● Actions may range from 'self termination' to 'clean-up'
  • 31. Signals Identification Each signal in Linux has got a name starting with 'SIG' and a number associated with it. For example Signal 'SIGSEGV' has got a number 11. This is defined in /usr/include/bits/signum.h * * The path might vary on different distributions
  • 32. Signals Origins ● The kernel sends signals to processes in response to specific conditions. For instance, any of these Signals may be sent to a process that attempts to perform an illegal operation : – SIGBUS (bus error), – SIGSEGV (segmentation violation), ● A Process may also send a Signal to another Process. ● A Process may also send a Signal to itself
  • 33. Signals Handling ● When a process receives a signal, it processes ● Immediate handling ● For all possible signals, the system defines a default disposition or action to take when a signal occurs ● There are four possible default dispositions: – Exit: Forces process to exit – Core: Forces process to exit and create a core file – Stop: Stops the process – Ignore: Ignores the signal ● Handling can be done, called ‘signal handling’
  • 34. Signals Set Behaviour ● The sigaction function can be used to set a signal disposition whose prototype is: int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact); ● The first parameter is the signal number. ● The next two parameters are pointers to sigaction structures ● The first of these contains the desired disposition for that signal number, while the second receives the previous disposition
  • 35. Signals Note ● A signal handler should perform the minimum work necessary to respond to the signal, and then return control to the main program (or terminate the program). ● In most cases, this consists simply of recording the fact that a signal occurred. ● The main program then checks periodically whether a signal has occurred and reacts accordingly.
  • 36. Signals vs Interrupt ● Signals can be described as soft-interrupts ● The concept of 'signals' and 'signals handling' is analogous to that of the 'interrupt' handling done by a microprocessor ● When a signal is sent to a process or thread, a signal handler may be entered (depending on the current disposition of the signal), which is similar to the system entering an interrupt handler as the result of receiving an interrupt.
  • 37. Signals Process Termination ● Normally, a process terminates in one of two ways. Either the executing program calls the exit function, or the program’s main function returns. ● Each process has an exit code: a number that the process returns to its parent. The exit code is the argument passed to the exit function, or the value returned from main. ● A process may also terminate abnormally, in response to a signal. For instance, the SIGBUS, SIGSEGV, and SIGFPE signals mentioned previously cause the process to terminate.
  • 38. Stay connected About us: Emertxe is India’s one of the top IT finishing schools & self learning kits provider. Our primary focus is on Embedded with diversification focus on Java, Oracle and Android areas Branch Office: Corporate Headquarters: Emertxe Information Technologies, Emertxe Information Technologies, No-1, 9th Cross, 5th Main, 83, Farah Towers, 1st Floor, Jayamahal Extension, MG Road, Bangalore, Karnataka 560046 Bangalore, Karnataka - 560001 T: +91 809 555 7333 (M), +91 80 41289576 (L) E: training@emertxe.com https://www.facebook.com/Emertxe https://twitter.com/EmertxeTweet https://www.slideshare.net/EmertxeSlides