SlideShare a Scribd company logo
1 of 67
Download to read offline
1
Threads Programming in Linux
2
What is a Process ?
• Fundamental to almost
all operating systems
• = program in execution
• Seperate address space
• program counter, stack
pointer, hardware
registers
3
What is a Process ?
• Processes contain lots of information:
• Process ID, process group ID, user ID, and group ID
• Environment
• Working Directory
• Program Instructions
• Registers
• Stack
• Heap
• File Descriptors
• Signal Actions
• Shared Libraries
• Inter-process communication tools (such as message queues, pipes,
semaphores, or shared memory)
4
What is a Process ?
• OS alternate between processes
– run process on CPU
– clock interrupt happens
– save process state
• registers (PC, SP, numeric)
• memory map (address space)
• memory (core image)  possibly swapped to disk
•  process table (PCB)
– continue some other process
Context Switch
Scheduling
5
Remember Unix process creation: fork()
#include <sys/types.h>
#include <unistd.h>
int v = 42;
int main (void)
{
pid_t pid;
if ((pid = fork()) < 0) {
perror("fork");
exit(1);
} else if (pid == 0) {
printf("child %d of parent %dn", getpid(), getppid());
v++;
printf(“child:%d n”,v);
}
else {
sleep(10);
printf(“child:%d n”,v);
}
return 0;
}
Global variable
Child process executes here
Parent process executes here
Parent process forked child process here
6
fork()
fork()
Process A
Global
Variables
Code
Stack
Process B
Global
Variables
Code
Stack
Creation of a new process using fork is expensive (time
& memory).
7
Threads
• A thread (sometimes called a lightweight process) does
not require lots of memory or startup time.
• We can see processes as unit of allocation. A process
groups resources together.
•Resources, privileges, open files, allocated memory...
• We can see threads as unit of execution. They execute
on allocated resources.
•PC, SP, registers...
• Each process may include many threads
• Each thread belongs to one process ( executes inside a
process )
8
Threads
9
Thread-Specific Resources
Each thread has it’s own hardware
execution state:
 Thread ID (integer)
 Stack (SP), Registers,
Program Counter (PC)
Each thread share following with its
process
 Same code and data
(address space)
 The same privilege
 The same resources
 Global variables
 Open files
 Signals and signal handlers ...
