5

I am writing a simple code in c on a Linux(A13-OLinuXino-MICRO) with A13 processor. I am using gcc compiler if that's important. Let's say I have a simple loop that is just counting from 1 to 100 and I want to have as less as possible interruption in this loop. I assume the best that I can do is disabling interrupts(even the timer interrupt if possible) and enabling it after the loop. I have multiple places in my program that I need to enforce this. Can anybody help me with a simple C code that a loop is protected from interruption...something that I can compile and run on my platform?

EDIT: Kernel thread might be an answer. How can I run something with kernel privilege? I see people talk about disable_local_interrupt() but I don't know how to use it.

EDIT: This is the actual problem that I am trying to solve. I need to run my applications in a interrupt-free environment.

EDIT: Yes I am sure that Linux is what I need to use and I know that interrupts are important for OS and that's why I don't want to disable them forever. I just want to enforce no-interrupt for a fraction of second on OS when I am running this. Also, There is no main problem that I need to solve, this is "the problem" that I need to solve, so stop asking why I want to do this.

15
  • 8
    There hopefully is no way to do this! You should not even think about doing modifications at such a low-level from user code (and be very careful in kernel code). This is an XY problem. What is your actual problem you try to solve by this? Commented Jan 25, 2017 at 18:34
  • 1
    If that was a generally-available capability, anyone could write a program with a never-ending loop that would block the entire system until someone physically pulled the plug, because task scheduling relies on clock-generated interrupts. Commented Jan 25, 2017 at 18:40
  • 1
    Disabling interrupt is as simple as processor instruction. Usually the mnemonic is something similar to cli. However, it is priviliged instruction, and can only be executed in a priviliged context. Commented Jan 25, 2017 at 18:42
  • 2
    Use a realtime operating system. Commented Jan 25, 2017 at 18:52
  • 3
    "I need to run my applications in a interrupt-free environment" -- then you don't want to run it on Linux. Nor on Windows, or OS X, or any other multitasking operating system. If you need to be certain that your program is not interrupted then you probably need to run it as a freestanding program on the bare metal. Commented Jan 25, 2017 at 18:53

3 Answers 3

9

Userspace cannot disable or enable interrupts.

Sign up to request clarification or add additional context in comments.

4 Comments

Can you demo how to enter kernel space and do it then?
@GabrielStaples write a kernel module and disable interrupts.
@sevko, you just reworded my request. Me: "doron, can you demo how to enter kernel space and do it then?". You: "[In other words doron, can you please demo how to] write a kernel model and disable interrupts?"
Take a look at implementing a char device. There are examples on the internet. Once in kernel mode, there is a simple API to disable interrupts. Just note, you cannot return to userspace before interrupts are re-enabled
0

According to ARM.com http://infocenter.arm.com/help/index.jsp?topic=%2Fcom.arm.doc.ddi0344k%2FBeidabib.html this CPU does have different operating modes, which suggests that you probably can't disable interrupts from a user process.

2 Comments

well then is there any possible way to do it with a higher privilege?
yes, run your code in the kernel. By writing a kernel driver. I am no kernel developer, but I'm going to guess that you don't want to have a way to disable and then enable interrupts from the userland; you should run your entire operation in kernel mode: disable interrupts, do whatever it is you want, re-enabled interrupts. This still sounds weird and a RTOS is most likely a better choice.
0

I think @doron is right, you can't disable/enable interrupt in user mode, but you have a work around. You can write a driver which encapsulates the "loop" code in an interface, and you can enable/disable interrupt in your driver. Your application just need to call that interface.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.