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");
}