Threads share the same memory (address space) together with their process
but have separate execution context.
10
Thread Creation
Process A
Thread 1
Global
Variables
Code
Stack
Process A
Thread 2
Stack
thread_create()
Each thread has its own stack to store its local variables.
11
Multiple processes vs Multiple threads
• Multiple Threads
– Creation
• Much faster and easy
– Context switching
• Much faster and easy
– Inter-thread Communication
• Much faster and easy
– Protection
• Threads are not protected
from each other
12
Why use threads?
• Because threads have minimal internal state,
it takes less time to create a thread than a
process (10x speedup in UNIX).
• It takes less time to terminate a thread.
• It takes less time to switch to a different
thread.
• A multi-threaded process is much cheaper
than multiple (redundant) processes.
13
Examples of Using Threads
• Threads are useful for any application with multiple
tasks that can be run with separate threads of control.
• A Word processor may have separate threads for:
– User input
– Spell and grammar check
– displaying graphics
– document layout
• A web server may spawn a thread for each client
– Can serve clients concurrently with multiple threads.
– It takes less overhead to use multiple threads than to
use multiple processes.
14
Examples of multithreaded programs
• Most modern OS kernels
– Internally concurrent because have to deal with
concurrent requests by multiple users
– But no protection needed within kernel
• Database Servers
– Access to shared data by many concurrent users
– Also background utility processing must be done
• Parallel Programming (More than one physical CPU)
– Split program into multiple threads for parallelism. This
is called Multiprocessing
15
What is a POSIX thread?
• An IEEE standardized way of using
threads on a given system.
• Systems that support POSIX threads
include:
– Unix
– Linux
– Mac OS X
– Windows (through Cygwin, etc)
16
POSIX Threads API
int pthread_create (pthread_t *tid, pthread_attr_t
*attr,void *(start_routine)(void *),void *arg);
void pthread_exit(void *retval);
int pthread_join (pthread_t tid, void **thread_return);
#include <pthread.h>
Linux - you need to link with “-lpthread” library
gcc -lpthread main.c -o main.out
C/C++ programs
pthread_t pthread_self ();
Some Posix Thread API functions:
17
Creating a New Thread
int pthread_create(pthread_t *thread, pthread_attr_t *attr,
void *(*start_routine)(void *), void *arg);
the thread handle gets stored here
returns a 0 if successful
otherwise an errno!
thread attributes,
usually NULL.
the function to begin
executing on this thread.
It must take a void * as an
argument and return a void *.
When the function returns, the
thread exits.
argument given to
the thread function.
usually a pointer
to a struct or an
array
18
Getting Current Thread Handle (ID)
Occasionally, pthread_self useful for a sequence of code to
determine which thread is running it.
pthread_t pthread_self ( );
returns thread handle of
the caller thread.
19
Terminating a Thread
this must point to data
that exists after the thread
terminates!
A thread exits when the thread function returns. Alternatively, the
thread can call the pthread_exit function.
void pthread_exit(void *retval);
20
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_THREADS 5
void *HelloWorld(void *threadid)
{
int tid;
tid = (int)threadid;
printf("Hello World! It's me, thread #%d!n", tid);
pthread_exit(0);
}
int main(int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
int i;
for(i=0;i<NUM_THREADS;i++)
{
pthread_create(&threads[i], 0, HelloWorld, (void *)i);
}
pthread_exit(0);
return 0;
}
Start of thread
execution
Thread handles
Address of start
function Argument to the function
Handle of the created thread
Cast (void *) to integer
(since void * is a pointer and
pointer is nothing other than
32 bit integer value.)
21
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_THREADS 8
char *messages[NUM_THREADS];
void *HelloWorld(void *threadid)
{
int id;
sleep(1);
id = (int) threadid;
printf("Thread %d: %sn", id, messages[id]);
pthread_exit(0);
}
int main(int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
int i;
messages[0] = "English: Hello World!";
messages[1] = "French: Bonjour, le monde!";
messages[2] = "Spanish: Hola al mundo";
messages[3] = "Klingon: Nuq neH!";
messages[4] = "German: Guten Tag, Welt!";
messages[5] = "Russian: Zdravstvytye, mir!";
messages[6] = "Japan: Sekai e konnichiwa!";
messages[7] = "Latin: Orbis, te saluto!";
for(i=0;i<NUM_THREADS;i++)
{
pthread_create(&threads[i], NULL,
HelloWorld, (void *)i);
}
pthread_exit(0);
return 0;
}
Global data
Fill global data
Create threads
Access and print global data
22
Passing Data to Threads
The thread argument is of type void* . This allows us to pass a lot of
data to a thread by passing a pointer to a struct or an array of data with
casting it to the void * pointer.
struct data_t
{
int thread_id;
int x;
};
...
struct data_t thread_data;
thread_data.thread_id = 0;
thread_data.x = 1;
...
int pthread_create(pthread_t *thread, pthread_attr_t *attr,
void *(*start_routine)(void *), void *arg);
pthread_create(th,NULL,start, (void *) &thread_data);
23
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_THREADS 8
char *messages[NUM_THREADS];
struct thread_data
{
int thread_id;
int sum;
char *message;
};
struct thread_data thread_data_array[NUM_THREADS];
Structure for passing data to
the thread
Global array that holds arguments to the
threads
24
int main(int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
int i,sum= 0;
messages[0] = "English: Hello World!";
messages[1] = "French: Bonjour, le monde!";
messages[2] = "Spanish: Hola al mundo";
messages[3] = "Klingon: Nuq neH!";
messages[4] = "German: Guten Tag, Welt!";
messages[5] = "Russian: Zdravstvytye, mir!";
messages[6] = "Japan: Sekai e konnichiwa!";
messages[7] = "Latin: Orbis, te saluto!";
for(i=0;i<NUM_THREADS;i++)
{
sum = sum + i;
thread_data_array[i].thread_id = i;
thread_data_array[i].sum = sum;
thread_data_array[i].message = messages[i];
pthread_create(&threads[i], NULL, HelloWorld, (void *)&thread_data_array[i]);
}
pthread_exit(0);
}
Fill data structure to pass to
the thread function ( fill ith
element of the global array)
Pass the ith element of the
global array to the thread
start function
25
Waiting for a Thread
the thread to wait for
pointer to the return value.
NULL if there is nothing to return.
0 on success
or error code.
int pthread_join (pthread_t tid, void **thread_return);
26
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_THREADS 3
void *BusyWork(void *null)
{
int i;
double result=0.0;
for (i=0; i<1000000; i++)
{
result = result + (double)random();
}
printf("Thread result = %en",result);
pthread_exit(0);
}
int main(int argc, char *argv[])
{
pthread_t thread[NUM_THREADS];
pthread_attr_t attr;
int i;
for(i=0;i<NUM_THREADS;i++)
{
pthread_create(&thread[i],0, BusyWork, NULL);
}
for(i=0;i<NUM_THREADS;i++)
{
pthread_join(thread[i],0);
}
printf("Completed join with thread %dn",i);
pthread_exit(0);
}
Create threads
Main thread is waiting other
threads to finish
All created threads
finished here
27
• Purpose:
Forces the calling thread to relinquish use of its processor
• NOTE: Also is common to Use sched_yield, from unistd.h, instead.
• Library:
Threads Library (libpthreads.a)
• Syntax:
#include <pthread.h>
void pthread_yield ()
• Description:
The pthread_yield subroutine forces the calling thread to relinquish use of
its processor, and to wait in the run queue before it is scheduled again
If the run queue is empty when the pthread_yield subroutine is called, the
calling thread is immediately rescheduled.
pthread_yield Subroutine
28
Shared Global Variables
int counter=0;
....
void *start(void *arg) {
counter++;
printf("Thread %u is number %dn", pthread_self(),counter);
}
main() {
int i; pthread_t tid;
for (i=0;i<10;i++)
pthread_create(&tid,NULL,start,NULL);
}
Threads share all global variables
Sharing global variables is dangerous - two threads may attempt to
modify the same variable at the same time.
Threads must be synchronized to avoid problems on shared global
variables.
29
A synchronization variable that is used to synchronize
access on shared memory regions. It has 2 blocking
operations on it:
Lock(mutex): an atomic operation that locks mutex.
If the mutex is already locked, it blocks the caller
thread.
Unlock(mutex): an atomic operation that unlocks
mutex. If there are any waiting threads, this
functions unblocks one of them.
Permit only one thread to execute a section of the code
that access shared variables (shared memory regions :
critical sections)
The general idea is to lock it before accessing global
variables and to unlock as soon as we are done.
Mutex
30
Mutex
A1 statement
A2 mutex.wait()
A3 wolski.balance =
wolski balance - 200
A4 mutex.signal()
A5 statement
mutex = 1
B1 statement
B2 mutex.wait()
B3 wolski.balance =
wolski balance - 200
B4 mutex.signal()
B5 statement
balance = 1000
31
Mutex
A1 statement
A2 mutex.wait()
A3 wolski.balance =
wolski balance - 200
A4 mutex.signal()
A5 statement
mutex = 1
B1 statement
B2 mutex.wait()
B3 wolski.balance =
wolski balance - 200
B4 mutex.signal()
B5 statement
balance = 1000
32
Mutex
A1 statement
A2 mutex.wait()
A3 wolski.balance =
wolski balance - 200
A4 mutex.signal()
A5 statement
mutex = 0
B1 statement
B2 mutex.wait()
B3 wolski.balance =
wolski balance - 200
B4 mutex.signal()
B5 statement
balance = 1000
33
Mutex
A1 statement
A2 mutex.wait()
A3 wolski.balance =
wolski balance - 200
A4 mutex.signal()
A5 statement
mutex = 0
B1 statement
B2 mutex.wait()
B3 wolski.balance =
wolski balance - 200
B4 mutex.signal()
B5 statement
balance = 800
34
Mutex
A1 statement
A2 mutex.wait()
A3 wolski.balance =
wolski balance - 200
A4 mutex.signal()
A5 statement
mutex = 0
B1 statement
B2 mutex.wait()
B3 wolski.balance =
wolski balance - 200
B4 mutex.signal()
B5 statement
balance = 800
35
Mutex
A1 statement
A2 mutex.wait()
A3 wolski.balance =
wolski balance - 200
A4 mutex.signal()
A5 statement
mutex = -1
B1 statement
B2 mutex.wait()
B3 wolski.balance =
wolski balance - 200
B4 mutex.signal()
B5 statement
balance = 800
36
Mutex
A1 statement
A2 mutex.wait()
A3 wolski.balance =
wolski balance - 200
A4 mutex.signal()
A5 statement
mutex = -1
B1 statement
B2 mutex.wait()
B3 wolski.balance =
wolski balance - 200
B4 mutex.signal()
B5 statement
balance = 800
37
Mutex
A1 statement
A2 mutex.wait()
A3 wolski.balance =
wolski balance - 200
A4 mutex.signal()
A5 statement
mutex = 0
B1 statement
B2 mutex.wait()
B3 wolski.balance =
wolski balance - 200
B4 mutex.signal()
B5 statement
balance = 800
38
Mutex
A1 statement
A2 mutex.wait()
A3 wolski.balance =
wolski balance - 200
A4 mutex.signal()
A5 statement
mutex = 0
B1 statement
B2 mutex.wait()
B3 wolski.balance =
wolski balance - 200
B4 mutex.signal()
B5 statement
balance = 800
39
Mutex
A1 statement
A2 mutex.wait()
A3 wolski.balance =
wolski balance - 200
A4 mutex.signal()
A5 statement
mutex = 0
B1 statement
B2 mutex.wait()
B3 wolski.balance =
wolski balance - 200
B4 mutex.signal()
B5 statement
balance = 800
40
Mutex
A1 statement
A2 mutex.wait()
A3 wolski.balance =
wolski balance - 200
A4 mutex.signal()
A5 statement
mutex = 0
B1 statement
B2 mutex.wait()
B3 wolski.balance =
wolski balance - 200
B4 mutex.signal()
B5 statement
balance = 600
41
Mutex
A1 statement
A2 mutex.wait()
A3 wolski.balance =
wolski balance - 200
A4 mutex.signal()
A5 statement
mutex = 1
B1 statement
B2 mutex.wait()
B3 wolski.balance =
wolski balance - 200
B4 mutex.signal()
B5 statement
balance = 600
42
Mutex
A1 statement
A2 mutex.wait()
A3 wolski.balance =
wolski balance - 200
A4 mutex.signal()
A5 statement
mutex = 1
B1 statement
B2 mutex.wait()
B3 wolski.balance =
wolski balance - 200
B4 mutex.signal()
B5 statement
balance = 600
43
• pthreads includes support for Mutual Exclusion
primitives that can be used to synchronize threads.
Pthread Mutex API
int pthread_mutex_init(pthread_mutex_t *mutex,
const pthread_mutexattr_t *mutexattr);
int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_unlock(pthread_mutex_t *mutex);
int pthread_mutex_destroy(pthread_mutex_t *mutex);
Posix Mutex API functions:
44
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
pthread_mutex_t mutex;
int x = 0;
void *MyThread(void *arg)
{
char *sbName;
sbName = (char *)arg
pthread_mutex_lock(&mutex);
x = x + 1;
pthread_mutex_unlock(&mutex);
printf("X = %d in Thread %sn", x, sbName);
}
int main()
{
pthread_t idA, idB;
if (pthread_mutex_init(&mutex, NULL) < 0)
{
perror("pthread_mutex_init");
exit(1);
}
if (pthread_create(&idA, NULL, MyThread, (void *)"A") != 0)
{
perror("pthread_create");
exit(1);
}
if (pthread_create(&idB, NULL, MyThread, (void *)"B") != 0)
{
perror("pthread_create");
exit(1);
}
pthread_join(idA, NULL);
pthread_join(idB, NULL);
pthread_mutex_destroy(&mutex);
}
Global mutex variable that
will be used for
synchronization
Initialize mutex before
using it
Destroy mutex after using
it
Lock mutex before
changing global variable.
This will block the caller
thread if the mutex is
locked.
Unlock mutex after
changing global variable
Shared global variable
45
A synchronization variable that takes on positive integer
values. It has 2 blocking operations on it:
Wait(semaphore): an atomic operation that waits for
semaphore to become positive, then decrements it
by 1. (blocks caller if semaphore is negative)
Signal(semaphore): an atomic operation that
increments semaphore by 1. (if the value of the
semafore is negative, this function unblocks one of
the waiting threads)
Permit a limited number of threads to execute a section
of the code
Similar to mutexes
Generally used for signalling existence of an event or
action
Semaphores
46
Signalling
.
.
.
A1 statement
A2 sem.signal()
.
.
.
.
.
.
B1 sem.wait()
B2 statement
.
.
.
sem = 0
47
Signalling
.
.
.
A1 statement
A2 sem.signal()
.
.
.
.
.
.
B1 sem.wait()
B2 statement
.
.
.
sem = 0
48
Signalling
.
.
.
A1 statement
A2 sem.signal()
.
.
.
.
.
.
B1 sem.wait()
B2 statement
.
.
.
sem = 0
49
Signalling
.
.
.
A1 statement
A2 sem.signal()
.
.
.
.
.
.
B1 sem.wait()
B2 statement
.
.
.
sem = 0
50
Signalling
.
.
.
A1 statement
A2 sem.signal()
.
.
.
.
.
.
B1 sem.wait()
B2 statement
.
.
.
sem = 0
51
Signalling
.
.
.
A1 statement
A2 sem.signal()
.
.
.
.
.
.
B1 sem.wait()
B2 statement
.
.
.
sem = -1
52
Signalling
.
.
.
A1 statement
A2 sem.signal()
.
.
.
.
.
.
B1 sem.wait()
B2 statement
.
.
.
sem = -1
53
Signalling
.
.
.
A1 statement
A2 sem.signal()
.
.
.
.
.
.
B1 sem.wait()
B2 statement
.
.
.
sem = -1
54
Signalling
.
.
.
A1 statement
A2 sem.signal()
.
.
.
.
.
.
B1 sem.wait()
B2 statement
.
.
.
sem = 0
55
Signalling
.
.
.
A1 statement
A2 sem.signal()
.
.
.
.
.
.
B1 sem.wait()
B2 statement
.
.
.
sem = 0
56
Signalling
.
.
.
A1 statement
A2 sem.signal()
.
.
.
.
.
.
B1 sem.wait()
B2 statement
.
.
.
sem = 0
57
Signalling
.
.
.
A1 statement
A2 sem.signal()
.
.
.
.
.
.
B1 sem.wait()
B2 statement
.
.
.
sem = 0
58
Signalling
.
.
.
A1 statement
A2 sem.signal()
.
.
.
.
.
.
B1 sem.wait()
B2 statement
.
.
.
sem = 0
59
Signalling
.
.
.
A1 statement
A2 sem.signal()
.
.
.
.
.
.
B1 sem.wait()
B2 statement
.
.
.
sem = 0
60
Signalling
.
.
.
A1 statement
A2 sem.signal()
.
.
.
.
.
.
B1 sem.wait()
B2 statement
.
.
.
sem = 0
61
Signalling
.
.
.
A1 statement
A2 sem.signal()
.
.
.
.
.
.
B1 sem.wait()
B2 statement
.
.
.
sem = 0
62
#include < semaphore.h> C/C++ programs
int sem_init(sem_t *sem, int pshared, unsigned int value);
int sem_post(sem_t *sem);
int sem_wait(sem_t *sem);
int sem_destroy(sem_t *sem);
Semaphore API functions:
Semaphore object Generally 0 Gives an initial value to
the semaphore
atomically increases the value of a semaphore by 1
atomically decreases the value of a semaphore by 1
Frees resources allocated for semaphore
63
#include <pthread.h>
#include <semaphore.h>
void *thread_function( void *arg );
sem_t semaphore;
int main()
{
int tmp;
tmp = sem_init( &semaphore, 0, 0 );
pthread_create( &thread[i], NULL, thread_function, NULL );
while ( still_has_something_to_do() )
{
sem_post( &semaphore );
}
pthread_join( thread[i], NULL );
sem_destroy( &semaphore );
return 0;
}
void *thread_function( void *arg )
{
sem_wait( &semaphore );
...
pthread_exit( NULL );
}
Global semaphore variable
initialize semaphore
signal semaphore
wait semaphore
destroy semaphore
64
#include <stdio.h> /* Input/Output */
#include <stdlib.h> /* General Utilities */
#include <pthread.h> /* POSIX Threads */
#include <semaphore.h> /* Semaphore */
#define BUFFER_SIZE 5
int headPos = BUFFER_SIZE - 1;
int endPos = 0;
int Buffer[BUFFER_SIZE];
sem_t producerSem;
sem_t consumerSem;
pthread_mutex_t mutex;
int main()
{
int i[2];
pthread_t thread_a;
pthread_t thread_b;
pthread_mutex_init(&mutex, NULL);
pthread_create (&thread_a, 0, producer, (void *)0);
pthread_create (&thread_b, 0, consumer, (void *)0);
pthread_join(thread_a, NULL);
pthread_join(thread_b, NULL);
/* exit */
exit(0);
}
Global buffer to store
produced values
Producer semaphore
used for the notification
from consumer thread
Consumer semaphore
used for the notification
from producer thread
Mutex that will be used
to access shared Buffer
İnit mutex that will be
used to access shared
Buffer
65
void *producer(void *arg)
{
int itemCount = 10;
sem_init(&producerSem, 1,BUFFER_SIZE);
while(itemCount)
{
sem_wait(&producerSem);
pthread_mutex_lock(&mutex);
Buffer[endPos] = 1;
printf("Producer Thread : Buffer[%d] = 1 n",endPos);
endPos = (endPos + 1) % BUFFER_SIZE;
itemCount--;
pthread_mutex_unlock(&mutex);
sem_post(&consumerSem);
}
pthread_exit(0);
return 0;
}
İnit semaphore with BUFFER_SIZE
Wait notification from
consumer thread
Enter critical section
Access shared memory
and produce value
Exit critical section
Notify consumer thread
66
void *consumer(void *arg)
{
int itemCount = 10;
sem_init(&producerSem, 0,0);
while(itemCount)
{
sem_wait(&consumerSem);
pthread_mutex_lock(&mutex);
Buffer[headPos] = 0;
printf("Consumer Thread : Buffer[%d] = 0 n",headPos);
headPos = (headPos + 1) % BUFFER_SIZE;
itemCount--;
pthread_mutex_unlock(&mutex);
sem_post(&producerSem);
}
pthread_exit(0);
return 0;
}
İnit semaphore with 0
Wait notification from
consumer thread
Enter critical section
Access shared memory
and consume value
Exit critical section
Notify producer thread
67
References
• https://computing.llnl.gov/tutorials/pthreads/
• http://students.cs.byu.edu/~cs345ta/

