进程角度和内核角度看进程运行

老规矩,先上图

 1.站在进程角度——内核只属于自己,整个执行流贯穿用户进程代码到内核代码。或者换句话说,可以把内核看成用户进程的一个动态链接库,内核代码仅仅是进程的一部分代码。

 2.站在内核角度看——内核属于所有进程,所有进程共用内核代码。在执行流层面,内核中的同一函数(比如一个驱动的ioctl)被多个进程并发执行。在数据流层面,所有在同一函数的并发执行流他们的数据流都是独立的(无全局变量,都是栈上的局部变量)。

3.那内核(比如:驱动程序)中的全局变量会有什么后果?——那就会出现多个执行流并发访问的情况,需要加锁保护。那么,换个角度,是不是可以在驱动程序中定义全局变量来实现进程间通信?上个代码看看:

驱动代码:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
 
static const char devname[] = "ldengdev";
static int major = 0;
static struct class* devclass = NULL;
static struct device *dev;

static unsigned long buffer = 100;

static ssize_t hello_read(struct file *filep, char __user *buf, size_t len, loff_t *pos)
{
	if(copy_to_user(buf,&buffer,len))
	{
		return -EFAULT;
	}
    printk(KERN_ERR"ldeng hello_read\n");
	return len;
}
static ssize_t hello_write(struct file *filep, const char __user *buf, size_t len, loff_t *pos)
{
	if(copy_from_user(&buffer,buf,len))
	{
		return -EFAULT;
	}
    printk(KERN_ERR"ldeng hello_write\n");
	return len;
}
 
struct file_operations hello_fops = {
    .owner = THIS_MODULE,
    .read = hello_read,
    .write = hello_write,
};
 
static int __init hello_init (void)
{
    int err = 0;
    
    if((err = major = register_chrdev(0, devname, &hello_fops)) < 0)
        return err;
    
    devclass = class_create(THIS_MODULE, devname);
    if(IS_ERR(devclass)) 
    {
        err = PTR_ERR(devclass);
        printk("%s class_create failed!\n",__func__);
        goto class_err;
    }
    
    dev = device_create(devclass, NULL, MKDEV(major, 0), NULL, devname);
    if (IS_ERR(dev)) {
        err = PTR_ERR(dev);
        printk("%s device_create failed!\n",__func__);
        goto device_err;
    }
    
    printk("hello module initialization\n");
    return 0;
    
device_err:
    class_destroy(devclass);
class_err:
    unregister_chrdev(major, devname);

    return err;
}
 
static void __exit hello_exit (void)
{
    device_destroy(devclass, MKDEV(major, 0));
    class_destroy(devclass);
    unregister_chrdev(major, devname);

    printk (KERN_INFO "char driver cleaned up\n");
}
 
module_init (hello_init);
module_exit (hello_exit);
 
MODULE_LICENSE ("GPL");

驱动Makefile: 

KVERS = $(shell uname -r)

obj-m += hello.o

build: kernel_moudles

kernel_moudles:
	make -C /lib/modules/$(KVERS)/build M=$(CURDIR) modules

clean:
	make -C /lib/modules/$(KVERS)/build M=$(CURDIR) clean

用户测试代码:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
  
unsigned long buffer = 0;

int main(int argc, char ** argv)
{
    int fd,len;
 
    fd = open("/dev/ldengdev",O_RDWR);
    if(fd<0)
    {
        perror("open fail \n");
        return -1;
    }
 
    len=read(fd,&buffer,8);
    printf("read len = %d, buffer = %lu\n",len, buffer);

    buffer++;

    write(fd, &buffer, 8);
 
    close(fd);

    return 0;
}

insmod ko以后开两个shell,分别执行测试程序,观察读出来的buffer的值的变化。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

denglin12315

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值