Linux system kernel-driven bit operation

Source: Internet
Author: User

Linux system kernel-driven bit operation

The atomic_t type is good when it comes to integer arithmetic. However, it does not work well when you need to manipulate individual bits in an atomic manner. To do this, the kernel provides a set of functions to atomically modify or test a single bit.  No interrupts (or other processors) can interfere because the entire operation occurs in one step. Www.ahlinux.com

Atomic bits operate very quickly because they operate using a single machine instruction, and at any time the low-level platform does not prohibit interrupts. Functions are system-dependent and are declared in. They are guaranteed to be atomic, even on SMP computers, and are useful for consistency across processors.

Unfortunately, typing the data in these functions is also a system-dependent. The Nr parameter, which describes which bit to manipulate, is often defined as int, but is unsigned long in several systems. The address to be modified is often a unsigned long pointer, but several systems use void * instead.

The various bit operations are:

void Set_bit (nr, void *addr);

Set the NR bit in the data item that the addr points to.

void Clear_bit (nr, void *addr);

Clears the unsigned long data that is positioned at the addr. Its semantics are the opposite of set_bit.

void Change_bit (nr, void *addr);

Flip this bit.

Test_bit (nr, void *addr);

This function is the only one that does not need to be an atomic bit operation; It simply returns the current value of this bit.

int Test_and_set_bit (nr, void *addr);

int Test_and_clear_bit (nr, void *addr);

int Test_and_change_bit (nr, void *addr);

Atomic actions are listed earlier, except that they return the previous value of the bit.

    • This article is from: Linux Tutorial Network

Linux system kernel-driven bit operation

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.