添加Linux内核api,进程调度 – Linux内核API add_wait_queue_exclusive

add_wait_queue_exclusive函数功能描述:函数add_wait_queue_exclusive( )实现将等待队列元素加入到等待队列的尾部,并设置等待队列元素的f lags值为WQ_FLAG_EXCLUSIVE,即为1,表示此进程是高优先级进程。

add_wait_queue_exclusive文件包含

#include

add_wait_queue_exclusive函数定义

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

函数定义格式:

void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait)

add_wait_queue_exclusive输入参数说明

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

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

add_wait_queue_exclusive返回参数说明

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

add_wait_queue_exclusive实例解析

编写测试文件:add_wait_queue_exclusive.c

头文件引用:

#include

#include

#include

#include

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_exclusive_init(void)

{

// 局部变量定义

char namefrm[] = "add_wait_queue.c";

char namefrm1[] = "add_wait_queue1.c";

char namefrm2[] = "add_wait_queue2.c";

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 add_wait_queue_exclusive_init.\n");

/*创建3个新进程*/

result=kthread_create_on_node(my_function, NULL, -1, namefrm);

result1=kthread_create_on_node(my_function, NULL, -1, namefrm1);

result2=kthread_create_on_node(my_function, NULL, -1, namefrm2);

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_exclusive(&head, &data1);

add_wait_queue_exclusive(&head, &data2);

add_wait_queue(&head, &data);

/*循环显示等待队列中的进程的信息*/

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);

// 显示当前等待队列中的进程数

// __wake_up(&head, TASK_ALL,0, NULL);

/*显示创建新进程的进程号*/

printk("the pid of result is :%d\n", result->pid);

printk("the pid of result1 is :%d\n", result1->pid);

printk("the pid of result2 is :%d\n", result2->pid);

printk("the current pid is:%d\n", current->pid); //显示当前进程的PID值

printk("out add_wait_queue_exclusive_init.\n");

return 0;

}

模块退出函数定义:

static void __exit add_wait_queue_exclusive_exit(void)

{

printk("Goodbye add_wait_queue_exclusive\n");

}

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

module_init(add_wait_queue_exclusive_init);

module_exit(add_wait_queue_exclusive_exit);

实例运行结果及分析:

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

cf4984ffdddc54f68997d4ef87c8428f.png

结果分析:

由上图可以看出经过函数add_wait_queue_exclusive( )加入等待队列中的等待队列元素f lags字段的值为1,经过函数add_wait_queue( )加入等待队列中的等待元素的flags字段的值为0,说明函数add_wait_queue_exclusive( )在加入等待队列元素时设置其f lags字段的值为1,即为WQ_FLAG_EXCLUSIVE。进程20856的添加在后,而位于等待队列头,说明函数add_wait_queue( )将等待队列元素加入等待队列头,进程20857、20858在等待队列中的顺序与添加的顺序相同,说明函数add_wait_queue_exclusive( )将等待队列元素加入等待队列尾部。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值