-
Signal Handling
This is a program which is exec by a execvp function, its purpose is to read anything that is in the "pipe" which is created in the primary program. Before the exec, I have "written" something that goes into the pipe. So it will write the information of the pipe into the outgoing file.
The signal handling code is suppose to catch a signal that is sent out by another 3rd program which is exec by the primary program. The 3rd program is reading a source file and writting it into the pipe.
So this code is suppose to read the pipe which contains new information and into the outgoing file again.
Question, does the pause() really work? Can one background process be put to sleep while waiting for the signal from the 3rd program? When I run the program, it seems to stop at the while loop and not write into the outgoing file.
Code:
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <signal.h>
#define BUF_SIZE 255
#define OUTPUT_MODE 0700
typedef void Sigfunc(int);
Sigfunc *Signal(int signum, Sigfunc *signalHandler){
struct sigaction newAction, oldAction;
newAction.sa_handler = signalHandler;
sigemptyset(&newAction.sa_mask);
newAction.sa_flags = 0;
if (sigaction(signum, &newAction, &oldAction) < 0){
return(SIG_ERR);
}
else{
return(oldAction.sa_handler);
}
}
static void mySIGUSR1Handler(int);
int main(int argc, char *argv[]){
int out_fd;
struct stat buf;
char c;
char buffer[BUF_SIZE+1];
int counter;
fprintf(stderr, "What from writer : %s\n", argv[0]);
if(argc != 1){
perror("Insufficient files to write from main\n");
}
if(Signal(SIGUSR1, SIG_IGN) != SIG_IGN){
if(Signal(SIGUSR1, mySIGUSR1Handler) != SIG_IGN){
perror("ERROR! Signal handler not established!\n");
exit(EXIT_FAILURE);
}
}
out_fd = creat(argv[0], OUTPUT_MODE);
if(out_fd < 0){
perror("Unable to create output file\n");
}
else{
fprintf(stderr, "Opened my outgoing file\n");
}
if(lstat(argv[0], &buf) < 0){
fprintf(stderr,"lstat error");
exit(EXIT_FAILURE);
}
counter = 0;
while((c=getchar())){
fprintf(stderr,"%c",c);
buffer[counter] = c;
counter++;
}
/**Program stops here***/
write(out_fd, buffer, counter);
pause();
return EXIT_SUCCESS;
}
static void mySIGUSR1Handler(int signum){
Signal(SIGUSR1, mySIGUSR1Handler);
fprintf(stderr, "Received to write from SIGUSR1\n");
}
-
My handler seems to be doing something else. I'll correct it now.
-
Code:
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <signal.h>
#define BUF_SIZE 255
#define OUTPUT_MODE 0700
static void mySIGUSR1Handler(int);
int main(int argc, char *argv[]){
int out_fd;
struct stat buf;
char c;
char buffer[BUF_SIZE+1];
int counter;
if(argc != 1){
perror("Insufficient files to write from main\n");
}
signal(SIGUSR1, mySIGUSR1Handler);
out_fd = creat(argv[0], OUTPUT_MODE);
if(out_fd < 0){
perror("Unable to create output file\n");
exit(EXIT_FAILURE);
}
else{
fprintf(stderr, "Opened my outgoing file\n");
}
if(lstat(argv[0], &buf) < 0){
fprintf(stderr,"lstat error");
exit(EXIT_FAILURE);
}
pause();
counter = 0;
while((c=getchar())){
fprintf(stderr,"%c",c);
buffer[counter] = c;
counter++;
}
/**Program stops here***/
write(out_fd, buffer, counter);
return EXIT_SUCCESS;
}
static void mySIGUSR1Handler(int signum){
fprintf(stderr, "Received to write from SIGUSR1\n");
return;
}
it seems that the program wants a keyboard input to terminate, but it still doesn't write anything to the outgoing file. I've changed my signal handler to just wake up the sleeping process.
Is there any explanation of my problem?
*EDIT
The problem should be because of the pipe I created in the primary program and I should close it after reading the input through. However, since this program doesn't recognize the pipe but only through "STDOUT_FILENO", how do I make it stop reading?
-
> while((c=getchar()))
What's this supposed to do?
If getchar() returns EOF, that's still true as far as the while loop is concerned.
Also, c should be an int.
while ( count < BUF_SIZE && (c=getchar()) != EOF )
-
True, my mistake, I don't know what made me write that in the first place. Prolly while I was implementing one feature at a time till I assumed that its working. I've fixed it and it seems that I'm able to solve my problem along with other changes in the codes as well.
Thanks alot for your attention and your great help!