Skip to content

Instantly share code, notes, and snippets.

@pstachula-dev
Created May 7, 2013 14:43
Show Gist options
  • Save pstachula-dev/5533114 to your computer and use it in GitHub Desktop.
Save pstachula-dev/5533114 to your computer and use it in GitHub Desktop.
Sygnały lab5
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <stdlib.h>
int usr1_ile, usr2_ile;
int i, j;
pid_t child_1;
pid_t child_2;
int pid1, pid2;
sigset_t iset;
struct sigaction act, old;
void obslugaint()
{
++i;
}
void obslugatstp()
{
++j;
}
void usr1(int sig, siginfo_t *siginfo)
{
printf("Sygnal NR: (%d), Child PID sygnalu: (%d) \n", sig, siginfo->si_pid);
}
void susr1()
{
printf("Proces (%d) wyslal SIGUSR1 (%d) razy \n", getpid(), usr1_ile);
exit(EXIT_SUCCESS);
return;
}
void usr2(int sig, siginfo_t *siginfo)
{
printf("Sygnal: (%d), Child PID sygnalu: (%d) \n", sig, siginfo->si_pid);
}
void susr2()
{
int iusr2 = 0;
printf("Proces (%d) wyslal SIGUSR2 (%d) razy \n", getpid(), usr2_ile);
iusr2++;
exit(EXIT_SUCCESS);
}
int main(int argc, char **argv)
{
//if (argc < 1)
// return 0;
printf("Rodzic PID sygnalu: (%d) \n\n", getpid());
sigemptyset(&iset);
act.sa_handler = &obslugaint;
act.sa_mask = iset;
act.sa_flags = SA_SIGINFO;
sigaction(SIGINT, &act, &old);
act.sa_handler = &obslugatstp;
sigaction(SIGTSTP, &act, &old);
act.sa_handler = &usr1;
sigaction(SIGUSR1, &act, &old);
act.sa_handler = &usr2;
sigaction(SIGUSR2, &act, &old);
// Obsluga sygnalow
sigaddset(&iset, SIGINT);
sigaddset(&iset, SIGTSTP);
child_1 = fork();
if (child_1 == 0)
{
pid1 = (int) getpid();
sigprocmask(SIG_BLOCK, &iset, NULL);
act.sa_handler = &susr1;
sigaction(SIGTERM, &act, &old);
while(1)
{
sleep(1);
kill(getppid(), SIGUSR1);
usr1_ile++;
}
}
// Koniec 1 procesu
child_2 = fork();
if (child_2 == 0)
{
pid2 = (int) getpid();
sigprocmask(SIG_BLOCK, &iset, NULL);
act.sa_handler = &susr2;
sigaction(SIGTERM, &act, &old);
while(1)
{
sleep(3);
kill(getppid(), SIGUSR2);
usr2_ile++;
}
}
// Koniec 1 procesu
time_t t;
//int czas = atoi(argv[1]);
int czas = 6;
struct timeval tv;
gettimeofday(&tv, NULL);
t = tv.tv_sec;
do
gettimeofday(&tv, NULL);
while (tv.tv_sec - t <= czas);
kill(child_1, SIGTERM);
kill(child_2, SIGTERM);
printf("\nSIGINT wlaczony (%d) razy,\nSIGTSTP wlaczony (%d) razy \n", i, j);
sigprocmask(SIG_UNBLOCK, &iset, NULL);
sigaction(SIGINT, &old, NULL);
sigaction(SIGTSTP, &old, NULL);
sigaction(SIGTERM, &old, NULL);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment