为什么清除输入标记会导致C中的分段错误? [英] why clear interrput flag cause segmentation fault in C?

查看:77
本文介绍了为什么清除输入标记会导致C中的分段错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习一些有关Assembly和C的基础知识.出于学习目的,我决定编写一个简单的程序来禁用Interrupts,并且当用户想要在控制台中键入某些内容时,他/她将无法:

I am learning some basics about Assembly and C. for learning purpose I decide to write a simple program that disable Interrupts and when user wants to type something in the console he/she can't :

#include <stdio.h>
int main(){
    int a;
    printf("enter your number : ");
    asm ("cli");
    scanf("%d", &a);
    printf("your number is %d\n" , a);     
    return 0;
}

但是当我用GCC编译时,出现了段错误:

but when I compile this with GCC I got segmentation fault :

Segmentation fault (core dumped)

当我使用gdb对其进行调试时,当程序到达asm("cli");行时,我会收到此消息:

And when I debug it with gdb I got this message when program reach to the asm("cli"); line:

Program received signal SIGSEGV, Segmentation fault.
main () at cli.c:6
6       asm ("cli");

推荐答案

之所以发生这种情况,是因为您无法禁用用户空间程序的中断.所有中断都在内核的控制之下.您需要从内核空间执行此操作.在您这样做之前,您需要首先学习内核内部知识,并且玩中断非常关键,并且根据我的知识,还需要更多有关内核的知识.

This is happening because You can't disable interrupts from user space program. All interrupts are under the control of kernel. You need to do it from kernel space. Before you do it you need to learn kernel internals first and playing with interrupts are very critical and requires more knowledge on kernel according to my knowledge.

您需要编写一个内核模块,该模块可以通过/dev/(或其他一些接口)与用户空间进行交互.用户空间代码应请求内核模块禁用中断.

You need to write a kernel module that can interact with user space through /dev/ (or some other) interface. User space code should request kernel module to disable interrupts.

这篇关于为什么清除输入标记会导致C中的分段错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