binary semaphore vs mutex

I always had a doubt about why do we use mutex instead of binary semaphores or vice a versa. Basically what is practical difference between both. 

I knew theoretical answers like semaphores are used to lock shared variables between processes as well, unlike mutex. 

Today I realized that I was confused because of linux’s implementation of mutex. Ideally mutex lock shouldnt be released by other process, but this is allowed in linux and hence process synchronization can be done using mutex. But theoretically mutex should be used for mutual exclusion and binary semaphores for process synchronization. 

main()

{

create thread1

create thread2

}

thread1()

{

toss coin

signal

print result

}

thread2(){

wait

print result

}

here mutex can not be used. 

Also mutex can be used for shared memory between two processes. (It is by defualt not enabled and you have to pass some flag).

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: