Добавил:
Fragga
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:лабораторки по ОС препод Челноков / 2семестр / wi2 / chernok / lab32 / 32
.c/***************************************************************************
main.c - description
-------------------
begin : чФТ нБК 4 23:32:08 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 <sys/types.h>
#include <signal.h>
static pid_t fpid,spid;
static int fd[2];
static void onsigint(int sig);
static void onsigusr11(int sig);
static void onsigusr12(int sig);
char buff[2];
int main(int argc, char *argv[])
{
signal(SIGINT,&onsigint);
fpid=getpid();
pipe(fd);
switch(spid=fork())
{
case -1:;
case 0:
signal(SIGUSR1,&onsigusr12);
while(1)
pause();
default:
signal(SIGUSR1,&onsigusr11);
buff[0]='1';
buff[1]='2';
write(fd[1],buff,2);
kill(spid,SIGUSR1);
while(1)
pause();
}
}
void onsigint(int sig)
{
signal(SIGINT,&onsigint);
printf("Recieved signal to abort process %d\n",getpid());
exit(0);
}
void onsigusr11(int sig)
{
signal(SIGUSR1,&onsigusr11);
read(fd[0],buff,2);
printf("Recieved from SECOND process: %s\n",buff);
buff[0]='1';
buff[1]='2';
sleep(1);
write(fd[1],buff,2);
kill(spid,SIGUSR1);
}
void onsigusr12(int sig)
{
signal(SIGUSR1,&onsigusr12);
read(fd[0],buff,2);
printf("Recieved from FIRST process: %s\n",buff);
buff[0]='2';
buff[1]='1';
sleep(1);
write(fd[1],buff,2);
kill(fpid,SIGUSR1);
}