You are on page 1of 4

#include <unistd.

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

int power(double t, int n)


{
if (n==0) return 1;
else
{
int p=1;
while(n!=0)
{
if(n%2 !=0) p=p*t;
n=n/2;
t=t*t;
}
return((int)p);
}
return 0;
}

enum { FALSE, TRUE };


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

// ------------------------------------------------------------------
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)
{
// d �������� ������� �������� ����� ���������� �������� ��������
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;
}
const int digit = 5;

start_signalset();

switch( pid = fork())


{
case -1 :
{fprintf(stderr, "Can't fork()\n");
return;}
case 0 :
{

char child_digit;

int d = 15;
int accepted_didgit = 0;

for(x=2;x<=32;x+=2)
{
wait_for_parents();

char s_child_r[1];
memset(s_child_r, '\0', 2);
char r;
size = read(fd[0], s_child_r, 2);
if(size < 0)
{
printf("Can't write all string\n");
perror(NULL);
return;
}
printf("Child read -> %s\n",s_child_r);

if (s_child_r[0]=='1')
accepted_didgit += power(2.0,d);
d--;
message_for_parents(getppid());
}
printf("Child --> %d\n", accepted_didgit);
return ;
}

default :
{
char bit_d[16];
int iValue = digit;
int i,j;
printf("Digit is %d, or ==", digit);
for (i=0; i<16; i++, iValue<<=1)
{
bit_d[i] = (iValue & 0x8000)== 0 ? '0' : '1';
printf("%c",bit_d[i]);
}
printf ("\n");
int f=0;
for(y=1;y<=31;y+=2)
{
char s_parent_w[2];
s_parent_w[0] = bit_d[f++];
s_parent_w[1] = '\0';

//printf ("\n%d:%c-%c.", sizeof(s_parent_w), s_parent_w[0],


s_parent_w[1] );
size = write(fd[1], s_parent_w, 1);
if(size < 0)
{
printf("2 - Can't write all string\n");
return 1;
}
printf("%d Parent wrote -> %s\n",y,s_parent_w);

message_for_child(pid);
wait_for_child();

}
return 0;
}
}
printf("\n\n");
close(fd[0]);
close(fd[1]);
return 0;
}