Linux内核 remove_wait_queue()

函数:remove_wait_queue( )

文件包含:

        #include <linux/wait.h>

函数定义:

在内核源码中的位置:linux-3.19.3/kernel/sched/wait.c

函数定义格式:

void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait)

函数功能描述:

函数remove_wait_queue( )实现将等待队列元素从等待队列中删除。

输入参数说明:

  • 函数的第一个输入参数q是wait_queue_head_t类型的指针,代表等待队列的头指针,其定义及详细信息参考函数__wake_up( )分析文档的输入参数说明部分。

  • 函数第二个输入参数wait是wait_queue_t类型的指针,代表等待队列中的一个元素,其定义及详细信息参考函数abort_exclusive_wait( )分析文档的输入参数说明部分。

返回参数说明:

此函数的返回值类型是void类型,即不返回任何类型的值。

实例解析:

编写测试文件:remove_wait_queue.c

头文件引用:

#include <linux/module.h>
#include <linux/sched.h>
#include <linux/list.h>
#include <linux/kthread.h>
MODULE_LICENSE("GPL");

子进程处理函数定义:

int my_function(void * argc)
{
    printk("in the kernel thread function! \n");
    printk("the current pid is:%d\n", current->pid); //显示当前进程PID值
    printk("out the kernel thread function\n");
    return 0;
}

模块加载函数定义:

static int __init remove_wait_queue_init(void)
{
    // 局部变量定义
    struct task_struct * result, *result1, *result2;
    int   wait_queue_num=0;
    wait_queue_head_t head;
    wait_queue_t data, data1, data2, *curr, *next;
    printk("into remove_wait_queue_init.\n");

    /*创建3个新进程*/
    result=kthread_create_on_node(my_function, NULL, -1, "remove_wait_queue");
    result1=kthread_create_on_node(my_function, NULL, -1, "remove_wait_queue1");
    result2=kthread_create_on_node(my_function, NULL, -1, "remove_wait_queue2");
    wake_up_process(result);
    wake_up_process(result1);
    wake_up_process(result2);

    init_waitqueue_head(&head);           //初始化等待队列头指针

    /*用新进程初始化等待队列元素*/
    init_waitqueue_entry(&data, result);
    init_waitqueue_entry(&data1, result1);
    init_waitqueue_entry(&data2, result2);

    /*将新进程加入等待队列中*/
    add_wait_queue(&head, &data);
    add_wait_queue_exclusive(&head, &data1);
    add_wait_queue(&head, &data2);

    /*循环显示等待队列中的进程的信息*/
    list_for_each_entry_safe(curr, next, &(head.task_list), task_list)
    {
        wait_queue_num++;                  //累加等待队列进程个数

        /*显示等待队列中当前元素的flags字段的值*/
        printk("the flag value of the current data of the waitqueue is:%d\n", curr->flags);

        /*显示等待队列中当前进程的PID值*/
        printk("the pid value of the current data of the waitqueue is:%d\n", ((struct task_struct *)(curr->private))->pid);
    }
    printk("the value of the wait_queue_num is :%d\n", wait_queue_num);
                                          // 显示当前等待队列中的进程数

    wait_queue_num=0;
    remove_wait_queue(&head, &data2);     //删除等待队列中某一元素
    /*循环显示等待队列中的进程的信息*/
    list_for_each_entry_safe(curr, next, &(head.task_list), task_list)
    {
        wait_queue_num++;                  //累加等待队列进程个数

        /*显示等待队列中当前元素的flags字段的值*/
        printk("the flag value of the current data of the waitqueue is:%d\n", curr->flags);

        /*显示等待队列中当前进程的PID值*/
        printk("the pid value of the current data of the waitqueue is:%d\n", ((struct task_struct *)(curr->private))->pid);
    }
    printk("the value of the wait_queue_num is :%d\n", wait_queue_num);
                                          // 显示当前等待队列中的进程数

    /*显示创建新进程的进程号*/
    printk("the pid of new thread is :%d\n", result->pid);
    printk("the pid of new thread is :%d\n", result1->pid);
    printk("the pid of new thread is :%d\n", result2->pid);
    printk("the current pid is:%d\n", current->pid); //显示当前进程的PID值
    printk("out remove_wait_queue_init.\n");
    return 0;
}

模块退出函数定义:

static void __exit remove_wait_queue_exit(void)
{
    printk("Goodbye remove_wait_queue\n");
}

模块加载、退出函数调用:

module_init(remove_wait_queue_init);
module_exit(remove_wait_queue_exit);

实例运行结果及分析:

首先编译模块,执行命令insmod remove_wait_queue.ko插入内核模块,然后输入命令dmesg -c查看模块插入结果,会出现如图所示的结果。

Linux内核 remove_wait_queue()

结果分析:

由上图可以看出函数remove_wait_queue( )执行后,等待队列中的元素的个数减少了,由3个变为2个,说明函数能够将等待队列元素从等待队列中成功删除。

酷客网相关文章:

赞(0)

评论 抢沙发

评论前必须登录!