UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2006-12-13 05:06 PM

eusta
Member
Registered: 2006-12-13
Posts: 33

Re: Signal begginer

Hello guys

Can anyone explain to me why the kill(my_pid,SIGINT) terminates the programm without executing the line sleep(15) but executing only the printf("Finish\n") ?


thank you


#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>	
#include <stdlib.h>

void catch_int(int sig_num) {
signal(SIGINT, catch_int);
printf("interrupt \n");
}
 
 int main()
 { 
     
     signal(SIGINT, catch_int);
     int pid;
     pid_t my_pid = getpid();
     
     printf("HELLO \n");
     
     pid=fork();
     if(pid==0)
     {
               printf("CHILD \n");
               kill(my_pid,SIGINT);
               exit(1);
               }
     
     sleep(15);
     printf("Finish\n");        
     return 0;
}

Offline

#2 2006-12-13 05:12 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Signal begginer

Offline

#3 2006-12-13 05:15 PM

eusta
Member
Registered: 2006-12-13
Posts: 33

Re: Signal begginer

Offline

#4 2006-12-13 05:35 PM

eusta
Member
Registered: 2006-12-13
Posts: 33

Re: Signal begginer

Hello again

Can you understand why this code creates an error in accepting after the first connection? I think i have mistake in the kill() line


THANK YOU!

while(1)
   {  
           ns = accept(sd, &sockaddr, &adrlen);
            if ( ns == -1 ) 
              {
                    printf("Error in accepting ... \n");
                     exit(0);
                     }
    
            pid_t parent_pid = getpid();

            //code do something

            signal(SIGINT, catch_int);

            //code do something
 
            if(fork()==0)
            { 
                                 
                          printf("wait...\n");
                          read(ns, &free,sizeof(int));
                         
                          kill(parent_pid,SIGINT);  
                          close(ns);
                          exit(1);
             }        
   }

Offline

#5 2006-12-13 08:26 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: Signal begginer

Um, because the accept() is the call being interrupted by the signal, this time...
If you had printed out strerror(errno) or called perror(), you would've know that,
because it would've said "Interrupted system call"...

Basically, nearly any syscall that blocks is going to be interruptable by any signal
you are hit with, in general...  (Unless you setup things to restart interrupted syscalls
automatically, or your system does it for you by default...  See sigaction() and the
SA_RESTART flag...)

On a side-note: you're not closing "ns" in the parent process, so you're leaking a
file descriptor...  Also, you know, the kill(SIGINT) thing is kind of pointless if the
child is just going to be exiting right after, since just the act of the child terminating
will generate a SIGCHLD in the parent, so you could simply handle that signal
instead...

Offline

#6 2006-12-13 08:55 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Signal begginer

Again, same as for sleep, accept is probably interrupted by that signal and returned -1 with errno set to EINTR. Most systemcalls can be interrupted by signals.

Offline

#7 2006-12-13 11:21 PM

eusta
Member
Registered: 2006-12-13
Posts: 33

Re: Signal begginer

Thank you very much!
I ask for your compassion cause i am newbie, sigaction is like a signal-filter? Sigaction decide which is the next signal to handle?
On the previous example i 've changed the SIGINT to SIGCHLD and it works ok.
Why i am not able to take the same result using SIGINT , because a second signal ( SIGCHLD ) is producing ?

thanks in advance

Offline

#8 2006-12-13 11:49 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: Signal begginer

Offline

Board footer

Powered by FluxBB