Kernel里面FIFO的使用

本文介绍了一个在 Linux 环境下实现 ByteStream FIFO 的示例代码。该代码展示了如何创建并使用 FIFO 结构来处理字节流,并通过生产者-消费者模式演示了其工作流程。
/*
 * Sample kfifo byte stream implementation
 *
 * Copyright (C) 2010 Stefani Seibold <stefani@seibold.net>
 *
 * Released under the GPL version 2 only.
 *
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/mutex.h>
#include <linux/kfifo.h>
#include <linux/kthread.h>
#include <linux/time.h>
#include <linux/delay.h>

/*
 * This module shows how to create a byte stream fifo.
 */

/* fifo size in elements (bytes) */
#define FIFO_SIZE   32

/* name of the proc entry */
#define PROC_FIFO   "bytestream-fifo"

/* lock for procfs read access */
static DEFINE_MUTEX(read_lock);

/* lock for procfs write access */
static DEFINE_MUTEX(write_lock);

/*
 * define DYNAMIC in this example for a dynamically allocated fifo.
 *
 * Otherwise the fifo storage will be a part of the fifo structure.
 */
#if 1
#define DYNAMIC
#endif

#ifdef DYNAMIC
static struct kfifo test;
#else
static DECLARE_KFIFO(test, unsigned char, FIFO_SIZE);
#endif

static const unsigned char expected_result[FIFO_SIZE] = {
     3,  4,  5,  6,  7,  8,  9,  0,
     1, 20, 21, 22, 23, 24, 25, 26,
    27, 28, 29, 30, 31, 32, 33, 34,
    35, 36, 37, 38, 39, 40, 41, 42,
};

static int __init testfunc(void)
{
    unsigned char   buf[32]={'0'};
    unsigned char   i, j;
    unsigned int    ret;

    printk(KERN_INFO "byte stream fifo test start\n");

    if(!kfifo_is_empty(&test)){

            printk(KERN_INFO"1111not empty \n!");
    }else{
            printk(KERN_INFO "empty ! \n");
    }
    /* put string into the fifo */
    kfifo_in(&test, "hello",5);

    /* get max of 2 elements from the fifo */
    ret = kfifo_in(&test, "121212121212121212121212121212121212",36);
    printk(KERN_INFO "ret: %d buf = %s\n" , ret,buf);

    if(!kfifo_is_full(&test)){

            printk(KERN_INFO"2222not kfifo_is_full \n!");
    }else{
            printk(KERN_INFO "kfifo_is_full ! \n");
    }
    /* and put it back to the end of the fifo */
    ret = kfifo_out(&test,buf,9);
    printk(KERN_INFO "ret: %d buf = %s\n", ret,buf);
    if(!kfifo_is_empty(&test)){

            printk(KERN_INFO"2222not empty ! \n");
    }else{
            printk(KERN_INFO "empty ! \n");
    }
  memset(buf,0,32);
    ret = kfifo_out(&test,buf,8);
    printk(KERN_INFO "test passed buf = %s\n",buf);

  memset(buf,0,32);
    ret = kfifo_out(&test,buf,8);
    printk(KERN_INFO "test passed buf = %s\n",buf);

    if(!kfifo_is_empty(&test)){

            printk(KERN_INFO "3333333333not empty ! \n");
    }else{
            printk(KERN_INFO "empty ! \n");
    }

    return 0;
}

static ssize_t fifo_write(struct file *file, const char __user *buf,
                        size_t count, loff_t *ppos)
{
    int ret;
    unsigned int copied;

    if (mutex_lock_interruptible(&write_lock))
        return -ERESTARTSYS;

    ret = kfifo_from_user(&test, buf, count, &copied);

    mutex_unlock(&write_lock);

    return ret ? ret : copied;
}

static ssize_t fifo_read(struct file *file, char __user *buf,
                        size_t count, loff_t *ppos)
{
    int ret;
    unsigned int copied;

    if (mutex_lock_interruptible(&read_lock))
        return -ERESTARTSYS;

    ret = kfifo_to_user(&test, buf, count, &copied);

    mutex_unlock(&read_lock);

    return ret ? ret : copied;
}

static const struct file_operations fifo_fops = {
    .owner      = THIS_MODULE,
    .read       = fifo_read,
    .write      = fifo_write,
    .llseek     = noop_llseek,
};


static int productor_thread(void *p){

    unsigned char mac[7]={0x00};
    int i = 0;
    unsigned int    ret;

    while(i<100){

        mac[0] = i;
        mac[1] = i+1;
        mac[2] = i+2;
        mac[3] = i+3;
        mac[4] = i+4;
        mac[5] = i+5;
        mac[6] = i+6;


         ret = kfifo_in(&test,mac,7);

     i++;

    msleep(1000);

  }


    return 0;
}

static int consumer_thread(void *p){

    unsigned char mac[7]={0x00};
    unsigned int    ret;
    int i = 0;
    while(i<10){


        ret = kfifo_out(&test,mac,7);

        msleep(1000);
        if(kfifo_is_empty(&test)){
            i++;
        }else{
                printk(KERN_INFO "mac =0x%x:0x%x:0x%x:0x%x:0x%x:0x%x:0x%x \n",mac[0],mac[1],mac[2],mac[3],mac[4],mac[5],mac[6]);
        }

    }

    printk("iiiiiiiiiiiiiiiiiiiiiiii=%d \n",i);
    return 0;
}

