L-3.8: Semaphores | Wait, Signal Operation | Counting Semaphore | Example| Operating system
Вставка
- Опубліковано 15 лип 2024
- 👉Subscribe to our new channel: / @varunainashots
Semaphore is a variable that is non-negative and shared between threads. A semaphore is a signaling mechanism, and a thread that is waiting on a semaphore can be signaled by another thread. It uses two atomic operations, 1) Wait, and 2) Signal for the process synchronization.
0:00 - Introduction
0:48 - Semaphore
2:42 - Various operations in Semaphore
4:59 - Entry section code
12:03 - Exit section code
19:56 - Questions on Semaphore
►Operating System (Complete Playlist):
• Operating System (Comp...
Other subject-wise playlist Links:
--------------------------------------------------------------------------------------------------------------------------------------
►Design and Analysis of algorithms (DAA):
• Design and Analysis of...
►Database Management System:
• DBMS (Database Managem...
► Theory of Computation
• TOC(Theory of Computat...
►Artificial Intelligence:
• Artificial Intelligenc...
►Computer Networks (Complete Playlist):
• Computer Networks (Com...
►Computer Architecture (Complete Playlist):
• Computer Organization ...
►Structured Query Language (SQL):
• Structured Query Langu...
►Discrete Mathematics:
• Discrete Mathematics
►Compiler Design:
• Compiler Design (Compl...
►Number System:
• Number system
►Cloud Computing & BIG Data:
• Cloud Computing & BIG ...
►Software Engineering:
• Software Engineering
►Data Structure:
• Data Structure
►Graph Theory:
• Graph Theory
►Programming in C:
• C Programming
►Digital Logic:
• Digital Logic (Complet...
---------------------------------------------------------------------------------------------------------------------------------------
Our social media Links:
► Subscribe to us on UA-cam: / gatesmashers
►Subscribe to our new channel: / @varunainashots
► Like our page on Facebook: / gatesmashers
► Follow us on Instagram: / gate.smashers
► Follow us on Instagram: / varunainashots
► Follow us on Telegram: t.me/gatesmashersofficial
► Follow us on Threads: www.threads.net/@gate.smashers
--------------------------------------------------------------------------------------------------------------------------------------
►For Any Query, Suggestion or notes contribution:
Email us at: gatesmashers2018@gmail.com
Dude, i actually struggled to understand the usage and application of a semaphore through the slides of my engineering course, but after watching your toilet example, i finally understand. Thank you so much, you are the professor we don't deserve, but the professor we need.
During exams my entire college only remembers one name: Gatesmashers. You are really the teacher and guide our college professors (whom we pay) couldn't be. Thank you so much for providing such valuable content free of cost :)
U r amazing .....far better than our faculty....👌👌👍 keep going
It right
exactly
The best comment i ever read😂😂😂😂
@@achyutkumarsingh7308 flirting ,lol
Strongly agree
I failed my intial exams of previous semester and then i started learning Data structure from your playlist and socred almost full marks in terminal exam. Now for OS i daily watch two or three videos and lectures are very addictive, sometimes i spend entire weekend on youtube learning for your videos.
Keep learning and achieving
jobless
kinda nerd
Are you also watching this 1 day before your exam?
Yaaa
me too
yes XD
Haaa
Hm
Finally, I can confidently tell that I have understood this topic!! Loved it!!
Yaar ek baat samajh nhi aayi, khali 440k subscriber kaise hai ab tak. Poore youtube pei aur koi operating system aur koi nahi padhaya aise. Deserves more support.
Freakin Fantastic, thanks a ton for the course!
Wow..thank u..jaldi ho jayenge million
@@GateSmashers I have a doubt, when process (P4) waking up from the block state and there is already some space created in critical section as one process(P1) have come outside, and also the P4 has already run the entry level code uptill the suspend was called and should resume after that(also the value of S is decreased already one time), so it should go to critical section directly resuming from that line but as you said in video it is running again entry code, why?
Please Reply
And Thank you for these amazing videos
@@hiteshbansal4878 Any process to enter into critical section have to execute the entry code and exit code, even the process is from wake up() state (blocking state) like P4, P5 ....or the new process like P6, P7, P8, ....
so it should not directly go to critical section, as discussed in video it is running again entry code.....
@@hiteshbansal4878 Yeah bro, it should not execute the entry code again but rather enter the Critical Section directly
Maine ek aur jageh yeh comment post kia tha, toh yaha pe paste kar rha hu in case you're still struggling or for anyone who doesn't understand it in the future:
Reposting a comment from 9 months ago that went unanswered:
"sir in 18:23 p4 try karta hi enter karane ke liye then s.value -1 hoti hi ...assume karo wo dobara suspend ho jata hi aur p3 exit code chalata hi to uske baad s. value =0 hogi ..phir iske baad to koi process critical section mei ni hoga ...aur koi next process agar aata hi to s. value negative hoti jayegi , aise case mei to koi andar ni jaa payegaa, humeshaa suspend hote rahengey..pls provide solution"
Another comment in English:
"what if value of S = 1 and one process entered in CS, value of S becomes 0, Now 50 process try to enter but they get blocked, Now value of S becomes -50, one process which was in CS now come out of CS, value of S becomes -49, now how another process entered in CS"
I have the same doubt.
From what I've understood, there a couple of things that are therefore incorrect.
First off, whenever a process comes back from block list, it doesn't restart its execution of Down(), it would rather resume where it left off from, ie last line of if block.
So secondly, this means that the code should actually be 'return;' instead of ' else { return; } ' so that it can actually resume its execution and enter into the critical state.
Edit:
Actually, I'm watching this again 4 months later. Perhaps you don't need to make the code change. The code is perhaps right, the explanation was wrong. So yes, whenever a process comes back from block list, it doesn't restart its execution of Down() like I said earlier. It continues from where it left indeed. And it left at a place where there's nothing left to execute(There's nothing after if else), so it's done executing Down() and can now enter the critical section. So the code as it is right now is correct, just the explanation that went with it was a little flawed.
Your teaching ability is more capable than some of our university faculty.
True
Perfect comment for him 👍👍
some ??
frfr
💯
just amazing...very easy to understand when i see ur lecture.
thq so much....
I wish you could teach us face to face in University. Totally in love with your teaching skills and knowledge transfer.
Love from Pakistan.
Best explanation. The words you choose to speak and even a small bit of information is elaborated so nicely. Thank you
went through few comments....people are watching this video 1 day before exam,mean while me watching this video during exam... #covid19
thank you sir..!!
Your teaching skills are amazing, you convey us the concept in 5 mins which our teachers do in 1.5 hours and we still don't understand them.
Love from Pakistan
Happy to see that u guys are having a pen in your hand not a weapon.
@@amanbhadani8840 great thinking ...sir
I can only say YOU are a true GEM personality, none of the UA-camrs, even our faculty staff in uni are not as good as YOU are Sir. Much appreciated and lots of love from Pakistan
Aray saad bahi
YOU ARE THE DEFINITION OF A GOOD TUTOR I SWEARRRR
U are best sir... Aj se phle kbhi process synchronization , semaphore kbhi smjh nahi aaya, but apki video dekhne k bad crystal clear ho gya concept
Perfect example of a Best Teacher
If I own a college and I have to select only one teacher, it will be Varun Singla. Huge respect sir!!!
Please dont delete this OS series ever from youtube. I am not making notes expecting that I can access these videos anytime in future..
sabas
We love you sir..You are a good teacher at all.
Keep doing it sir.
May god keep you alwz happy and healthy.
i can understand only your videos to mark scores in my academic yr ,,,may god give you long life and safe your family from problems and disease
Thanks I had too much confusion on this topic but now cleared
mine faculty is like complicating everything and
you sir
simplicity at next level ...
watch my video also for this topic
Sir aapne semaphores ko ittna clearly explain kiya jisse classical problems bohat ache se samaj aayi iske liye sir thanku so much aapke padaane ka tarika bohat acha hai
This is the best explanation in UA-cam
I see half of video and all concept got clear!🙏🙏
Very informative and understandable lecture... Stay blessed
So thankful, so easy to understand by your teaching❤
Amazing explanation....after watching your video it is clear to me...
Thank you Varun Sir for explaining the complex concepts in such simple terms. Indebted to you for my interview preparation
U r awesome sir,understood the topic very well and now I am ready to answer all the questions from this topic....Thank you sir
Sir you are an Amazing teacher 🙏👌all doubts are cleared now..
great sir
you are looking very pretty
@@soumyadeepbarik5711 well done
Very well explained Sir. Your videos are very helpful. :)
after watchig your videos my confidence on this on this subject is increased thank you sir your way of explanation is vary easy
Sir YOU are the best teacher i have ever seen , such a clear cut explaination.Thank you Sir
I have a big job interview next week and I need to cover all this stuff I studied 7 years ago. Your videos have been so helpful buddy, thanks a lot 😊
kaisa gaya interview?
a lot of appreciation from Pakistan :*
Awesome Explanation brother....keep it up...God bless you..Thank you Very MUch for making such Video
Sir you are such a great teacher and your teaching technique also best........thank you so much sir🙏
Sir Varunn you are great.... I am natuical officer.... nd i love to learn computer ... i have learnt many things from u... ur videos very help full....
ur videos are exam nights last hope....
Haha.. Thank you so much..
First time i got this concept .Truly amazing Sir!
thanks
Very Nice Explanation Sir, Thank you !
thank you so much sir
you are so amazing .....your teaching really helps me
Thank u sir ji 🙏 way of explanation is lit🔥
Thank you for providing industry level education for free.That too such a great course free is the best thing one can avail.
Really it's very helpful . Such a nice and clear explanation Thanks a lot Sir🙏🙏😊😊
This is the easiest video to understand..thanks a lot Sir
This channel is Amazing 😍😍😍
woah mind blowing explanation. I'm from non-IT branch but i have started loving all your tutorials.. u have made this subject easy. Thank you so much sir More power to you.
Amazing way of teaching.. 👍
Sir.... Aaap ne dil khush kr diya.... Waah ...
I wish you were my professor in my college ☺️😅. You are just amazing as teacher 🤩😍
so true.
This is how teaching become fun
Thank u soo much sir for explaining each and every point in such an easy way....
Interactive lectures. Thanks
I challenge in the whole UA-cam u r the best teacher i have come through 🔥
well then you watching only Indian youtube.
Your videos are really great and easy to understand. They develop a very clear and organized view of the topic enabling student to grasp and gain command on it. As a student the only problem i faced while watching your videos was revision of the topic. For this student has to watch the whole video again (which is not always possible).
If possible, you can add a revision notes to each video which covers the topic and are meant for revision purpose. This would not only enhance the content of your channel (which would also promote your channel when students will share notes among them) but also provide students with good study content.
Thank you.
excellent work , that much simplified ....thanks
sir you are great, huge respect and love from pakistan. you are far more better than our phd professors. jinki hmy zra smjh ni ati.
Best explanation!
These vedio lecture series are really helpful.. I m following them for OS,CD.. And will follow for many more.. These lectures are responsible for completing my syllabus on time.. Thank you sir
You are really amazing Sir!.. Great explanation 👌👌 . All doubts cleared 😊
Good explanation boss..ty very much..now i'm very clear about this topic
Always prefer Gate smashers for my semesters related computer subjects♥️♥️♥️♥️🔥🔥🔥 love your explanation 🔥🔥🔥
awesome explantion.........plz make a vdeo on computer org.
Sir your lecture is too good .your teaching style is very impressive .
aap ka chahra bhut cute he positive vibes and 25 ke 25 min sun ne me dikkat nahi hoti
Exam Tomorrow at 11:30 AM , watching now at 10:45 PM
Hello Sir, I was not able to grasp the concepts of Operating system, but after seeing your videos, I'm able to understand very easily. Thank you very much for your efforts in helping us understand concepts very easily.
I had a question: Does counting semaphore provide mutual exclusion? Because I saw many processes are entering the critical section simultaneously. And providing mutual exclusion is a necessity for process synchronization.
When we have a teacher like him on youtube then it is so easy for distance students. Thank u so much sir❤
Wowww. Nicely explained 🤟 now crystal clear 🙏
aapko 1.5x pe dekhne ki habit ho gyi hai jaise hi 1x pe video chalti hai bada funny lagta hai 😂
Dear Friend,
If you can prepare series on Computer architecture and organization then plz prepare that. We would love to learn from your videos. God bless you.
Thank u so much sir .....i watch only the vedios which u upload and it helps me a lot and i watch it only one day before my exams but i score good marks....thank u sir
nice expiation sir
superb method i understand easily all concept thank your so much sir......
Correction:
15:58 When P4 wakes up it can directly enter the critical section because it has already executed the entry section code !
Thank you for this wonderful lecture series ❤️
I had some doubts about it but I Advice you to solve an example.
if you read about multi threading you will see that when a thread is running and wait() is called and after waiting the thread does not directly jump to running state. The thread scheduler(program which decides which thread to run) decides when the thread will run again. Mean while all the operations done by the thread before calling wait() is saved.
@@suryasikharej8116 take 2 processes p1 and p2, suppose p1 comes first and executes down() (take s = 1 initially) means s will become 0 (1-1), now suppose while p1 is in cs , p2 wants to execute down(), where s will become -1(0-1), so 'if' will run for p2 and p2 will get blocked, now suppose p1 completes it's cs and executes up() where s will become 0(-1+1), means 'if' will run and p2 will get wake up but s is currently 0 means if according to sir that p2 will again check down() which means s will become -1(0-1), so in that case 'if' will run means p2 will again get blocked , so in this way p2 will starve, am i right ?
or p2 will not execute down() from starting , will it resume after the sleep() call where then it will get the access to cs ? just clear this doubt
@@utkarshgangwar2173 You are using binary or counting in your example?
p2 will execute down and will go to a list which contains process which are waiting to execute cs. After p1 completes cs and while doing up() it will take one process(lets say p2) from the waiting list and start its exeution .
In critical section chapter, it is told that only one process can execute the critical section. But here it is told that more than one process can enter into the critical section even if there is another one.
same doubt. Help me to understand.
7:59 mutual exclusion doubt will clear at this point of time.
a counting semaphore can allow multiple processes or threads to access the critical section, hence mutual exclusion is not guaranteed.
@@madhu19993 mutual exclusion may or may not be achieved
Amazing understanding u gave us
2nd best teacher i hv learned from .. Too good 👍👍
sir consider if semaphore value is 1......if process P1 tries to enter critical section the S will become 0 and it will enter. Next if process P2 wants to enter S value will become -1 and it will go in suspended state . Now consider P1 is completed and it incremented S to 0 which will wake up P2 and will bring it to ready state,now if P2 again wants to enter critical section then S value is currently 0 so it will still go back to suspended state and P1 is also completed....please solve this doubt sir!
Exactly, do comment back if you manage to find an answer to that.
nice observation
5 minutes before exam.
Hum bhai exam me hi phone leke aaye😊
amazing teaching...my favorite teachers
thankyou sir for amazing lecture
Your videos are amazing. I have the following queries and would like to know the answers of them.
1. How mutual exclusion is satisfied in case of counting semaphores ?
2. Is there any difference between mutex and semaphore. Explain in detail, if any.
3. If S=1, p1 enters. P2,p3,p4 will be -1,-2,-3. And if p1 is moved out of Critical section with exit code. Semaphore value becomes -2, in such a case; the critical section is left idle as the CS is not utilised by any process. ? Can you explain this state in detail.
Counting semaphores doesn't satisfy mutual exclusion
ua-cam.com/video/DvF3AsTglUU/v-deo.html --- check this out to understand difference between semaphore and mutex
our teachers even suggests us ur videos here in pakistann ... lOv u sir
pakistan mein CS padhte hai log?
awesome explation.....topic cleared
You are truly a saviour ❤️
I have a doubt, If S=3, which means, Critical section is capable of having 3 processes inside.
Now, 5 processes executes the wait() operation, Now, S = -2, and 3 processes (P1,P2,P3) are already inside the CS.
Now, as we know, critical section is capable of having 3 processes, so if P1 exits the CS (Now, S= -1), there are currently 2 processes in the CS, which means, One
process must enter the CS, but because S=-1 it cannot enter.
So, In order to make P4 enter the CS, one more process should exit the CS, so that S is positive, which is something illogical.Suppose P2 also exits CS. Now S=0.
Now if P4 enters the CS, P5 cannot enter the CS now because S becomes -1 after P4 entered. CS has a space of one process but p5 cannot enter.
I am not able to understand Counting Semaphore concept!
Can anybody explain?
As per my understanding the process would resume after waking up from sleep so they won't run the Down() function again, they shall enter to the critical section only when the Semaphore value is greater than 1,, there would be a infinite while sloop kind scenery ig,,,,,,,, I came up with this explanation myself just to statisfy my thrust to meet the inconsistency in the video,,, please correct me if you find more suitable ans ..
@@niranjansaha5135 Yep, I too think that should be it. It is very weird how only a handful on comments are pointing out this obvious mistake.
Damn, same doubt. There are hundreds of students hailing this video but not able to get this doubt. Can you tell me from where to understand this concept?
@@adityasankhla1433 ++
One question here, won't decrementing or incrementing S in Down and Up result in race conditions as explained in previous videos? Which will lead to inconsistent value for S
Yes , i have also same confusion.
Semaphore is also using critical section type code. This result is indeterministic state of program.
Let take two process p1,p2.
1)Both performing down code , while sValue =1; means only one program can enter in CS.
2) process 1 : fetch sValue , decrement operation 1-1=0, but not store in ram , process preempted.
3) process 2: fetch (sValue ie 1),
Decrement operation 1-1=0, and save to ram, and check s
Incrementing/Decrementing are atomic operations i.e., they either execute fully or don't execute at all. So, when more than 1 process will try to update the value of "s" then ultimately "s" will either be equal to 0 or 1.
Very well explained conceptually!
Thank you sir. You are an amazing teacher sir......
Sir, suppose initially s=1. Then p1 enter in CS(s=0) and then p2, p3 and p4 tries to enter in CS(s=-3) and then p1 exit(s=-2) now the CS is empty but still p2 cannot enter in CS why?????
Iska answer mila kya bhai ?
@@yogesh8275 same question
I guess because p2, p3 and p4 are in the waiting state (block state) and can only enter when s>0
There will be deadlock
Coz we r try to awake the process which is not actually in sleep condition
(for set the value of semaphore greater than one)
As s value is less then 0 and when value is less than 0 its goes in sleep mode
What is the utility of counting semaphore if it does not follow mutual exclusion?
Yes please clear this
I think....counting semaphore is used for synchronisation and binary semaphore is used for mutual exclusion.
@@naveetakharb7647 if you are using the word synchronisation then under synchronisation their is a condition Mutual exclusion
According to you one is following synchronisation and other mutual exclusion is kind of absurd type of answer because if any algorithm is synchronised for multi processing that means that it is also following mutual exclusion because to achieve synchronisation you must achieve mutual exclusion as it is primary goal to obtain a synchronised algorithm for multi processing
Thank u Sir... You are great.. All doubts clear.. lots of love from
Pakistan
very helpful video sir great work
Reposting a comment from 9 months ago that went unanswered:
"sir in 18:23 p4 try karta hi enter karane ke liye then s.value -1 hoti hi ...assume karo wo dobara suspend ho jata hi aur p3 exit code chalata hi to uske baad s. value =0 hogi ..phir iske baad to koi process critical section mei ni hoga ...aur koi next process agar aata hi to s. value negative hoti jayegi , aise case mei to koi andar ni jaa payegaa, humeshaa suspend hote rahengey..pls provide solution"
Another comment in English:
"what if value of S = 1 and one process entered in CS, value of S becomes 0, Now 50 process try to enter but they get blocked, Now value of S becomes -50, one process which was in CS now come out of CS, value of S becomes -49, now how another process entered in CS"
I have the same doubt.
From what I've understood, there a couple of things that are therefore incorrect.
First off, whenever a process comes back from block list, it doesn't restart its execution of Down(), it would rather resume where it left off from, ie last line of if block.
So secondly, this means that the code should actually be 'return;' instead of ' else { return; } ' so that it can actually resume its execution and enter into the critical state.
Do correct me if I'm wrong @Gate Smashers
Understood now. This is actually helpful.. have been trying to find this out for a long time actually. Thanks!
glad it helps.
no a process can never directly go to the running state. The long term scheduler will decide which process to put from the new queue to the ready queue.
No-one talked about 'directly going to the running state'..
It was a comment on the point of resumption of execution of a process when it comes back from the block list.
Actually, I'm watching this again 4 months later. Perhaps you don't need to make the code change. The code is perhaps right, the explanation was wrong. So yes, whenever a process comes back from block list, it doesn't restart its execution of Down() like I said earlier. It continues from where it left indeed. And it left at a place where there's nothing left to execute(There's nothing after if else), so it's done executing Down() and can now enter the critical section. So the code as it is right now is correct, just the explanation that went with it was a little flawed.
sir, how can a critical section handle more than one process(p1,p2,p3).it is supposed to run only one program at a time right?
its for an example when multiple process occurs at a time in general its one but in computer without ur knowledge a lot of processor at background work
There are some resources which can allow more than one processes to run it at a time
Is it supporting mutual exclusion?
Thank you very much sir for this information 🤗🤗
best teacher i have ever seen on youtube
Sir, when S = 3, 3 processes can enter Critical Section and other processes running the wait() are put to block list. Now when a process exits the CS why can't we simply let a suspended process to enter critical section if it can allow 3 process?
Yes you are right, we will directly add that process from suspended list to critical section
Sir explained it wrong, according to sir's explanation if at anytime 4 processes reached in the suspended list then no process can enter in critical section as semaphore value will be -4 and exit code of 3 processes can make it -1 which is less than 0, so no process can enter in the critical section
@@AshishKumar-zs3nk BHAI LOG KYA PUCH RHE VO TO PADH.