You are on page 1of 4

#include <unistd.

h>
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <sys/types.h>

enum { FALSE, TRUE };


sigset_t sig_m1, sig_m2, sig_null;
int signal_flag=FALSE;
int fd[2]; // for pipe
size_t size;

// ------------------------------------------------------------------Read
void child_func_read(int fdr)
{
char s_child_r[20];
memset(s_child_r, '\0', 20);
size = read(fdr, s_child_r, 18);
if(size < 0)
{
printf("Can't write all string\n");
perror(NULL);
return;
}
//size = read(fdr, s_child_r, 20);
//close(fdr);
printf("Child read -> %s\n",s_child_r);
}

// ------------------------------------------------------------------
void parent_func_read(int fdr)
{
// ������ ������
char s_parent_r[20];
size = read(fdr, s_parent_r, 20);
if(size < 0)
{
/* ���� ���������� ������� ���������� ����, ��������
�� ������ � ��������� ������ */
printf("2 - Can't write all string\n");
return;
}

printf("Parent read -> %s\n",s_parent_r);


printf("Parent exit\n");
close(fdr);
}

// ------------------------------------------------------------------Write
void child_func_write(int fdw)
{
const char s_child_w[] = "Hello, world!";
size = write(fdw, s_child_w, 13);
if(size < 0)
{
printf("Can't read string\n");
perror(NULL);
return;
}
printf("Child wrote -> %s\n",s_child_w);
printf("Child exit\n\n");
close(fdw);
}

// ---------------------------------------------------------------------
void parent_func_write(int fdw)
{
const char s_parent_w[] = "Hello, I'm Parent!";
size = write(fdw, s_parent_w, 18);
if(size < 0)
{
/* ���� ���������� ������� ���������� ����, ��������
�� ������ � ��������� ������ */
printf("2 - Can't write all string\n");
return;
}
printf("Parent wrote -> %s\n",s_parent_w);
//close(fdw);
}

// ------------------------------------------------------------------
void start_signalset(void);
void sig_func(int signr)
{
start_signalset();
signal_flag = TRUE;
}

// ------------------------------------------------------------------
void start_signalset(void)
{
if(signal(SIGUSR1, sig_func) == SIG_ERR) return ;
if(signal(SIGUSR2, sig_func) == SIG_ERR) return ;

sigemptyset(&sig_m1);
sigemptyset(&sig_null);
sigaddset(&sig_m1,SIGUSR1);
sigaddset(&sig_m1,SIGUSR2);
if(sigprocmask(SIG_BLOCK, &sig_m1, &sig_m2) < 0) return ;
}

// ------------------------------------------------------------------Parent
void message_for_parents(pid_t pid)
{
kill(pid,SIGUSR2);
}
// ------------------------------------------------------------------
void wait_for_parents()
{
while(signal_flag == FALSE)
sigsuspend(&sig_null);
signal_flag = FALSE;
if(sigprocmask(SIG_SETMASK, &sig_m2, NULL) < 0) return ;
}
// ------------------------------------------------------------------Child
void message_for_child(pid_t pid)
{
kill(pid, SIGUSR1);
}

// ------------------------------------------------------------------
void wait_for_child(void)
{
while(signal_flag == FALSE)
sigsuspend(&sig_null);
signal_flag = FALSE;
if(sigprocmask(SIG_SETMASK, &sig_m2, NULL) < 0) return;
}

// ------------------------------------------------------------------
int main()
{
pid_t pid;
char x,y;

if(pipe(fd) < 0)
{
printf("Can't create pipe\n");
return 1;
}

start_signalset();

switch( pid = fork())


{
case -1 : fprintf(stderr, "������ fork()\n");
return;

case 0 : /* Child */
for(x=2;x<=10;x+=2)
{
wait_for_parents();
child_func_read(fd[0]);
message_for_parents(getppid());
}
return ;

default : /* Parent */
for(y=1;y<=9;y+=2)
{

parent_func_write(fd[1]);
message_for_child(pid);
wait_for_child();
}
}
printf("\n\n");
close(fd[0]);
close(fd[1]);
return 0;
}