zoukankan      html  css  js  c++  java
  • linux读写锁

    #include<linux/fs.h>
    #include<linux/sched.h>
    #include<linux/kthread.h>
    #include<linux/module.h>
    #include<linux/delay.h>
    static int i=0,j=100;//一个线程从0開始加,一个线程从100開始加
    struct task_struct *MyThread1=NULL;//线程1
    struct task_struct *MyThread2=NULL;//线程2
    static int myVar = 0;//定义变量
    static int count = 0;
    rwlock_t lock;//定义读写锁
    static void setMyVar(int input)//写变量
    {
            write_lock(&lock);//加写锁
            //临界区
            if(count)//count=1进入
            {
                printk("busy setMyVar ");
            }
            count++;//conut=1
        myVar = input;//写变量,将input值赋值给myVar
        printk("setMyVar is %d ",myVar);//打印写的变量值
        write_unlock(&lock);//释放写锁
        count--;//count=0;
    }
    static int getMyVar(void)//读变量
    {
        int res = 0 ;//用于读取变量myVar的值
            read_lock(&lock);//加读锁
            //临界区
            if(count)//count=1进入
            {
                printk("busy setMyVar ");
            }
            count++;//conut=1
        res = myVar;//读变量。将myVar变量值赋值给res
        printk("getMyVar is %d ",res);//打印读取的变量res的值
        read_unlock(&lock);//释放读锁
        count--;//count=0;
        return 0;
    }
    static int print1(void *data)//线程1打印函数
    {
        while(!kthread_should_stop())//推断该线程是否停止,若线程未停止则进入
        {
            printk("this is thread1...... ");//提示这是线程1
            getMyVar();//读变量
            setMyVar(i);//写变量
            ssleep(1);//沉睡1秒
            i++;//i+1
        }
        return 0;
    }
    static int print2(void *data)//线程2打印函数
    {
        while(!kthread_should_stop())//推断该线程是否停止,若线程未停止则进入
        {
            printk("this is thread2...... ");//提示这是线程2
            getMyVar();//读变量
            setMyVar(j);//写变量
            ssleep(1);//沉睡1秒
            j++;//j+1
        }
        return 0;
    }

    static int __init hello_init(void){//模块载入入口函数
        rwlock_init(&lock);//读写锁初始化
        MyThread1 = kthread_run(print1,NULL,"mythread1");//创建线程1,名字是mythread1,调用的函数是print1函数
        MyThread2 = kthread_run(print2,NULL,"mythread2");//创建线程2,名字是mythread2,调用的函数是print2函数
        return 0;
    }
    static void __exit
    hello_exit(void){//模块退出函数
        if(MyThread1)//假设线程1还存在。那么就停止该线程
        {
            printk("kthread1 stop.... ");//提示即将停止线程1
            kthread_stop(MyThread1);//调用kthread_stop函数停止线程1
            MyThread1=NULL;//将结构体指针MyThread1指向空
        }
        if(MyThread2)//假设线程2还存在,那么就停止该线程
        {
            printk("kthread2 stop.... ");//提示即将停止线程2
            kthread_stop(MyThread2);//调用kthread_stop函数停止线程2
            MyThread2=NULL;//将结构体指针MyThread2指向空
        }
            
    }
    module_init(hello_init);//模块载入
    module_exit(hello_exit);//模块退出
    MODULE_LICENSE("GPL");//模块许可证
    MODULE_AUTHOR("Valerie Henson val@nmt.edu");//模块作者信息
    MODULE_DESCRIPTION(""rwlock" minimal module");//模块描写叙述
    MODULE_VERSION("printk");//模块版本号
  • 相关阅读:
    Cannot resolve symbol 'SpringBootApp
    Java读取ZIP文件ZipEntry.getsize()总是返回-1?
    java 读取zip里面的xml文件
    导出:xml zip
    jquery form submit提交方式
    [转][C#]无法创建虚拟目录。ASP.NET 2.0 尚未在 Web服务器上注册。
    [转][C#].Net反编译利器
    [转][echarts]地图轮播
    [转][C#]AutoFac 使用方法总结
    [转]用代码访问 Https
  • 原文地址:https://www.cnblogs.com/yjbjingcha/p/6850338.html
Copyright © 2011-2022 走看看