static struct task_struct *ps_tsks1;
static struct task_struct *ps_tsks2;

static int __init example_init(void)
{
#ifdef DYNAMIC
    int ret;

    ret = kfifo_alloc(&test, FIFO_SIZE, GFP_KERNEL);
    if (ret) {
        printk(KERN_ERR "error kfifo_alloc\n");
        return ret;
    }
#else
    INIT_KFIFO(test);
#endif

    ps_tsks1 = kthread_run(productor_thread,(void *)(unsigned long)NULL,"productor_thread");


    ps_tsks2 = kthread_run(consumer_thread,(void *)(unsigned long)NULL,"consumer_thread");





/*
    if (testfunc() < 0) {
#ifdef DYNAMIC
        kfifo_free(&test);
#endif
        return -EIO;
    }
*/




    if (proc_create(PROC_FIFO, 0, NULL, &fifo_fops) == NULL) {
#ifdef DYNAMIC
        kfifo_free(&test);
#endif
        return -ENOMEM;
    }
    return 0;
}

static void __exit example_exit(void)
{

    //kthread_stop(ps_tsks1);
    //kthread_stop(ps_tsks2);
    remove_proc_entry(PROC_FIFO, NULL);
#ifdef DYNAMIC
    kfifo_free(&test);
#endif
}

module_init(example_init);
module_exit(example_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Stefani Seibold <stefani@seibold.net>");

<think>好的,我现在需要帮用户查找Linux内核中task_struct相关代码所在的文件位置。首先,我应该回忆一下自己了解的Linux内核结构相关知识。 用户提到的task_struct是Linux内核中非常重要的数据结构,用来表示进程或线程的信息。根据之前的经验,进程管理相关的代码通常位于内核源码的某个特定目录下。比如,可能是在include/linux/sched.h头文件中定义task_struct结构体,因为sched.h通常包含调度相关的定义。不过,我需要确认这一点是否正确。 接下来,用户可能需要具体的文件路径。我需要回想内核源码的目录结构。一般来说,核心数据结构如task_struct可能会在include/linux目录下,而具体的实现代码可能在kernel目录下的某个子目录中,比如kernel/fork.c可能负责进程的创建和销毁,所以task_struct的初始化或相关函数可能在这里。还有,调度相关的实现可能在kernel/sched/目录下的文件中,例如core.c或某个具体的调度器文件。 另外,用户可能不仅需要结构体的定义位置,还想了解相关的实现文件。比如,进程状态管理、调度、信号处理等功能的代码可能分布在不同的文件中。例如,信号处理可能在kernel/signal.c中,进程间通信可能在ipc目录下,而内存管理可能和mm目录相关。 我需要确保提供的路径准确,所以最好参考官方内核源码或者可靠的文档。根据引用内容中的结构体如resv_map,虽然这和内存管理有关,但用户的问题是关于task_struct的,所以应该集中在进程管理部分。根据引用[2],线程的创建和销毁涉及资源管理,这可能与fork.c中的代码有关联,因为fork处理进程的创建。 总结一下,可能的文件路径包括: - include/linux/sched.h:定义task_struct结构体。 - kernel/fork.c:包含进程创建和销毁的代码,比如copy_process函数。 - kernel/sched/目录下的文件:处理调度相关的实现。 - 其他如kernel/exit.c处理进程退出,kernel/pid.c管理进程ID等。 需要检查这些文件是否确实包含相关代码。例如,在sched.h中查找struct task_struct的定义,确认其是否存在。同时,在fork.c中查找比如fork()系统调用的实现,里面应该会调用copy_process来创建新的task_struct实例。 最后,用户可能需要知道如何在内核源码中查找这些文件。比如使用git仓库,或者通过在线源码浏览工具。此外,不同内核版本可能会有路径变化,但主要结构通常保持稳定。所以建议用户根据自己使用的内核版本进行确认,或者参考官方文档。</think>在Linux内核中,`task_struct`结构体是进程管理的核心数据结构,其定义和相关实现分布在以下文件中: ### 1. **结构体定义位置** `task_struct`的完整定义位于: ``` include/linux/sched.h ``` 该文件包含进程调度、状态、上下文切换等基础定义,其中`task_struct`结构体记录了进程的PID、内存映射、信号处理、线程信息等所有元数据[^1]。 ### 2. **关键实现文件** - **进程创建与销毁** ```kernel/fork.c``` 包含`copy_process()`函数,负责复制父进程的`task_struct`结构体以创建新进程[^2]。 - **进程调度** ```kernel/sched/core.c``` 实现进程调度算法(如CFS),通过`task_struct`中的调度策略字段(`policy`)和优先级(`prio`)进行决策。 - **进程状态管理** ```kernel/exit.c``` 处理进程终止逻辑,如`do_exit()`函数会清理`task_struct`关联的资源。 - **进程ID管理** ```kernel/pid.c``` 管理进程的PID命名空间,通过`task_struct`中的`pid`和`tgid`字段实现。 ### 3. **相关功能扩展** - **信号处理** ```kernel/signal.c``` 通过`task_struct`的`sighand`字段处理信号队列和信号处理函数。 - **内存管理** ```mm/mmap.c``` 管理进程的虚拟内存映射(`vm_area_struct`),与`task_struct`中的`mm`字段关联。 - **线程实现** ```kernel/fork.c``` 线程与进程共享`task_struct`结构,通过`CLONE_THREAD`标志区分。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值