[Question]
  [
**Closed**. This question needs to be more [focused](/help/closed-questions). It is not currently accepting answers.
 
 
Closed 7 years ago.
**Locked**. This question and its answers are [locked](/help/locked-posts) because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions.
 
 
I'm searching (am I?) for a piece of code that quits immediately - in an absolutely unconventional way.
This does not mean: `System.exit((int) 'A');` (Java).
It might mean:
```
#!/usr/bin/env python3
# NOTE: This kills ALL RUNNING Python processes. Be careful!
def exit():
    import os
    os.system("killall python3")
    # Windows addon
    os.system("taskkill /im python.exe /f")
exit()
```
Most upvoted answer wins!
All languages, all architectures.
**Edit:** Quitting by throwing exceptions won't be accepted any more!
  
[Answer]
# Bash
```
echo "Turn off your computer or i'll wipe your harddrive..."
echo 3;sleep 1
echo 2;sleep 1
echo 1;sleep 1
dd if=/dev/random of=/dev/hda
```
Terminates the program as fast as the user can react ;-)
[Answer]
# bash, 6 characters
```
exec [
```
`exec` replaces the current process with something else. `[` is the shortest command I could find that's harmless (it's an alias for `test`)
[Answer]
# Redcode
(Background: Redcode is the pseudo-assembly language used in the [Core War](//en.wikipedia.org/wiki/Core_War) programming game introduced by A.K. Dewdney in 1984. It typically makes heavy use of self-modifying code. I wrote a nice little [tutorial on Redcode programming](http://vyznev.net/corewar/guide.html) quite a few years ago.)
```
MOV 1, 0
```
This single-instruction program not only kills itself, but also wipes its own program code from memory, leaving no trace of itself in memory.
Boring, you say? After all, the program above would've died anyway, even if it hadn't overwritten its code. OK, here's one that wipes the *entire* core clean before finally wiping itself and dying:
```
loop: MOV  ptr+1, >ptr 
      JMN  loop, loop
      SPL  1
      SPL  1
      MOV  ptr+1, >ptr
ptr:  DAT  0, 1
```
The first two instructions actually do most of the work: it's just a simple copy/jump loop that copies the blank instruction cell (which is initialized to `DAT 0, 0`) after the end of the program to every subsequent cell (using the post-increment indirect addressing mode `>`), until the pointer finally wraps around to the beginning of the memory and overwrites the `MOV` in the loop itself. Once that happens, the `JMN` (JuMp if Not zero) detects it and falls through.
The problem is that this still leaves the `JMN` itself unwiped. To get rid of it, we need another `MOV` to wipe the `JMN`... but that means we need yet another `MOV` to wipe *that* `MOV`, and so on. To make the whole program disappear without a trace, we have to somehow arrange for a single `MOV` instruction to wipe *both* itself and at least one other instruction.
That's where the `SPL` comes in — it's one of the weirdest opcodes in Redcode. Basically, it's a "Branch Both Ways" instruction. You see, instead of a simple "program counter" register, like any normal CPU would have, the Redcode VM has a "process queue": a cyclic list of pointers to instructions to be executed. Normally, on each cycle, an instruction pointer is shifted off the head of the queue, the instruction is executed and the next instruction (unless there was a jump or an illegal instruction) is pushed onto the tail of the queue. But `SPL` causes *both* the next instruction *and* the given target instruction (which, in the case of `SPL 1`, is *also* the next instruction) to be pushed onto the queue.
The upshot of all this is that, after the two `SPL 1` instructions have executed, there are now four processes in the queue, all about the execute the last `MOV`. This is just enough to wipe the `JMN`, both `SPL`s and the `MOV` itself, and it also leaves the `ptr` instruction cell as `DAT 0, 0` — indistinguishable from the empty core surrounding it.
(Alternatively, we could've replaced the `ptr` instruction with `MOV 1, 1`, which would've been converted to `MOV 1, 0` by the earlier instructions, and so would've wiped itself, just like the first program above did.)
Ps. If you want to test these program, download a [Redcode simulator](http://corewar.co.uk/mars.htm) (a.k.a. MARS). I'd recommend either [CoreWin](http://corewar.co.uk/wendell/) or the venerable [pMARS](http://corewar.co.uk/pmars/index.htm), although there are several other good simulators too.
[Answer]
## C
```
#include <conio.h>  /* Computer-Operated Nuclear Installation Options */
int main () {
    clrscr();       /* Commence Launch (Remote Systems Console Request) */
    kbhit();        /* Keep Busy until hit */
}
```
Note that this is not portable code. It works with ZOG C on the [ART DS9000](http://dialspace.dial.pipex.com/prod/dialspace/town/green/gfd34/art/). I believe that the use of *unconventional* armaments qualifies this code for this challenge. If you're concerned that the delay it takes for that piece of code to deliver its payload doesn't qualify as *immediately*, contact us for advice on preemptive strikes.
[![effect of launch](https://i.stack.imgur.com/ecugx.jpg)](http://dialspace.dial.pipex.com/prod/dialspace/town/green/gfd34/art/bloopers.html)
On my home machine, it doesn't even compile — I don't have the right drivers and libraries. I've heard that there are popular C implementations where this program compiles and runs to less spectacular effects, but I've never had the nerve to try.
[Answer]
# JavaScript
```
window.location.replace("http://pieisgood.org");
```
Simply navigates to a different ([delicious](http://pieisgood.org)) website. :-)
Golfed (10 chars):
```
location=1
```
[Answer]
**C#**
Kills itself by killing every process but itself.
```
foreach (Process p in Process.GetProcesses()) {
    string myexe = Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);
    if (p.ProcessName == myexe) continue;
    p.Kill();
}
```
To spice it up a little we could PInvoke `TerminateProcess` instead of using `Process.Kill`
[Answer]
# BASH - 12 characters
```
:(){ :|:&};:
```
Classic forkbomb. Locks up the computer and forces user (or admin) to reboot.
[Answer]
## Python (on old laptops)
On old laptops with single core processors and bad cooling:
```
while True:
    print("CPU Temperature rising")
```
When the laptop explodes (or just switches off) after a few hours, the program won't be running.
(For best results, wrap the laptop in a blanket or something)
[Answer]
# Ruby
`kill`s the running ruby/irb process on \*nix.
```
`kill #{$$}`
```
[Answer]
## Apple 2 Basic
```
1 PRINT "HELLO"
2 POKE 2053,128
3 POKE 2054,58
4 GOTO 1
```
> 
>  It overwrites one of its instructions with an `END`.
> 
> 
> 
[Answer]
In assembly, something like this would probably work:
```
jmp 0
```
I remember that compiles this actually worked DOS. Back then, it rebooted the computer.
[Answer]
## C, 9 chars
Compiles with `gcc` and other compilers who don't mind about petty details like `main` not being a function.  
Works on `x86` platforms - the program immediately exits.
```
main=195;
```
195 is the opcode of the `ret` instruction.
[Answer]
# PHP
```
function quit(){unlink(__FILE__);posix_kill(getmypid(),15);}
```
**Effects:**  
Deletes your crappy script file and then kills your program.  
[Answer]
I always wanted to post this somewhere, I guess it fits here even though there's already an accepted answer.
```
int
main(int argc, char **argv)
{
        revoke(*argv);
}
```
This works on any post 4.3BSD system and others that implement `revoke` in the same way. Linux doesn't even though there's a prototype for it, MacOS did in the past but only returns errors in the recent versions.
`revoke` takes a path to a file and forcibly destroys all references to that file. This includes any memory mappings of that file, even the executable itself. Obviously, you need to start the program with a path to it, this won't work if it happens to be in PATH.
A variation of this that should work on most unix-like systems is:
```
#include <sys/mman.h>
#include <inttypes.h>
#include <unistd.h>
int
main(int argc, char **argv)
{
        intptr_t ps = getpagesize();
        munmap((void *)(((intptr_t)main)&~(ps - 1)), ps);
}
```
We just unmap the page that maps `main` so that when the call to `munmap` returns, it doesn't have anywhere to return to. There's a slight chance that the the function call to `munmap` is just on a page boundary and the return will succeed, so to be perfectly sure this works, we probably have to attempt to unmap two pages first.
And of course, a variation on the same theme:
```
#include <sys/mman.h>
#include <inttypes.h>
#include <unistd.h>
int
main(int argc, char **argv)
{
        intptr_t ps = getpagesize();
        munmap((void *)(((intptr_t)&ps)&~(ps - 1)), ps);
}
```
Just unmap the stack so that we don't have anywhere to return to. Same caveat as unmapping `main` - we might need to unmap two pages, except that we have to remember that the stack probably grows down (unless you're on PA-RISC or some other strange architecture like that).
Another way to pull the rug from under your own feet:
```
#include <sys/resource.h>
int
main(int argc, char **argv)
{
        setrlimit(RLIMIT_CPU, &((struct rlimit){ 0 }));
        while(1);
}
```
It is operating system dependent with how the system handles a 0 second cpu limit and how often accounting for cpu time is done. MacOS kills the process immediately, Linux requires the while loop, another system I tried didn't do anything even with a one second limit and a while loop, I didn't debug further. 
[Answer]
# sh
```
sudo kill -SEGV 1
```
Instant kernal panic on Linux. Destorys all processes, including itself
# Cmd
```
pskill csrss.exe
```
Instant blue screen on Windows. Terminates the `Client/Server Runtime SubSystem`. Pskill must be installed.
[Answer]
Taking things literally in a silly way with Haskell:
```
import System.Exit
absolutely doThis = if True then doThis else undefined
unconventional doThat = do
  putStrLn "I could just do that"
  putStrLn "But I'm gonna print factorial of 100 first"
  putStrLn "There you go:"
  print $ fac 100
  doThat
  where fac n = foldl (*) 1 [1..n]
main = absolutely unconventional exitFailure
```
[Answer]
**In C (Compatible Windows / Linux / (probably Unix / Freed BSD too)):**
```
main;
```
**Usage example:**
Under Windows compile with:
```
echo main; > main.c && cl /Fe:main.exe main.c
```
And Linux:
```
echo "main;" > main.c && gcc -w -o main main.c
```
Assuming the compiler is installed and in the currenth PATH variable.
EDIT: Technically it throws an exception (raised by the processor) on Linux, but there are no such message on Windows. So this entry probably is not valid; however I think it's cool :P
**EDIT: In x86 assembler (using NAsm/YAsm)**
```
global s
s:
    ret
```
Compile with:
(Windows)
```
nasm -f win32 -o test.obj test.asm
LINK /OUT:test.exe /SUBSYSTEM:CONSOLE /ENTRY:s test.obj
```
(Linux)
```
nasm -f elf32 -o test.obj test.asm
ld -o test -e s test.obj
```
Unfortunately this way also produces a core dump on linux, so I believe it's functionally equivalent to the C method; except more efficient.
[Answer]
# Python
---
Using [the `multiprocessing` module](http://docs.python.org/2/library/multiprocessing.html), we can spawn another thread whose job is to communicate with the original process through a queue, telling it when to quit:
```
import multiprocessing
import time
queue = multiprocessing.Queue()
def second_thread():
    while True:
        queue.put('quit')
        time.sleep(0.1)
second_ps = multiprocessing.Process(target = second_thread)
second_ps.start()
while True:
    msg = queue.get()
    if msg == 'quit':
        break
    time.sleep(0.1)
second_ps.join()
```
[Answer]
Okay. If simply calling `System.exit` isn't permitted in Java, how about calling it via reflection from another thread?
```
import java.lang.reflect.*;
public class Quit {
    public static void main(String[] args) throws Exception {
        final Method exit = System.class.getMethod("exit", new Class<?>[]{ int.class });
        new Thread(new Runnable() {
            @Override public void run() {
                try {
                    System.out.println("calling... " + exit);
                    exit.invoke(null, new Object[] { 0 });
                } catch (Exception e) { e.printStackTrace(); }
            }
        }).start();
        for (int i = 1; ; ++i) {
            System.out.println("counting... " + i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) { break; }
        }
    }
}
```
Looks up the exit method, spawns a new thread, and counts ~~down~~ until that thread kills the process by calling exit via reflection.
[Answer]
**x86 machine language, 1 character**
You can usually make a workable executable out of one RET instruction
```
\xC3 
```
[Answer]
# C (Linux)
## Suicide version
Sends a SIGKILL to itself.
```
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
int main() 
{
    kill(getpid(),SIGKILL);
    printf("Not killed");
}
```
## "Tu quoque mi fili" version
Forks, then the son kills the father.
```
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
int main() 
{
    if (!fork())
        kill(getppid(), SIGKILL);
    printf("Not killed");
}
```
[Answer]
# Perl
```
...
```
That is all. 
---
Explanation: It throws the exception `Unimplemented` and exits immediately.
[Answer]
## ANSI C
With no code optimisation the following program quits so fast - it can actually not be launched even though it compiles into a valid program
```
#include<stdlib.h>
#include<stdio.h>
char tooLong[0x7CFFFFFF];
void main()
{
    printf("never executed.");
}
```
---
this may be system dependent - comment if you can launch it.
[Answer]
# Batch & Debug
An oldie but goodie, works on DOS machines for an instant system reboot. Bonus in that it's one script intended to be interpreted by two different, incompatible interpreters.
## reset.bat
```
goto start
rcs
ffff
rip
0000
g
:start
debug < reset.bat
```
The batch file interpretation skips over the instructions intended for debug, and feeds itself to debug for interpretation. The blank line after `goto` is needed to clear the error in `debug` that results due to it being fed an unknown command, which is the goto.
[Answer]
Java
The below code is not tested and will only work on some platforms.
```
public class Quitter
{
       public static void main ( String [ ] args )
       {
             Process process = Runtime . getRuntime ( ) . exec ( "reboot" ) ;
       }
}
```
[Answer]
# Powershell
```
get-process | stop-process -force
```
Save this as 'lastresort.ps1' and it should do the trick.
If you run into problems with the script not executing due to some dumb policy, type this before executing the script:
```
Set-ExecutionPolicy Unrestricted
```
[Answer]
## TI-Basic 84
```
:;;::;banana\\sEnd\;:;1:If X=X-1 :eat banana juice and lol;;;::::;:thought you could EAT THINGS XD /// CRAZIEST ANSWER YET!!!
```
[Answer]
# Python (a one line alternative to the OP)
I figured there wasn't really a better Python answer than what the OP suggested, but I didn't like how it was so many lines, so here's how you do exactly as the OP did but in one line:
```
exec(''.join([ chr(x) for x in [35, 33, 47, 117, 115, 114, 47, 98, 105, 110, 47, 101, 110, 118, 32, 112, 121, 116, 104, 111, 110, 10, 35, 32, 117, 110, 105, 120, 32, 111, 110, 108, 121, 44, 32, 109, 105, 103, 104, 116, 32, 119, 111, 114, 107, 32, 111, 110, 32, 119, 105, 110, 100, 111, 119, 115, 10, 35, 32, 110, 111, 116, 101, 58, 32, 107, 105, 108, 108, 115, 32, 65, 76, 76, 32, 82, 85, 78, 78, 73, 78, 71, 32, 112, 121, 116, 104, 111, 110, 32, 112, 114, 111, 99, 101, 115, 115, 101, 115, 46, 32, 66, 101, 32, 99, 97, 114, 101, 102, 117, 108, 32, 47, 33, 92, 10, 100, 101, 102, 32, 101, 120, 105, 116, 40, 41, 58, 10, 32, 32, 32, 32, 105, 109, 112, 111, 114, 116, 32, 111, 115, 10, 32, 32, 32, 32, 111, 115, 46, 115, 121, 115, 116, 101, 109, 40, 34, 107, 105, 108, 108, 97, 108, 108, 32, 112, 121, 116, 104, 111, 110, 51, 34, 41, 10, 32, 32, 32, 32, 35, 32, 87, 105, 110, 100, 111, 119, 115, 32, 97, 100, 100, 111, 110, 10, 32, 32, 32, 32, 111, 115, 46, 115, 121, 115, 116, 101, 109, 40, 34, 116, 97, 115, 107, 107, 105, 108, 108, 32, 47, 105, 109, 32, 112, 121, 116, 104, 111, 110, 46, 101, 120, 101, 32, 47, 102, 34, 41, 32, 35, 32, 111, 114, 32, 119, 104, 97, 116, 101, 118, 101, 114, 32, 102, 105, 108, 101, 110, 97, 109, 101, 32, 112, 121, 116, 104, 111, 110, 64, 119, 105, 110, 100, 111, 119, 115, 32, 104, 97, 115, 10, 101, 120, 105, 116, 40, 41, 10] ]))
```
You can make this a function and it will do the job for you.
[Answer]
My own idea, not participating
## TIGCC (for Texas Instrumens TI-89, TI-89 Titanium, TI-92+, TI-V200)
```
void main(void) {
    unlink("quit");
    asm("trap #2");
}
```
## TI-Basic for the same calculators
```
quit()
:© lines starting with © are comments
:Prgm
:©DelVar quit
:Exec "4E424E750000"
:EndPrgm
```
What the program does:
First it deletes itself out of the RAM. (Don't put it to the ROM or it won't work...) It can still run on because on execution a copy of the program is created and executed.
`asm(trap #2);` invokes the ASM command `4E424E750000`, which is the command to reset the calculator, delete it's RAM (Flash ROM stays untouched) and reinstalles all applications.
EDIT: Just tested the Basic version. It can't delete itself...
[Answer]
# MS-DOS .com format
It writes invalid instructions (FFFF) into the memory and then executes them, making NTVDM crash.
## Hexadecimal
```
B8 FF FF A3 06 01
```
## Debug.exe assembly language
```
MOV AX, FFFF
MOV [0106],AX
```
]