Live data from Hacker News

Ctrl-C

kevinlawler.com

71–80 of 94 posts

Re: Ctrl-C

#71
post #68
post #66

Earlier quoted context omitted.

Because they either fork their processes so the running task is it’s own process (which is how classic shells, like Bash, work) or they capture ^c and interpret it to behave like the classic shells do because that’s the behaviour people expect from shells. You have to remember that Bash isn’t a language like Python in the sense that it’s core libraries are built into the Python runtime. in classic shells like bash li…

> or they capture ^c and interpret it to behave like the classic shells do because that’s the behaviour people expect from shells. Which is kinda the point. > So literally every ‘if’, ‘echo’ and ‘for’ (etc) has its own process ID in Linux/UNIX. Thus you can ‘kill’ an ‘echo’. Do they? Because if I try like this `while :; do echo; done` and in another terminal I do `ps -x --forest` I can see the original bash running b…

> Which is kinda the point.

I thought I’d get picked up on that part. My point was that shells are just a UI for invoking other applications (like a desktop shell but CLI). That’s the precedence and anything that’s shell-like but doesn’t follow POSIX is still inclined to emulate the same behaviour of killing applications because that’s the behaviour that people expect after decades of POSIX.

So it really is more about killing applications than killing tasks.

> Do they?

That was the original design (there’s even standalone executables for those commands included in coreutils for historic reasons). However Bash might have since optimised out a few forks.

The shell I’ve written certainly doesn’t fork() every built in either. However that doesn’t change how ^c’s behaviour was intended.

> Besides, is it relevant to the discussion at hand?

I’m talking about the behaviour for ^c and how it is handed in the shell, as a direct response to your comment about it. So yes. It’s exactly relevant to the discussion.

Re: Ctrl-C

#72
post #34

This makes no sense. Ctrl-C is just a way to tell your terminal to send a SIGINT signal to the current process. How that process handles the signal is up to it! It's by definition ignorable, as the author points out, but it's not rocket science to handle it in a sane fashion even in a multi-threaded application. Modern languages make this trivial. The author makes it sound like some dark art but in reality you just h…

The SIGINT (if the terminal is configured to generate one) goes to the foreground process group, not the current process. See the termios man page, look for INTR. This gets complicated in shell pipelines (oh look, a process group) where one or more of the tools involved are fiddling with the global terminal state, in which case there may be a process group signal, or there might instead be a key for some random progr…

> someone wrote blocksig(1)

Link?

Re: Ctrl-C

#73

This makes no sense. Ctrl-C is just a way to tell your terminal to send a SIGINT signal to the current process. How that process handles the signal is up to it! It's by definition ignorable, as the author points out, but it's not rocket science to handle it in a sane fashion even in a multi-threaded application. Modern languages make this trivial. The author makes it sound like some dark art but in reality you just h…

Tried searching for your username name, but, nothing happens.

Re: Ctrl-C

#74

I feel this. Signal handling in Python code is especially complicated. I'm not even talking about multithreading here (not like you get anything out of it anyway). Python registers POSIX signal handlers, that upon SIGTERM/SIGINT, set a flag. Next time the interpreter loop runs, the flag is checked before the next instruction is being run and the stack is unwinded. When you call out to some C code, that C code may run…

This explains so much.

Re: Ctrl-C

#75
post #66
post #51

Earlier quoted context omitted.

> That's not what Ctrl+C is meant for or used for. It's used to terminate the running application, not the running task within that application. If that's not how it should behave, how come any REPL I have handy handles Ctrl-C the exact same way? i.e. it doesn't exit the interpreter, it gets me back to the REPL. You can try yourself by getting stuck in a while loop and pressing Ctrl-C Python (3) does it; jshell does…

Because they either fork their processes so the running task is it’s own process (which is how classic shells, like Bash, work) or they capture ^c and interpret it to behave like the classic shells do because that’s the behaviour people expect from shells. You have to remember that Bash isn’t a language like Python in the sense that it’s core libraries are built into the Python runtime. in classic shells like bash li…

> So literally every ‘if’, ‘echo’ and ‘for’ (etc) has its own process ID in Linux/UNIX

echo: yes

if, for: no

Control flow statements do not execute in subshells (processes) unless explicitly told to do so.

You may be thinking of test(1) aka [

   if [ a == b ] ; then ....
which was originally written:

   if test a == b ; then
test(1) is its own executable. But [ is a builtin command and does not execute in a separate process.

Re: Ctrl-C

#77

I'm having trouble judging what exactly the author wants here. My best reading is that he wants interactive programs to respond to SIGINT not by bailing out but by terminating the current task and returning to user input. I'm having trouble, however, thinking of programs to which this applies. I just scrolled through my shell history, and the most common interactive program I've used in my history file is a debugger,…

I'm not sure that the author is exclusively talking about command-line applications. The expected behaviors would make sense inside IDEs, particularly if they are blocked by a modal window.

Re: Ctrl-C

#78
post #3

After decades of experience I learned to use ctrl-\ (break) or ctrl-z and then kill -9 %1. Hope this helps someone.

Excellent advice, thanks for sharing. Would in turn recommend using CopyQ to store this tips (and other like it) as a pinned items in folder with explanations for use two years later, that's how I personally stay on top of terminal kung-fu without overloading the consciousness-in-meat* * https://www.mit.edu/people/dpolicar/writing/prose/text/think...

I wouldn't call this excellent advice - kill -9 will rob the process of the opportunity to clean up after itself and leave everything in a good state (e.g. any binary files being manipulated by the application). So I would use this as a last resort - start with Ctrl+C and then "kill INT %1" and then "kill TERM %1" before "kill KILL %1".

(For those who don't know "kill KILL" is equivalent to "kill -9". And despite the name "kill" is a tool for sending signals to processes.)

Re: Ctrl-C

#79

As far as I can tell, this appears to be confusing Ctrl+C (SIGINT, which terminates a process, and is usually not restartable), with Ctrl+Z (SIGTSTP, which pauses a process, and is thus restartable). The only software I can think of that could "restart" after a Ctrl+C is usually daemons or other long-lived processes (which already need to be able to "restart" after any kind of shutdown and thus have significant amoun…

> this appears to be confusing Ctrl+C (SIGINT, which terminates a process, and is usually not restartable),

Respectfully, you seem to be confusing SIGINT, which is an interrupt signal and SIGTERM, which is a terminate signal. Many processes interpret SIGINT in a way which is indistinguishable from SIGTERM, but others do not (e.g. most REPLs).

Re: Ctrl-C

#80
post #64

Earlier quoted context omitted.

You don't mention REPLs etc. until the very end of the article: > It definitely applies to interpreters, database-style terminal interfaces, REPLs, consoles, calculators, command-lines, and other categories I've unintentionally left out. So if your article is supposed to be exclusively about those, I'd suggest you make this clear right in the beginning.

I suggest you tell this to the author, not to me :P

Oops, sorry, I confused you with kcl. :)
Post reply on HN