C++ Program to delete itself after execution ?

M

Mike

Greetings,
I hear this question is one famous interview question . Just
wondering what could be the ways to do this ? Does storing process
info in a text file while running and invoking a small background
script on atexit() a good solution ? if the script keeps pinging for
the pid & binary name and delete it if not found?

Cheers
 
V

Victor Bazarov

Mike said:
Greetings,
I hear this question is one famous interview question . Just
wondering what could be the ways to do this ? Does storing process
info in a text file while running and invoking a small background
script on atexit() a good solution ? if the script keeps pinging for
the pid & binary name and delete it if not found?

This has nothing to do with the subject of comp.lang.c++. Please
post your question to a newsgroup that deals with your OS.
 
B

bartek

(e-mail address removed) (Mike) wrote in
I hear this question is one famous interview question . Just
wondering what could be the ways to do this ? Does storing process
info in a text file while running and invoking a small background
script on atexit() a good solution ? if the script keeps pinging for
the pid & binary name and delete it if not found?

I've heard it's the famous "how do I write a virus" question. ;)

This question is actually quite platform specific, by the way. Please ask
in a forum appropriate for your platform.
For MS Windows, see For unix-related, see
Cheers.
 
T

Thomas Matthews

Mike said:
Greetings,
I hear this question is one famous interview question . Just
wondering what could be the ways to do this ? Does storing process
info in a text file while running and invoking a small background
script on atexit() a good solution ? if the script keeps pinging for
the pid & binary name and delete it if not found?

Cheers

#include <cstdlib>
#include <cstdio>

int main(int argc, char * * argv)
{
remove(argv[0]);
return EXIT_SUCCESS;
}

Provided that the program was launched from a file.


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
R

Richard Herring

Thomas said:
Mike said:
Greetings, I hear this question is one famous interview question
. Just
wondering what could be the ways to do this ? Does storing process
info in a text file while running and invoking a small background
script on atexit() a good solution ? if the script keeps pinging for
the pid & binary name and delete it if not found?
Cheers

#include <cstdlib>
#include <cstdio>

int main(int argc, char * * argv)
{
remove(argv[0]);
return EXIT_SUCCESS;
}

Provided that the program was launched from a file.
.... under a cooperative OS.
 
K

Kelsey Bjarnason

[snips]
#include <cstdlib>
#include <cstdio>

int main(int argc, char * * argv)
{
remove(argv[0]);
return EXIT_SUCCESS;
}

Provided that the program was launched from a file.

And on a platform/implementation that fills argv[0] with useful
information. And on a platform/implementation that allows you to delete
executables. And on a platform/implementation that doesn't lock
executables while they're running. And on a platform/implementation that
doesn't suport symbolic links, or passes the link's target, rather than
the link itself, in usable format, to argv[0]. And on a
platform/implementation for which you have delete/write/whatever
permissions to the file. And...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top