What is a semaphore? How do they work? (Example in C)

แชร์
ฝัง
  • เผยแพร่เมื่อ 24 ส.ค. 2020
  • Patreon ➤ / jacobsorber
    Courses ➤ jacobsorber.thinkific.com
    Website ➤ www.jacobsorber.com
    ---
    What is a semaphore? How do they work? (Example in C) // Semaphores cause a lot of confusion for students, largely because they are super flexible and a little weird. In this video, I try to provide some insight into how they work, and an example of where you could use semaphores to coordinate different processes.
    Shared Memory Videos:
    • How to Set up Shared M...
    • Simple Shared Memory i...
    Threads and Processes:
    • Programming with Threads
    • Programming with Proce...
    ***
    Welcome! I post videos that help you learn to program and become a more confident software developer. I cover beginner-to-advanced systems topics ranging from network programming, threads, processes, operating systems, embedded systems and others. My goal is to help you get under-the-hood and better understand how computers work and how you can use them to become stronger students and more capable professional developers.
    About me: I'm a computer scientist, electrical engineer, researcher, and teacher. I specialize in embedded systems, mobile computing, sensor networks, and the Internet of Things. I teach systems and networking courses at Clemson University, where I also lead the PERSIST research lab.
    More about me and what I do:
    www.jacobsorber.com
    people.cs.clemson.edu/~jsorber/
    persist.cs.clemson.edu/
    To Support the Channel:
    + like, subscribe, spread the word
    + contribute via Patreon --- [ / jacobsorber ]
    + rep the channel with nerdy merch --- [teespring.com/stores/jacob-so...]
    Source code is also available to Patreon supporters. --- [jsorber-youtube-source.heroku...]

