0

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

Embedded Programmer
  • 519
  • 4
  • 8
  • 22
  • You'll need to come up with a better idea, as you don't get to do that in user mode. – Chris Stratton Jan 28 '14 at 16:44
  • Do you have any idea to do? – Embedded Programmer Jan 29 '14 at 01:11
  • Instead of trying to prevent context switching *away* from the busy process (which is not allowed), focus your attention on preventing switching *to* the process that should not run until the other has completed the critical task. A blocking call to a synchronization mechanism which will not return until you are ready for that process to continue should work. – Chris Stratton Jan 29 '14 at 02:29
  • 1
    Why don't use mutex, semaphores or similar mechanism ? – Mali Jan 30 '14 at 10:00

3 Answers3

1

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.

BraveNewCurrency
  • 12,654
  • 2
  • 42
  • 50
0

For all user space application, and vast majority of kernel code, it is valid that you can't disable context switching. The reason for this is that context switching is not responsibility of application, but operations system.

In scenario that you mentioned, you should use a mutex. All processes must follow convention that before accessing shared resource, they acquire mutex, and after they are done with accessing shared resource, they release the mutex.

Lets say an application accessing the shared resource acquired mutex, and is doing some processing of shared resource, and that operating system performed context switch, thus stopping the application from processing shared resource. OS can schedule other processes wanting to access shared resource, but they will be in waiting state, waiting for mutex to be released, and none of such processes will not do anything with shared resource. After certain number of context switches, OS will again schedule original application, that will continue processing of shared resource. this will continue until original application finally releases the mutex. And then, some other process will start accessing shared resource in orderly fashion, as designed.

If you want more authoritative and detailed explanations of whats and whys of similar scenarios, you can watch this MIT lesson, for example.

Hope this helps.

VividD
  • 10,456
  • 6
  • 64
  • 111
  • That is Ok for applications, which are under my control. suppose, if i provide that Linux system. user can installed his own application, where user is also using same shared resource,whatever i am using. So i do not have any control to avoid shared resource access. How can i come out of such race condition? – Embedded Programmer Feb 03 '14 at 01:07
0

I would suggest looking into named semaphores. sem_overview (7). This will allow you to ensure mutual exclusion in your critcal sections.

wkz
  • 2,203
  • 1
  • 15
  • 21