Linux system is an operating system that supports concurrent execution of multi-tasks. It can run multiple processes at the same time, thereby improving system utilization and efficiency. However, not all processes need to occupy processor resources all the time. Some processes can temporarily give up the processor under certain conditions, enter the sleep state, wait for the conditions to be met, then wake up and continue execution. The advantage of this is that it can save processor resources and give other processes that need to be executed more opportunities. It can also reduce the power consumption and heat of the system and improve the stability and lifespan of the system. This article will introduce the sleep and wake-up methods of processes in Linux systems, including the sleep reasons, sleep types, sleep functions, wake-up functions, and wake-up mechanisms of the process.
Of course, a process can also actively release control of the CPU. The function schedule() is a scheduling function that can be actively called by a process to schedule other processes to occupy the CPU. Once the process that voluntarily gives up the CPU is rescheduled to occupy the CPU, it will start execution from where it last stopped, that is, it will start execution from the next line of code that calls schedule().
Sometimes, a process needs to wait until a specific event occurs, such as device initialization, I/O operation completion, or timer expiration. In this case, the process must be removed from the run queue and added to a waiting queue. At this time, the process enters the sleep state.
One is an interruptible sleep state, its status flag is TASK_INTERRUPTIBLE;
The other is an uninterruptible sleep state, and its status flag is TASK_UNINTERRUPTIBLE. A process in an interruptible sleep state will sleep until a certain condition becomes true. For example, generating a hardware interrupt, releasing system resources that the process is waiting for, or delivering a signal can be the conditions to wake up the process. The uninterruptible sleep state is similar to the interruptible sleep state, but it has one exception, that is, the process that delivers the signal to this sleep state cannot change its state, that is, it does not respond to the signal to wake up. The uninterruptible sleep state is generally less used, but it is still very useful in some specific situations. For example, the process must wait and cannot be interrupted until a specific event occurs.
In modern Linux operating systems, processes generally enter the sleep state by calling schedule(). The following code performs
shows how to put a running process to sleep.
sleeping_task = current; set_current_state(TASK_INTERRUPTIBLE); schedule(); func1(); /* Rest of the code ... */
In the first statement, the program stores a process structure pointer sleeping_task, current is a macro, which points to the executing process
process structure. set_current_state() changes the state of the process from execution state TASK_RUNNING to sleep state
TASK_INTERRUPTIBLE. If schedule() is scheduled by a process with the status TASK_RUNNING, then schedule() will schedule another process to occupy the CPU; if schedule() is scheduled by a process with the status TASK_INTERRUPTIBLE or TASK_UNINTERRUPTIBLE, then there is an additional step to Being executed: The currently executing process will be removed from the run queue before another process is scheduled, which will cause the running process to go to sleep because it is no longer in the run queue.
We can use the following function to wake up the process that just went to sleep.
wake_up_process(sleeping_task);
After calling wake_up_process(), the status of the sleeping process will be set to TASK_RUNNING, and the scheduler
It will be added to the run queue. Of course, this process can only be put into operation the next time it is scheduled by the scheduler.
Invalid wakeup
In almost all cases, the process will go to sleep after checking certain conditions and finding that the conditions are not met. But sometimes
However, the process will start sleeping after the judgment condition is true. If this is the case, the process will sleep indefinitely. This is the so-called invalid wake-up problem. In the operating system, when multiple processes attempt to perform some processing on shared data, and the final result depends on the order in which the processes are run, a race condition occurs. This is a typical problem in the operating system. Invalid wake-up It is precisely due to competitive conditions.
Suppose there are two processes A and B. Process A is processing a linked list. It needs to check whether the linked list is empty. If it is not empty, process the link
The data in the table is undergoing some operations, and process B is also adding nodes to the linked list. When the linked list is empty, because there is no data to operate, process A goes to sleep. When process B adds a node to the linked list, it wakes up process A. The code is as follows:
A process:
1 spin_lock(&list_lock); 2 if(list_empty(&list_head)) { 3 spin_unlock(&list_lock); 4 set_current_state(TASK_INTERRUPTIBLE); 5 schedule(); 6 spin_lock(&list_lock); 7 } 8 9 /* Rest of the code ... */ 10 spin_unlock(&list_lock);
B process:
100 spin_lock(&list_lock); 101 list_add_tail(&list_head, new_node); 102 spin_unlock(&list_lock); 103 wake_up_process(processa_task);
这里会出现一个问题,假如当A进程执行到第3行后第4行前的时候,B进程被另外一个处理器调度
投 入运行。在这个时间片内,B进程执行完了它所有的指令,因此它试图唤醒A进程,而此时的A进程还没有进入睡眠,所以唤醒操作无效。在这之后,A 进程继续执行,它会错误地认为这个时候链表仍然是空的,于是将自己的状态设置为TASK_INTERRUPTIBLE然后调用schedule()进入睡 眠。由于错过了B进程唤醒,它将会无限期的睡眠下去,这就是无效唤醒问题,因为即使链表中有数据需要处理,A 进程也还是睡眠了。
避免无效唤醒
如何避免无效唤醒问题呢?我们发现无效唤醒主要发生在检查条件之后和进程状态被设置为睡眠状
态之前, 本来B进程的wake_up_process()提供了一次将A进程状态置为TASK_RUNNING 的机会,可惜这个时候A进程的状态仍然是TASK_RUNNING,所以wake_up_process()将A进程状态从睡眠状态转变为运行状态的努力 没有起到预期的作用。要解决这个问题,必须使用一种保障机制使得判断链表为空和设置进程状态为睡眠状态成为一个不可分割的步骤才行,也就是必须消除竞争条 件产生的根源,这样在这之后出现的wake_up_process ()就可以起到唤醒状态是睡眠状态的进程的作用了。
找到了原因后,重新设计一下A进程的代码结构,就可以避免上面例子中的无效唤醒问题了。
A进程:
1 set_current_state(TASK_INTERRUPTIBLE); 2 spin_lock(&list_lock); 3 if(list_empty(&list_head)) { 4 spin_unlock(&list_lock); 5 schedule(); 6 spin_lock(&list_lock); 7 } 8 set_current_state(TASK_RUNNING); 9 10 /* Rest of the code ... */ 11 spin_unlock(&list_lock);
可以看到,这段代码在测试条件之前就将当前执行进程状态转设置成TASK_INTERRUPTIBLE了,并且在链表不为空的情况下又将自己置为TASK_RUNNING状态。这样一来如果B进程在A进程进程检查
了链表为空以后调用wake_up_process(),那么A进程的状态就会自动由原来TASK_INTERRUPTIBLE
变成TASK_RUNNING,此后即使进程又调用了schedule(),由于它现在的状态是TASK_RUNNING,所以仍然不会被从运行队列中移出,因而不会错误的进入睡眠,当然也就避免了无效唤醒问题。
Linux内核的例子
在Linux操作系统中,内核的稳定性至关重要,为了避免在Linux操作系统内核中出现无效唤醒问题,
Linux内核在需要进程睡眠的时候应该使用类似如下的操作:
/* ‘q’是我们希望睡眠的等待队列 /
DECLARE_WAITQUEUE(wait,current);
add_wait_queue(q, &wait);
set_current_state(TASK_INTERRUPTIBLE);
/ 或TASK_INTERRUPTIBLE /
while(!condition) / ‘condition’ 是等待的条件*/
schedule();
set_current_state(TASK_RUNNING);
remove_wait_queue(q, &wait);
上面的操作,使得进程通过下面的一系列步骤安全地将自己加入到一个等待队列中进行睡眠:首先调
用DECLARE_WAITQUEUE ()创建一个等待队列的项,然后调用add_wait_queue()把自己加入到等待队列中,并且将进程的状态设置为 TASK_INTERRUPTIBLE 或者TASK_INTERRUPTIBLE。然后循环检查条件是否为真:如果是的话就没有必要睡眠,如果条件不为真,就调用schedule()。当进程 检查的条件满足后,进程又将自己设置为TASK_RUNNING 并调用remove_wait_queue()将自己移出等待队列。
从上面可以看到,Linux的内核代码维护者也是在进程检查条件之前就设置进程的状态为睡眠状态,
然后才循环检查条件。如果在进程开始睡眠之前条件就已经达成了,那么循环会退出并用set_current_state()将自己的状态设置为就绪,这样同样保证了进程不会存在错误的进入睡眠的倾向,当然也就不会导致出现无效唤醒问题。
下面让我们用linux 内核中的实例来看看Linux 内核是如何避免无效睡眠的,这段代码出自Linux2.6的内核(linux-2.6.11/kernel/sched.c: 4254):
4253 /* Wait for kthread_stop */
4254 set_current_state(TASK_INTERRUPTIBLE);
4255 while (!kthread_should_stop()) {
4256 schedule();
4257 set_current_state(TASK_INTERRUPTIBLE);
4258 }
4259 __set_current_state(TASK_RUNNING);
4260 return 0;
上面的这些代码属于迁移服务线程migration_thread,这个线程不断地检查kthread_should_stop(),
直 到kthread_should_stop()返回1它才可以退出循环,也就是说只要kthread_should_stop()返回0该进程就会一直睡 眠。从代码中我们可以看出,检查kthread_should_stop()确实是在进程的状态被置为TASK_INTERRUPTIBLE后才开始执行 的。因此,如果在条件检查之后但是在schedule()之前有其他进程试图唤醒它,那么该进程的唤醒操作不会失效。
This article introduces the sleep and wake-up methods of processes in Linux systems, including the sleep reason, sleep type, sleep function, wake-up function and wake-up mechanism of the process. By understanding and mastering this knowledge, we can better manage and control the processes in the Linux system, making the system run more energy-saving and efficient. Of course, there are many details and techniques for sleeping and waking up processes in Linux systems, which require us to continue to learn and practice
The above is the detailed content of Sleeping and waking up Linux processes: Make your system more energy-saving and efficient. For more information, please follow other related articles on the PHP Chinese website!