Search code examples
parallel-processingembedded-linuxembedded-resourceinterprocessinter-process-communicat

Avoid Application[process] switching for shared resource in linux


Shared resource is used in two application process A and in process B. To avoid race condition, decided that when executing portion of code dealing with shared resource disable context switching and again enable process switching after exiting shared portion of process.

But don't know how to avoid process switching to another process, when executing shared resource part and again enable process switching after exiting shared portion of process.

Or is there any better method to avoid race condition?

Regards,
Learner


Solution

  • But don't know how to avoid process switching to another process, when executing shared resource part and again enable process switching after exiting shared portion of process.

    You can't do this directly. You can do what you want with kernel help. For example, waiting on a Mutex, or one of the other ways to do IPC (interprocess communication).

    If that's not "good enough", you could even make your own kernel driver that has the semantics you want. The kernel can move processes between "sleeping" and "running". But you should have good reasons why existing methods don't work before thinking about writing your own kernel driver.

    Or is there any better method to avoid race condition?

    Avoiding race conditions is all about trade-offs. The kernel has many different IPC methods, each with different characteristics. Get a good book on IPC, and look into how things like Postgres scale to many processors.