1

I was trying to connect daemons (group of daemons without a leader) with main process as in title, the problem is that i have to send statement from each daemon(which are supporting SIGUSR1 signal) to main process, but i don' t even know how to do that, in my code i used mkfifo, but it's not working at all..
here is the main process source:

int main(int argc, char* argv[])
{
   pid_t pid;
   int i;
   int n = atoi(argv[1]);
   char c, message[255];
   if(argc!=2){
      printf("please insert one parametr\n");
      return -1;
   }

   int fd = open("pipe", O_RDONLY);
   if (fd == -1) {
      perror("Failed open fifo to read");
      return EXIT_FAILURE;
   }

   for( i = 0; i < n; i++) {        
      pid=fork();
      if (pid==0){
         printf("daemon created..\n");
      }
      else{
         execl("daemons", "daemons", argv[1], NULL);
         while(1){
            sleep(2);
            read(fd, message, c);           
            printf("P received: %s\n", message);
            close(fd);
            //read(fd[0], message, sizeof(message));
         }      
      }
   } 
   return 0;   
}

and here is some source code in which i create daemons:

int fd = open("pipe", O_WRONLY);
if (fd < 0){
    perror("cannot open fifo: ");   
    return EXIT_FAILURE;
}

if ( getppid() == 1 )
    return 0;
/*  Creating daemon     */
pid[n] = fork();
if (pid[n] < 0)     
    exit(EXIT_FAILURE);  

if (pid[n] > 0)    
    exit(EXIT_SUCCESS);  
/*  Setting leader of session */
sid = setsid();  
if (sid < 0){  
    exit(EXIT_FAILURE);  
}  
/*  fork one more time to make children 
to have an opportunity to destroy
session leader  */

for ( i = 0; i < n; i++){
    pid[i] = fork();

    if(pid[i] < 0){
        printf("filed to fork...\n");    
        exit(EXIT_FAILURE);  
    }        
    if(pid[i]==0){

        while(1){                   
            sleep(2);
            printf("Demon[%d]: My ID in pipe.%d\n", i+1, getpid()); 
            signal(SIGUSR1, sigHandler);    
            write(fd, "Hi\n", strlen("Hi\n"));
            close(fd);
        }
    }       

chdir(".");

close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);

file = fopen("log_file.txt", "w+");

fclose(file);

umask(027);
}

at least i' m not sure about that i am creating daemons in good way.. And where i should put signal, which can be later executed?

Do you have any suggestions?

Jorgusss
  • 7
  • 1
  • 3
  • Are you trying to send a signal (`SIGUSR1`) to a process, or to send a stream of data through a pipe? – Some programmer dude Jan 12 '16 at 11:43
  • to send stream of data from daemon to main process – Jorgusss Jan 12 '16 at 11:44
  • By the way, aren't you supposed to call `execl` in the *child* of your "main" process? Right now you do it in the parent (or if `fork` failed), and the `exec` family of functions *never returns*. – Some programmer dude Jan 12 '16 at 11:47
  • Also, if you move the `execl` call to the child, so the loop can run, then you will have *undefined behavior* as you use the uninitialized variable `c` as the number of bytes to read. Uninitialized local non-static variables have an *indeterminate* value. – Some programmer dude Jan 12 '16 at 11:49
  • Lastly about that `SIGUSR1` thing, you set a signal handler to handle `SIGUSR1` (which you only should do once) but you actually never send it to the program. And you *really* need to check for errors, especially when reading from a pipe. Oh, and in the "main" process you will print out an unterminated string, again leading to *undefined behavior*. – Some programmer dude Jan 12 '16 at 11:52
  • Ok, i' ve just done it, but now i have other problem, it' s strange because when i call execl it is not working in child, but when i execute daemon program, the main process achives some messages from daemons – Jorgusss Jan 12 '16 at 11:53
  • i have declared sigHandler above the main function, something like that: void sigHandler(int signum){ if (signum == SIGUSR1){ printf("signal catched!"); }} – Jorgusss Jan 12 '16 at 12:01
  • Yes, but nowhere (not in the code you show) do you actually *send* the `SIGUSR1` signal. You send signals with the [`kill`](http://man7.org/linux/man-pages/man2/kill.2.html) system call. And you haven't really explained why you need it, and what it has to do with the pipe? – Some programmer dude Jan 12 '16 at 12:04
  • oh, so firstly, daemon have to send statement, next statements are send within executing SIGUSR1 signal – Jorgusss Jan 12 '16 at 12:07
  • If you want my opinion, I think you are trying to do to much at one time, there are lots of code you show that doesn't really make any sense. Instead I suggest you take a few steps back, and start over, and start *simple*. Like just having your "main" program fork and execute another program, and where the other program just prints a "hello world" type of string. Then add one little bit at a time, one by one, until the system does what you want it to. That will make it easier to learn, but also easier to debug and find out and fix problems. – Some programmer dude Jan 12 '16 at 12:08
  • so i would start from that, i have main process which is executing second process - daemons - with some number of arguments, next, i create daemons which are in one group and have no leader, but i don' t know how to do something like each daemon send simple statement – Jorgusss Jan 12 '16 at 12:18
  • All problems I can see above -- signaling and FIFO ordering -- could be solved easily switching to an Unix domain socket instead. Then, no signals would be needed, nor would there be any of the other issues FIFOs often exhibit. – Nominal Animal Jan 12 '16 at 12:58

0 Answers0