mirror of
https://github.com/sysprog21/lkmpg.git
synced 2024-11-22 04:54:20 +08:00
e56fc58bf3
dmesg only flushes when it encounter a newline. Without a newline, the line is held in memory pending another printk. In this particular example (example_atomic.c), the last pr_info in atomic_bitwise() prints when another printk happens (either by another module, or __exit for this module. This can be confusing to new learner. This patch adds a newline to the last pr_info forcing dmesg to print to the screen when the module is loaded.
76 lines
1.9 KiB
C
76 lines
1.9 KiB
C
/*
|
|
* example_atomic.c
|
|
*/
|
|
#include <linux/atomic.h>
|
|
#include <linux/bitops.h>
|
|
#include <linux/module.h>
|
|
#include <linux/printk.h>
|
|
|
|
#define BYTE_TO_BINARY_PATTERN "%c%c%c%c%c%c%c%c"
|
|
#define BYTE_TO_BINARY(byte) \
|
|
((byte & 0x80) ? '1' : '0'), ((byte & 0x40) ? '1' : '0'), \
|
|
((byte & 0x20) ? '1' : '0'), ((byte & 0x10) ? '1' : '0'), \
|
|
((byte & 0x08) ? '1' : '0'), ((byte & 0x04) ? '1' : '0'), \
|
|
((byte & 0x02) ? '1' : '0'), ((byte & 0x01) ? '1' : '0')
|
|
|
|
static void atomic_add_subtract(void)
|
|
{
|
|
atomic_t debbie;
|
|
atomic_t chris = ATOMIC_INIT(50);
|
|
|
|
atomic_set(&debbie, 45);
|
|
|
|
/* subtract one */
|
|
atomic_dec(&debbie);
|
|
|
|
atomic_add(7, &debbie);
|
|
|
|
/* add one */
|
|
atomic_inc(&debbie);
|
|
|
|
pr_info("chris: %d, debbie: %d\n", atomic_read(&chris),
|
|
atomic_read(&debbie));
|
|
}
|
|
|
|
static void atomic_bitwise(void)
|
|
{
|
|
unsigned long word = 0;
|
|
|
|
pr_info("Bits 0: " BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(word));
|
|
set_bit(3, &word);
|
|
set_bit(5, &word);
|
|
pr_info("Bits 1: " BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(word));
|
|
clear_bit(5, &word);
|
|
pr_info("Bits 2: " BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(word));
|
|
change_bit(3, &word);
|
|
|
|
pr_info("Bits 3: " BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(word));
|
|
if (test_and_set_bit(3, &word))
|
|
pr_info("wrong\n");
|
|
pr_info("Bits 4: " BYTE_TO_BINARY_PATTERN, BYTE_TO_BINARY(word));
|
|
|
|
word = 255;
|
|
pr_info("Bits 5: " BYTE_TO_BINARY_PATTERN "\n", BYTE_TO_BINARY(word));
|
|
}
|
|
|
|
static int __init example_atomic_init(void)
|
|
{
|
|
pr_info("example_atomic started\n");
|
|
|
|
atomic_add_subtract();
|
|
atomic_bitwise();
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void __exit example_atomic_exit(void)
|
|
{
|
|
pr_info("example_atomic exit\n");
|
|
}
|
|
|
|
module_init(example_atomic_init);
|
|
module_exit(example_atomic_exit);
|
|
|
|
MODULE_DESCRIPTION("Atomic operations example");
|
|
MODULE_LICENSE("GPL");
|