What does it mean by code holding semaphore can be preempted











up vote
0
down vote

favorite












I was going through the Robert Love Book and was bit confused about this line. What does it mean by code holding semaphore can be preempted?



If an interrupt occurs accessing the same variable which the user-space application has while it is executing the code in critical section then the user-space application can be preempted?



If my above understanding is true then there is no other alternative than spin-locks to disable an interrupt whenever an user-space application is in critical section?



So what is the use of semaphore in the context of OS? Interrupts might occur anytime when the user application is in critical section and in-order to avoid interrupt intervention we need to use spin-locks all the time.










share|improve this question






















  • The problem with your question is that we do not know the context. What kind of semaphore? It is possible for a kernel to implement a semaphore so that the process holding it cannot be preempted. Or it a semaphore could be implement so that the process holding it could not be preempted.
    – user3344003
    Nov 2 at 22:32










  • Yes, but according to my understanding we can't implement a semaphore which cannot be preempted right when the interrupt occurs?
    – Smita
    Nov 2 at 22:59










  • You could block preemption as part of grabbing the semaphore. That's not normal but it could be done.
    – user3344003
    Nov 3 at 0:21















up vote
0
down vote

favorite












I was going through the Robert Love Book and was bit confused about this line. What does it mean by code holding semaphore can be preempted?



If an interrupt occurs accessing the same variable which the user-space application has while it is executing the code in critical section then the user-space application can be preempted?



If my above understanding is true then there is no other alternative than spin-locks to disable an interrupt whenever an user-space application is in critical section?



So what is the use of semaphore in the context of OS? Interrupts might occur anytime when the user application is in critical section and in-order to avoid interrupt intervention we need to use spin-locks all the time.










share|improve this question






















  • The problem with your question is that we do not know the context. What kind of semaphore? It is possible for a kernel to implement a semaphore so that the process holding it cannot be preempted. Or it a semaphore could be implement so that the process holding it could not be preempted.
    – user3344003
    Nov 2 at 22:32










  • Yes, but according to my understanding we can't implement a semaphore which cannot be preempted right when the interrupt occurs?
    – Smita
    Nov 2 at 22:59










  • You could block preemption as part of grabbing the semaphore. That's not normal but it could be done.
    – user3344003
    Nov 3 at 0:21













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I was going through the Robert Love Book and was bit confused about this line. What does it mean by code holding semaphore can be preempted?



If an interrupt occurs accessing the same variable which the user-space application has while it is executing the code in critical section then the user-space application can be preempted?



If my above understanding is true then there is no other alternative than spin-locks to disable an interrupt whenever an user-space application is in critical section?



So what is the use of semaphore in the context of OS? Interrupts might occur anytime when the user application is in critical section and in-order to avoid interrupt intervention we need to use spin-locks all the time.










share|improve this question













I was going through the Robert Love Book and was bit confused about this line. What does it mean by code holding semaphore can be preempted?



If an interrupt occurs accessing the same variable which the user-space application has while it is executing the code in critical section then the user-space application can be preempted?



If my above understanding is true then there is no other alternative than spin-locks to disable an interrupt whenever an user-space application is in critical section?



So what is the use of semaphore in the context of OS? Interrupts might occur anytime when the user application is in critical section and in-order to avoid interrupt intervention we need to use spin-locks all the time.







linux operating-system synchronization semaphore spinlock






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 2 at 22:04









Smita

336




336












  • The problem with your question is that we do not know the context. What kind of semaphore? It is possible for a kernel to implement a semaphore so that the process holding it cannot be preempted. Or it a semaphore could be implement so that the process holding it could not be preempted.
    – user3344003
    Nov 2 at 22:32










  • Yes, but according to my understanding we can't implement a semaphore which cannot be preempted right when the interrupt occurs?
    – Smita
    Nov 2 at 22:59










  • You could block preemption as part of grabbing the semaphore. That's not normal but it could be done.
    – user3344003
    Nov 3 at 0:21


















  • The problem with your question is that we do not know the context. What kind of semaphore? It is possible for a kernel to implement a semaphore so that the process holding it cannot be preempted. Or it a semaphore could be implement so that the process holding it could not be preempted.
    – user3344003
    Nov 2 at 22:32










  • Yes, but according to my understanding we can't implement a semaphore which cannot be preempted right when the interrupt occurs?
    – Smita
    Nov 2 at 22:59










  • You could block preemption as part of grabbing the semaphore. That's not normal but it could be done.
    – user3344003
    Nov 3 at 0:21
















