| Author |
When a program exit.
|
|
| Raymond 2005-06-15, 8:59 pm |
| Hi all:
I have two questions about program exit.
Thank you very much for your time.
1. When a program exit. The program will invoke a clean up procedure.
How the clean up procedure work?
2. In a multi-threads program. When the main thread returns. What
happens. Dose the system terminate all the other thread or not? Dose any
Unix platform do the same policy?
Best Regard.
Raymond
| |
| David Schwartz 2005-06-15, 8:59 pm |
|
"Raymond" <rshen_bea@yahoo.com> wrote in message
news:42b0dc03$0$18646$14726298@news.sunsite.dk...
> Hi all:
>
> I have two questions about program exit.
> Thank you very much for your time.
>
> 1. When a program exit. The program will invoke a clean up procedure. How
> the clean up procedure work?
I can't understand the question. Are you talking about specific
program's cleanup procedure or are you asking what every UNIX does when any
process exits? If the latter, it closes all open file descriptors and
releases most resources associated with the program. The exact behavior
depends upon the resource.
> 2. In a multi-threads program. When the main thread returns. What happens.
> Dose the system terminate all the other thread or not? Dose any Unix
> platform do the same policy?
All other threads are terminated by the system. This is a POSIX
requirement so you will see it on any POSIX-compliant system where POSIX
threads are involved.
DS
| |
| Raymond 2005-06-15, 8:59 pm |
| David.
Thank you very much
David Schwartz wrote:
> "Raymond" <rshen_bea@yahoo.com> wrote in message
> news:42b0dc03$0$18646$14726298@news.sunsite.dk...
>
>
>
> I can't understand the question. Are you talking about specific
> program's cleanup procedure or are you asking what every UNIX does when any
> process exits? If the latter, it closes all open file descriptors and
> releases most resources associated with the program. The exact behavior
> depends upon the resource.
My question is about the every UNIX dose when any process exits. And you
give me a answer.
But I still wander the order of resource releasing. Which resource will
be release first and which will be release secondly. And in a
multi-threads program, Which thread perform cleanup procedure and When
to terminate the other threads.
>
>
>
>
> All other threads are terminated by the system. This is a POSIX
> requirement so you will see it on any POSIX-compliant system where POSIX
> threads are involved.
>
> DS
>
>
| |
| Kurtis D. Rader 2005-06-16, 2:49 am |
| On Thu, 16 Jun 2005 10:20:46 +0800, Raymond wrote:
> My question is about the every UNIX dose when any process exits. And you
> give me a answer.
>
> But I still wander the order of resource releasing. Which resource will
> be release first and which will be release secondly.
I've been writing applications under UNIX for twenty years. For the past
fifteen I've earned a living providing Linux (and before that DYNIX/ptx)
kernel support. I've never had a customer ask this question. Nor can I
think of why it would matter. I suspect my confusion is due to not
understanding what you mean by "resource".
The exact sequence will depend on the operating system implementation. To
get some idea of how this is handled look at the source for any of the
open source implementations that provide a UNIX like environment. For
example, go to http://kernel.org and download the source for Linux and
look at the do_exit() function.
| |
| David Schwartz 2005-06-16, 2:49 am |
|
"Raymond" <rshen_bea@yahoo.com> wrote in message
news:42b0e201$0$18636$14726298@news.sunsite.dk...
> But I still wander the order of resource releasing. Which resource will be
> release first and which will be release secondly.
I can't imagine why you think this would matter. We have a
misunderstanding about what a "resource" is. By "resource", I mean things
the process got from the kernel such as open files, sockets, memory areas,
file locks, and so on. Effectively, it all disappears at once when the
process terminates.
> And in a multi-threads program, Which thread perform cleanup procedure and
> When to terminate the other threads.
This totally varies from implementation to implementation. They all try
to create the illusion that as soon as process termination is requested, all
the threads terminate at the same instant.
[vbcol=seagreen]
[vbcol=seagreen]
I should add to this that the main thread can terminate itself without
terminating the process by exiting (calling pthread_exit) instead of
returning. In this case, the process terminates when the last thread exits
or something causes the process to terminate (such as receipt of a fatal
signal, calling 'abort' or 'exit', or whatever).
DS
| |
| Raymond 2005-06-16, 2:49 am |
| Dear All:
Thank you very much for your attention.
This is really a problem in my application. I have a program running on
AIX4.33. It have several threads.
A main() is written like this.
int main()
{
running = true;
//
//Spawn other threads, Including IOWorkerThread.
//
// Do some work.
running = false;
return 0;
}
The IOWorkerThread is write like this:
void * IOWorkerThread(void * v)
{
sigthreadmask(...some signal...)
while(running)
ret = sigwait(&use_set, &sig);
// Do some work using memory space A.
}
return NULL;
}
When my main() is return. I find the IOWorkerThread catch a signal
"SIGIO" and do some work. But the memory A is already freed by main().
So the function core dump.
When I use dbx to analyse the core file. The error message is :
0xd3cf7b10 ($PTRGL) 800b0000 lwz r0,0x0(r11)
So I think I need know the exit procedure very detail.
Thank you.
Raymond.
| |
| David Schwartz 2005-06-16, 2:49 am |
|
"Raymond" <rshen_bea@yahoo.com> wrote in message
news:42b10c90$0$18644$14726298@news.sunsite.dk...
> When my main() is return. I find the IOWorkerThread catch a signal "SIGIO"
> and do some work. But the memory A is already freed by main().
How was the memory allocated? My bet is it was allocated local to the
'main' function. This means your problem has nothing to do with process
termination and everything to do with not allocating memory in the proper
scope for its usage.
If the problem really is as you describe, then the solution is to
disable core dumping (setrlimit may be the best way, I'm not sure on you
platform) before returning from main.
DS
| |
| Raymond 2005-06-16, 2:49 am |
| Hi David:
Thank you for your help.
David Schwartz wrote:
> "Raymond" <rshen_bea@yahoo.com> wrote in message
> news:42b10c90$0$18644$14726298@news.sunsite.dk...
>
>
>
>
> How was the memory allocated? My bet is it was allocated local to the
> 'main' function. This means your problem has nothing to do with process
> termination and everything to do with not allocating memory in the proper
> scope for its usage.
>
Actually. This memory will be freed by main. But why the IOWorkerThread
always catch SIGIO signal after the main() returns.
When the SIGIO signal raise on AIX platform?
After the main() returns. The IOWorkerThread is still active. Why?
When will the thread will be terminated?
> If the problem really is as you describe, then the solution is to
> disable core dumping (setrlimit may be the best way, I'm not sure on you
> platform) before returning from main.
>
> DS
>
>
| |
| David Schwartz 2005-06-16, 5:52 pm |
|
"Raymond" <rshen_bea@yahoo.com> wrote in message
news:42b11d17$0$18644$14726298@news.sunsite.dk...
[vbcol=seagreen]
[vbcol=seagreen]
> Actually. This memory will be freed by main. But why the IOWorkerThread
> always catch SIGIO signal after the main() returns.
Because the thread is terminated *after* main() returns. But main's
stack becomes invalid when main() returns. There can be a window in-between
when 'main' returns and when the thread terminates.
> When the SIGIO signal raise on AIX platform?
>
> After the main() returns. The IOWorkerThread is still active. Why?
> When will the thread will be terminated?
The thread will be terminated after main() returns, but not necessarily
right after. What does the thread dump core on? What is it trying to access
and how was that allocated?
DS
| |
| Bjorn Reese 2005-06-16, 5:52 pm |
| Raymond wrote:
> When my main() is return. I find the IOWorkerThread catch a signal
> "SIGIO" and do some work. But the memory A is already freed by main().
If I understand your problem correctly, then you could wait for
IOWorkerThread to exit before you exit main. That is, insert a
pthread_join (assuming you use pthreads) before the return statement
in main.
--
mail1dotstofanetdotdk
|
|
|
|