diff --git a/ThreadCancellation_async/master_slave/.vs/linux-debug/Ubuntu.scan.fgp.copy b/ThreadCancellation_async/master_slave/.vs/linux-debug/Ubuntu.scan.fgp.copy index c7f50b9..6f8e487 100644 --- a/ThreadCancellation_async/master_slave/.vs/linux-debug/Ubuntu.scan.fgp.copy +++ b/ThreadCancellation_async/master_slave/.vs/linux-debug/Ubuntu.scan.fgp.copy @@ -1,6 +1,6 @@ 1870684405 E:\MultiThreading_Part_A\ThreadCancellation_async\master_slave\CMakeLists.txt 190002877 E:\MultiThreading_Part_A\ThreadCancellation_async\master_slave\CMakePresets.json 465106864 E:\MultiThreading_Part_A\ThreadCancellation_async\master_slave\main.c -1536483663 E:\MultiThreading_Part_A\ThreadCancellation_async\master_slave\master_slave.c +180745754 E:\MultiThreading_Part_A\ThreadCancellation_async\master_slave\master_slave.c 393795895 E:\MultiThreading_Part_A\ThreadCancellation_async\master_slave\master_slave.h -1551962192 E:\MultiThreading_Part_A\ThreadCancellation_async\master_slave\ +-1338719569 E:\MultiThreading_Part_A\ThreadCancellation_async\master_slave\ diff --git a/ThreadCancellation_async/master_slave/.vs/master_slave/v17/.wsuo b/ThreadCancellation_async/master_slave/.vs/master_slave/v17/.wsuo index b85139e..6a9c449 100644 Binary files a/ThreadCancellation_async/master_slave/.vs/master_slave/v17/.wsuo and b/ThreadCancellation_async/master_slave/.vs/master_slave/v17/.wsuo differ diff --git a/ThreadCancellation_async/master_slave/.vs/master_slave/v17/Browse.VC.db b/ThreadCancellation_async/master_slave/.vs/master_slave/v17/Browse.VC.db index 4668bd2..c257e2a 100644 Binary files a/ThreadCancellation_async/master_slave/.vs/master_slave/v17/Browse.VC.db and b/ThreadCancellation_async/master_slave/.vs/master_slave/v17/Browse.VC.db differ diff --git a/ThreadCancellation_async/master_slave/.vs/master_slave/v17/DocumentLayout.json b/ThreadCancellation_async/master_slave/.vs/master_slave/v17/DocumentLayout.json index cd676fd..7cbaa84 100644 --- a/ThreadCancellation_async/master_slave/.vs/master_slave/v17/DocumentLayout.json +++ b/ThreadCancellation_async/master_slave/.vs/master_slave/v17/DocumentLayout.json @@ -52,7 +52,7 @@ "RelativeDocumentMoniker": "master_slave.c", "ToolTip": "E:\\MultiThreading_Part_A\\ThreadCancellation_async\\master_slave\\master_slave.c", "RelativeToolTip": "master_slave.c", - "ViewState": "AQIAAAAAAAAAAAAAAAAAAA0AAAAZAAAA", + "ViewState": "AQIAAB4AAAAAAAAAAAAQwC4AAAARAAAA", "Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000423|", "WhenOpened": "2024-03-14T10:31:49.359Z", "EditorCaption": "" @@ -65,7 +65,7 @@ "RelativeDocumentMoniker": "main.c", "ToolTip": "E:\\MultiThreading_Part_A\\ThreadCancellation_async\\master_slave\\main.c", "RelativeToolTip": "main.c", - "ViewState": "AQIAACEAAAAAAAAAAAAAAAEAAAAAAAAA", + "ViewState": "AQIAACQAAAAAAAAAAAAAAAEAAAAAAAAA", "Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000423|", "WhenOpened": "2024-03-14T10:21:23.52Z", "EditorCaption": "" diff --git a/ThreadCancellation_async/master_slave/.vs/slnx.sqlite b/ThreadCancellation_async/master_slave/.vs/slnx.sqlite index 1a2d892..efb64e2 100644 Binary files a/ThreadCancellation_async/master_slave/.vs/slnx.sqlite and b/ThreadCancellation_async/master_slave/.vs/slnx.sqlite differ diff --git a/ThreadCancellation_async/master_slave/master_slave.c b/ThreadCancellation_async/master_slave/master_slave.c index a6e9016..077914e 100644 --- a/ThreadCancellation_async/master_slave/master_slave.c +++ b/ThreadCancellation_async/master_slave/master_slave.c @@ -1,5 +1,20 @@ #include "master_slave.h" +void +memory_cleanup_handler(void* arg) { + + printf("%s invoked ...\n", __FUNCTION__); + free(arg); +} + +void +file_cleanup_handler(void* arg) { + + printf("%s invoked ...\n", __FUNCTION__); + fclose((FILE*)arg); + +} + void * write_into_file(void* arg) { @@ -14,15 +29,22 @@ write_into_file(void* arg) { /* Mode of cancellation */ pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0); int* thread_id = (int*)arg; + + /* When pthread_cancel is invoked and this thread get the + signal to get cancelled, this API is invoked to free up the + resources used by this thread.*/ + pthread_cleanup_push(memory_cleanup_handler, arg); sprintf(file_name, "thread_%d.txt", *thread_id); FILE* fptr = fopen(file_name, "w"); + pthread_cleanup_push(file_cleanup_handler, fptr); + if (!fptr) { printf("Error : Could not open log file %s, errno = %d\n", file_name, errno); - return 0; + pthread_exit(0); } while (1) { @@ -32,6 +54,16 @@ write_into_file(void* arg) { sleep(1); } - fclose(fptr); + /* If this thread is successfully executed and the cancellation request + is not invoked till now then, the stack where resource free API are + need to be cleaned.*/ + pthread_cleanup_pop(0); + pthread_cleanup_pop(0); + + /*This is not executed as this thread is in infinite loop + and when the pthread_cancel is executed this thread is cancelled + while it was executing in the loop. Hence, this causes resource + leakage.*/ + // fclose(fptr); return 0; } \ No newline at end of file