ความคิดเห็น • 197

  • @vaggelismanousakis6147
    @vaggelismanousakis6147 3 ปีที่แล้ว +236

    Bookmarked! If I had watched this video 6 months ago I would have passed the Operating Systems class. Thanks

    • @playerguy2
      @playerguy2 3 ปีที่แล้ว +13

      Oh, well. Better luck next time.

    • @vaggelismanousakis6147
      @vaggelismanousakis6147 3 ปีที่แล้ว +2

      @@playerguy2 thank you sir

    • @JacobSorber
      @JacobSorber  3 ปีที่แล้ว +45

      Sorry I was late. Next time around will hopefully be better. Best of luck.

    • @justinyork6006
      @justinyork6006 3 ปีที่แล้ว +14

      Literally just found this as I am trying to understand my OS assignment!

    • @vaggelismanousakis6147
      @vaggelismanousakis6147 3 ปีที่แล้ว +7

      @@justinyork6006 Keep working hard, you can do it! Same here

  • @LDS-Hellgineer
    @LDS-Hellgineer ปีที่แล้ว +3

    This video saved my sanity in a current project.
    Thank you very much, keep it up!!
    greetings from Austria

  • @af9466
    @af9466 ปีที่แล้ว

    Thank you very much! This really helped me understand semaphores and apply them in my Unix lab work.

  • @thangbui6383
    @thangbui6383 ปีที่แล้ว

    Thanks for the explanation, your video is much easier to understand than grinding at my college's book.

  • @avineshwar
    @avineshwar ปีที่แล้ว +4

    Great resource.
    It would be great if you could shed some light on memory barriers (possibly with an code-level example) and why typically we should/not worry about.

  • @mrunalsanghvi
    @mrunalsanghvi 3 ปีที่แล้ว +4

    the whole thread playlist is awesome. great learning. but please add more videos to make the threadpool more robust whenever you get time. I am feeling that i kind of missing on something there. especially handling the slow reading client or a thread pool with EPOLL could also be a great thing to learn and apply, i feel. big thumbs up for all the effort. Cheers!!.

  • @agustinlawtaro
    @agustinlawtaro ปีที่แล้ว

    Muchas gracias, Jacob!!! Clarísimo.

  • @Email1212xxpl
    @Email1212xxpl 3 ปีที่แล้ว +1

    This channel is really good mate :) Keep on doing great stuff.

  • @kartikeykushwah3926
    @kartikeykushwah3926 ปีที่แล้ว

    Really needed to know!

  • @DipietroGuido
    @DipietroGuido 3 ปีที่แล้ว

    Wonderful explanation. Everything makes sense now!

  • @sd_1989
    @sd_1989 3 ปีที่แล้ว

    Wonderful explanation. Thank you for your time.

  • @peepypoopy8917
    @peepypoopy8917 3 ปีที่แล้ว +21

    This is what I needed. Would be cool, if you were to make a video on Mutexes also and some sort of concrete comparison between the binary semaphore and a mutex. Thank you.

    • @kylek.3689
      @kylek.3689 2 ปีที่แล้ว +10

      It's a bit late to respond but I can share anyways,
      In a Mutex, only the thread that originally locked it can unlock it, but with a semaphore, any thread can wait() or post(), regardless of who has called what.

  • @chadj1797
    @chadj1797 2 ปีที่แล้ว +10

    This is exactly what I needed for my OS class assignment.
    Thanks Jacob, cheers from India. You're doing God's work!

  • @NGBigfield
    @NGBigfield 3 ปีที่แล้ว

    Such a great hand-on example and explanation . Thank you!

    • @JacobSorber
      @JacobSorber  3 ปีที่แล้ว

      You're very welcome! Glad you enjoyed it.

  • @miserablepile
    @miserablepile 3 ปีที่แล้ว

    Thank you for your very clear explanation!

  • @MazharKhan-jm7pj
    @MazharKhan-jm7pj 3 ปีที่แล้ว +2

    Hi Jacob,
    Thanks a lot! your videos are very informative and helpful. I have a request, can you make some videos on Operating system basic concepts like what are user and kernel modes? What are system calls and what is a Linux boot up process etc.

  • @friction5001
    @friction5001 3 ปีที่แล้ว

    this was very helpful and well explained cheers mate

  • @DetectivePoofPoof
    @DetectivePoofPoof 3 ปีที่แล้ว +23

    Huh, that's cool, I always thought it sounded like some kind of civil engineering structure.
    "Oh we used to have _dreadful_ floods back in the day! But then the gu'vment came and they installed that 'ol semaphore up the creek and we've been as dry as a nun's gusset ever since!"

  • @Sir.NathanDrake
    @Sir.NathanDrake 3 ปีที่แล้ว

    Nice video. Thanks for the clear explaination!

  • @ReD4eva94
    @ReD4eva94 3 ปีที่แล้ว

    Amazing. Best instructor on youtube.

  • @dusan7777
    @dusan7777 3 ปีที่แล้ว +36

    "Getting fancy with semaphores is a great way to end up with software that doesn't work"
    I laughed so much at that :D
    Great explanation tho, thanks a bunch

    • @rickarmbruster8788
      @rickarmbruster8788 3 ปีที่แล้ว

      but fixing it is kind of easy because you know directly that you caused trouble with 01010 sequences that just doesnt seem to run in the right order ^^

  • @shamanthakrishnakg1978
    @shamanthakrishnakg1978 3 ปีที่แล้ว +2

    Man !!!!, Damn!!!! ,you are doing a great help to the humanity, your videos are very helpful,thank you very much. 🙏🙏🙏

  • @yonasmulissa5083
    @yonasmulissa5083 2 ปีที่แล้ว

    Very helpful video. Thank you for making this. One little comment: To get all points across easily can you please slow down the pace at which you speak and take a brief pause in between important points?

  • @mrcrackerist
    @mrcrackerist 3 ปีที่แล้ว +7

    semaphore sound interesting, but thanks to this video I am going to lookup atomic types more.
    Thanks :)

  • @thepickicool97
    @thepickicool97 3 ปีที่แล้ว +3

    thank you for saving my semester

  • @othmanekaramat1962
    @othmanekaramat1962 ปีที่แล้ว

    Insane, Thank you !

  • @tianrongwang5061
    @tianrongwang5061 2 ปีที่แล้ว

    Hi Jacob. You are a great teacher. Your videos are helping me a lot with my OS course project. Thank you!

    • @JacobSorber
      @JacobSorber  2 ปีที่แล้ว

      You're welcome. Glad I could help.

  • @aravinds123
    @aravinds123 2 ปีที่แล้ว

    The best video on semaphores! Wonderful!

  • @bartekburmistrz8679
    @bartekburmistrz8679 3 ปีที่แล้ว

    Amnazing Tutorial, well explained, a very good example, helped me a lot. Thanks!

    • @JacobSorber
      @JacobSorber  3 ปีที่แล้ว +1

      You're welcome. Glad it helped.

  • @luizfrancois7472
    @luizfrancois7472 3 ปีที่แล้ว

    Very good lesson.Thanks!

  • @techtechlearn3438
    @techtechlearn3438 ปีที่แล้ว

    Great video !!!!!!!!!!

  • @artemiocabrillosjr.244
    @artemiocabrillosjr.244 3 ปีที่แล้ว +1

    Well explained. Thanks !

  • @alexandersteinbach3555
    @alexandersteinbach3555 3 ปีที่แล้ว +1

    Finally I understand it xD Thank you!

  • @miladgolfam49
    @miladgolfam49 ปีที่แล้ว +1

    Matthew Mcconaughey is teaching C, I loved the this tutorial thanks.

  • @CheesyAceGameplay
    @CheesyAceGameplay หลายเดือนก่อน

    I use semaphores for when I'm writing to a queue in one or multiple threads... and another thread is dequeing (removing) items from the queue.

  • @cambrown5777
    @cambrown5777 ปีที่แล้ว

    Using this to prep for Northwestern's OS midterm, didn't realize that Prof. Hester took a job at GT! Sad, but glad he's killing it

    • @JacobSorber
      @JacobSorber  ปีที่แล้ว +1

      Yeah, it will be nice to have him closer to home. I'm sure he'll be missed up north. Good luck on your midterms.

  • @unknown2.03
    @unknown2.03 ปีที่แล้ว +1

    Hi can you please tell me where this named semaphore has been stored after creation as mine. # cd /dev/shm directory isn't having my named semaphore.

  • @kakerlakenmilch1155
    @kakerlakenmilch1155 3 หลายเดือนก่อน

    I love this stuff

  • @dduartez99
    @dduartez99 3 ปีที่แล้ว +1

    you literally saved me, I have a project due on the 2nd of sept on System V shared mem and semaphores and didn't know a thing

  • @olegkuzenko
    @olegkuzenko 3 ปีที่แล้ว

    Thank you. Great video!

  • @abdelrhmanahmed1378
    @abdelrhmanahmed1378 ปีที่แล้ว

    should it be convenient to use cv(conditional variables) in consumer producer case ?

  • @subhasishnathdev
    @subhasishnathdev 2 ปีที่แล้ว

    Thanks a lot.

  • @georgioziozas9031
    @georgioziozas9031 3 ปีที่แล้ว

    great video , bookmarked. waiting for a video which uses processes and semaphores , a full example ! gj

  • @tete1445
    @tete1445 3 ปีที่แล้ว

    Thanks Jacob! I've been following your videos recently and they're all informative and amazing. I gotta say I'm quite intrigued!
    I have a few questions though;
    I'm learning C++ and I've gotten to an "advanced beginner level" and I wonder if I get to a decent level in C++, will the transition to C be easier? And what about Java, C#, python etc?
    Second question, can these programming concepts (such as Semaphores and whatnot) also apply to C++?
    Thank you!

    • @JacobSorber
      @JacobSorber  3 ปีที่แล้ว +3

      Yes, you can (in most cases) think of C++ as just C with a set of language extensions. So, anything you can do in C, you can generally also do in C++. So, learning C++ will strengthen your C, and vice versa. And, any of my C examples, can be used in C++. You just have to keep in mind, what is supported in each language.
      How this relates to java, python, c#, etc, really requires a longer answer than I can give in a comment. Maybe in a future video.

    • @tete1445
      @tete1445 3 ปีที่แล้ว

      @@JacobSorber Thanks, Jacob. I intend to transition to other languages once I feel unstoppable in C++ haha.

  • @yaroslavpanych2067
    @yaroslavpanych2067 5 หลายเดือนก่อน

    Okay, 1st: in what world barries is anyhow part of IPC? Should I listen to the rest, or it is adequate?

  • @eddaly4160
    @eddaly4160 ปีที่แล้ว +1

    Great video. As usual. Seems like there could easily be a case were I can post repeatedly with out a wait happening because my "consumer" is busy or unresposive...do I need to guard against an overflow on a semaphore...if so, is there a proper way to deal with this scenario?

  • @SteaM992
    @SteaM992 3 ปีที่แล้ว +1

    Hello. Thank you for sharing your knowledge with us. Could you recommend a book or 2 that goes deeper into the subject of semaphores, memory allocation in unix like OS (mmap), file descriptors, etc. Thank you!

  • @00killerix
    @00killerix 2 ปีที่แล้ว

    Which sw are u using for your videos?

  • @erkanturan7215
    @erkanturan7215 3 ปีที่แล้ว

    Another awesome video... thanks a lot

  • @sanketgandhi3139
    @sanketgandhi3139 3 ปีที่แล้ว +1

    Thank you for such nice video.
    One small request if possible please make videos on mutex locks and condition variables

  • @quocvnguyen7117
    @quocvnguyen7117 2 ปีที่แล้ว

    Thank you so much,
    This is very helpful.

  • @teeceetime2
    @teeceetime2 3 ปีที่แล้ว

    Liked and subscribed! This presentation format is superb and I hope more people come across your channel!

  • @dieTbshow
    @dieTbshow ปีที่แล้ว

    Can somebody explain to me what exactly the parameters for sem_open(...) express?

  • @khoaang5712
    @khoaang5712 2 ปีที่แล้ว

    Thank you, your video is very helpful

  • @sumaiyatasnim102
    @sumaiyatasnim102 3 ปีที่แล้ว

    Very helpful. Is there any way to manipulate times (like arrival time or wait time in FCFS order of something) using semaphore and multiple threads.

  • @n0kodoko143
    @n0kodoko143 2 ปีที่แล้ว

    now I'm understanding where go channels come from. (You also caught me immediately thinking about named pipes... guess I'll be using that in my own time). -- love the shirt!

  • @benjaminshinar9509
    @benjaminshinar9509 3 ปีที่แล้ว

    can you do one about microsoft zune?

  • @lcsmuller
    @lcsmuller 3 ปีที่แล้ว

    Thank you this helps me a lot! Could you perhaps make a video on the topic of C11 atomics?

    • @glumfish6862
      @glumfish6862 3 ปีที่แล้ว

      Yeah there is very little info on them

  • @user-cf5uf9sw1u
    @user-cf5uf9sw1u 3 ปีที่แล้ว

    Awesome, as always

  • @eanerickson8915
    @eanerickson8915 3 ปีที่แล้ว

    Best description ever!

    • @JacobSorber
      @JacobSorber  3 ปีที่แล้ว

      Thanks. Glad you enjoyed it.

  • @anelemadonda6191
    @anelemadonda6191 3 ปีที่แล้ว

    fanks a lot Mr Sorber

  • @mohammedzaid6634
    @mohammedzaid6634 3 ปีที่แล้ว

    hey jacob thanks to you. your videos and teaching methods are very clear and its so helpful to understand how network programming works and what are thread process etc. so much help from you to community. and i think most of us(students) don't know what do to with network programming so please make video about some of YOUR ideas or project ideas or useful theories about network programming in C.

    • @JacobSorber
      @JacobSorber  3 ปีที่แล้ว

      I have some network programming videos. And, I'm sure there will be more in the future. Let me know what you think I'm missing.

    • @mohammedzaid6634
      @mohammedzaid6634 3 ปีที่แล้ว

      @@JacobSorber i already watched your whole network programming videos and i wrote all of codes on my own its really helped me. but i am still confused what are the core usefulness of network programming? and how does peer-to-peer network works? how can i develop onion routed chat program using C with a central server? how real world encrypted communication looks like? what encryption use to?
      these are the questions that confuse me a LOT!!!! i hope, you will do something about it
      thanks for your kind reply!!! :)

  • @abhisheksa6635
    @abhisheksa6635 7 หลายเดือนก่อน

    Why not discuss all other stuffs like spin lock, barriers, monitors and usage case basis, will be a great add.

  • @robinjohannsen5029
    @robinjohannsen5029 3 ปีที่แล้ว

    great video !

  • @gabrielkolletalves493
    @gabrielkolletalves493 ปีที่แล้ว

    2:20 strange... you're presenting the code as if it's a kind of busy waiting (thread locks in while loop untill the variable is incremented by another thread) - but semaphores are not a kind of busy waiting. I'm having a hard time trying to differenciate that from a simple "N" variable who counts the buffer size.

  • @konulaliyevaa
    @konulaliyevaa 2 ปีที่แล้ว

    Thanks for the awesome video.

  • @thinktubers
    @thinktubers 2 ปีที่แล้ว +1

    Hello sir,
    Thanks you for the video , I tried the same but when I try to run the program am getting error like sem_open/producer: no such file or directory
    Am I doing anything wrong , if anyone know how to solve this issue , please help me to solve this problem
    Thank you !

    • @thinktubers
      @thinktubers 2 ปีที่แล้ว

      Hello??

    • @jesusalanlopezaldaran1663
      @jesusalanlopezaldaran1663 7 หลายเดือนก่อน

      Hello!
      I had the following problem: "shm_open no such file or directory"
      I solved it by changing "IPC_CREAT" to "O_CREAT". To make this change you will need to import the library "#include "
      "O_CREAT" creates the semaphore if it does not exist.
      Here is the code snippet for the "readshmem.c" file
      ...
      //setup some semaphores
      sem_unlink(SEM_CONSUMER_FNAME);
      sem_unlink(SEM_PRODUCER_FNAME);
      sem_t *sem_prod = sem_open(SEM_PRODUCER_FNAME, O_CREAT, 0660, 0);
      if(sem_prod == SEM_FAILED){
      dogr("sem_open/producer");
      exit(EXIT_FAILURE);
      }
      sem_t *sem_cons = sem_open(SEM_CONSUMER_FNAME, O_CREAT, 0660, 1);
      if(sem_cons == SEM_FAILED){
      dogr("sem_open/consumer");
      exit(EXIT_FAILURE);
      }
      ...

  • @user-is2uw7ee3u
    @user-is2uw7ee3u 2 ปีที่แล้ว

    Thanks, it's very helpful,

  • @l0_0l45
    @l0_0l45 3 ปีที่แล้ว +2

    Please make a video on lock free programming! Cpp con has a talk on it but it was way too difficult to understand!

    • @lewiscole5193
      @lewiscole5193 3 ปีที่แล้ว +1

      Well, if he does do a vide on lock free programming, I would suggest that he might as well deal with wait free programming either in the same video or in it's own.

  • @cyberceel
    @cyberceel 3 ปีที่แล้ว

    Very well made 👍

  • @anon_y_mousse
    @anon_y_mousse 2 ปีที่แล้ว

    I like the messages you send to the consumer. Used to hear that all the time as a kid.

  • @marcel151
    @marcel151 3 ปีที่แล้ว

    2 months earlier and I would have a better degree in my exam about operating systems. Thanks for that video.

  • @m4l490n
    @m4l490n 3 ปีที่แล้ว +19

    Be careful with semaphores, can turn things very confusing and bugs would be hard to find.

    • @JacobSorber
      @JacobSorber  3 ปีที่แล้ว +15

      Amen.

    • @u263a3
      @u263a3 3 ปีที่แล้ว

      Just because it’s there doesn’t mean you need to use it

  • @shivaprasad4379
    @shivaprasad4379 2 ปีที่แล้ว

    Please post more videos on other IPC mechanisms and Synchronization concepts, bottlenecks & downsides around that code in production environment

  • @dibbyabarua9499
    @dibbyabarua9499 3 ปีที่แล้ว +1

    Very good description of semaphores... very clear and concise! Could you suggest me a good book for Operating Systems? I am using two books right now: (1) OS by William Stallings and (2) OS Principles and Practice by Thomas Anderson and Mike Dahlin. I like the one by Anderson more than the Stallings one so far but I was wondering if there is any better suggestion?

    • @JacobSorber
      @JacobSorber  3 ปีที่แล้ว +1

      I use Anderson & Dahlin in my class. It's probably my favorite, at the moment.

    • @dibbyabarua9499
      @dibbyabarua9499 3 ปีที่แล้ว

      @@JacobSorber ya. I like that one too haha

  • @jaminyah
    @jaminyah 3 ปีที่แล้ว +1

    Hi Professor! I noticed that in the following code using a named semaphore:
    ```code
    int main(int argc, char* argv[]) {
    sem_t *sem = sem_open("/ex_sem", O_CREAT, O_RDWR, 1);
    if (sem == SEM_FAILED) {
    printf("Failed to acquire semaphore
    ");
    printf("%d
    ", errno);
    return -1;
    }
    ...
    return 0;
    ```
    On the second run of this code segment sem is assigned SEM_FAILED. Is there a way to check for an error and still have the code run any number of times after it is compiled? Without error checking there are no issues. Thanks.

  • @mustafakarakas1116
    @mustafakarakas1116 2 ปีที่แล้ว

    I was struggling with my midterm project. I did not even know what is wrong. I was feeling useless. Then I wrote those unlink lines and everything works just perfect. Thanks.

  • @arlenegrundy7671
    @arlenegrundy7671 9 หลายเดือนก่อน

    Very good info. Is it possible to enlarge the screen you use. It's somewhat difficult to actual read along. Just a friendly request...thanks.

  • @cubedude76
    @cubedude76 18 วันที่ผ่านมา

    Thanks for the good video, but please get a filter to smooth out the s's they are very loud

    • @JacobSorber
      @JacobSorber  17 วันที่ผ่านมา

      I've added one since this video was recorded. Has your listening experience improved?

  • @bhaveshvarma4313
    @bhaveshvarma4313 ปีที่แล้ว

    I wasted 2 hours to exactly understand but don't get difference b/w mutex and semaphore , But only one line makes clear that any thread can call post(Signal) where in mutex owned by thread only that thread can release it.

  • @bastawa
    @bastawa 3 ปีที่แล้ว

    great stuff! would love to see a video about lock-free queues! thanks🐼🐼🐼🐼🐼🐼

    • @lewiscole5193
      @lewiscole5193 3 ปีที่แล้ว

      I like lock-free and wait-free programming too, but when it's appropriate, namely when there's a reasonably possibility that the thread that wants to lock a structure (a list, queue, or stack) can die before it unlocks the structure thereby leaving other threads hanging in the wind ... something that is really *BAD NEWS* in an OS say.
      If that's not something that can happen or if you choose to ignore its possibility, the traditional locking methods work better (i.e. lower overhead) and are easier to understand.
      So while I wouldn't mind seeing Dr. Sorber cook up a video or two on lock free and/or wait free programming, I'm curious what the motivation is for others.
      Do you have something in particular in mind or are you just interested in the ideas behind it or something else entirely?

  • @cricket9282
    @cricket9282 3 ปีที่แล้ว

    hey! im not entirely sure where to ask you questions like this and i apologize for the off topic-ness, but,
    how do you feel about nim?

  • @greatbullet7372
    @greatbullet7372 3 ปีที่แล้ว

    I love you so much :D

  • @michaelkotthaus7120
    @michaelkotthaus7120 3 ปีที่แล้ว +1

    Again a very interesting topic, Jacob. (Also very important in embedded real-time applications.)
    I wonder, if a simple mutex (or a binary semaphore) was more simple for the example. With only a single mutex, the writer (producer) has to check by the first byte (block[0]==0) if the reader (consumer) has already processed the data.

  • @thanakornpasangthien5590
    @thanakornpasangthien5590 3 ปีที่แล้ว

    Please do OS videos

  • @caesar104
    @caesar104 3 ปีที่แล้ว +4

    Could you make videos about library and its functions and also Annex K functions ? Are these functions necessary? Btw your videos are awesome and very informative. I learned more than my college lessons :) Thank you very much for your great effort sir ,please keep going :)

  • @carl-johanlandin1419
    @carl-johanlandin1419 2 ปีที่แล้ว

    Not sure why but I fail to run this code on my machine. It seems it exits here at this segment:
    ```
    sem_t *sem_prod = sem_open(SEM_PRODUCER_FNAME, IPC_CREAT, 0660, 0);
    if (sem_prod == SEM_FAILED) {
    perror("sem_open/producer");
    exit(EXIT_FAILURE);
    }
    ```
    And the terminal output looks like this:
    ```
    ❯ ./readshmem.elf
    sem_open/producer: No such file or directory
    ```
    Any ideas why? I'm running Linux Debian and have tried to compile with both clang and gcc. I noticed you didn't need to link to pthreads library in your makefile which I had to do for a build to work.

    • @carl-johanlandin1419
      @carl-johanlandin1419 2 ปีที่แล้ว

      It seems the directory /dev/sem did not exist on my OS, creating that folder solved the issue. Really like your videos btw! The best C content on youtube.

  • @adambishop328
    @adambishop328 2 ปีที่แล้ว

    LOL the API Soup is definitely relatable. Just getting into socket programming in C and it has like 5 different structs of structs that are predefined

  •  3 ปีที่แล้ว

    Hello! I was able to make this code work in my mac, but when I try to run in a ubuntu machine, I cant. I added the flag -lpthread which allowed me to compile, but after that, I get the error "sem/open/producer: No such file or directory". What could it be?

    • @vapula87
      @vapula87 2 ปีที่แล้ว

      Hey Éverton! Not sure if you ever figured it out, but semaphores on Linux are implemented slightly differently.

  • @SleepyChoco
    @SleepyChoco 8 หลายเดือนก่อน

    👍👍👍

  • @honey-py9pj
    @honey-py9pj 3 ปีที่แล้ว

    when we write in the producer function : ```sem_wait(sem_cons) ```, is it because we don't want to write anything , until a reader is avaible to read it (consume it?) ?

  • @justusmzb7441
    @justusmzb7441 3 ปีที่แล้ว

    I have to wonder: Our professor presented semaphores as if they did not contain a busy wait. But your presentation clearly suggests that they do.
    Would it be possible eg. for the wait() function to put the process into a ready state so that it can try again when it is next dispatched, and the core it would elsewise be used for a busy wait is free for use?

  • @migtvill
    @migtvill หลายเดือนก่อน

    Can someone here link non-youtube studies of atomic studies of semaphores outside of programming, as in physical or physics applications?🎉

  • @jonathanluo0
    @jonathanluo0 3 ปีที่แล้ว +3

    In my ubuntu 18.04.2 system, in semaphore reader, "sem_t *sem_prod = sem_open(SEM_PRODUCER_FNAME, IPC_CREAT, 0660, 0)" failed, but "sem_t *sem_prod = sem_open(SEM_PRODUCER_FNAME, O_CREAT, 0660, 0)" succeeded

    • @sidneysherrill4870
      @sidneysherrill4870 3 ปีที่แล้ว +1

      I'm experiencing the same thing

    • @maryjonson9692
      @maryjonson9692 3 ปีที่แล้ว +1

      Yeah, changing IPC_CREAT with O_CREAT solved the issues for me !

  • @marios2liquid
    @marios2liquid 3 ปีที่แล้ว

    I had an issue recently were i was compiling many files and linking them together that had globals in them and externed all over the place with a global .h file. The globals were also volatiles. During runtime I could see that some variables were not correctly initialized as they were in the source. Compiling with different versions of GCC (>6) fixed this issue but introduced other issues. My gut says that the compiler running at -Os was basically optimizing these variables out (but not triggering any warning with -Wall, -pedantic since they were used in other files). I ended up converting all the globals to statics in each file with API access functions if another file wanted to access them or edit them.. That solved the issue.. I searched countless hours about translation units, shadow variables, or any other things that could happen. Couldnt find any. Also tried removing -Os but since this project was embedded it was introducing other issues (speed of execution and interrupts was messed up and the binary size was too big). I was able to also compile this project to x86 in addition to ARM and this bug wasnt present, sigh.. Compilers that had issues were GCC 4.8-6.

    • @JacobSorber
      @JacobSorber  3 ปีที่แล้ว

      That sounds rough. Glad you got it sorted out.

  • @socat9311
    @socat9311 3 ปีที่แล้ว

    Bit irrelevant q: what specs and software you have for the screen capture? Quality is pretty good. Trying to do it from my laptop and ends up rubbish...

  • @joseville
    @joseville 3 ปีที่แล้ว

    v is the value of the semaphore, right?
    At 2:59, thread A calls wait(), v is > 0, so v-- and then returns, right? (I'm following the code for wait() at 2:20)

    • @lewiscole5193
      @lewiscole5193 3 ปีที่แล้ว +1

      Correct. "v" is the value of the semaphore.
      The important thing to keep in mind is that the pseudo-code that Professor Sorber gave for the semaphore primitives is for show and have the theoretical potential to allow a process to get stuck indefinitely waiting on the semaphore.
      I'm sure that Professor Sorber is well aware of this fact and only gave the pseudo-code examples to show what conceptually goes on which is why he says don't compile it.
      On the off-chance that someone thinks that the code looks close enough and tries to use it anyway, one should be aware that while it'll probably always work, there is a very remote chance that it won't, so stick with a library implementation which will no doubt call an OS routine to actually get there from here.

  • @koraytecimer6080
    @koraytecimer6080 3 ปีที่แล้ว

    ohh now I get it thanks!

  • @shobhitmishra92
    @shobhitmishra92 3 ปีที่แล้ว

    If possible it would be utmost helpful to see video just like semaphores(with an example) for inter-processes synchronization with mutex and monitors

    • @shobhitmishra92
      @shobhitmishra92 3 ปีที่แล้ว

      I was trying your code and while compilation it started throwing undefined reference error for the sem functions, I am using a gcc compiler on ubuntu distro , i after adding the -pthread with -lrt flags it started to work

  • @tktktktktk826
    @tktktktktk826 3 ปีที่แล้ว

    Wouldn't it be easier to use 1 mutex in the example shown? Or am I missing something?