Linux concurrency control---One operation---> Atomic operation for solving race state

Source: Internet
Author: User

-An action---> Atomic operation to solve a race state

The way to solve the race is to "secure mutually exclusive access to the shared resource."

Atomic operation

Atomic manipulation refers to operations that are not interrupted by other code during execution.

There are many ways to operate in Linux, with integral atoms and bit atoms, and they are all atomic in any case, and the implementations of these atomic operations are CPU dependent, so these functions are closely related to the CPU architecture.

Integral type Atom

The atom implementation of ARM architecture in kernel/arch/arm/include/asm/atomic.h

Macro definitions provided in the kernel:

1. Set the value from the variable

        

    Static void int // Set the value       of an atom atomic_t = Atomic_init (0);          // define atomic variables and initialize to 0           

  2. Get the value of an atomic variable

      1. #define Atomic_read (v)  ((v)->counter)   // Return the value of the atomic variable  (* (volatile int *) & (v)->counter)

3. Atomic variable addition and subtraction, self-increment self-reduction

      1. #define Atomic_add (i, v)    // put the value of v plus i#define atomic_inc (v)       (void) atomic_ Add_return (1, v)     //vz self-adding #define atomic_sub (i, v)    (void) Atomic_sub_return (i , v)   #define atomic_dec (v)       (void) Atomic_sub_return (1, v)   //v self-reduction

4. Operation and Testing

Open_atomic_int_one and Open_atomic_int_two two programs
To mirror operations on/dev/atomic_int device nodes
Run the first Program 1, assign the variable to 1, release the value is assigned to 0
If program 1 is not released, program 2 calls the device node and returns directly, unable to invoke.

#include <linux/init.h>#include<linux/module.h>/*The driver registers the header file, contains the driver's structure and registers and unloads the function*/#include<linux/platform_device.h>/*registering miscellaneous device header files*/#include<linux/miscdevice.h>/*registering the file structure of a device node*/#include<linux/fs.h>//function header file for atomic manipulation#include <asm/atomic.h>#include<asm/types.h>#defineDriver_name "Atomic_int"#defineDevice_name "Atomic_int"Module_license ("Dual BSD/GPL"); Module_author ("Hky");//define an atomic variable and initialize it to 0Staticatomic_t value_atomic = Atomic_init (0);Static intAtomic_int_open (structInode *inode,structFile *file) {PRINTK (Kern_emerg"Atomic_int Open in!\n"); if(Atomic_read (&value_atomic)) {        return-Ebusy; } atomic_inc (&value_atomic); PRINTK (Kern_emerg"Atomic_int Open success!\n"); return 0;}Static intAtomic_int_release (structInode *inode,structFile *file) {PRINTK (Kern_emerg"Atomic_int release\n"); Atomic_dec (&value_atomic); return 0;}Static structFile_operations Atomic_int_ops ={. Owner=this_module,. Open=Atomic_int_open,. Release=Atomic_int_release,};Static  structMiscdevice Atomic_int_dev ={. Minor=Misc_dynamic_minor,. Name=device_name,. FoPs= &Atomic_int_ops,};Static intAtomic_int_probe (structPlatform_device *PDV) {PRINTK (Kern_emerg"\tinitialized\n"); Misc_register (&Atomic_int_dev); return 0;}Static intAtomic_int_remove (structPlatform_device *PDV) {PRINTK (Kern_emerg"\tremove\n"); Misc_deregister (&Atomic_int_dev); return 0;}structPlatform_driver Atomic_int_driver ={. Probe=atomic_int_probe,. Remove=atomic_int_remove,. Driver={. Name=driver_name,. Owner=This_module,}};Static intAtomic_int_init (void){    intdriverstate; PRINTK (Kern_emerg"HELLO World enter!\n"); Driverstate= Platform_driver_register (&atomic_int_driver); PRINTK (Kern_emerg"\tdriverstate is%d\n", driverstate); return 0;}Static voidAtomic_int_exit (void) {PRINTK (Kern_emerg"HELLO World exit!\n"); Platform_driver_unregister (&atomic_int_driver); }module_init (Atomic_int_init); Module_exit (atomic_int_exit);

Linux concurrency control---One operation---> Atomic operation for solving race state

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.