\documentclass{article}
\usepackage{graphicx,times}
\usepackage{psfig,epsfig}
\usepackage{amsfonts}
\usepackage{amsmath}

\newcommand{\no}{\noindent{\bf Notation:}}
\newtheorem{ex}{Exercice}
\newtheorem{de}{D\'efinition}[section]
\newtheorem{theorem}{Th\'eor\`eme}
\newtheorem{prop}{Proposition}


\begin{document}

\hspace{-.6cm}Universit\'e Paris 7

\hspace{-.6cm}Licence

\hspace{-.6cm}Syst\`eme

\hspace{-.6cm}Ann\'ee 2003-2004

\begin{center}
\textbf{TD no 6}

\textbf{
Signaux : envoi et capture
}
\end{center}


\vspace{.6cm}\hspace{-.6cm}\textbf{Exercice 1 [ ]}

Motivation : fonction \verb_kill_ et action par d\'efaut \`a la
r\'eception de signaux (exple : SIGUSR1 termine le processus).

\vspace{.6cm}\hspace{-.6cm}\textbf{Exercice 2 [ ]}

Motivation : L'utilisateur fournit une fonction pour traiter le
signal. Utilisation de \verb_sigaction_ et \verb_struct sigaction_.

\vspace{.6cm}\hspace{-.6cm}\textbf{kill}

L'appel syst\`eme \verb_kill_ peut \^etre utilis\'e pour envoyer un
signal \`a un processus ou \`a un groupe de processus.

\begin{verbatim}
KILL(2)                    Linux Programmer's Manual                   KILL(2)

NAME
       kill - send signal to a process

SYNOPSIS
       #include <sys/types.h>
       #include <signal.h>

       int kill(pid_t pid, int sig);

DESCRIPTION
       The  kill  system  call  can  be used to send any signal to any
       process group or process.

       If pid is positive, then signal sig is sent to pid.

       If pid equals 0, then sig is sent to every process in the
       process group of the current process.

       If  pid equals -1, then sig is sent to every process except for 
       process 1 (init), but see below.

       If pid is less than -1, then sig is sent to every process in
       the process group -pid.

       If  sig  is 0, then no signal is sent, but error checking is still 
       performed.

RETURN VALUE
       On success, zero is returned.  On error, -1 is returned, and  errno
       is set appropriately.

\end{verbatim}

On peut aussi envoyer des signaux \`a partir d'un autre terminal en
utilisant \verb_kill -s signal pid_


\vspace{.6cm}\hspace{-.6cm}\textbf{signal}

\begin{verbatim}
Pour positionner l'action, il y a:

	#include <signal.h>

	int (*signal(int sig,void (*func)(int)))(int);

(variante: void au lieu de int) La fonction peut être SIG_DFL, SIG_IGN ou l'adresse de la fonction à appeler. Cette fonction a un argument entier: le numéro du signal. Exemple:

    #include <signal.h>
    #include <stdio.h>
    
    int i = 0;
    
    void onsig(int code) {
            fprintf(stderr,"\nSignal %d, i=%d\n",code,i);
            if (code == SIGQUIT) exit(1);
            signal(code,onsig);
    }
    
    main() {
            signal(SIGINT,onsig);
            signal(SIGTERM,onsig);
            signal(SIGQUIT,onsig);
            while (1) {
                    if (++i % 100000 == 0) fprintf(stderr,".");
            }
            exit(0);
    }



Le signal SIGINT correspond à l'interruption du terminal (CTRL-C). 
Son action par défaut est la terminaison.

\end{verbatim}

\vspace{.6cm}\hspace{-.6cm}\textbf{sigaction}

\begin{verbatim}


SYNOPSIS
       #include <signal.h>

       int sigaction(int signum, const struct sigaction *act, struct sigaction
       *oldact);

       int sigprocmask(int how, const sigset_t *set, sigset_t *oldset);

       int sigpending(sigset_t *set);

       int sigsuspend(const sigset_t *mask);

DESCRIPTION
       The  sigaction system call is used to change the action taken by a pro-
       cess on receipt of a specific signal.

       signum specifies the signal and can be any valid signal except  SIGKILL
       and SIGSTOP.

       If  act is non-null, the new action for signal signum is installed from
       act.  If oldact is non-null, the previous action is saved in oldact.

       The sigaction structure is defined as something like

              struct sigaction {
                  void (*sa_handler)(int);
                  void (*sa_sigaction)(int, siginfo_t *, void *);
                  sigset_t sa_mask;
                  int sa_flags;
                  void (*sa_restorer)(void);
              }



\end{verbatim}







\end{document}