More Related Content

Similar to posix.pdf

OpenHPI - Parallel Programming Concepts - Week 3
OpenHPI - Parallel Programming Concepts - Week 3OpenHPI - Parallel Programming Concepts - Week 3
OpenHPI - Parallel Programming Concepts - Week 3Peter Tröger
 
Who go Types in my Systems Programing!
Who go Types in my Systems Programing!Who go Types in my Systems Programing!
Who go Types in my Systems Programing!Jared Roesch
 
Basic Multithreading using Posix Threads
Basic Multithreading using Posix ThreadsBasic Multithreading using Posix Threads
Basic Multithreading using Posix ThreadsTushar B Kute
 
An Introduction to User Space Filesystem Development
An Introduction to User Space Filesystem DevelopmentAn Introduction to User Space Filesystem Development
An Introduction to User Space Filesystem DevelopmentMatt Turner
 
The Effect of Hierarchical Memory on the Design of Parallel Algorithms and th...
The Effect of Hierarchical Memory on the Design of Parallel Algorithms and th...The Effect of Hierarchical Memory on the Design of Parallel Algorithms and th...
The Effect of Hierarchical Memory on the Design of Parallel Algorithms and th...David Walker
 
MULTI-THREADING in python appalication.pptx
MULTI-THREADING in python appalication.pptxMULTI-THREADING in python appalication.pptx
MULTI-THREADING in python appalication.pptxSaiDhanushM
 
