Linux read-write lock

Source: Internet
Author: User

#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;//A thread starting from 0 plus a thread starting from 100
struct Task_struct *mythread1=null;//thread 1
struct Task_struct *mythread2=null;//thread 2
static int myVar = 0;//define Variable
static int count = 0;
rwlock_t lock;//Defining read-write Locks
static void Setmyvar (int input)//write variable
{
Write_lock (&lock);//Add Write Lock
Critical section
if (count)//count=1 enters
{
PRINTK ("Busy setmyvar\n");
}
Count++;//conut=1
MyVar = input;//Write variable, assign input value to MyVar
PRINTK ("Setmyvar is%d\n", myVar);//print the written variable value
Write_unlock (&lock);//Release Write lock
count--;//count=0;
}
static int Getmyvar (void)//Read variable
{
int res = 0;//used to read the value of the variable MyVar
Read_lock (&lock);//Add read lock
Critical section
if (count)//count=1 enters
{
PRINTK ("Busy setmyvar\n");
}
Count++;//conut=1
res = myvar;//read variable, assigning the value of MyVar variable to res
PRINTK ("Getmyvar is%d\n", res);//print Read the value of the variable res
Read_unlock (&lock);//Release read lock
count--;//count=0;
return 0;
}
static int print1 (void *data)//thread 1 print function
{
while (!kthread_should_stop ())//Determine if the thread is stopped and enter if the thread is not stopped
{
PRINTK ("This is thread1......\n");//hint it is thread 1
Getmyvar ();//Read variable
Setmyvar (i);//write variable
Ssleep (1);//sleep 1 seconds
I++;//i+1
}
return 0;
}
static int print2 (void *data)//thread 2 print function
{
while (!kthread_should_stop ())//Determine if the thread is stopped and enter if the thread is not stopped
{
PRINTK ("This is thread2......\n");//hint it is thread 2
Getmyvar ();//Read variable
Setmyvar (j);//write variable
Ssleep (1);//sleep 1 seconds
J++;//j+1
}
return 0;
}

static int __init hello_init (void) {//Module load entry function
Rwlock_init (&lock);//read/write lock initialization
MyThread1 = Kthread_run (Print1,null, "mythread1");//create thread 1, name is Mythread1, call function is Print1 function
MyThread2 = Kthread_run (Print2,null, "mythread2");//Create thread 2, name is mythread2, call function is Print2 function
return 0;
}
static void __exit
Hello_exit (void) {//Module Exit function
if (MYTHREAD1)//If thread 1 still exists, then stop the thread
{
PRINTK ("Kthread1 stop....\n");//hint about stopping thread 1
Kthread_stop (MYTHREAD1);//call kthread_stop function Stop thread 1
mythread1=null;//the struct pointer MyThread1 point to an empty
}
if (MyThread2)//If thread 2 still exists, then stop the thread
{
PRINTK ("Kthread2 stop....\n");//hint about stopping thread 2
Kthread_stop (MyThread2);//call kthread_stop function Stop thread 2
mythread2=null;//the struct pointer MyThread2 point to an empty
}

}
Module_init (hello_init);//Module loading
Module_exit (hello_exit);//Module exit
Module_license ("GPL");//Module license
Module_author ("Valerie Henson [email protected]");//Module author information
Module_description ("\" rwlock\ "minimal Module");//Module description
Module_version ("PRINTK");//Module version

Linux read-write lock

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.