2020年10月4日 星期日

[Linux] Kernel函式庫

Kernel

●忙碌等待(busy wait)

會占住CPU,不讓CPU處理其他事,不適合長時間使用.
#include <linux/delay.h>
void mdelay(unsigned long msecs); //毫秒
void udelay(unsigned long msecs); //微秒
void ndelay(unsigned long msecs); //奈秒


不忙碌等待(non-busy wait)
#include <linux/delay.h>
void ssleep(unsigned int seconds);
void msleep(unsigned int mecs);
unsigned long msleep_interruptible(unsigned int mecs);


取時間
#include "linux/time.h"
void do_gettimeofday(struct timeval *tv);

struct timeval
{
time_t tv_sec; //seconds
suseconds_t tv_usec;  //microseconds
};

和user process的gettimeofday使用方式一樣


---
在多執行序下,為避免同時執行無法同時執行的區塊,須把他保護起來,此區塊叫(critical section).

旗標(smaphore)
但他會sleep,不能用在interrupt,一次只讓一個執行序執行critical section.
init_MUTEX_LOCKED();

禁止中斷
void local_irq_save(unsigned long flags);
void local_irq_restore(unsigned long flags);

spinlock
保護interrupt context內的critical section,會卡住CPU,不會sleep,所以要極力縮短鎖定時間.
spin_lock_irqsave(...)
spin_unlock_irqrestore(...)
---


Process(user) context is the current state of process, process context can be go into the sleep, preemptable, It perform time consumable task, acquiring and releasing mutex.

Interrupt context is when the interrupt occurs state/priority goes to interrupt handler, and current process stops/saves until we complete interrupt, Interrupt context is not time consumable, non preemptable, It cannot go into the sleep.



沒有留言:

張貼留言