A Process is: A Unit of resource ownership:
- A virtual address space for the process image
- Control of some resources (files, I/O devices...)
A Unit of execution:
- process is an execution path through one or more programs
- may be interleaved with other processes execution state (Ready, Running, Blocked...) and dispatching priority
A light weight process which sharres common memory when created. THreads are sharing memory. Certain threads will modifies same variables.
For checking the race condition we created three threads and also created a function which increments itself everrytime it is called by a thread. Thus our mail functions increases and it gives us the desired value as lng as we provided the desired value. But when we created a looop inside the mail funciton and mad the value too high like I incremented a Millions time then it is not providing the desired output. Thus we came to the race condition. Multicore processor face this problem.
- Read the value in mails
- Increament the value
- Writing back to the memory This works fine when we are working with a single thread but when we intriduce multiple thread then it will gives us a garbage value which is not the desired output. Critical Section : When we hae a resource example a global variable
void* function1( void*arg) {
}
void* function2(void* arg) {
} int main()
{ pthread_t t[4]; for(int i =0 ; i < 4 ; i++) { pthread_create(t+1 , NULL , function1 , NULL);
}
| Read File | 1000 | 2003 |
|---|---|---|
| Increament | 1001 | 2004 |
| :---: | :---: | :---: |
| Write file | 1001 | 2004 |
| :---: | :---: | :---: |
This race condition ocuured for large value of loop iterator not for a small number because for small number first threads creates and execute the function body in less time and in that time second thread is created but for large value it takes more time second thread is created and it also calls the function which creates ambiguity. And we get wrong answers.
Using a mutex we will solve
- This we will introduce Pthread_mutex
- Use pthread_mutex-Lock
- Then pthread_mutex_unlock
- Initialise this in the main
- Now run te program again
- You will get the right answer locking and unlocking of mutex is protecting like it is protecting the block of code wihtin in a bracket to be executed by another thread. If in above example thread1 is executing the code in function , while incrementing the value thread2 and thread3 cannot execute the same line.
===================================
- For i = 0 created first thread Then we are also joining it
- We are going to get only on thread executing at a time
- If we are doing both create and join in the same for loop then only one thread will execute and remaining will wait n watch.
- We don't have to do such bu******
- So to avoid this create another loop for join and make it seprate loop for create_function.
- THings will start getting executed parallel
- All threads will be create and same time they will finish their execution
- If your function has a void return type no return value
- pthread_join will take a refrence to pointer
- Void pointers are wild cards they canbe anything
- We cannot return a simple variable from the function because after execution stack frame of that function will be deallocated all variables will be de allocated. So for that we have to allocate chunk of memory Dynamically.
- In this we can access the value of function