Добавил:
Fragga
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:лабораторки по ОС препод Челноков / 2семестр / wi2 / chernok / lab33 / 33
.c/***************************************************************************
main.c - description
-------------------
begin : уТД нБК 5 03:27:25 MSD 2004
copyright : (C) 2004 by бМЕЛУЕК
email : achernok@mail.ru
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
static int onsigint(int sig);
static int onsigusr1(int sig);
static int onsigusr2(int sig);
static int onsigusr3(int sig);
static pid_t fpid,spid,tpid;
pid_t fd[2];
int mes;
int main(int argc, char *argv[])
{
signal(SIGINT,&onsigint);
pipe(fd);
switch(fpid=fork())
{
case -1:printf("FORK ERROR\n");
case 0:fpid=getpid();
switch(spid=fork())
{
case -1:printf("FORK ERROR\n");
case 0:
spid=getpid();
switch(tpid=fork())
{
case -1:printf("FORK ERROR\n");
case 0:
tpid=getpid();
signal(SIGUSR1,&onsigusr3);
while(1)
pause();
default:
signal(SIGUSR1,&onsigusr2);
while(1)
pause();
}
default:
signal(SIGUSR1,&onsigusr1);
mes=1;
write(fd[1],&mes,sizeof(mes));
sleep(1);
kill(spid,SIGUSR1);
while(1)
pause();
}
default:while(1)
pause();
}
}
int onsigint(int sig)
{
signal(SIGINT,&onsigint);
printf("Recieved signal to abort process %d\n",getpid());
exit(0);
}
int onsigusr1(int sig)
{
signal(SIGUSR1,&onsigusr1);
read(fd[0],&mes,sizeof(mes));
printf("FIRST process recieved: %d from THIRD process\n",mes);
mes=mes+1;
write(fd[1],&mes,sizeof(mes));
sleep(1);
kill(spid,SIGUSR1);
return EXIT_SUCCESS;
}
int onsigusr2(int sig)
{
signal(SIGUSR1,&onsigusr2);
read(fd[0],&mes,sizeof(mes));
printf("SECOND process recieved: %d from FIRST process\n",mes);
mes=mes+1;
write(fd[1],&mes,sizeof(mes));
sleep(1);
kill(tpid,SIGUSR1);
return EXIT_SUCCESS;
}
int onsigusr3(int sig)
{
signal(SIGUSR1,&onsigusr3);
read(fd[0],&mes,sizeof(mes));
printf("THIRD process recieved: %d from SECOND process\n",mes);
mes=mes+1;
write(fd[1],&mes,sizeof(mes));
sleep(1);
kill(fpid,SIGUSR1);
return EXIT_SUCCESS;
}