lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


* On 2009-04-29 Adrian Sietsma <adrian_groups@sietsma.com> wrote  :

>> 4) suppose this scenario: i run a LUA script inside a process.
>> the process receives a finish-application message.
>> does it exists any "terminated" flag to know this "terminating process"
>>
>> i mean, how do i have to do to exit an infinit loop ?
>>
>> while not(lua.terminating)
>>   -- do loop
>> end
>
> I'll leave that for someone else.

I can only comment on this from the Unix/POSIX point of view, since I am
completely ignorant of programming in the win32 environment.

In POSIX, a running process can be killed by sending it a signal - this
is done with the kill command or the kill() function, or by hitting
Ctrl-C in an interactive shell. (For simplicity I will only talk about
the 'TERM' signal, which is usually used to request the termination of a
process)

The program that receives the signal has a few ways to deal with this:

The default is to do nothing special at all. In this case, the operating
system decides to terminate the program, which means that it just stops
running right in the middle of whatever it happened to be doing, all its
memory is freed, its file descriptors are closed: it simply ceases to
exist. This can be ok for your program, but if you want to make sure
your code can cleanup and exit gracefully, this is probably not
sufficient.

Another method would be to request the operating system to call a given
function when the application receives the TERM signal. This function
can then indicate your inifinite loop that the process is requested to
exit, after which you can do a proper cleanup and terminate the program.

The problem in your case might be that Lua has no native support for
working with signals, e.g, there is no way to register a Lua function as
a signal handler. This is because Lua is mostly oblivious about
OS-specific things like processes and signals. This sounds like a bad
thing, but it is one of the reasons why lua is so small and lean, and
portable to virtually any platform with support for a C compiler and
enough memory avaialble.

So these are your options:

- Do not rely on signals at all, but provide another mechanism for
  instructing your program to exit. (network, command line, etc ?)

- Add some code written in C to your program which does the signal
  handling. The simplest implementation would simply set a global flag
  when the signal is received, and your mainloop can poll the value
  of this flag through a simple C function.

- Learn to live with the program termination. This is really not a big
  problem for a lot of applications.


Ico

-- 
:wq
^X^Cy^K^X^C^C^C^C