ANDY wrote:
> I am a little confused about process creation and termination.
> I think I'll just provide example code to stress my question.
>
> ------------------------------------------
> int main(){
>
> int child_pid;
>
> if( (child_pid = fork()) == -1 )
> perror("cannot fork\n");
>
> else if(child_pid){ /* parent */
> if(wait(NULL) == -1)
> perror("can't wait\n");
> printf("parent\n");
> }
>
> else{ /* child */
>
> printf("child process\n");
> /* exit(0); */
> }
>
> }
> ----------------------------------------------------
> I guess my questions were:
> 1. why doesn't " the exit(0) statement ( that is commented ) "
> make any difference ?
Without it, the child will return from main(), which will result in
exit being called with the value which main() returns (note that you
aren't returning a value from main(); the compiler should warn you
about this).
Also, one of the processes should use _exit() to terminate, as exit()
flushes stdio buffers. If both processes use exit() (either explicitly
or by returning from main()), both processes will flush the stdio
buffers. If there was anything in any of the buffers when fork() was
called, it will be written twice.
> If I understand it right. A process will terminate by the call
> to exit(status) system call. And status will be
> passed to the parent via the wait() syscall with an argument that is not
> NULL.
Yes.
> which leads to my other question
>
> 2.can a newly created process( in my program is the child )
> be terminated if there is no more instruction to execute which in my case
> the else block statement only has one printf statement ?
> eg: else{ printf("foo\n"); }
All processes implicitly call exit() when returning from main().
> 3.Does it also imply that a newly created process can be terminated
> WITHOUT calling exit() or catching some other "terminating" signals
> from the kernel ?
Yes.
--
Glynn Clements <[EMAIL PROTECTED]>