Why does it close? Because you told it to!
Look at your code:
while(1){
...
return G_SOURCE_CONTINUE;
}
When the
main
function hits a
return
statement, that's it: the program ends. That is exactly what it is designed to do.
And since that
return
is the only way out of your
while
loop, the code below that loop is never, ever, executed.
Stop guessing: sit down and think about exactly what you are trying to do, plan it out manually, and then try to work out how to replicate that as English instructions. Then try to "execute" those English language instructions exactly and see if it works. If it doesn't, improve the instructions and try again.
When they do work, start translating them into code and testing that.
But at the moment, you are rushing right into code without thinking about what you need to do, and that will never work.