Professional Documents
Culture Documents
Signaux et pipes
Pipe nomm
Fichiers
Signaux
Type de signaux
#defineSIGHUP1/*Hangup(POSIX).*/
#defineSIGINT2/*Interrupt(ANSI).*/
#defineSIGQUIT3/*Quit(POSIX).*/
#defineSIGILL4/*Illegalinstruction(ANSI).*/
#defineSIGTRAP5/*Tracetrap(POSIX).*/
#defineSIGABRT6/*Abort(ANSI).*/
#defineSIGIOT6/*IOTtrap(4.2BSD).*/
#defineSIGBUS7/*BUSerror(4.2BSD).*/
#defineSIGFPE8/*Floatingpointexception(ANSI).*/
#defineSIGKILL9/*Kill,unblockable(POSIX).*/
#defineSIGUSR110/*Userdefinedsignal1(POSIX).*/
#defineSIGSEGV11/*Segmentationviolation(ANSI).*/
#defineSIGUSR212/*Userdefinedsignal2(POSIX).*/
#defineSIGPIPE13/*Brokenpipe(POSIX).*/
#defineSIGALRM14/*Alarmclock(POSIX).*/
#defineSIGTERM15/*Termination(ANSI).*/
#defineSIGSTKFLT16/*Stackfault.*/
#defineSIGCLDSIGCHLD/*SameasSIGCHLD(SystemV).*/
#defineSIGCHLD17/*Childstatushaschanged(POSIX).*/
#defineSIGCONT18/*Continue(POSIX).*/
#defineSIGSTOP19/*Stop,unblockable(POSIX).*/
#defineSIGTSTP20/*Keyboardstop(POSIX).*/
#defineSIGTTIN21/*Backgroundreadfromtty(POSIX).*/
#defineSIGTTOU22/*Backgroundwritetotty(POSIX).*/
#defineSIGURG23/*Urgentconditiononsocket(4.2BSD).*/
#defineSIGXCPU24/*CPUlimitexceeded(4.2BSD).*/
#defineSIGXFSZ25/*Filesizelimitexceeded(4.2BSD).*/
#defineSIGVTALRM26/*Virtualalarmclock(4.2BSD).*/
#defineSIGPROF27/*Profilingalarmclock(4.2BSD).*/
#defineSIGWINCH28/*Windowsizechange(4.3BSD,Sun).*/
#defineSIGPOLLSIGIO/*Pollableeventoccurred(SystemV).*/
#defineSIGIO29/*I/Onowpossible(4.2BSD).*/
#defineSIGPWR30/*Powerfailurerestart(SystemV).*/
#defineSIGSYS31/*Badsystemcall.*/
Handler
Signal
signal()
#include<signal.h>
voidspint(intsig)
{
printf("Recusignald'interruption%d\n",sig);
}
intmain()
{
signal(SIGINT,spint);
pause();
exit(0);
}
kill()
#include<stdio.h>
voidspsig(intsig)
{printf("Signal%drecu\n",sig);}
intmain()
{
intidfils,status;
if(idfils=fork()){/*pere*/
sleep(5);
kill(idfils,SIGUSR1);
wait(&status);
exit(0);
}
else{/*fils*/
signal(SIGUSR1,spig);
pause();
exit(1);
}
}
Proprits
Exemple de gestionnaire
#include<stdio.h><stdlib.h><sys/types.h><unistd.h><signal.h><sys/wait.h>
voidgestionnaire(intsig,siginfo_t*info,void*inutile)
{
char*origine;
printf("Receptiondusignaln%d",sig);
switch(info>si_code){
caseSI_USER:
printf("envoyaumoyendekill()ouraise()parleprocessus%ld\n",info>si_pid);
break;
caseSI_QUEUE:
printf("envoyaumoyendesigqueue()parleprocessus%ldaveclavaleur%d\n",
(long)info>si_pid,info>si_value.sival_int);
break;
default:
printf("\n");
break;
}
}
Exemple d'utilisation
intmain()
{
pid_tpid_fils;
switch(pid_fils=(long)fork()){
case1:perror("Erreurlorsdefork");returnEXIT_FAILURE;
case0:
{sigset_tmasque;structsigactionaction;
sigemptyset(&masque);
sigaddset(&masque,SIGUSR1);
sigprocmask(SIG_BLOCK,&masque,NULL);
action.sa_sigaction=gestionnaire;
action.sa_mask=masque;
action.sa_flags=SA_SIGINFO;
sigaction(SIGUSR1,&action,NULL);
sigprocmask(SIG_UNBLOCK,&masque,NULL);
while(1);
}
default:
{unionsigvalval;
val.sival_int=123;
sigqueue(pid_fils,SIGUSR1,val);
wait(NULL);
}}
returnEXIT_SUCCESS;
}
Pipes
pipe()
fd1
fd2
r_pos
w_pos
ref=2
iptr
ref=1
text
w_position
task_struct
iptr
data
ref=1
r_position
stack
pipe()
Ou l'inverse...
Exemple de code
intmain()
{
intstatus,pipedes[2];
charbuf[100];
intlen;
char*msg="SalutFred!";
structstatpipestat;
if(pipe(pipedes))exit(1);
if(fork()){/*Codedupere*/
write(pipedes[1],msg,strlen(msg));
wait(&status);
return;
}
else{/*Codedufils*/
while(len==0)/*Attendquelepipesoitrempli*/
{
if(fstat(pipedes[0],&pipestat))exit(3);
len=(int)pipestat.st_size;
}
read(pipedes[0],buf,len);
printf("Monpereadorel'artdedecalerlessons:%s\n",buf);
return;
}
}
Exemple 2 pipes
intpipdes1[2],pipdes2[2];
voidfils()
{
charbuff[21];
/*Fermeturedupipe1en
ecritureet0enlecture*/
close(pipdes1[1]);
close(pipdes2[0]);
read(pipdes1[0],buff,21);
printf("fils:%s\n",buff);
write(pipdes2[1],"Message
recu",13);
return;
}
intmain()
{
intstatus;
charbufp[13];
if(pipe(pipdes1))exit(1);
if(pipe(pipdes2))exit(1);
if(fork())
{
/*Fermeturedupipe0en
ecritureet1enlecture*/
close(pipdes1[0]);
close(pipdes2[1]);
write(pipdes1[1],"Salutlesprocess...",21);
read(pipdes2[0],bufp,13);
printf("Pere:%s\n",bufp);
wait(&status);
exit(0);
}
else
{
fils();
}
}
Pipes nomms
Sockets
Exemple
#include <sys/socket.h>
int main() {
int sock;
if(( sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf( error socket\n );
return -1;
}
return 0;
}
Ct serveur
En gnral, on fork()
Le pre retourne l'coute
Le fils effectue le traitement
Exemple
Cration
Attachement
Attente
Nouvelle connection
Retour en attente
Traitement
Ct client
int connect(int sock, struct sockaddr *a, int len)
Exemple
#define MY_SOCK_PATH /tmp/mysock
int main(int argc, char *argv[])
{
int cfd; struct sockaddr_un my_addr;
char buffer[64];
cfd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sfd == -1) handle_error("socket");
memset(&my_addr, 0, sizeof(struct sockaddr_un));
my_addr.sun_family = AF_UNIX;
strncpy(my_addr.sun_path, MY_SOCK_PATH, sizeof(my_addr.sun_path)
1);
if(connect(cdf, &my_addr, sizeof(struct sock_addr_un)) < 0) }
handle_error( connect );
}
read(cdf, buffer, 64);
printf( lu : %s\n , buffer);
return 0;
}
COLE NATIONALE SUPRIEURE DE L'LECTRONIQUE ET DE SES APPLICATIONS