3

I have created a kernel module, which handles interrupts. Also, there is a C program.

This program causes interrupts during its execution. When the interrupt is coming, the program should be suspended, and stay suspended - until the interrupt handler in kernel module will finish handling this interrupt.

Please tell me, how to achieve that?

Jake Badlands
  • 1,016
  • 3
  • 23
  • 46

2 Answers2

1

You could wait on a mutex with attribute PTHREAD_PROCESS_SHARED set while your kernel module is doing it's thing, when the kernel module is done, you can signal the mutex so that your process can continue.

To set this you can use pthread_mutexattr_setpshared

There is also this:

For inter-process synchronization, a mutex needs to be allo- cated
in memory shared between these processes. Since the memory for such a mutex must be allocated dynamically, the mutex needs to be explicitly initialized using mutex_init().

Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
  • Please, could you provide a simple example of a named mutex, in regard to this problem? – Jake Badlands Mar 06 '13 at 12:05
  • 1
    @JakeBadlands actually in Linux you have to set a few properties of the mutex to make it cross proc shareable and it has to be in memory that can be accessed by both procs. [Here](http://stackoverflow.com/questions/9389730/is-it-possible-to-use-mutex-in-multiprocessing-case-on-linux-unix) is some more info. – Tony The Lion Mar 06 '13 at 12:06
1
This program causes interrupts during its execution

I assume the User Space program is doing a soft interrupt/system call , and you have edited the kernel system call table , and assigned the number of your custom system call and recompiled and installed the kernel with your custom system call/soft interrupt.

When the interrupt is coming, the program should be suspended, and stay suspended - until the interrupt handler in kernel module will finish handling this interrupt.

For this to happen when your program calls the soft interrupt , you must make your irq , to run atomically , this way , before your program calls another soft interrupt , the previous interrupt would have been handled at a go , without being preempted by any other , higher priority interrupt. To achieve this atomicity , you can use spinlocks in your irq handler.

Example

spinlock_t mLock = SPIN_LOCK_UNLOCK;
unsigned long flags;

spin_lock_irqsave(&mLock, flags); // save the state, if locked already it is saved in flags
// IRQ Handler Code here
spin_unlock_irqsave(&mLock, flags); // return to the formally state specified in flags
Barath Ravikumar
  • 5,658
  • 3
  • 23
  • 39