In my C console application, I would like to exit the application and on the same time fire an exit command to close a terminal. However, following code seems not work. Once I run the application, it exit the application, but not close the terminal.
int main(void)
{
system("exit");
return 0;
}
Please give any开发者_开发问答 advise.
Your "exit" command is not being sent to the shell that launched your program, but rather a new shell, executed expressly for the purpose of executing your command. It doesn't accomplish anything.
There's no way for your program to cleanly exit the shell that launched it without some cooperation from that shell. For example, assuming we're running with bash or Bourne shell on UNIX, if in the terminal, you start your program using
exec theprogname
then the launching shell is replaced by your program, and so when your program exits, the shell exits.
The system
command creates a new shell process and issues the command to that shell, so when you issue exit
it will close only the new shell.
The way to do what you ask for is this:
#include <unistd.h>
#include <signal.h>
int main(int argc, char *argv[])
{
kill(getppid(), SIGKILL);
return 0;
}
However, this is A BAD IDEA (for many reasons: you are blindly killing some process that spawned yours with no real clue what it actually is... it could be the user's login shell, or it could be init
). You probably just want to close the terminal window, right? Then launch your program like this:
xterm -e my_program
This will run your program in its own window, which closes when the program finishes. No trickery, and it works with any program.
Maybe it will work if You use signal on terminating the program. a. k. a.
#include <signal.h>
#include <stdio.h>
void terminate(int param)
{
system("exit");
}
int main()
{
void (*sig)(int);
sig = signal(SIGTERM, terminate);
return 0;
}
If you just want to compile, run, and then close the window, you could create a make file, then all you need to do is type make at command prompt. To do this with your make file, you can do something like this:
EXEC = program
NAME = prog
RUN = gcc
EXIT = exit
$(EXEC):
$(RUN) -o $(EXEC) $(NAME)
$(EXEC)
$(EXIT)
I did not test this, but this is basically what you would need. Here is another example of a make file: http://www-rohan.sdsu.edu/faculty/gleonard/html/Makefile
That one will remove the file that is created so you don't get the "file is up to date" error. Hope this helps
Simple - just run it on windows. The terminal ins windows will close automatically whenever your program exits. (Maybe there's a way to change this, but I'm not aware of any).
But I take it you're looking for something peculiar to C. Sorry, but you're out of luck. Everything's going to OS specific. As an example, imagine yo're working on an old DOS system or something, where all that exists is the console. You can't really close the terminal, aside from a shutdown, 'cause that's all that's there. This example should at least illustrate how a language like C cannot guarantee this functionality.
Well, 9Ake, you need to use void main() instead of int main(void) and you need to call the getch(); function at end of procedures inside main() and include the header 'conio.h' at the very top of your program.
精彩评论