Introduction to semaphores in C

Поділитися
Вставка
  • Опубліковано 17 січ 2025

КОМЕНТАРІ • 139

  • @sunilkeshava
    @sunilkeshava 3 роки тому +68

    Since semaphores are almost always discussed along with mutex, another good difference to mention is that that semaphores are not necessarily owned by a thread. Let me give an example.
    Consider a semaphore_a which has been initialized to have a value of 0
    1) thread_a could wait on it (i.e. it will only decrement the semaphore count)
    2) thread_b could post on it (i.e. it will only increment the semaphore count)
    This is a valid design using semaphores (but not with mutex) since the same thread does no need to wait and also post a semaphore. Here thread_a will only execute semaphore wait & never execute semaphore post. Similarly, thread_b will only execute semaphore post and never semaphore wait. The example you gave could be mistaken to suggest a thread which executes semaphore wait (& decrements the semaphore count), also needs to post the semaphore too (& increment the semaphore count). However, the is not true with mutex. If a thread locks a mutex, the same thread needs to unlock the mutex.
    A good example this could be a thread which waits for a particular event to be available and an ISR which gets called every time the event occurs. Let me explain a bit:
    - You have an event, let us say a GPIO interrupt and everytime the GPIO goes to 1 & interrupt is signaled. In the application , you want a certain function to be executed when the GPIO goes to 1. A simple design for this would be:
    - Create an ISR to handle the GPIO interrupt & a thread which executes the GPIO function.
    - During initialization, we create a semaphore such that the ISR does a post when an interrupt occurs and the thread does a wait.
    - When there are no interrupts, the ISR never executes semaphore post and hence thread does keeps waiting on the semaphore.
    - When an interrupt does occur, the ISR will post the semaphore (increment to 1), then when the thread gets to execute, the wait would return and the GPIO function will be executed.
    - If multiple interrupts occur, then ISR executes semaphore post each time & accordingly increment the semaphore count. And the thread will execute the GPIO function repeatedly until the semaphore count is zero.

    • @CodeVault
      @CodeVault  3 роки тому +18

      Very nice explanation! It's a very important distinction to make. I will pin this for future viewers.
      I do tackle this topic in the video comparing binary semaphores with mutexes here: code-vault.net/lesson/bu9ehmp0mx:1609433599490

    • @kippie80
      @kippie80 3 роки тому +1

      this functionality looks just like completions.h. Why are there completions?

    • @your-mom-irl
      @your-mom-irl Рік тому

      Mutexes are for sharing a single resource exclusively between threads, semaphores are for like a queue in which one thread consumes something the other other thread produces. Of course you could implement the same behavior with mutexes and viceversa but i think this is the more convenient way to look at it

  • @kotravaijm250
    @kotravaijm250 2 місяці тому +5

    2 years ago, i went through all ur videos for an interview. Cracked it. Now again for an upcoming one. I pass or not, three things are constants. 1. You are the absolute BEST. 2. Your channel is waaay under rated. 3. I love you, totally. THANK YOU

    • @CodeVault
      @CodeVault  2 місяці тому

      Good luck at the interview!

  • @barishachowdhury2327
    @barishachowdhury2327 Рік тому +4

    Counting my blessings this year. You are one of them. Didn't use to like OS much, after watching your videos, got me hooked on OS. Stay blessed.

  • @tylerganderson
    @tylerganderson 3 роки тому +19

    Thank you for this. I had a difficult time understanding how to implement this after our instructor introduced it to us. You explained it wonderfully. The visual at 5:55 also helped immensely.

  • @sangamthapa9325
    @sangamthapa9325 3 роки тому +38

    Honestly mate you are phenomenal. I appreciate your videos and time, you are actually helping me so much I can't thank you enough. Keep it up buddy!

  • @reflex2627
    @reflex2627 3 роки тому +21

    Dude you are the best ! so appreciated , greetings from the Turkey

  • @Lexyvil
    @Lexyvil 2 роки тому +2

    One of the best explanations on semaphores. I had a bit of trouble following what all the code did, but the demonstration starting at 5:55 really made it click!

  • @alperari9496
    @alperari9496 3 роки тому +4

    this guy really deserves more views. Thanks a lot

  • @narasarajv5278
    @narasarajv5278 4 роки тому +2

    Today I understood semaphore concept.
    These graphical representation will help me a lot in interviews for me.
    Thank you...

  • @ebelinmoralesbenites2779
    @ebelinmoralesbenites2779 2 роки тому +3

    Literally saved my life with this. So straightforward and so helpful!!!

  • @sftekin4040
    @sftekin4040 Рік тому

    I can not thank you more. Very clear explanation with code examples. Thank you. Please continue what you are doing.

  • @mouadflp1480
    @mouadflp1480 Рік тому +3

    top explanation of semaphores

  • @pulkitjoshi6196
    @pulkitjoshi6196 3 роки тому +4

    You are helping me in my OS Lab basics, a lot.

  • @novigradmerchant2400
    @novigradmerchant2400 4 роки тому +5

    Dude your explanation is insaaaaneee!! When i first face with the semaphores, i couldn't get it but now i got it. Again Thanks for explanation 😊

  • @StrawhatCrunchy
    @StrawhatCrunchy 3 роки тому +1

    Thank you so much ! You really helped me. Watched a lot of your vids now. And they are awesome ! Greetings from Germany

  • @trocandobytes
    @trocandobytes 2 роки тому +1

    Very well explained. You really know how to break such complex things in small chunks so everyone can understand. Thank you mate!

  • @yunhuiwan1891
    @yunhuiwan1891 2 роки тому +1

    You are literally a life saver! Wonderful video!

  • @xxbutler86xx
    @xxbutler86xx 2 роки тому

    Fantastic explanation of semaphores! I watched two other videos and read a few articles on semaphores, but did not fully understand them until watching this video! Thank you!

  • @bama2619
    @bama2619 3 роки тому

    Again, you are awesome! Thanks from Moscow! Great job!

  • @hanyangxie4730
    @hanyangxie4730 3 роки тому +1

    Thank you so much, mate! Just saved me from my O.S. class assignment.

  • @abuhuraira2637
    @abuhuraira2637 27 днів тому

    Great tutorial, you explained very practically.

  • @osamaakhtar6525
    @osamaakhtar6525 3 роки тому +1

    You are a good teacher. Thanks, mate!

  • @DanielSColao
    @DanielSColao 3 роки тому +1

    Great video and well explained, mate! Thanks for sharing your knowledge with us!

  • @krishnakumar-rp9wc
    @krishnakumar-rp9wc 3 роки тому

    Thanks a lot man for all your videos! You are a mine of knowledge! Subscribed !

  • @JOXAVLOGS
    @JOXAVLOGS 2 роки тому +1

    you are better than my professor, 😂😂😂 Thank you life saver

  •  3 роки тому

    Excellent explanation. And a great mix of Bottom-Up-Bottom approach.

  • @modusponeis1205
    @modusponeis1205 3 роки тому

    thank you, my friend... I love you, from Brazil.

  • @bestsaurabh
    @bestsaurabh 3 роки тому

    Really appreciate the work you are doing. Very helpful videos.

  • @vignaanjunior382
    @vignaanjunior382 4 роки тому +1

    You are amazing DUDE ! LOVE YOU

  • @tahmeerimtiaz3123
    @tahmeerimtiaz3123 4 роки тому +1

    love your videos bro thanks a lot keep it up. blessing for you.

  • @ShubhamKumar-id9sm
    @ShubhamKumar-id9sm 4 роки тому

    Thanks man this is what i really needed for this week. What a coincidence!

  • @Kuchenklau
    @Kuchenklau 3 роки тому +1

    I have a question: At 3:18 on line 21, you are allocating memory for the local variable a. Why? I thought dynamic memory allocation is only used for global variables or when size is not known at compile time.
    Anyway excellent video, it helps me a lot with my systems programming course.

    • @CodeVault
      @CodeVault  3 роки тому +2

      Dynamic memory is memory that you have control over when it is allocated and deallocated.
      I dynamically allocate the a there so that for each iteration of the for loop, each thread gets a separate place in memory for that a. Otherwise, all of them would use the same memory address and have the same value for all the threads.

    • @Kuchenklau
      @Kuchenklau 3 роки тому

      @@CodeVault Ah yes, of course the threads share the same address space so that makes sense. Thank you!

  • @Sarah-su1zq
    @Sarah-su1zq 3 роки тому +1

    Thank you SO much! this is extremely helpful and you are so good at explaining this very complicated subject.

  • @suicidbyt7830
    @suicidbyt7830 2 роки тому

    This helped a lot, thanks and greetings from Germany! 👍

  • @zemeuzhuri7319
    @zemeuzhuri7319 2 роки тому

    10/10 video and tutorial, thank you!

  • @thormind7338
    @thormind7338 Рік тому

    Excellent explanation, tks!

  • @VoidloniXaarii
    @VoidloniXaarii 4 місяці тому

    Thank you very much. I'm still not quite getting this but i feel this just got me 20% clouser, thank you

  • @swedishpsychopath8795
    @swedishpsychopath8795 Рік тому +1

    Nice explanation of what you COULD do with semaphores. BUT which use cases are examples of situations where you WANT to have more than 1 thread into the critical code section between wait and post? If we use semaphores, the idea is to guarantee exclusive access to the critical section, so why would we want to bypass that idea?

    • @CodeVault
      @CodeVault  Рік тому +1

      For example, when you're using a thread pool you might need multiple threads executing parts of a critical section simultaneously. The main point of learning about semaphores is that they are the basis of the more complex synchronization entities that exist in the pthread API (barriers, mutexes, conditions etc.)

  • @keyuwu4253
    @keyuwu4253 2 роки тому

    nice visualizations, neat and clear

  • @jiemingou2010
    @jiemingou2010 3 місяці тому

    Thank you so much!

  • @MurtagBY
    @MurtagBY 2 роки тому

    Did not understand how semaphores work from Tanenbaum's OS book chapter.
    This vid was great in giving hands-on example

  • @ibrahimaba8966
    @ibrahimaba8966 3 роки тому

    I am a python programmer but your course is realy interesting and allows me to have a better understanting of python thread api and multiprocessing. Thanks!

    • @CodeVault
      @CodeVault  3 роки тому

      Oh, does python have a similar API? I didn't know that

    • @ibrahimaba8966
      @ibrahimaba8966 3 роки тому +1

      @@CodeVault Hi, yes in python you have two standard libraries : threading and multiprocessing. You can find the same concepts such as : semaphore, lock(mutex), barrier, condition, event and some data structure(thread safe) such as Queue, Value, Array etc...!
      sorry for my english : from Paris :) .

  • @omarbouassida
    @omarbouassida Рік тому

    you such a legend sir❤

  • @ruby-vn7sz
    @ruby-vn7sz 2 роки тому

    thank you so much what a great teacher you're

  • @hichemladhari4542
    @hichemladhari4542 4 роки тому

    thank you very much, you are the best

  • @ParsaDoesThings
    @ParsaDoesThings 2 роки тому

    9:54 won't there be a race condition in this instance since both are going to try and increase the same value at the same time? what if a thread goes to sleep by the OS before hitting post() while another hits post() without interference?

    • @CodeVault
      @CodeVault  2 роки тому +2

      That's a good question. An important aspect to note here is that sem_post and sem_wait are both atomic functions. They are basically incrementing/decrementing the values in one single CPU instruction meaning that no race conditions can occur
      Race conditions occur because, usually, you have one instruction reading the data and another writing to it and, if the thread pauses in between, that data might have changed. But if you do both the read and write in the same instruction, there's no time for a race condition to even occur

  • @jameszaman3326
    @jameszaman3326 3 роки тому

    Loved your video but it would be nice if you give links to previous videos for us to refer to. Like here giving a link to the video where you wrote this thread code would have been very helpful.

    • @CodeVault
      @CodeVault  3 роки тому +2

      Oh sorry. I forgot to mention that you can find the whole course about Threads in C on the website: code-vault.net/course/6q6s9eerd0:1609007479575/lesson/v9l3sqtpft:1609091934815
      The code that I am presenting should be from this video: code-vault.net/course/6q6s9eerd0:1609007479575/lesson/18ec1942c2da46840693efe9b51f24b6

  • @aditya3984
    @aditya3984 Рік тому

    nice explanation. Thanks bro.

  • @rahul-patil
    @rahul-patil 3 роки тому +2

    Awesome explanation! Small doubt, semaphores are used to access critical sections right?
    If we are allowing multiple threads in CS using semaphores then how is data consistency achieved?
    Thanks.

    • @CodeVault
      @CodeVault  3 роки тому +4

      What do you mean? The API guarantees that sem_wait and sem_post are atomic operations and can't have race conditions

  • @lavredisgoume
    @lavredisgoume 3 роки тому

    Nice visualisation!

  • @oliveryt7168
    @oliveryt7168 3 роки тому

    Such a good explanation!

  • @SteaM992
    @SteaM992 4 роки тому +1

    It would be nice to tackle the subject of semaphores in shared memory multi-process

  • @sanjeevramduraivelu6466
    @sanjeevramduraivelu6466 4 роки тому

    Great Video, This helped me a lot!!

  • @gunnarmundt956
    @gunnarmundt956 3 роки тому

    thanks bro u are so much better than my professor

  • @stelioskoumpoyiannis8841
    @stelioskoumpoyiannis8841 11 місяців тому

    very well explained

  • @mathdx8254
    @mathdx8254 3 роки тому

    Thank you!

  • @sporkspork8556
    @sporkspork8556 Рік тому

    Good explanation

  • @Whammybird
    @Whammybird 2 роки тому

    Thank you

  • @tiwaritejaswo
    @tiwaritejaswo Рік тому

    sir, can you also please make a playlist on network/socket programming?

    • @CodeVault
      @CodeVault  Рік тому +1

      Yes, it's on my todo list

  • @Jonathan-ru9zl
    @Jonathan-ru9zl Рік тому

    This is better than the best university

  • @amirahmedhassan6087
    @amirahmedhassan6087 Рік тому

    Hello I have a question here 5:27 by wait do you mean getting blocked? Or would it still waste cpu cycles while waiting

    • @CodeVault
      @CodeVault  Рік тому

      It won't waste CPU cycles since it's actually waiting for a signal and signal handlers are handled fairly efficiently in modern OSes. (Though, some implementations could waste cycles I guess)

  • @wlcrutch
    @wlcrutch 9 місяців тому

    The sound of your keyboard is delicious…mind sharing the model?

    • @CodeVault
      @CodeVault  9 місяців тому +1

      Pretty sure it's the Corsair K65 with red switches. I don't really recommend it, honestly

  • @OlliS71
    @OlliS71 11 місяців тому

    It is better to use SysV semaphores, they are more versatile and you can wait for several operations in a SysV semaphore set at the same time. And if you want to start several threads at the same time using a semaphore, you can not only increment the semaphore by one, but directly by a "short" value. In principle, semaphores are needed relatively rarely; mutexes and condition variables, some of which are based on semaphores, are usually sufficient.

  • @DavideAicardi
    @DavideAicardi 4 роки тому +1

    Thankssssss

  • @SammanMahmoud
    @SammanMahmoud 2 роки тому

    Can we use it to create an infinite number of threads but only run a limit number at time ?

    • @CodeVault
      @CodeVault  2 роки тому

      Infinite number of threads: no. Since the OS does limit the number of threads a system can have running at a time. But you could definitely use this with a really large number of threads

  • @thosewhowish2b693
    @thosewhowish2b693 3 роки тому

    Can't I subscribe, like, a 100 times to this channel?

  • @elektronicznyjuzek9910
    @elektronicznyjuzek9910 Рік тому

    pure gold! :)

  • @samjoseph1144
    @samjoseph1144 4 роки тому

    well explained tutorial.

  • @badereddinebenhirt9675
    @badereddinebenhirt9675 3 роки тому

    Nta rojola azb knbghik fi llah❤️❤️

  • @ABBC-fg7ch
    @ABBC-fg7ch 4 роки тому

    are increment and decrement operations inside wait and post atomic?? then how it takes care race condition i.e. two thread simultaneously. pls answer.

    • @CodeVault
      @CodeVault  4 роки тому +1

      Yes, from the user's perspective, they are atomic, no need to worry about race conditions there

    • @snigdhaupadhyay5106
      @snigdhaupadhyay5106 3 роки тому

      @@CodeVault If we are using a global variable and printing its value after incrementing it.Output is dependent on order of threads execution leaading to race condition.

  • @MaddiralaKarthikcsb
    @MaddiralaKarthikcsb 2 роки тому

    sir, when we initialize the value of semaphore as 2 , two threads are running in parallel . so will there be any chance of getting the race condition while the threds are decrementing the semaphore value?? i mean like both may read 2 and decrement 2 only.

    • @CodeVault
      @CodeVault  2 роки тому +1

      No. The decrementing and incrementing operations of semaphores are atomic (effectively they execute in 1 instruction so there's no room for a race condition)

  • @SekhonSatinder
    @SekhonSatinder Рік тому

    Hello, much appreciate for all these well made videos. Could you make a video on named semaphores. On mac os, sem_init and sem_destroy apis are deprecated as unnamed semaphore is not supported by macos.

    • @CodeVault
      @CodeVault  Рік тому

      I didn't know that about MacOS. I'll look into it

  • @caseyquinn3787
    @caseyquinn3787 2 роки тому

    I feel like I understand the explanation but when I run the code, a second passes, and then all the print statements happen. Can anyone explain why this is?

    • @CodeVault
      @CodeVault  2 роки тому

      Hmm... Maybe you forgot to add a
      at the end of the printf line? Or something is wrong with the code maybe

  • @cy4n_knight_q8
    @cy4n_knight_q8 2 роки тому

    can someone explain how does it work when we set sem to 0 initially instead of 1 or 2

    • @CodeVault
      @CodeVault  2 роки тому +2

      Basically the same way except some thread needs to sem_post first before any thread that uses sem_wait can continue execution

  • @DebakantaBiswal
    @DebakantaBiswal 2 роки тому

    fd = open("a.txt", O_RDONLY);
    sem_acquire(one); ==> take_lock(one);
    sem_acquire(two); ==> take_lock(two);
    critical section;
    sem_release(one); ==> rel_lock(one);
    sem_release(two); ==> rel_lock(two);
    dup(fd);
    is above sequence correct for single process?

    • @CodeVault
      @CodeVault  2 роки тому

      I'm not exactly sure what the code is trying to achieve

  • @nishadkanago4393
    @nishadkanago4393 Рік тому

    Awesome

  • @naseerahmed3162
    @naseerahmed3162 3 роки тому

    @CodeVault . what is the editor you are using in this video . appreciated if you could share steps to install and run it on my windows machine

    • @CodeVault
      @CodeVault  3 роки тому

      There's a video about it, although you'll need a separate machine that runs Linux. It could also probably be done with WSL... but I never got it running properly

    • @sabahathunnisafnu9736
      @sabahathunnisafnu9736 3 роки тому

      Hi friend . I really love all your videos . Appreciate the great amount effort you have put in creating these videos
      Do you plan to make videos on data structures and algorithms..? Or suggest a good one to start with
      Thanks

    • @CodeVault
      @CodeVault  3 роки тому

      They are on the TODO list

  • @Zjk580
    @Zjk580 6 місяців тому

    thanks bud.

  • @evolagenda
    @evolagenda 4 роки тому

    Fantastic

  • @pranithshetty1682
    @pranithshetty1682 4 роки тому

    Could you make a video on POSIX message queues??

    • @CodeVault
      @CodeVault  4 роки тому

      They are on my TODO list

  • @uditarath0911
    @uditarath0911 13 днів тому

    could you please explain named semaphores

    • @CodeVault
      @CodeVault  5 днів тому

      I might make a video on this

  • @zxuiji
    @zxuiji 2 роки тому

    Sounds like I may have designed something similar to a semaphore that does not assume a limit, instead it leaves limiting active threads as a decision the developer can make, all they need is the char* pointer that's linked by the object I designed, I've still to get round to testing the object but the functions I designed for it amount to just 2, SeekGrip( &shared, &thread ) and FreeGrip( &shared, &thread ), no extras, you just declare your global like this: GRIP *shared = NULL; and your thread like this: GRIP thread = {NULL}; You can then extract the taken ranges just by doing something like this:
    seeking = {0};
    seeking.end = size;
    for ( GRIP *bound = shared; bound; bound = bound->next )
    {
    while ( bound->lock[0] == '\0' );
    sscanf( bound->lock, "%p", &range );
    if ( /* seeking falls in range conditions */ )
    {
    shift = range->start - range->end;
    seeking->start += shift;
    seeking->end += shift;
    }
    }
    Thinking about it now I suppose it could also be used for inter-thread communication as a pseudo queue

  • @ameynaik2743
    @ameynaik2743 3 роки тому

    If semaphore value is 0 initially does it crash the code?

    • @CodeVault
      @CodeVault  3 роки тому

      It shouldn't, you can try it!

  • @Fahodinho
    @Fahodinho 2 роки тому

    cheers

  • @navguest1740
    @navguest1740 4 роки тому

    Hi, Could you please tell me, how did you get the pthread extension in VScode?

    • @CodeVault
      @CodeVault  4 роки тому +1

      I'm using a remote version of VSCode called code-server (link here: github.com/cdr/code-server) and running this on my Linux server (So it's like I'm using VSCode on Linux) And Linux has the pthread API built in. There's this library which you can include if you're on Windows: sourceware.org/pthreads-win32/

    • @navguest1740
      @navguest1740 4 роки тому

      @@CodeVault Thanks for the reply..understood. With Linux, by default Posix libs are available. I tried integrating a standalone pthread library with Vscode on Windows. But it's not working. Anny how, i will try as you suggested.

  • @kartonology3732
    @kartonology3732 2 роки тому

    Can someone explain for me what does it mean *(int*)args please

    • @CodeVault
      @CodeVault  2 роки тому +1

      First, args is a void* so we have to cast it before using it.
      *(int*) args can be read as:
      1) cast args from a void* (void pointer) to an int* (int pointer). This is what the part `(int*) args` means
      2) Look at the address this args pointer is pointing and get the value at that address

    • @kartonology3732
      @kartonology3732 2 роки тому

      @@CodeVault The best explanation ever! thank you

  • @HeisenbergHK
    @HeisenbergHK Рік тому

    Why I can't init my semaphore?
    the return value of this function is -1
    :(((

    • @CodeVault
      @CodeVault  Рік тому

      Can you share the whole code?

  • @abdullahh-1
    @abdullahh-1 3 роки тому

    Can you pls guide me how to set up VS Code to use threads in builds. Also my sleep function doesn't seem to work, I tried your code but still sleep didn't work. I am using WSL 1

    • @CodeVault
      @CodeVault  3 роки тому

      You'll have to add "-l pthread" to the gcc parameters. I'm not sure about setting it up on WSL, haven't done that successfully yet.

  • @desireedoronzo5188
    @desireedoronzo5188 9 місяців тому

    It gives me a warning regarding the sea_init being deprecated, same for the destroy, so it won't work. Anyone knows how to resolve this?

    • @CodeVault
      @CodeVault  9 місяців тому

      Interesting. On Mac they seem to be deprecated now. You could refer to this answer for the solution: stackoverflow.com/questions/27736618/why-are-sem-init-sem-getvalue-sem-destroy-deprecated-on-mac-os-x-and-w

  • @Codality
    @Codality 2 роки тому

    THE BEST

  • @Killernochance
    @Killernochance 2 роки тому +2

    What in the world are you doing with all of these pointers??????????????

  • @jeffreydijkstra3391
    @jeffreydijkstra3391 3 роки тому

    still no clue what the purpose of a semaphore is lol :(

    • @CodeVault
      @CodeVault  3 роки тому +2

      It's just a tool like any other. Semaphores are the most basic entity for synchronization between threads. With it you can make mutexes for example.
      Look into the next videos, I show some uses in the producer/consumer problem: code-vault.net/lesson/tlu0jq32v9:1609364042686

  • @khalidsiddiqui3549
    @khalidsiddiqui3549 Рік тому

    your voice is not clear sir.
    difficulty in understanding.

    • @CodeVault
      @CodeVault  Рік тому

      I recommend using the automated captions

    • @lina-ln1oj
      @lina-ln1oj 11 місяців тому

      i believe that your voice is brilliant and intresting to hear, thanks for the video
      @@CodeVault

  • @b.u.g.i.g.a.n.g.a
    @b.u.g.i.g.a.n.g.a Рік тому

    Thank you!