After 11 ..
Mr. Wang is about to graduate from college. Now he wants to write his resume, submit his resume, and find a job.
Wang was ecstatic when he got to work at home. He looked at it again, and his heart was cool ..
"What's the matter, Mr. Wang? Looking at your job, I am here to help you. I am afraid of anything. Isn't there any more .. "Looking at the Long Dragon that exists at the elevator exit, I said (in fact, I am also afraid, but I cannot say it ).
"No, you don't know. I'm not afraid of any questions during the interview. Are you technically still a problem? I don't believe that I still don't believe you, I am afraid that I don't even have the chance to interview. If you look at so many people, you don't know where to go. You said that every year, I don't want a good way to improve my skills in the HR authorities .. "Mr. Wang complained.
"Haha, there will be progress only when there is demand," I laughed. "Well, let's look at this. I just told you about the Linux Device Driver.ProgramBlocking/non-blocking IO in, maybe, you just met during the interview later, you don't have to worry about it .."
"Well, that's great. I can't find you here .."
When I saw Mr. Wang's interest, I also sold a token and said with a sigh of relief: "Let's hear about Linux Device Driver blocking/non-blocking Io"
Blocking: As the name implies, it means that when a device operation fails to obtain the resource, the operation is suspended until the operation is completed. The suspended process enters the sleep state, moved from the running queue of the Scheduler
Go until the waiting conditions are met.
Non-blocking: In turn, a process does not stop when it is unable to perform device operations. It either gives up or keeps querying until the location is available.
"Mr. Wang understands these two basic concepts. For example, today's interview is a blocking problem." I added, "of course, the answer is no. For example, if the driver is not blocked, if you want to obtain device operations, you can only use CPU queries continuously (of course, you cannot give up). Obviously, this will consume unnecessary CPU resources. There is no such problem when access is blocked. The process that cannot obtain the resource goes to sleep, and it gives CPU resources to other processes ".
"When I hear you say this, combined with today's situation, I understand, just what you said about sleep... If the conditions are met, then sleep will wake up the whole thing... "said John.
"That's good. I was a little impressed. I know how to touch the analogy. It's true. In this case, I'll give you two paragraphs.Code, Feel it for yourself"
Block all take a character from the serial port |
Non-blocking all take the serial port a character |
char Buf; FD = open ("/dev/TTYs ", o_rdwr); .... res = Read (FD, & Buf, 1); // return only when input is available on the serial port If (RES = 1) {< br> printf (" % C \ n ", Buf ); } |
CharBuf; FD =Open("/Dev/TTYs", O_rdwr | o_nonblock ); .... While (read (FD, & Buf, 1 )! = 1); // return if no input is input on the serial port,Institute // Read the serial port cyclically Printf("% C \ n", Buf ); |
L
Now we have a blocking method for reading, so the blocked process will enter the sleep state because no resources are obtained. Now we have to talk about the wake-up process. In the Linux Device Driver, you can use the wait queue
Wait queue to wake up blocking processes. Waiting queues can be used to implement asynchronous event notification in the kernel. Linux provides queue-waiting operations:
1) wait_queue_head_t my_queue; // defines the waiting queue Header
2) init_waitqueue_head (& my_queue); // initialize the queue Header
If the above two steps are troublesome, you can directly use declare_wait_queue_head (name)
3) declare_waitqueue (name, tsk); // defines the waiting queue
4) void fastcall add_wait_queue (wait_queue_head_t * q, wait_queue_t * Wait );
Void fastcall remove_wait_queue (wait_queue_head_t * q, wait_queue_t * Wait );
Adds the waiting queue wait to the waiting queue linked list pointed by the waiting queue header.
5) wait_event (queue, conditon );
Wait_event_interruptible (queue, condition); // can be interrupted by a signal
Wait_event_timeout (queue, condition, timeout );
Wait_event_interruptible_timeout (queue, condition, timeout); // cannot be interrupted by signals
Queue: The waiting queue is awakened as the waiting queue header.
Conditon: must be met, otherwise Blocking
Timeout has a higher priority than Conditon
6) void wake_up (wait_queue_head_t * Queue );
Void wake_up_interruptible (wait_queue_head_t * Queue );
The preceding operations will wake up all processes in the waiting queue that use the queue as the waiting queue header that belong to the waiting queue header.
7) sleep_on (wait_queue_head_t * q );
Interruptible_sleep_on (wait_queue_head_t * q );
Sleep_on is used to set the status of the current process to task_uninterruptible, define a waiting queue, and attach it to the waiting queue header Q until the resource is available. The Q-guided waiting queue is awakened. Interruptible_sleep_on works the same, but it sets the Process status to task_interruptible.
The two functions define and initialize the waiting queue, set the process status to task_uninterruptible or task_interruptible, and add the treated queue to the waiting queue header.
Then, use schedule to discard the CPU and schedule other processes for execution. Finally, when the process is awakened elsewhere, the waiting queue will be removed from the waiting queue header.
In the Linux kernel, use the set_current_state () and _ add_wait_queue () functions to change the current process status. Use current-> state = task_uninterruptible directly.
Similar statements are also supported.
Therefore, we may also see in many drivers that it does not call sleep_on or interruptible_sleep_on (), but changes and switches the Process status in person.
"Mr. Wang, after listening to so many questions, you have a rough idea. If the examiner asks this question, it should be smooth. Now it's too late, I will give you an example later. You can check it out. You'll be ready soon. Good luck .."