进程休眠之sleepy不懂的地方

本文探讨了Linux中通过sleepy_read和sleepy_write函数实现的进程间通信机制。具体分析了当多个进程调用sleepy_read进入等待状态时,如何通过调用sleepy_write来唤醒这些进程,并解释了唤醒顺序及重复唤醒时的行为。
无法理解:

ssize_t sleepy_read (struct file *filp, char __user *buf, size_t count, loff_t *pos)
{
    printk(KERN_DEBUG "process %i (%s) going to sleep\n",
    current->pid, current->comm);
    wait_event_interruptible(wq, flag != 0);
    flag = 0;
    printk(KERN_DEBUG "awoken %i (%s)\n", current->pid, current->comm);
    return 0; /* EOF */
}

ssize_t sleepy_write (struct file *filp, const char __user *buf, size_t count,
loff_t *pos)
{
printk(KERN_DEBUG "process %i (%s) awakening the readers...\n",
current->pid, current->comm);
  flag = 1;
    wake_up_interruptible(&wq);

    return count; /* succeed, to avoid retrial */

}


假设有两个进程在等待时调用了sleepy_write函数,具体的过程是怎么样的?
为什么说第二个被唤醒的进程马上又会进入休眠呢?

等待是什么意思啊?是休眠了,是不是调用read休眠了,如果是那为什么还可以调用sleepy_write。

写的测试程序:

//testsleepy.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
    int fd;
    char* filename;  
    char buf[1] ={0x54};
    if (argc != 3){
        return 0;
    }
 
    filename = argv[1];

    fd = open(filename, O_RDWR);
    
    if (fd < 0){
        printf("error, can't open %s\n", filename);
        return 0;
    }
    if (!strcmp("read", argv[2])){ 
read(fd, buf, 1);
    }
    
    else if(!strcmp("write", argv[2])){
write(fd, buf, 1);    
    }
    return 0;
}

terminal1:
$ ./testsleepy /dev/sleepy read  

terminal2:
$ ./testsleepy /dev/sleepy read 

ternimal1 && terminal2 running testsleepy is sleepy

How can the testsleepy to wake_up throught calling write function ?

When I open another terminal and run ./testsleepy /dev/sleepy write。


Please guess the result which the terminal will wake up。

The result is that the second terminal program will wake up.

when I run ./testsleepy /dev/sleepy write again. 
The first terminal will wake up


user four terminal to read 
cat /var/log/kern.log
Oct 16 23:52:15 ubuntu kernel: [19326.544325] process 7211 (testsleep) going to sleep //terminal1
Oct 16 23:52:18 ubuntu kernel: [19329.207916] process 7212 (testsleep) going to sleep //terminal2
Oct 16 23:52:19 ubuntu kernel: [19331.122049] process 7213 (testsleep) going to sleep //terminal3
Oct 16 23:52:23 ubuntu kernel: [19334.319102] process 7214 (testsleep) going to sleep //terminal4
Oct 16 23:52:26 ubuntu kernel: [19337.326668] process 7216 (testsleep) awakening the readers... //terminal write
Oct 16 23:52:26 ubuntu kernel: [19337.326801] awoken 7214 (testsleep)
Oct 16 23:52:28 ubuntu kernel: [19339.622754] process 7217 (testsleep) awakening the readers... //terminal write
Oct 16 23:52:28 ubuntu kernel: [19339.622837] awoken 7211 (testsleep)
Oct 16 23:52:34 ubuntu kernel: [19345.777920] process 7218 (testsleep) awakening the readers... //terminal write
Oct 16 23:52:34 ubuntu kernel: [19345.778015] awoken 7213 (testsleep)
Oct 16 23:52:36 ubuntu kernel: [19347.477079] process 7219 (testsleep) awakening the readers... //terminal write
Oct 16 23:52:36 ubuntu kernel: [19347.477159] awoken 7212 (testsleep)
 
I don't know why the results will look like this ! 
 
PS:尚待我去理解的地方!有时间在去慢慢理解吧!
### 关于 Verv Sleepy 的使用指南 Verv Sleepy 是一款用于自动化任务和简化命令行操作的工具,主要应用于 Windows 系统下的批处理脚本编写与执行。以下是关于 Verv Sleepy 的基本使用指南[^4]。 #### 1. 安装与启动 - 下载并安装 Verv Sleepy 软件包。通常可以从其官方网站或可信的第三方资源获取。 - 安装完成后,通过双击桌面快捷方式或在开始菜单中搜索“Verv Sleepy”来启动程序。 #### 2. 基本功能介绍 Verv Sleepy 提供了一系列便捷的功能,包括但不限于: - **任务调度**:可以设置定时任务,例如每天自动运行某个批处理文件。 - **日志记录**:所有任务的执行情况都会被记录到日志文件中,方便后续排查问题。 - **命令行集成**:支持直接调用 CMD 指令,并结合图形化界面进行参数配置。 #### 3. 使用示例 以下是一个简单的 Verv Sleepy 批处理脚本示例,用于备份指定目录中的文件: ```batch @echo off :: 设置源文件夹和目标文件夹 set source=C:\Users\Username\Documents set destination=D:\Backup :: 创建目标文件夹(如果不存在) if not exist "%destination%" mkdir "%destination%" :: 复制文件 xcopy /s /e /y "%source%" "%destination%" ``` 将上述代码保存为 `.bat` 文件后,可以在 Verv Sleepy 中配置任务以定期运行此脚本。 #### 4. 查看帮助文档 类似于 CMD 的 `help` 指令,Verv Sleepy 也提供了内置的帮助功能: - 打开软件后,点击菜单栏中的“Help”选项。 - 或者按快捷键 `F1` 来访问详细的在线文档[^5]。 #### 5. 高级配置 对于需要更复杂功能的用户,可以参考以下高级配置方法: - **环境变量设置**:通过右键单击“我的电脑”->“属性”->“高级系统设置”->“环境变量”,为 Verv Sleepy 添加自定义路径。 - **插件扩展**:部分版本支持安装额外的插件以增强功能,具体请参考官方文档。 ### 注意事项 - 在运行任何脚本之前,请确保已备份重要数据,以免因错误操作导致数据丢失。 - 如果遇到兼容性问题,尝试以管理员身份运行 Verv Sleepy[^6]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

john_liqinghan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值