if(GenNum[num_of_chl]==0)
{
thr_end=1;
}
else {
printf("Thread #%d want to be locked (res=%d GenNum=%d)\n",num_of_chl,res,GenNum[num_of_chl]);
pthread_mutex_lock(&my_mux);
printf("Thread #%d is locked\n",num_of_chl);
gl_GenNum=GenNum[num_of_chl];
gl_n=num_of_chl;
cond_sig=1;
pthread_cond_signal(&gl_cond);
printf("Thread #%d is unlocked\n",num_of_chl);
pthread_mutex_unlock(&my_mux);
}
}
printf("Thread #%d end PID=%d\n",num_of_chl,getpid());
}
/*end of read_thread*/
/*begin of parent*/
void parent_prc()
{
printf("Parent begin (PID=%d)\n",getpid());
char sms[5];
sleep(1);
start_thrds=1;
pthread_mutex_lock(&my_mux);
while(cnt_alive_chd!=0)
{
while(cond_sig!=1){pthread_cond_wait(&gl_cond,&my_mux);}
if(cnt_alive_chd!=0)
{
cond_sig=0;
time(&curtime);
printf("Parent (PID=%d) RECV \"%d\" from child #%d. Current time: %s",getpid(),gl_GenNum,gl_n,ctime(&curtime));
strcpy(sms,"OK");
close(fd1[gl_n][0]);
write(fd1[gl_n][1],sms,strlen(sms)+1);
time(&curtime);
printf("Parent (PID=%d) SEND \"%s\" to child #%d. Current time: %s",getpid(),sms,gl_n,ctime(&curtime));
}
}
pthread_mutex_unlock(&my_mux);
printf("Parent end\n");
}
/*end of parent*/
/*begin of child*/
void child_prc(int i)
{
int sum=0;
int resw, resr;
char answ[5];
time(&curtime);
printf("Child #%d (with PID=%d) is created. Current time: %s",i,getpid(),ctime(&curtime));
sleep(1);
n[i]=i;
PID[i]=getpid();
while(sum<=SUM)
{
GenNum[i]=rand()%5+1;
sleep(GenNum[i]/2);
close(fd2[n[i]][0]);
resw=write(fd2[n[i]][1],(void*)&GenNum[i],sizeof(int));
time(&curtime);
printf("Child #%d (PID=%d) SEND \"%d\" (%d) to parent (with PPID=%d). Current time: %s",n[i],PID[i],GenNum[i],resw,getppid(),ctime(&curtime));
close(fd1[n[i]][1]);
resr=read(fd1[n[i]][0],answ,5);
time(&curtime);
printf("Child #%d (PID=%d) RECV \"%s\" (%d) from parent (with PPID=%d). Current time: %s",i,getpid(),answ,resr,getppid(),ctime(&curtime));
sum+=GenNum[i];
}
time(&curtime);
printf("Child #%d (PID=%d) want to be killed...\n",i,getpid());
GenNum[i]=0;
close(fd2[n[i]][0]);
write(fd2[n[i]][1],(void*)&GenNum[i],sizeof(int));
pthread_mutex_lock(&end_mux);
close(fd0[0]);
write(fd0[1],(void*)&PID[i],sizeof(int));
kill(getppid(),SIGUSR1);
pthread_mutex_unlock(&end_mux);
while(1){};
}
/*end of child*/
int main(int argc, char* argv[])
{
int i, PIDf;
int k=atoi(argv[1]);
int (*oldHandler) ( ) ;
cnt_alive_chd=k;
start_thrds=0;
oldHandler=signal(SIGINT,SIG_IGN);
signal(SIGUSR1,SigHndlr);
time(&curtime);
printf("The main prog (PID:%d) is started. Current time: %s",getpid(),ctime(&curtime));
pthread_t pth_kids[k];
pthread_mutex_init(&my_mux,NULL);
pthread_mutex_init(&end_mux,NULL);
pthread_cond_init(&gl_cond,NULL);
pipe(fd0);
for(i=1;i<=k;i++)
{
pipe(fd1[i]);
pipe(fd2[i]);
pthread_create(&pth_kids[i],NULL,read_thrd,i);
}
for(i=1;i<=k;i++)
{
if(!(PIDf=fork()))
{/*child*/
child_prc(i);
exit(1);
}
}
if(PIDf!=0)
{/*parent*/
parent_prc();
time(&curtime);
printf("The end of main prog (PID:%d). Current time: %s",getpid(),ctime(&curtime));
signal(SIGINT,oldHandler);
return 1;
}
}
Результат выполнения программы:
[serge@localhost lab6]$ lab_6.o 5
The main prog (PID=2870) is started. Current time: Thu May 27 09:14:50 2004
Child #1 (with PID=2877) is created. Current time: Thu May 27 09:14:50 2004
Child #2 (with PID=2878) is created. Current time: Thu May 27 09:14:51 2004
Child #3 (with PID=2879) is created. Current time: Thu May 27 09:14:51 2004
Child #4 (with PID=2880) is created. Current time: Thu May 27 09:14:51 2004
Child #5 (with PID=2881) is created. Current time: Thu May 27 09:14:51 2004
Child #1 (PID=2877) SEND "4" (4) to parent (with PPID=2870). Current time: Thu May 27 09:14:53 2004
Parent (PID=2870) RECV "4" from child #1. Current time: Thu May 27 09:14:53 2004
Parent (PID=2870) SEND "OK" to child #1. Current time: Thu May 27 09:14:53 2004
Child #1 (PID=2877) RECV "OK" (3) from parent (with PPID=2870). Current time: Thu May 27 09:14:53 2004
Child #2 (PID=2878) SEND "4" (4) to parent (with PPID=2870). Current time: Thu May 27 09:14:54 2004
Parent (PID=2870) RECV "4" from child #2. Current time: Thu May 27 09:14:54 2004
Уважаемый посетитель!
Чтобы распечатать файл, скачайте его (в формате Word).
Ссылка на скачивание - внизу страницы.