Process and Threads in Linux - PPT
Process and Threads in Linux - PPTProcess and Threads in Linux - PPT
Process and Threads in Linux - PPTQUONTRASOLUTIONS
 
Operating Systems 1 (7/12) - Threads
Operating Systems 1 (7/12) - ThreadsOperating Systems 1 (7/12) - Threads
Operating Systems 1 (7/12) - ThreadsPeter Tröger
 
002 - Introduction to CUDA Programming_1.ppt
002 - Introduction to CUDA Programming_1.ppt002 - Introduction to CUDA Programming_1.ppt
002 - Introduction to CUDA Programming_1.pptceyifo9332
 
Threads and multi threading
Threads and multi threadingThreads and multi threading
Threads and multi threadingAntonio Cesarano
 
Php and threads ZTS
Php and threads ZTSPhp and threads ZTS
Php and threads ZTSjulien pauli
 
Introto netthreads-090906214344-phpapp01
Introto netthreads-090906214344-phpapp01Introto netthreads-090906214344-phpapp01
Introto netthreads-090906214344-phpapp01Aravindharamanan S
 
Operating Systems 1 (5/12) - Architectures (Unix)
Operating Systems 1 (5/12) - Architectures (Unix)Operating Systems 1 (5/12) - Architectures (Unix)
Operating Systems 1 (5/12) - Architectures (Unix)Peter Tröger
 

