/dev/zero처럼 /dev/one을 만드는 방법은 무엇입니까?

/dev/zero처럼 /dev/one을 만드는 방법은 무엇입니까?

연속적인 문자 스트림을 생성하는 파일을 어떻게 만들 수 있나요 0x01? /dev/zero내가 선택한 캐릭터를 생성하는 것을 제외하고 와 같습니다 .

/dev/zero – 기록된 모든 입력을 수락하고 삭제합니다. 읽을 때 출력으로 널 문자(값이 0인 바이트)의 연속 스트림을 생성합니다.

로 읽을 수 있어야 합니다 cat. 명명된 파이프로 루프를 실행하는 것을 고려했지만 이를 위해서는 활성 프로세스가 필요합니다.

답변1

… 하지만 그러기 위해서는 적극적인 프로세스가 필요합니다.

원본 /dev/zero도 처리함활성 "프로세스", 하지만커널에서. 사용자 공간 수준에서 처리하고 싶지 않다면 다른 선택은 매우 제한적입니다. 그렇다면 커널 드라이버가 되어야 합니다.

답변2

tr글쎄요, 항상 /dev/zero 또는 다른 것을 통해 파이프할 수 있습니다 .

# cat /dev/zero | tr '\000' '\001' | hexdump -C                                    
00000000  01 01 01 01 01 01 01 01  01 01 01 01 01 01 01 01  |................|
*

고양이를 건너뛰고 프로세스 대체를 사용하려는 경우.

# hexdump -C <( tr '\000' '\001' </dev/zero ) 
00000000  01 01 01 01 01 01 01 01  01 01 01 01 01 01 01 01  |................|

대.

# cat /dev/zero | hexdump -C
00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*

답변3

뻔뻔하게 적응Linux 커널 모듈 프로그래밍 가이드

#include <linux/cdev.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/irq.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/poll.h>

/*  Prototypes - this would normally go in a .h file */
static int device_open(struct inode *, struct file *);
static int device_release(struct inode *, struct file *);
static ssize_t device_read(struct file *, char __user *, size_t, loff_t *);
static ssize_t device_write(struct file *, const char __user *, size_t,
                            loff_t *);

#define SUCCESS 0
#define DEVICE_NAME "one" /* Dev name as it appears in /proc/devices   */

/* Global variables are declared as static, so are global within the file. */

static int major; /* major number assigned to our device driver */

static struct class *cls;

static struct file_operations chardev_fops = {
    .read = device_read,
    .write = device_write,
    .open = device_open,
    .release = device_release,
};

static int __init chardev_init(void)
{
    major = register_chrdev(0, DEVICE_NAME, &chardev_fops);

    if (major < 0) {
        pr_alert("Registering char device failed with %d\n", major);
        return major;
    }

    pr_info("I was assigned major number %d.\n", major);

    cls = class_create(THIS_MODULE, DEVICE_NAME);
    device_create(cls, NULL, MKDEV(major, 0), NULL, DEVICE_NAME);

    pr_info("Device created on /dev/%s\n", DEVICE_NAME);

    return SUCCESS;
}

static void __exit chardev_exit(void)
{
    device_destroy(cls, MKDEV(major, 0));
    class_destroy(cls);

    /* Unregister the device */
    unregister_chrdev(major, DEVICE_NAME);
}

/* Methods */

/* Called when a process tries to open the device file, like
 * "sudo cat /dev/chardev"
 */
static int device_open(struct inode *inode, struct file *file)
{
    try_module_get(THIS_MODULE);

    return SUCCESS;
}

/* Called when a process closes the device file. */
static int device_release(struct inode *inode, struct file *file)
{
    /* Decrement the usage count, or else once you opened the file, you will
     * never get rid of the module.
     */
    module_put(THIS_MODULE);

    return SUCCESS;
}

/* Called when a process, which already opened the dev file, attempts to
 * read from it.
 */
static ssize_t device_read(struct file *filp, /* see include/linux/fs.h   */
                           char __user *buffer, /* buffer to fill with data */
                           size_t length, /* length of the buffer     */
                           loff_t *offset)
{
    int i;

    for (i = 0; i < length; i++) {
      put_user('1', buffer + i);
    }

    /* Most read functions return the number of bytes put into the buffer. */
    return length;
}

/* Called when a process writes to dev file: echo "hi" > /dev/hello */
static ssize_t device_write(struct file *filp, const char __user *buff,
                            size_t len, loff_t *off)
{
    pr_alert("Sorry, this operation is not supported.\n");
    return -EINVAL;
}

module_init(chardev_init);
module_exit(chardev_exit);

MODULE_LICENSE("GPL");

메이크파일

obj-m += onedev.o

PWD := $(CURDIR)

ifeq ($(CONFIG_STATUS_CHECK_GCC),y)
CC=$(STATUS_CHECK_GCC)
ccflags-y += -fanalyzer
endif

all:
    $(MAKE) -C /lib/modules/$(shell uname -r)/build CC=$(CC) M=$(PWD) modules

clean:
    $(MAKE) -C /lib/modules/$(shell uname -r)/build CC=$(CC) M=$(PWD) clean
    $(RM) other/cat_noblock *.plist

indent:
    clang-format -i *[.ch]
    clang-format -i other/*[.ch]

관련 정보