The difference between Process and Thread?
- A process is an instance of computer program and it has the whole running resource like memory space and it is private. Thus in order to communicate with the shared memory or Inter Process Communication(IPC) is needed.
- One process may have multiple threads which can be treat as a light weighted process. All the threads share the memory space of the process.
- And each process has it own run time status like registers and run time stack. Thus switch threads is more efficient than switch processes.
- The communication between threads, the synchronization is the importation part of multithreading.
How to synchronize multiple threads?
1. Race Condition
Race Condition is that the output of program is dependent on the sequence of uncontrolled events like multiple threads. When the events does not happen as programmer intend the bug generated.
2. How to avoid race condition
Synchronize threads to avoid race condition.
- Mutex: mutual exclusive lock. Enforce exclusive access
- Join: Make a thread wait until another thread terminate
- Condition Variable: Make a thread wait until some condition is true
- Barrier: Make all the threads wait until all the threads called the barrier function
- Semaphore: used to solve the consumer and producer problem (a variable used to control access to a shared resource by multiple processes)
3. Problem from CrackCode 4th Edition
Suppose we have the following code:class Foo {public:f.A(.....); f.B(.....); f.C(.....); f.A(.....); f.B(.....); f.C(.....);A(.....); /* If A is called, a new thread will be created and * the corresponding function will be executed. */B(.....); /* same as above */C(.....); /* same as above */ }Foo f; f.A(.....); f.B(.....); f.C(.....);i) Can you design a mechanism to make sure that B is executed after A, and C is ex- ecuted after B?iii) Suppose we have the following code to use class Foo. We do not know how the threads will be scheduled in the OS.Foo f;Can you design a mechanism to make sure that all the methods will be executed in sequence?Answer Cool
Semaphore s_a(0);
Semaphore s_c(1);
A{
s_c.acquire(1);
run_some_thing;
s_a.release(1);
}
B{
s_a.acquire(1);
run_some_thing;
s_b.release(1);
}
C{
s_b.acquire(1);
run_some_thing;
s_c.release(1);
}
You are given a class with synchronized method A, and a normal method C. If you have two threads in one instance of a program, can they call A at the same time? Can they call A and C at the same time?Answer
1. If a method is synchronized method then two instance of a program cannot be called at same time because they are mutual exclusive
2. Yes.
4.Deadlock
Because of mutex deadlock could be happened.
A deadlock is a situation that two or more actions are each waiting for other to finish, and thus neither ever does.
- Mutual exclusive
- No preemption
- Circular wait
5. Thread Safe
Thread safe means the program can be used by multiple threads at the same time and without causing any problem.
Did you know that that you can generate money by locking special pages of your blog / site?
ReplyDeleteTo begin just join AdWorkMedia and use their Content Locking widget.