Similar to posix.pdf (20)

SDAccel Design Contest: Xilinx SDAccel
SDAccel Design Contest: Xilinx SDAccel SDAccel Design Contest: Xilinx SDAccel
SDAccel Design Contest: Xilinx SDAccel
 
OpenHPI - Parallel Programming Concepts - Week 3
OpenHPI - Parallel Programming Concepts - Week 3OpenHPI - Parallel Programming Concepts - Week 3
OpenHPI - Parallel Programming Concepts - Week 3
 
Who go Types in my Systems Programing!
Who go Types in my Systems Programing!Who go Types in my Systems Programing!
Who go Types in my Systems Programing!
 
Basic Multithreading using Posix Threads
Basic Multithreading using Posix ThreadsBasic Multithreading using Posix Threads
Basic Multithreading using Posix Threads
 
Threading
ThreadingThreading
Threading
 
An Introduction to User Space Filesystem Development
An Introduction to User Space Filesystem DevelopmentAn Introduction to User Space Filesystem Development
An Introduction to User Space Filesystem Development
 
The Effect of Hierarchical Memory on the Design of Parallel Algorithms and th...
The Effect of Hierarchical Memory on the Design of Parallel Algorithms and th...The Effect of Hierarchical Memory on the Design of Parallel Algorithms and th...
The Effect of Hierarchical Memory on the Design of Parallel Algorithms and th...
 
MULTI-THREADING in python appalication.pptx
MULTI-THREADING in python appalication.pptxMULTI-THREADING in python appalication.pptx
MULTI-THREADING in python appalication.pptx
 
Process and Threads in Linux - PPT
Process and Threads in Linux - PPTProcess and Threads in Linux - PPT
Process and Threads in Linux - PPT
 
LSA2 - 02 Namespaces
LSA2 - 02  NamespacesLSA2 - 02  Namespaces
LSA2 - 02 Namespaces
 
Operating Systems 1 (7/12) - Threads
Operating Systems 1 (7/12) - ThreadsOperating Systems 1 (7/12) - Threads
Operating Systems 1 (7/12) - Threads
 
Chapter 6 os
Chapter 6 osChapter 6 os
Chapter 6 os
 
002 - Introduction to CUDA Programming_1.ppt
002 - Introduction to CUDA Programming_1.ppt002 - Introduction to CUDA Programming_1.ppt
002 - Introduction to CUDA Programming_1.ppt
 
Threads and multi threading
Threads and multi threadingThreads and multi threading
Threads and multi threading
 
Next .NET and C#
Next .NET and C#Next .NET and C#
Next .NET and C#
 
PDCCLECTUREE.pptx
PDCCLECTUREE.pptxPDCCLECTUREE.pptx
PDCCLECTUREE.pptx
 
Php and threads ZTS
Php and threads ZTSPhp and threads ZTS
Php and threads ZTS
 
Introto netthreads-090906214344-phpapp01
Introto netthreads-090906214344-phpapp01Introto netthreads-090906214344-phpapp01
Introto netthreads-090906214344-phpapp01
 
Thread
ThreadThread
Thread
 
Operating Systems 1 (5/12) - Architectures (Unix)
Operating Systems 1 (5/12) - Architectures (Unix)Operating Systems 1 (5/12) - Architectures (Unix)
Operating Systems 1 (5/12) - Architectures (Unix)
 

Recently uploaded

APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 

Recently uploaded (20)

APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 

