Smart Home-Multithreading Experience of Linux System

Smart home development-precautions for multi-threaded use of Linux systems

statement:

The code of this project is not open source, it may not help you. . .

process

I have been working on a small smart home project in the past two days (fewer functions...), I used the Raspberry Pi-3B development board, and encountered multi-threading issues during the development process. Let’s take a look... (The interface is true Ugly QAQ )
Insert picture description here

1. File descriptor

Features

File descriptorNo need to talk about it (hereinafter collectively referred to as "fd"), everyone who knows a little about Linux systems knows this stuff. Raspberry Pi uses it when receiving, reading, and sending certain data!
After calling some functions, the return fdis an integer (File descriptor array subscript)。

Focus! !

When one fdoccurs, remember to use it in timeClose it! !, Especially under the global variable fd, otherwise, you will find that fdthe value will increase step by step until it reaches the maximum value of the file descriptor array (under Linux, fdit will exceed the boundary when> 255)! At this time, you will find that although the program has not exited, the value can no longer be read normally. . .

2. Multithreading

Multithreading is a good thing, allowing a program to achieve multiple functions at the same time. However, you must be careful when using multithreading! ! ! It might be overturned...especially when multiple threads are used fd.

3. Problems encountered in the development of smart home projects

When developing a smart home, a C program may have to implement many functions at the same time, such as: controlling the input/output of the IO pin, reading the information sent by other nodes (the sensor data sent by the Arduino board), and accepting the 后端sent The control instructions, data processing, 服务器etc. are uniformly sent, etc.... Then multi-threading is needed. Multi-threading works well, and it is fun to play with anything! (But I'm a vegetable dog...)

3-1. Question:

When the program is running, I found that even if the close(fd)function is called , fdthe value will always be ++. After running for a long time, the program will run into a problem. I debugged this problem for a long time (it took almost an afternoon)

3-2. Reason: I have low hands, and walk by feeling

The reason is that I ran a function that reads information in a threaded manner in the Socket thread, and used 全局fdvariables, and the resulting fdvalues ​​were all incremented (the thread is set in the thread, and I know it in my mind. How terrible, except for programmers at the level of cattle...) After
debugging for a long time, after clearing the idea, I found that there is no need to put that function in the thread.
Insert picture description here
As shown in the picture: it 红框is the place where the problem occurred. I was still silly to fdreset it. . .
箭头The location is the correct wording, it should be just right, the system works perfectly! ! ! No fdmatter how long it runs, there will be no out-of-bounds problems.
Insert picture description here

4. Summary

1. When using multi-threading, if it is not necessary, try not to set threads in a thread.
2. Pay attention to fdthe value, use it up promptly close(fd);, don't exceed the boundary.

Guess you like

Origin blog.csdn.net/DSK_981029/article/details/111565857
Recommended