How to check if widget is related to spesific kernel/spesific notebook, if notebook is related to spesific kernel?

Hi, I want to check if widget is related to spesific kernel/spesific notebook, if notebook is related to spesific kernel. Purpose: when I shutdown kernel, I should programatically delete related notebook to it + dispose widgets related too it.
Or wise versa, when I close notebook/main area widget, it should also close kernel, notebook too. How can I achieve these things? Any code example? Thanks