How can I catch a ctrl-c event?

signal isn’t the most reliable way as it differs in implementations. I would recommend using sigaction. Tom’s code would now look like this : #include <signal.h> #include <stdlib.h> #include <stdio.h> #include <unistd.h> void my_handler(int s){ printf(“Caught signal %d\n”,s); exit(1); } int main(int argc,char** argv) { struct sigaction sigIntHandler; sigIntHandler.sa_handler = my_handler; sigemptyset(&sigIntHandler.sa_mask); sigIntHandler.sa_flags = 0; … Read more

Connecting n commands with pipes in a shell?

Nothing complex here, just have in mind that the last command should output to the original process’ file descriptor 1 and the first should read from original process file descriptor 0. You just spawn the processes in order, carrying along the input side of the previous pipe call. So, here’s are the types: #include <unistd.h> … Read more

Is file append atomic in UNIX?

A write that’s under the size of ‘PIPE_BUF’ is supposed to be atomic. That should be at least 512 bytes, though it could easily be larger (linux seems to have it set to 4096). This assume that you’re talking all fully POSIX-compliant components. For instance, this isn’t true on NFS. But assuming you write to … Read more