-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
1,678 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
all: ejemplo_waitpid ejemplo_waitpid2 alarms signalPerdido | ||
|
||
ejemplo_waitpid:ejemplo_waitpid.c | ||
gcc -o ejemplo_waitpid ejemplo_waitpid.c | ||
|
||
ejemplo_waitpid2:ejemplo_waitpid2.c | ||
gcc -o ejemplo_waitpid2 ejemplo_waitpid2.c | ||
|
||
alarms:alarm1 alarm2 alarm3 alarm4 | ||
|
||
alarm1:ejemplo_alarm1.c | ||
gcc -o alarm1 ejemplo_alarm1.c | ||
alarm2:ejemplo_alarm2.c | ||
gcc -o alarm2 ejemplo_alarm2.c | ||
alarm3:ejemplo_alarm3.c | ||
gcc -o alarm3 ejemplo_alarm3.c | ||
alarm4:ejemplo_alarm4.c | ||
gcc -o alarm4 ejemplo_alarm4.c | ||
signalPerdido:signalPerdido.c | ||
gcc -o signalPerdido signalPerdido.c | ||
|
||
clean: | ||
rm -rf alarm1 alarm2 alarm3 alarm4 ejemplo_waitpid ejemplo_waitpid2 signalPerdido | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
COMO COMPILAR | ||
------------- | ||
make -f Makefile.eventos | ||
|
||
EJEMPLOS CON ALARMAS | ||
-------------------- | ||
|
||
ejemplo_alarm1.c: alarm/sigsuspend. El programa se autoprograma un evento tipo alarma al cabo de 5 segundos. La accion por defecto de Linux es acabar el proceso. | ||
ejemplo_alarm2.c:sigprocmask/sigaction/alarm/sigsuspend. Realizamos una accion cada X segundos. Solo es necesario reprogramar el SIGALRM una vez | ||
ejemplo_alarm3.c:sigprocmask/sigaction/alarm/sigsuspend. Herencia de tabla de eventos | ||
ejemplo_alarm4.c:sigprocmask/sigaction/alarm/sigsuspend. La tabla de eventos se hereda pero no los eventos pendientes | ||
|
||
EJEMPLO CON SIGCHLD | ||
------------------- | ||
ejemplo_waitpid.c: Programa que crea varios hijos y espera a que finalicen mediante un bucle de waitpid's. | ||
|
||
ejemplo_waitpid2.c: Programa que crea varios hijos, después ejecuta su propia tarea y después espera a que finalicen sus hijos mediante un bucle de waitpid's. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#include <unistd.h> | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include <signal.h> | ||
/* ESTE PROCESO PROGRAMA UN TEMPORIZADOR PARA DENTRO DE 5 SEGUNDOS Y SE BLOQUEA A ESPERAR QUE LLEGUE */ | ||
/* LA ACCION POR DEFECTO DEL SIGALRM ES ACABAR EL PROCESO */ | ||
int main (int argc,char * argv[]) | ||
{ | ||
sigset_t mask; | ||
|
||
sigemptyset(&mask); | ||
sigaddset(&mask, SIGALRM); | ||
sigprocmask(SIG_BLOCK, &mask, NULL); | ||
|
||
alarm(10); | ||
sigfillset(&mask); | ||
sigdelset(&mask, SIGALRM); | ||
sigdelset(&mask, SIGINT); | ||
sigsuspend(&mask); | ||
exit(1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include <unistd.h> | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include <signal.h> | ||
#include <string.h> | ||
|
||
void error_y_exit(char *msg,int exit_status) | ||
{ | ||
perror(msg); | ||
exit(exit_status); | ||
} | ||
|
||
/* ESTA VARIABLE SE ACCEDE DESDE LA FUNCION DE ATENCION AL SIGNAL Y DESDE EL MAIN */ | ||
int segundos=0; | ||
/* FUNCION DE ATENCION AL SIGNAL SIGALRM */ | ||
void funcion_alarma(int s) | ||
{ | ||
char buff[256]; | ||
segundos=segundos+10; | ||
sprintf(buff, "ALARMA pid=%d: %d segundos\n",getpid(),segundos); | ||
write(1, buff, strlen(buff)); | ||
} | ||
int main (int argc,char * argv[]) | ||
{ | ||
struct sigaction sa; | ||
sigset_t mask; | ||
|
||
sigemptyset(&mask); | ||
sigaddset(&mask, SIGALRM); | ||
sigprocmask(SIG_BLOCK,&mask, NULL); | ||
|
||
/* REPROGRAMAMOS EL SIGNAL SIGALRM */ | ||
sa.sa_handler = &funcion_alarma; | ||
sa.sa_flags = SA_RESTART; | ||
sigfillset(&sa.sa_mask); | ||
|
||
if (sigaction(SIGALRM, &sa, NULL) < 0) error_y_exit("sigaction", 1); | ||
|
||
while (segundos<100) | ||
{ | ||
alarm(10); /* Programamos la alarma para dentro de 10 segundos */ | ||
/* Nos bloqueamos a esperar que nos llegue un evento */ | ||
sigfillset(&mask); | ||
sigdelset(&mask, SIGALRM); | ||
sigdelset(&mask, SIGINT); | ||
sigsuspend(&mask); | ||
} | ||
exit(1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include <unistd.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <signal.h> | ||
#include <string.h> | ||
|
||
void error_y_exit(char *msg,int exit_status) | ||
{ | ||
perror(msg); | ||
exit(exit_status); | ||
} | ||
|
||
int segundos=0; | ||
void funcion_alarma(int signal) | ||
{ | ||
char buff[256]; | ||
segundos=segundos+10; | ||
sprintf(buff,"ALARMA pid=%d: %d segundos\n",getpid(),segundos); | ||
write(1, buff, strlen(buff) ); | ||
} | ||
|
||
int main (int argc,char * argv[]) | ||
{ | ||
struct sigaction sa; | ||
sigset_t mask; | ||
|
||
/* EVITAMOS QUE NOS LLEGUE EL SIGALRM FUERA DEL SIGSUSPEND */ | ||
sigemptyset(&mask); | ||
sigaddset(&mask, SIGALRM); | ||
sigprocmask(SIG_BLOCK,&mask, NULL); | ||
|
||
/* REPROGRAMAMOS EL SIGNAL SIGALRM */ | ||
sa.sa_handler = &funcion_alarma; | ||
sa.sa_flags = SA_RESTART; | ||
sigfillset(&sa.sa_mask); | ||
|
||
if (sigaction(SIGALRM, &sa, NULL) < 0) error_y_exit("sigaction", 1); | ||
|
||
if (fork() < 0) error_y_exit("fork", 1); | ||
while (segundos<100) | ||
{ | ||
alarm(10); | ||
sigfillset(&mask); | ||
sigdelset(&mask, SIGALRM); | ||
sigdelset(&mask, SIGINT); | ||
sigsuspend(&mask); | ||
} | ||
exit(1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#include <unistd.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <signal.h> | ||
#include <string.h> | ||
#include <sys/types.h> | ||
|
||
void error_y_exit(char *msg,int exit_status) | ||
{ | ||
perror(msg); | ||
exit(exit_status); | ||
} | ||
|
||
int segundos=0; | ||
void funcion_alarma(int signal) | ||
{ | ||
char buff[256]; | ||
segundos=segundos+10; | ||
sprintf(buff,"ALARMA pid=%d: %d segundos\n",getpid(),segundos); | ||
write(1, buff, strlen(buff) ); | ||
} | ||
int main (int argc,char * argv[]) | ||
{ | ||
struct sigaction sa; | ||
sigset_t mask; | ||
|
||
/* EVITAMOS TRATAR EL SIGALRM FUERA DEL SIGSUSPEND */ | ||
|
||
sigemptyset(&mask); | ||
sigaddset(&mask, SIGALRM); | ||
sigprocmask(SIG_BLOCK,&mask, NULL); | ||
|
||
/* REPROGRAMAMOS EL SIGNAL SIGALRM */ | ||
sa.sa_handler = &funcion_alarma; | ||
sa.sa_flags = SA_RESTART; | ||
sigfillset(&sa.sa_mask); | ||
|
||
if (sigaction(SIGALRM, &sa, NULL) < 0) error_y_exit("sigaction", 1); | ||
|
||
switch (fork()) { | ||
case -1: error_y_exit("fork", 1); | ||
case 0: alarm(10); | ||
} | ||
while (segundos<100) | ||
{ | ||
sigfillset(&mask); | ||
sigdelset(&mask, SIGALRM); | ||
sigdelset(&mask, SIGINT); | ||
sigsuspend(&mask); | ||
alarm(10); | ||
} | ||
exit(1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include <sys/types.h> | ||
#include <sys/wait.h> | ||
#include <string.h> | ||
#include <signal.h> | ||
|
||
void error_y_exit(char *msg,int exit_status) | ||
{ | ||
perror(msg); | ||
exit(exit_status); | ||
} | ||
|
||
void trata_alarma(int s) | ||
{ | ||
|
||
} | ||
|
||
int main(int argc,char *argv[]) | ||
{ | ||
int pid,res; | ||
char buff[256]; | ||
int contador = 0; | ||
int hijos=0; | ||
struct sigaction sa; | ||
sigset_t mask; | ||
|
||
/* Evitamos recibir el SIGALRM fuera del sigsuspend */ | ||
|
||
sigemptyset(&mask); | ||
sigaddset(&mask,SIGALRM); | ||
sigprocmask(SIG_BLOCK,&mask, NULL); | ||
|
||
for (hijos=0;hijos<10;hijos++) | ||
{ | ||
sprintf(buff, "Creando el hijo numero %d\n", hijos); | ||
write(1, buff, strlen(buff)); | ||
|
||
pid=fork(); | ||
if (pid==0) /* Esta linea la ejecutan tanto el padre como el hijo */ | ||
{ | ||
|
||
sa.sa_handler = &trata_alarma; | ||
sa.sa_flags = SA_RESTART; | ||
sigfillset(&sa.sa_mask); | ||
if (sigaction(SIGALRM, &sa, NULL) < 0) error_y_exit("sigaction", 1); | ||
|
||
/* Escribe aqui el codigo del proceso hijo */ | ||
sprintf(buff,"Hola, soy %d\n",getpid()); | ||
write(1, buff, strlen(buff)); | ||
|
||
alarm(1); | ||
sigfillset(&mask); | ||
sigdelset(&mask, SIGALRM); | ||
sigdelset(&mask, SIGINT); | ||
sigsuspend(&mask); | ||
|
||
/* Termina su ejecución */ | ||
exit(0); | ||
} else if (pid<0) | ||
{ | ||
/* Se ha producido un error */ | ||
error_y_exit("Error en el fork", 1); | ||
} | ||
} | ||
/* Esperamos que acaben los hijos */ | ||
while (hijos > 0) | ||
{ | ||
pid=waitpid(-1,&res,0); | ||
sprintf(buff,"Termina el proceso %d\n",pid); | ||
write(1, buff, strlen(buff)); | ||
hijos --; | ||
contador++; | ||
} | ||
sprintf(buff,"Valor del contador %d\n", contador); | ||
write(1, buff, strlen(buff)); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include <sys/types.h> | ||
#include <sys/wait.h> | ||
#include <string.h> | ||
#include <signal.h> | ||
|
||
#define FIN_BUCLE 50000000ULL | ||
|
||
void error_y_exit(char *msg,int exit_status) | ||
{ | ||
perror(msg); | ||
exit(exit_status); | ||
} | ||
|
||
void hago_algo_util() | ||
{ | ||
unsigned long long i; | ||
|
||
for (i = 0; i < FIN_BUCLE; i++); | ||
} | ||
|
||
|
||
void trata_alarma(int s) | ||
{ | ||
|
||
} | ||
|
||
|
||
int main(int argc,char *argv[]) | ||
{ | ||
int pid,res; | ||
char buff[256]; | ||
int hijos=0; | ||
struct sigaction sa; | ||
sigset_t mask; | ||
|
||
for (hijos=0;hijos<10;hijos++) | ||
{ | ||
sprintf(buff, "Creando el hijo numero %d\n", hijos); | ||
write(1, buff, strlen(buff)); | ||
|
||
pid=fork(); | ||
if (pid==0) /* Esta linea la ejecutan tanto el padre como el hijo */ | ||
{ | ||
//signal (SIGALRM, trata_alarma); | ||
|
||
sa.sa_handler = &trata_alarma; | ||
sa.sa_flags = SA_RESTART; | ||
sigfillset(&sa.sa_mask); | ||
if (sigaction(SIGALRM, &sa, NULL) < 0) error_y_exit("sigaction", 1); | ||
|
||
/* Escribe aqui el codigo del proceso hijo */ | ||
sprintf(buff,"Hola, soy %d\n",getpid()); | ||
write(1, buff, strlen(buff)); | ||
|
||
alarm(1); | ||
//pause(); | ||
sigfillset(&mask); | ||
sigdelset(&mask, SIGALRM); | ||
sigdelset(&mask, SIGINT); | ||
sigsuspend(&mask); | ||
|
||
/* Termina su ejecución */ | ||
exit(0); | ||
} else if (pid<0) | ||
{ | ||
/* Se ha producido un error */ | ||
error_y_exit("Error en el fork", 1); | ||
} | ||
} | ||
|
||
hago_algo_util(); | ||
|
||
/* Esperamos que acaben el resto de hijos */ | ||
while ((pid=waitpid(-1,&res,0)) > 0) | ||
{ | ||
sprintf(buff,"Waitpid bloqueante. Termina %d\n",pid); | ||
write(1, buff, strlen(buff)); | ||
} | ||
return 0; | ||
} |
Oops, something went wrong.