posix.pdf

  • 2. 2 What is a Process ? • Fundamental to almost all operating systems • = program in execution • Seperate address space • program counter, stack pointer, hardware registers
  • 3. 3 What is a Process ? • Processes contain lots of information: • Process ID, process group ID, user ID, and group ID • Environment • Working Directory • Program Instructions • Registers • Stack • Heap • File Descriptors • Signal Actions • Shared Libraries • Inter-process communication tools (such as message queues, pipes, semaphores, or shared memory)
  • 4. 4 What is a Process ? • OS alternate between processes – run process on CPU – clock interrupt happens – save process state • registers (PC, SP, numeric) • memory map (address space) • memory (core image)  possibly swapped to disk •  process table (PCB) – continue some other process Context Switch Scheduling
  • 5. 5 Remember Unix process creation: fork() #include <sys/types.h> #include <unistd.h> int v = 42; int main (void) { pid_t pid; if ((pid = fork()) < 0) { perror("fork"); exit(1); } else if (pid == 0) { printf("child %d of parent %dn", getpid(), getppid()); v++; printf(“child:%d n”,v); } else { sleep(10); printf(“child:%d n”,v); } return 0; } Global variable Child process executes here Parent process executes here Parent process forked child process here
  • 7. 7 Threads • A thread (sometimes called a lightweight process) does not require lots of memory or startup time. • We can see processes as unit of allocation. A process groups resources together. •Resources, privileges, open files, allocated memory... • We can see threads as unit of execution. They execute on allocated resources. •PC, SP, registers... • Each process may include many threads • Each thread belongs to one process ( executes inside a process )
  • 9. 9 Thread-Specific Resources Each thread has it’s own hardware execution state:  Thread ID (integer)  Stack (SP), Registers, Program Counter (PC) Each thread share following with its process  Same code and data (address space)  The same privilege  The same resources  Global variables  Open files  Signals and signal handlers ... Threads share the same memory (address space) together with their process but have separate execution context.
  • 10. 10 Thread Creation Process A Thread 1 Global Variables Code Stack Process A Thread 2 Stack thread_create() Each thread has its own stack to store its local variables.
  • 11. 11 Multiple processes vs Multiple threads • Multiple Threads – Creation • Much faster and easy – Context switching • Much faster and easy – Inter-thread Communication • Much faster and easy – Protection • Threads are not protected from each other
  • 12. 12 Why use threads? • Because threads have minimal internal state, it takes less time to create a thread than a process (10x speedup in UNIX). • It takes less time to terminate a thread. • It takes less time to switch to a different thread. • A multi-threaded process is much cheaper than multiple (redundant) processes.
  • 13. 13 Examples of Using Threads • Threads are useful for any application with multiple tasks that can be run with separate threads of control. • A Word processor may have separate threads for: – User input – Spell and grammar check – displaying graphics – document layout • A web server may spawn a thread for each client – Can serve clients concurrently with multiple threads. – It takes less overhead to use multiple threads than to use multiple processes.
  • 14. 14 Examples of multithreaded programs • Most modern OS kernels – Internally concurrent because have to deal with concurrent requests by multiple users – But no protection needed within kernel • Database Servers – Access to shared data by many concurrent users – Also background utility processing must be done • Parallel Programming (More than one physical CPU) – Split program into multiple threads for parallelism. This is called Multiprocessing
  • 15. 15 What is a POSIX thread? • An IEEE standardized way of using threads on a given system. • Systems that support POSIX threads include: – Unix – Linux – Mac OS X – Windows (through Cygwin, etc)
  • 16. 16 POSIX Threads API int pthread_create (pthread_t *tid, pthread_attr_t *attr,void *(start_routine)(void *),void *arg); void pthread_exit(void *retval); int pthread_join (pthread_t tid, void **thread_return); #include <pthread.h> Linux - you need to link with “-lpthread” library gcc -lpthread main.c -o main.out C/C++ programs pthread_t pthread_self (); Some Posix Thread API functions:
  • 17. 17 Creating a New Thread int pthread_create(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *arg); the thread handle gets stored here returns a 0 if successful otherwise an errno! thread attributes, usually NULL. the function to begin executing on this thread. It must take a void * as an argument and return a void *. When the function returns, the thread exits. argument given to the thread function. usually a pointer to a struct or an array
  • 18. 18 Getting Current Thread Handle (ID) Occasionally, pthread_self useful for a sequence of code to determine which thread is running it. pthread_t pthread_self ( ); returns thread handle of the caller thread.
  • 19. 19 Terminating a Thread this must point to data that exists after the thread terminates! A thread exits when the thread function returns. Alternatively, the thread can call the pthread_exit function. void pthread_exit(void *retval);
  • 20. 20 #include <pthread.h> #include <stdio.h> #include <stdlib.h> #define NUM_THREADS 5 void *HelloWorld(void *threadid) { int tid; tid = (int)threadid; printf("Hello World! It's me, thread #%d!n", tid); pthread_exit(0); } int main(int argc, char *argv[]) { pthread_t threads[NUM_THREADS]; int i; for(i=0;i<NUM_THREADS;i++) { pthread_create(&threads[i], 0, HelloWorld, (void *)i); } pthread_exit(0); return 0; } Start of thread execution Thread handles Address of start function Argument to the function Handle of the created thread Cast (void *) to integer (since void * is a pointer and pointer is nothing other than 32 bit integer value.)
  • 21. 21 #include <pthread.h> #include <stdio.h> #include <stdlib.h> #define NUM_THREADS 8 char *messages[NUM_THREADS]; void *HelloWorld(void *threadid) { int id; sleep(1); id = (int) threadid; printf("Thread %d: %sn", id, messages[id]); pthread_exit(0); } int main(int argc, char *argv[]) { pthread_t threads[NUM_THREADS]; int i; messages[0] = "English: Hello World!"; messages[1] = "French: Bonjour, le monde!"; messages[2] = "Spanish: Hola al mundo"; messages[3] = "Klingon: Nuq neH!"; messages[4] = "German: Guten Tag, Welt!"; messages[5] = "Russian: Zdravstvytye, mir!"; messages[6] = "Japan: Sekai e konnichiwa!"; messages[7] = "Latin: Orbis, te saluto!"; for(i=0;i<NUM_THREADS;i++) { pthread_create(&threads[i], NULL, HelloWorld, (void *)i); } pthread_exit(0); return 0; } Global data Fill global data Create threads Access and print global data
  • 22. 22 Passing Data to Threads The thread argument is of type void* . This allows us to pass a lot of data to a thread by passing a pointer to a struct or an array of data with casting it to the void * pointer. struct data_t { int thread_id; int x; }; ... struct data_t thread_data; thread_data.thread_id = 0; thread_data.x = 1; ... int pthread_create(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *arg); pthread_create(th,NULL,start, (void *) &thread_data);
  • 23. 23 #include <pthread.h> #include <stdio.h> #include <stdlib.h> #define NUM_THREADS 8 char *messages[NUM_THREADS]; struct thread_data { int thread_id; int sum; char *message; }; struct thread_data thread_data_array[NUM_THREADS]; Structure for passing data to the thread Global array that holds arguments to the threads
  • 24. 24 int main(int argc, char *argv[]) { pthread_t threads[NUM_THREADS]; int i,sum= 0; messages[0] = "English: Hello World!"; messages[1] = "French: Bonjour, le monde!"; messages[2] = "Spanish: Hola al mundo"; messages[3] = "Klingon: Nuq neH!"; messages[4] = "German: Guten Tag, Welt!"; messages[5] = "Russian: Zdravstvytye, mir!"; messages[6] = "Japan: Sekai e konnichiwa!"; messages[7] = "Latin: Orbis, te saluto!"; for(i=0;i<NUM_THREADS;i++) { sum = sum + i; thread_data_array[i].thread_id = i; thread_data_array[i].sum = sum; thread_data_array[i].message = messages[i]; pthread_create(&threads[i], NULL, HelloWorld, (void *)&thread_data_array[i]); } pthread_exit(0); } Fill data structure to pass to the thread function ( fill ith element of the global array) Pass the ith element of the global array to the thread start function
  • 25. 25 Waiting for a Thread the thread to wait for pointer to the return value. NULL if there is nothing to return. 0 on success or error code. int pthread_join (pthread_t tid, void **thread_return);
  • 26. 26 #include <pthread.h> #include <stdio.h> #include <stdlib.h> #define NUM_THREADS 3 void *BusyWork(void *null) { int i; double result=0.0; for (i=0; i<1000000; i++) { result = result + (double)random(); } printf("Thread result = %en",result); pthread_exit(0); } int main(int argc, char *argv[]) { pthread_t thread[NUM_THREADS]; pthread_attr_t attr; int i; for(i=0;i<NUM_THREADS;i++) { pthread_create(&thread[i],0, BusyWork, NULL); } for(i=0;i<NUM_THREADS;i++) { pthread_join(thread[i],0); } printf("Completed join with thread %dn",i); pthread_exit(0); } Create threads Main thread is waiting other threads to finish All created threads finished here
  • 27. 27 • Purpose: Forces the calling thread to relinquish use of its processor • NOTE: Also is common to Use sched_yield, from unistd.h, instead. • Library: Threads Library (libpthreads.a) • Syntax: #include <pthread.h> void pthread_yield () • Description: The pthread_yield subroutine forces the calling thread to relinquish use of its processor, and to wait in the run queue before it is scheduled again If the run queue is empty when the pthread_yield subroutine is called, the calling thread is immediately rescheduled. pthread_yield Subroutine
  • 28. 28 Shared Global Variables int counter=0; .... void *start(void *arg) { counter++; printf("Thread %u is number %dn", pthread_self(),counter); } main() { int i; pthread_t tid; for (i=0;i<10;i++) pthread_create(&tid,NULL,start,NULL); } Threads share all global variables Sharing global variables is dangerous - two threads may attempt to modify the same variable at the same time. Threads must be synchronized to avoid problems on shared global variables.
  • 29. 29 A synchronization variable that is used to synchronize access on shared memory regions. It has 2 blocking operations on it: Lock(mutex): an atomic operation that locks mutex. If the mutex is already locked, it blocks the caller thread. Unlock(mutex): an atomic operation that unlocks mutex. If there are any waiting threads, this functions unblocks one of them. Permit only one thread to execute a section of the code that access shared variables (shared memory regions : critical sections) The general idea is to lock it before accessing global variables and to unlock as soon as we are done. Mutex
  • 30. 30 Mutex A1 statement A2 mutex.wait() A3 wolski.balance = wolski balance - 200 A4 mutex.signal() A5 statement mutex = 1 B1 statement B2 mutex.wait() B3 wolski.balance = wolski balance - 200 B4 mutex.signal() B5 statement balance = 1000
  • 31. 31 Mutex A1 statement A2 mutex.wait() A3 wolski.balance = wolski balance - 200 A4 mutex.signal() A5 statement mutex = 1 B1 statement B2 mutex.wait() B3 wolski.balance = wolski balance - 200 B4 mutex.signal() B5 statement balance = 1000
  • 32. 32 Mutex A1 statement A2 mutex.wait() A3 wolski.balance = wolski balance - 200 A4 mutex.signal() A5 statement mutex = 0 B1 statement B2 mutex.wait() B3 wolski.balance = wolski balance - 200 B4 mutex.signal() B5 statement balance = 1000
  • 33. 33 Mutex A1 statement A2 mutex.wait() A3 wolski.balance = wolski balance - 200 A4 mutex.signal() A5 statement mutex = 0 B1 statement B2 mutex.wait() B3 wolski.balance = wolski balance - 200 B4 mutex.signal() B5 statement balance = 800
  • 34. 34 Mutex A1 statement A2 mutex.wait() A3 wolski.balance = wolski balance - 200 A4 mutex.signal() A5 statement mutex = 0 B1 statement B2 mutex.wait() B3 wolski.balance = wolski balance - 200 B4 mutex.signal() B5 statement balance = 800
  • 35. 35 Mutex A1 statement A2 mutex.wait() A3 wolski.balance = wolski balance - 200 A4 mutex.signal() A5 statement mutex = -1 B1 statement B2 mutex.wait() B3 wolski.balance = wolski balance - 200 B4 mutex.signal() B5 statement balance = 800
  • 36. 36 Mutex A1 statement A2 mutex.wait() A3 wolski.balance = wolski balance - 200 A4 mutex.signal() A5 statement mutex = -1 B1 statement B2 mutex.wait() B3 wolski.balance = wolski balance - 200 B4 mutex.signal() B5 statement balance = 800
  • 37. 37 Mutex A1 statement A2 mutex.wait() A3 wolski.balance = wolski balance - 200 A4 mutex.signal() A5 statement mutex = 0 B1 statement B2 mutex.wait() B3 wolski.balance = wolski balance - 200 B4 mutex.signal() B5 statement balance = 800
  • 38. 38 Mutex A1 statement A2 mutex.wait() A3 wolski.balance = wolski balance - 200 A4 mutex.signal() A5 statement mutex = 0 B1 statement B2 mutex.wait() B3 wolski.balance = wolski balance - 200 B4 mutex.signal() B5 statement balance = 800
  • 39. 39 Mutex A1 statement A2 mutex.wait() A3 wolski.balance = wolski balance - 200 A4 mutex.signal() A5 statement mutex = 0 B1 statement B2 mutex.wait() B3 wolski.balance = wolski balance - 200 B4 mutex.signal() B5 statement balance = 800
  • 40. 40 Mutex A1 statement A2 mutex.wait() A3 wolski.balance = wolski balance - 200 A4 mutex.signal() A5 statement mutex = 0 B1 statement B2 mutex.wait() B3 wolski.balance = wolski balance - 200 B4 mutex.signal() B5 statement balance = 600
  • 41. 41 Mutex A1 statement A2 mutex.wait() A3 wolski.balance = wolski balance - 200 A4 mutex.signal() A5 statement mutex = 1 B1 statement B2 mutex.wait() B3 wolski.balance = wolski balance - 200 B4 mutex.signal() B5 statement balance = 600
  • 42. 42 Mutex A1 statement A2 mutex.wait() A3 wolski.balance = wolski balance - 200 A4 mutex.signal() A5 statement mutex = 1 B1 statement B2 mutex.wait() B3 wolski.balance = wolski balance - 200 B4 mutex.signal() B5 statement balance = 600
  • 43. 43 • pthreads includes support for Mutual Exclusion primitives that can be used to synchronize threads. Pthread Mutex API int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexattr); int pthread_mutex_lock(pthread_mutex_t *mutex); int pthread_mutex_unlock(pthread_mutex_t *mutex); int pthread_mutex_destroy(pthread_mutex_t *mutex); Posix Mutex API functions:
  • 44. 44 #include <pthread.h> #include <stdio.h> #include <stdlib.h> pthread_mutex_t mutex; int x = 0; void *MyThread(void *arg) { char *sbName; sbName = (char *)arg pthread_mutex_lock(&mutex); x = x + 1; pthread_mutex_unlock(&mutex); printf("X = %d in Thread %sn", x, sbName); } int main() { pthread_t idA, idB; if (pthread_mutex_init(&mutex, NULL) < 0) { perror("pthread_mutex_init"); exit(1); } if (pthread_create(&idA, NULL, MyThread, (void *)"A") != 0) { perror("pthread_create"); exit(1); } if (pthread_create(&idB, NULL, MyThread, (void *)"B") != 0) { perror("pthread_create"); exit(1); } pthread_join(idA, NULL); pthread_join(idB, NULL); pthread_mutex_destroy(&mutex); } Global mutex variable that will be used for synchronization Initialize mutex before using it Destroy mutex after using it Lock mutex before changing global variable. This will block the caller thread if the mutex is locked. Unlock mutex after changing global variable Shared global variable
  • 45. 45 A synchronization variable that takes on positive integer values. It has 2 blocking operations on it: Wait(semaphore): an atomic operation that waits for semaphore to become positive, then decrements it by 1. (blocks caller if semaphore is negative) Signal(semaphore): an atomic operation that increments semaphore by 1. (if the value of the semafore is negative, this function unblocks one of the waiting threads) Permit a limited number of threads to execute a section of the code Similar to mutexes Generally used for signalling existence of an event or action Semaphores
  • 51. 51 Signalling . . . A1 statement A2 sem.signal() . . . . . . B1 sem.wait() B2 statement . . . sem = -1
  • 52. 52 Signalling . . . A1 statement A2 sem.signal() . . . . . . B1 sem.wait() B2 statement . . . sem = -1
  • 53. 53 Signalling . . . A1 statement A2 sem.signal() . . . . . . B1 sem.wait() B2 statement . . . sem = -1
  • 62. 62 #include < semaphore.h> C/C++ programs int sem_init(sem_t *sem, int pshared, unsigned int value); int sem_post(sem_t *sem); int sem_wait(sem_t *sem); int sem_destroy(sem_t *sem); Semaphore API functions: Semaphore object Generally 0 Gives an initial value to the semaphore atomically increases the value of a semaphore by 1 atomically decreases the value of a semaphore by 1 Frees resources allocated for semaphore
  • 63. 63 #include <pthread.h> #include <semaphore.h> void *thread_function( void *arg ); sem_t semaphore; int main() { int tmp; tmp = sem_init( &semaphore, 0, 0 ); pthread_create( &thread[i], NULL, thread_function, NULL ); while ( still_has_something_to_do() ) { sem_post( &semaphore ); } pthread_join( thread[i], NULL ); sem_destroy( &semaphore ); return 0; } void *thread_function( void *arg ) { sem_wait( &semaphore ); ... pthread_exit( NULL ); } Global semaphore variable initialize semaphore signal semaphore wait semaphore destroy semaphore
  • 64. 64 #include <stdio.h> /* Input/Output */ #include <stdlib.h> /* General Utilities */ #include <pthread.h> /* POSIX Threads */ #include <semaphore.h> /* Semaphore */ #define BUFFER_SIZE 5 int headPos = BUFFER_SIZE - 1; int endPos = 0; int Buffer[BUFFER_SIZE]; sem_t producerSem; sem_t consumerSem; pthread_mutex_t mutex; int main() { int i[2]; pthread_t thread_a; pthread_t thread_b; pthread_mutex_init(&mutex, NULL); pthread_create (&thread_a, 0, producer, (void *)0); pthread_create (&thread_b, 0, consumer, (void *)0); pthread_join(thread_a, NULL); pthread_join(thread_b, NULL); /* exit */ exit(0); } Global buffer to store produced values Producer semaphore used for the notification from consumer thread Consumer semaphore used for the notification from producer thread Mutex that will be used to access shared Buffer İnit mutex that will be used to access shared Buffer
  • 65. 65 void *producer(void *arg) { int itemCount = 10; sem_init(&producerSem, 1,BUFFER_SIZE); while(itemCount) { sem_wait(&producerSem); pthread_mutex_lock(&mutex); Buffer[endPos] = 1; printf("Producer Thread : Buffer[%d] = 1 n",endPos); endPos = (endPos + 1) % BUFFER_SIZE; itemCount--; pthread_mutex_unlock(&mutex); sem_post(&consumerSem); } pthread_exit(0); return 0; } İnit semaphore with BUFFER_SIZE Wait notification from consumer thread Enter critical section Access shared memory and produce value Exit critical section Notify consumer thread
  • 66. 66 void *consumer(void *arg) { int itemCount = 10; sem_init(&producerSem, 0,0); while(itemCount) { sem_wait(&consumerSem); pthread_mutex_lock(&mutex); Buffer[headPos] = 0; printf("Consumer Thread : Buffer[%d] = 0 n",headPos); headPos = (headPos + 1) % BUFFER_SIZE; itemCount--; pthread_mutex_unlock(&mutex); sem_post(&producerSem); } pthread_exit(0); return 0; } İnit semaphore with 0 Wait notification from consumer thread Enter critical section Access shared memory and consume value Exit critical section Notify producer thread