Linux内核 add_wait_queue()

函数:add_wait_queue( )

文件包含:

        #include <linux/wait.h>

函数定义:

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

函数定义格式:

void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait)

函数功能描述:

函数add_wait_queue( )实现将等待队列元素插入等待队列第一个元素的位置,并设置等待队列元素的f lags值为非WQ_FLAG_EXCLUSIVE,即为0,表示此进程不是高优先级进程。

输入参数说明:

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

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

返回参数说明:

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

实例解析:

编写测试文件:add_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);     //显示当前进程的进程号
    printk("out the kernel thread function\n");
    return 0;
}

模块加载函数定义:

static int __init add_wait_queue_init(void)
{
    // 局部变量定义
    char namefrm[] = "add_wait_queue.c";
    char namefrm1[]= "add_wait_queue1.c";
    struct task_struct * result, *result1;
    int   wait_queue_num=0;
    wait_queue_head_t head;
    wait_queue_t data, data1, *curr, *next;
    printk("into add_wait_queue_init.\n");
    /*创建2个新进程*/
    result=kthread_create_on_node(my_function, NULL, -1, namefrm);
    result1=kthread_create_on_node(my_function, NULL, -1, namefrm1);
    init_waitqueue_head(&head);  //初始化等待队列头指针
    /*用新进程初始化等待队列元素*/
    init_waitqueue_entry(&data, result);
    init_waitqueue_entry(&data1, result1);
    /*将新进程加入等待队列中*/
    add_wait_queue(&head, &data);
    add_wait_queue(&head, &data1);
    __wake_up(&head, TASK_ALL,0, NULL);
    /*循环显示等待队列中的进程的信息*/
    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 result is :%d\n", result->pid);
    printk("the pid of result1 is :%d\n", result1->pid);
    printk("the current pid is:%d\n", current->pid); //显示当前进程的PID值
    printk("out add_wait_queue_init.\n");
    return 0;
}

模块退出函数定义:

static void __exit add_wait_queue_exit(void)
              {
                  printk("Goodbye add_wait_queue\n");
              }

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

module_init(add_wait_queue_init);
              module_exit(add_wait_queue_exit);

实例运行结果及分析:

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

Linux内核 add_wait_queue()

结果分析:

由上图可以看出新创建的两个进程都成功添加进等待队列中,并且对应的f lags字段的值都为0,即相应的进程不是高优先级进程。进程3112添加在前,进程3113添加在后,而输出信息表明进程3113在等待队列头部,进程3112在等待队列尾部,说明函数add_wait_queue( )将进程插入等待队列的头部。

酷客网相关文章:

赞(0)

评论 抢沙发

评论前必须登录!