Qualcomm Interview Question
Software Engineer / DevelopersCountry: United States
Best sync technique?? Well, that depends upon the context. Different contexts have their own implementation and as per that one scenario can have one best in one context but not in another.
So it's totally depends upon the implementation. So, here comes the Engineering skills, what to use and where to use that, so that you can get best outcome
Yes there is no best here. Then there is no need for implementing other synchronization mechanism. So we need to understand the application.
If a common memory is shared then we can say shared memory is good idea.
Again if i need to indicate a message or signal then there is no need for shared memory to stored that message or signal.
if its send a buffer from one thread to another i can use pipeing. ...
As per my understanding if an interviewer ask for best synchronization
There are two
wait: ( mutex( 2.6.3 kernel ) and semaphore)
Spin : Spinlock
Now the Question arises ?when to go for wait and when to for spin
when were the critical section is more and there are delay or sleep in code then better gor for semaphore/mutex
if critical section is small and there is no delay then go for spinlock
Correct me if I am wrong
the 2 main ones are spinlock (looping until we get the resource) and mutex (sleeping untill we get the resource)
but there are implicit ways too:
- putting the synchronization code in interrupt and calling it
- putting the synchronization code in kernel and calling it
- using a queue or pipes (which are themselves syncronized)
for simple counter variables or for bitwise ------->atomic operations are best methods.
- super star rajini kanth December 29, 2012atomic_t count=ATOMIC_INIT(0); or atomic_set(&count,0);
atomic_read(&count);
atomic_inc(&count);
atomic_dec(&count);
atomic_add(&count,10);
atomic_sub(&count,10);
spinlocks are used to hold critical section for short time and can use from interrupt context and locks can not sleep,also called busy wait loops.
fully spinlocks and reader/writer spin locks are available.
spinlock_t my_spinlock;
spin_lock_init( &my_spinlock );
spin_lock( &my_spinlock );
// critical section
spin_unlock( &my_spinlock );
Spinlock variant with local CPU interrupt disable
spin_lock_irqsave( &my_spinlock, flags );
// critical section
spin_unlock_irqrestore( &my_spinlock, flags );
if your kernel thread shares data with a bottom half,
spin_lock_bh( &my_spinlock );
// critical section
spin_unlock_bh( &my_spinlock );
if you have more readers than writers for your shared resource
Reader/writer spinlock can be used
rwlock_t my_rwlock;
rwlock_init( &my_rwlock );
write_lock( &my_rwlock );
// critical section -- can read and write
write_unlock( &my_rwlock );
read_lock( &my_rwlock );
// critical section -- can read only
read_unlock( &my_rwlock );
mutexs are used when you hold lock for longer time and if you use from process context.
DEFINE_MUTEX( my_mutex );
mutex_lock( &my_mutex );
mutex_unlock( &my_mutex );