c - How to free dynamic allocated variable by SIGTERM? -


I work on some code like this

  ... header .. Int * on; Zero kids () {... // some work free (var); Exhaust (EXIT_SUCCESSFUL); } Int main (zero) {... // recurrence variable var = (int *) malloc (N * sizeof (int)); ... // for work with var (int i; i & lt; PROC_COUNT; i ++) {pid_t child = fork (); If (pid == 0) {child (); // Break the main function of the new process; } Elseif (pid & lt; 0) {// There is a huge problem - & gt; Kill every process (0, cigterm); Wait point (0, zero, 0); // Waiting for free children (var); Exhaust (EXIT_FAILURE); }} Free (var); Return EXIT_SUCCESS; }  

When the process is cut, all the variables are also cloned. All types of copies are freed in regular cases.

If there is an error by byk () , then I send the signal SIGTERM for all the processes created. And I have to write the signal handler for SignerM, which will end the free version and the app. However, free () is not a signal safe function - so I should not call it.

Edit: Valgrind also shows still available variables:

 

code> == 5928 == Error summary: 0 errors out of 0 references (pressed: 1 to 15) == 5928 == malloc / free: Use on exit: 20 bytes in 1 byte == 5928 == malloc / free: 1 pan, 0 free , Allocate 20 bytes. == 5928 == To count the detected errors, walk again: -v == 5928 == Search for Pointer 1 without free blocks == 5928 == Check 49,164 bytes

I doubt you need it. Any OS that supports Kanka () will automatically free allocations from Molkok () when a process gets out, even though it does so (including termination).

There are present environments where programs do not participate in processes, and where you have to be very careful to leave lying on exit but those environments are not POSIX, and will not support fork () . They can not support signs, for that matter if you are writing for such an unusual environment, then check your document ...

If you want to see a clean Valgrind Report , Then you must handle the handler a event in the child's event loop (or set a flag and post a semaphore, or whatever), and process the event as a clean exit. It is also what you will do if your program was an interactive application and you wanted to save the user's data to SIGTERM, you have not translated SIGTERM to an event even before handling your UI structure.


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -