How to correctly free resources in an always running application?

How to release resources in the application that should work since the launch of the device to turn off? In my case the application is a driver. On the one hand, you can not release it - when you close the application the file will be closed, and all threads will be nailed by the operating system, on the other - to do so is still wrong, because I feel that I'm potentially loaded shotgun and pointed at my leg.

void* first_thread (void* arg)
{
 int fd = open("/path/to/file", O_RDWR);
 // ...
 while (true) {
 // work with file
}
}

void* second_thread (void* arg)
{
 int fd = open("/path/to/second_file", O_RDWR);
 // ...
 while (true) {
 // work with file
}
}

int main(int argc, char *argv[])
{
 pthread_create(NULL, NULL, first_thread, NULL);
 pthread_create(NULL, NULL, second_thread, NULL);

 return foo(argc, argv); // here something continue to do and the app is not terminated
}


At what point to close files and terminate threads?
April 7th 20 at 15:50
3 answers
April 7th 20 at 15:52
Solution
Before the end of the program you must pass to flow a signal about the completion, they will be checked for each iteration of the loop and complete it if necessary, and closing files to do after a cycle.
Thanks for the reply. Very interesting. Along the way, ask one more question. I have two threads. And let's say the first thread was unable to open the file, and as a consequence it is impossible to continue working. How to make so that this stream is said second stream to he ended? The issue is finding the correct way razrulivaniya this situation. And use a global variable as a flag, I don't think is a correct way. Inclined to think that for this situation it is also possible to apply signals. - jarvis_Metz37 commented on April 7th 20 at 15:55
@mariela.Bartoletti, depends on the details. Likely to exchange conditions suitable partial flow condition. - Jailyn commented on April 7th 20 at 15:58
April 7th 20 at 15:54
In POSIX systems, before the arrival of the OS needs to send a signal
https://ru.wikipedia.org/wiki/SIGTERM

Windows has a similar mechanism

Judging by the tag STM32, you have some sort of OS installed? See the documentation for the implementation of TASK MANAGER and SIGTERM if implemented, then insert the processor in it and close the file
tag STM32 there. I have Linux. - jarvis_Metz37 commented on April 7th 20 at 15:57
@mariela.Bartoletti, Mixed caption, and tags.

Here is an example of how you can make
citforum.ru/programming/unix/signals - Samantha.Mitchell commented on April 7th 20 at 16:00
April 7th 20 at 15:56
Since we are talking about the C language it is necessary to handle error conditions gracefully open/read/write/fseek/close and respond to them. And close the file handle in any possible outcome. Oh and don't forget to do a free() after malloc(). Roughly speaking, the number of malloc should always be equal to free.

If there was death posix thread must somehow find all the old descriptors, orphaned and close them too. Ie the process, a parent must somehow to see them.
it is necessary to correctly handle error open/read/write/fseek/close and respond to them. And close the file handle in any possible outcome. Oh and don't forget to do a free() after malloc(). Roughly speaking, the number of malloc should always be equal to free.


For me it is clear. It was not clear how to do it specifically, in my case. - jarvis_Metz37 commented on April 7th 20 at 15:59
@mariela.Bartoletti, well, you have inside the while - what? Perhaps the file read operations. Read sets the error code in errno. Check it out. There... if it is EBADF the file descriptor is dead. And priotkryvaet it through the open again. All about the question. You want to write fault tolerant applications that 100 years uptime downtime? Yes iron many lives. You hold your application at least a month running and watch for errors. And then you'll know where straw podstelit. - Claud99 commented on April 7th 20 at 16:02
... if it is EBADF the file descriptor is dead. And priotkryvaet it through the open again. ...

@rod.Reichel, here's also an interesting point - what if the file reopen failed? And the second, and the third and fourth, and so on... I understand that it is necessary to specify some limit of retries so that the program does not continue to engage in futile attempts to open the file. - jarvis_Metz37 commented on April 7th 20 at 16:05
@mariela.Bartoletti, presumably - doing like 10 attempts with a timeout of growing exponentially. Then get out of the program. It makes no sense to peck a disk that utkucan (unmounted) or it runs out of space. - Claud99 commented on April 7th 20 at 16:08

Find more questions by tags Cmultithreading