Межпроцессные коммуникации – использование общей памяти, страница 2

time(&curtime);

printf("Parent (PID=%d) SEND \"%d\" to child #%d. Current time: %s",getpid(),shm[gl_n],gl_n,ctime(&curtime));

}

}

pthread_mutex_unlock(&my_mux);

}

/*end of parent*/

/*begin of child*/

void child_prc(int num, int kol)

{

int sum;

sleep(3);

if((shmid=shmget(32547,kol,0))<0)

{perror("shmget"); exit(1);}

if((shm=shmat(shmid,NULL,0))==(char *)-1)

{perror("shmat"); exit(1);}

time(&curtime);

printf("Child #%d (with PID=%d) is created. Current time: %s",num,getpid(),ctime(&curtime));

sum=shm[num];

while(sum<=SUM)

{

shm[num]='m';

time(&curtime);

printf("Child #%d (PID=%d) SEND \"m\" to parent (with PPID=%d). Current time: %s",num,getpid(),getppid(),ctime(&curtime));

while(shm[num]=='m')usleep(100000);

time(&curtime);

sum+=shm[num];

printf("Child #%d (PID=%d) RECV \"%d\" from parent (with PPID=%d). sum=%d. Current time: %s",num,getpid(),shm[num],getppid(),sum,ctime(&curtime));

usleep((shm[num])*800000);

}

time(&curtime);

printf("Child #%d (PID=%d) want to be killed. Current time: %s",num,getpid(),ctime(&curtime));

shm[num]='e';

kill(getppid(),SIGUSR1);

while(1){};

}

/*end of child*/

main(int argc, char* argv[])

{

int (*oldHandler)() ;

int i;

k=atoi(argv[1]);

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_mutex_init(&end_mux,NULL);

for(i=0;i<=k-1;i++)

{

if(!(PIDs[i]=fork()))

{/*child*/

child_prc(i,k);

}

}

{/*parent*/

pthread_t pth_kids[k];

pthread_mutex_init(&my_mux,NULL);

pthread_cond_init(&gl_cond,NULL);

if((shmid=shmget(32547,k,IPC_CREAT|0666))<0)

{perror("shmget"); exit(1);}

if((shm=shmat(shmid,NULL,0))==(char *)-1)

{perror("shmat"); exit(1);}

for(i=0;i<=k-1;i++)

{

shm[i]=rand()%5;

pthread_create(&pth_kids[i],NULL,read_thrd,i);

}

parent_prc();

time(&curtime);

printf("The end of main prog (PID=%d). Current time: %s",getpid(),ctime(&curtime));

signal(SIGINT,oldHandler);

shmdt(shm);

if(shmctl(shmid,IPC_RMID,NULL)==-1)

{perror("shmctl"); exit(-1);}

}

}


Результат выполнения программы:

[serge@localhost lab7]$ lab_7.o 5

The main prog (PID=5144) is started. Current time: Tue Jun  1 18:08:59 2004

Child #0 (with PID=5145) is created. Current time: Tue Jun  1 18:09:02 2004

Child #0 (PID=5145) SEND "m" to parent (with PPID=5144). Current time: Tue Jun  1 18:09:02 2004

Child #1 (with PID=5146) is created. Current time: Tue Jun  1 18:09:02 2004

Child #1 (PID=5146) SEND "m" to parent (with PPID=5144). Current time: Tue Jun  1 18:09:02 2004

Child #2 (with PID=5147) is created. Current time: Tue Jun  1 18:09:02 2004

Child #2 (PID=5147) SEND "m" to parent (with PPID=5144). Current time: Tue Jun  1 18:09:02 2004

Child #3 (with PID=5148) is created. Current time: Tue Jun  1 18:09:02 2004

Child #3 (PID=5148) SEND "m" to parent (with PPID=5144). Current time: Tue Jun  1 18:09:02 2004

Child #4 (with PID=5149) is created. Current time: Tue Jun  1 18:09:02 2004

Child #4 (PID=5149) SEND "m" to parent (with PPID=5144). Current time: Tue Jun  1 18:09:02 2004

Parent (PID=5144) RECV "m" from child #1. Current time: Tue Jun  1 18:09:02 2004

Parent (PID=5144) SEND "0" to child #1. Current time: Tue Jun  1 18:09:02 2004

Parent (PID=5144) RECV "m" from child #2. Current time: Tue Jun  1 18:09:02 2004

Parent (PID=5144) SEND "1" to child #2. Current time: Tue Jun  1 18:09:02 2004

Parent (PID=5144) RECV "m" from child #3. Current time: Tue Jun  1 18:09:02 2004