The problem with your question is that we do not know the context. What kind of semaphore? It is possible for a kernel to implement a semaphore so that the process holding it cannot be preempted. Or it a semaphore could be implement so that the process holding it could not be preempted.
– user3344003
Nov 2 at 22:32




The problem with your question is that we do not know the context. What kind of semaphore? It is possible for a kernel to implement a semaphore so that the process holding it cannot be preempted. Or it a semaphore could be implement so that the process holding it could not be preempted.
– user3344003
Nov 2 at 22:32












Yes, but according to my understanding we can't implement a semaphore which cannot be preempted right when the interrupt occurs?
– Smita
Nov 2 at 22:59




Yes, but according to my understanding we can't implement a semaphore which cannot be preempted right when the interrupt occurs?
– Smita
Nov 2 at 22:59












You could block preemption as part of grabbing the semaphore. That's not normal but it could be done.
– user3344003
Nov 3 at 0:21




You could block preemption as part of grabbing the semaphore. That's not normal but it could be done.
– user3344003
Nov 3 at 0:21












1 Answer
1






active

oldest

votes

















up vote
1
down vote













What does it mean by code holding semaphore can be preempted?

It means that a process that is currently running in its critical section holding some lock for the purpose of synchronization can be preempted. Ideally interrupts have the highest
priority, so unless you disable the interrupt on that processor core, the running process
can be preempted and that might happen while the process is in its critical section.



While there are multiple spin_lock_XXX apis to disable interrupts, you might want to use
the spin_lock_irqsave as it saves the interrupt flags on that core and restores them while releasing the lock.






share|improve this answer








New contributor




AbhishekChoubey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53126351%2fwhat-does-it-mean-by-code-holding-semaphore-can-be-preempted%23new-answer', 'question_page');
    }
    );

    Post as a guest
































    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    What does it mean by code holding semaphore can be preempted?

    It means that a process that is currently running in its critical section holding some lock for the purpose of synchronization can be preempted. Ideally interrupts have the highest
    priority, so unless you disable the interrupt on that processor core, the running process
    can be preempted and that might happen while the process is in its critical section.



    While there are multiple spin_lock_XXX apis to disable interrupts, you might want to use
    the spin_lock_irqsave as it saves the interrupt flags on that core and restores them while releasing the lock.






    share|improve this answer








    New contributor




    AbhishekChoubey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      1
      down vote













      What does it mean by code holding semaphore can be preempted?

      It means that a process that is currently running in its critical section holding some lock for the purpose of synchronization can be preempted. Ideally interrupts have the highest
      priority, so unless you disable the interrupt on that processor core, the running process
      can be preempted and that might happen while the process is in its critical section.



      While there are multiple spin_lock_XXX apis to disable interrupts, you might want to use
      the spin_lock_irqsave as it saves the interrupt flags on that core and restores them while releasing the lock.






      share|improve this answer








      New contributor




      AbhishekChoubey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















        up vote
        1
        down vote










        up vote
        1
        down vote









        What does it mean by code holding semaphore can be preempted?

        It means that a process that is currently running in its critical section holding some lock for the purpose of synchronization can be preempted. Ideally interrupts have the highest
        priority, so unless you disable the interrupt on that processor core, the running process
        can be preempted and that might happen while the process is in its critical section.



        While there are multiple spin_lock_XXX apis to disable interrupts, you might want to use
        the spin_lock_irqsave as it saves the interrupt flags on that core and restores them while releasing the lock.






        share|improve this answer








        New contributor




        AbhishekChoubey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        What does it mean by code holding semaphore can be preempted?

        It means that a process that is currently running in its critical section holding some lock for the purpose of synchronization can be preempted. Ideally interrupts have the highest
        priority, so unless you disable the interrupt on that processor core, the running process
        can be preempted and that might happen while the process is in its critical section.



        While there are multiple spin_lock_XXX apis to disable interrupts, you might want to use
        the spin_lock_irqsave as it saves the interrupt flags on that core and restores them while releasing the lock.







        share|improve this answer








        New contributor




        AbhishekChoubey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        share|improve this answer



        share|improve this answer






        New contributor




        AbhishekChoubey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered Nov 8 at 10:04









        AbhishekChoubey

        111




        111




        New contributor




        AbhishekChoubey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        AbhishekChoubey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        AbhishekChoubey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53126351%2fwhat-does-it-mean-by-code-holding-semaphore-can-be-preempted%23new-answer', 'question_page');
            }
            );

            Post as a guest




















































































            Popular posts from this blog

            Schultheiß

            Liste der Kulturdenkmale in Wilsdruff

            Android Play Services Check