So I am writing a simple shell. The relevant codes are posted below.
void sig_int_handler(int signum) {
if (pid == -1) // do nothing
else kill(...);
}
signal(SIGINT, sig_int_handler);
..
while(1) {
pid = -1;
printf(COMMAND_PROMPT);
input_len = read(...);
if only enter is pressed: continue;
// parse inputs
pid = fork();
if (pid == 0) { // Do child process operations }
else if (pid > 0) { // Parent waits for child }
else {..}
}
But now when I press ctrl+c, it properly exits the child. However, if I press ctrl+c in regular terminal, it prints out "ctrl+c" on stdout, but then it doesn't do anything. How do I force a linefeed开发者_运维百科 into read so that it gives me another prompt?
I'm assuming you're trying to write something like a shell here.
If so: You shouldn't have to process ^C in your shell. If you set up a new process group for the child process (using setpgid()
and TIOCSPGRP
), only it will receive SIGINT.
精彩评论