Изучение средств межпроцессных коммуникаций, предоставляемых ОС Linux, на примере неименованных каналов

Страницы работы

Содержание работы

Цель работы: Изучение средств межпроцессных коммуникаций, предоставляемых ОС Linux, на примере неименованных каналов. Знакомство с сигналами ОС и с функциями для работы с ними.

1) Текст программы  и результаты работы программы.:

а)Текст программы :

#include <stdio.h>

#include <sys/types.h>

#include <sys/wait.h>

#include <unistd.h>

#include <signal.h>

#include <stdlib.h>

#include <sys/time.h>

#include <pthread.h>

#define SIZE 1024

#define DVE 200

char buf1[SIZE],

buf2[SIZE],

message[SIZE],

mes[SIZE];  

int n, i,nomproc,

pid[DVE],

status[DVE],

fd1[DVE][2],

fd2[DVE][2],

status[DVE],

sta,

kkk[DVE],

flag_st[DVE],

summa[DVE];

pthread_t ptr[DVE],

wrread,

killer[DVE];

time_t qtime;

void flag_stop();

void printbuf(char buf[SIZE],int e)

{

printf("I am process %d ",e);

printf("chislo ot Main: %s \n",buf);

}

void printdata(char buf[SIZE],int e)

{

printf("Data poluchenye ot %d ",e);

printf("data: %s \n",buf);

}

void writeMessage(char buf[SIZE], int fd[2], char message[SIZE])

{close(fd[0]);

strcpy(buf, message);

write(fd[1], buf, strlen(buf) + 1);

}

void readMessage(char buf[SIZE], int fd[2])

{close(fd[1]);

read(fd[0], buf, SIZE);

}

void *start_process(void *p)

{ int i = (int)p,chis,er,su;

char wer[SIZE];

pid[i] = fork();

if (pid[i] == 0)

{ time(&qtime);

printf("Children process nomber %d runing; ", i);

printf("my PID = %d; ", getpid());

printf("time: %s", ctime(&qtime));

}

while(1)

{if (pid[i]==0)

readMessage( buf1,fd1[i]);

printbuf(buf1,i);

er=atoi(buf1);    

chis=(99*rand())%15+er*3;                                

summa[i]=(summa[i]+chis);

su=summa[i];

//KIIL me

if(su>100)

{ kill(getppid(),SIGALRM);

writeMessage(buf2,fd2[i],"ready");     

sleep(1);

}else

{gcvt(su,3,wer);

writeMessage(buf2,fd2[i],wer);            

sleep(1);

}

}

}   

}

void *obmen_data(void *p)

{  int rnd,x;

int qq = (int)p;

while(1)

{for(x = 1; x <= qq; x++)

{if(kkk[x]==0)

{rnd=rand()%15+3;             

gcvt(rnd,2,mes);     

writeMessage(buf1,fd1[x],mes);              

readMessage(buf2,fd2[x]);

if (strcmp(buf2, "ready") == 0)nomproc=x;

printdata(buf2,x);                

sleep(1);

}

else 

sleep(1);

}         

}

}

int main(int argc, char *argv[])

{ int k;

signal(SIGINT, SIG_IGN);

signal(SIGALRM, flag_stop);

n = atoi(argv[1]);

for (i = 1; i <= n; i++)

{summa[i]=0;

flag_st[i]=0;kkk[i]=0;  

if (pipe(fd1[i]) == -1) {perror("pipe failed"); exit(1);}

if (pipe(fd2[i]) == -1) {perror("pipe failed"); exit(1);}

pthread_create(&ptr[i], NULL, start_process, (void *)i);

}

pthread_create(&wrread, NULL, obmen_data, (void *)n);

int asd=0;

time_t df[DVE];

while(1)

{

for(i = 1; i <= n; i++)

if((flag_st[i]==1)&&(kkk[i]==0))

{     printf("flag_st=1 ot %d \n",i);

kkk[i]=1;asd++;

kill(pid[i], SIGKILL);

printf("Process %d killed ",i);

time(&df[i]);

printf("Time :%s  \n",ctime(&df[i]));   

}    

sleep(1);

if (asd==n)

{     printf("POTOK PEREDACHI DATA KILLED!!!\n ");

pthread_cancel(wrread);

break;

}  

}

for (i = 1; i <= n; i++)

{ waitpid(pid[i], &status[i], WUNTRACED);  

pthread_cancel(ptr[i]);

}                       

return 0;

}

void flag_stop()

{

flag_st[nomproc]=1;

printf("\nSIGNAL na flag_stop ot %d \n",nomproc); 

}

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

[spike@localhost lb6]$ lab6.exe 7

Children process nomber 1 runing; my PID = 2762; time: Mon May 24 23:46:07 2004

Children process nomber 2 runing; my PID = 2765; time: Mon May 24 23:46:07 2004

Children process nomber 3 runing; my PID = 2767; time: Mon May 24 23:46:07 2004

Children process nomber 4 runing; my PID = 2769; time: Mon May 24 23:46:07 2004

Children process nomber 5 runing; my PID = 2770; time: Mon May 24 23:46:07 2004

Children process nomber 6 runing; my PID = 2772; time: Mon May 24 23:46:07 2004

I am process 1 chislo ot Main: 16

Data poluchenye ot 1 data: 48

Children process nomber 7 runing; my PID = 2775; time: Mon May 24 23:46:08 2004

I am process 2 chislo ot Main: 4

Data poluchenye ot 2 data: 12

I am process 3 chislo ot Main: 15

Data poluchenye ot 3 data: 45

I am process 4 chislo ot Main: 13

Data poluchenye ot 4 data: 39

I am process 5 chislo ot Main: 11

Data poluchenye ot 5 data: 33

I am process 6 chislo ot Main: 13

Data poluchenye ot 6 data: 39

I am process 7 chislo ot Main: 4

Data poluchenye ot 7 data: 12

I am process 1 chislo ot Main: 15

Data poluchenye ot 1 data: 82

I am process 2 chislo ot Main: 12

Data poluchenye ot 2 data: 37

I am process 3 chislo ot Main: 4

Data poluchenye ot 3 data: 46

I am process 4 chislo ot Main: 5

Data poluchenye ot 4 data: 43

I am process 5 chislo ot Main: 10

Data poluchenye ot 5 data: 52

I am process 6 chislo ot Main: 8

Data poluchenye ot 6 data: 52

I am process 7 chislo ot Main: 7

Data poluchenye ot 7 data: 22

I am process 1 chislo ot Main: 11

Data poluchenye ot 1 data: ready

SIGNAL na flag_stop ot 1

flag_st=1 ot 1

Process 1 killed Time :Mon May 24 23:46:23 2004

I am process 2 chislo ot Main: 4

Data poluchenye ot 2 data: 43

I am process 3 chislo ot Main: 3

Data poluchenye ot 3 data: 49

I am process 4 chislo ot Main: 9

Data poluchenye ot 4 data: 64

I am process 5 chislo ot Main: 10

Data poluchenye ot 5 data: 76

I am process 6 chislo ot Main: 4

Data poluchenye ot 6 data: 58

I am process 7 chislo ot Main: 14

Data poluchenye ot 7 data: 58

I am process 2 chislo ot Main: 11

Data poluchenye ot 2 data: 66

I am process 3 chislo ot Main: 15

Data poluchenye ot 3 data: 84

I am process 4 chislo ot Main: 12

Data poluchenye ot 4 data: 90

I am process 5 chislo ot Main: 5

Data poluchenye ot 5 data: 81

I am process 6 chislo ot Main: 8

Data poluchenye ot 6 data: 72

I am process 7 chislo ot Main: 5

Data poluchenye ot 7 data: 63

I am process 2 chislo ot Main: 16

Data poluchenye ot 2 data: ready

SIGNAL na flag_stop ot 2

flag_st=1 ot 2

Process 2 killed Time :Mon May 24 23:46:38 2004

I am process 3 chislo ot Main: 10

Data poluchenye ot 3 data: ready

SIGNAL na flag_stop ot 3

flag_st=1 ot 3

Process 3 killed Time :Mon May 24 23:46:39 2004

I am process 4 chislo ot Main: 13

Data poluchenye ot 4 data: ready

SIGNAL na flag_stop ot 4

flag_st=1 ot 4

Process 4 killed Time :Mon May 24 23:46:40 2004

I am process 5 chislo ot Main: 17

Data poluchenye ot 5 data: ready

SIGNAL na flag_stop ot 5

flag_st=1 ot 5

Process 5 killed Time :Mon May 24 23:46:41 2004

I am process 6 chislo ot Main: 15

Data poluchenye ot 6 data: ready

SIGNAL na flag_stop ot 6

flag_st=1 ot 6

Process 6 killed Time :Mon May 24 23:46:42 2004

I am process 7 chislo ot Main: 15

Data poluchenye ot 7 data: ready

SIGNAL na flag_stop ot 7

flag_st=1 ot 7

Process 7 killed Time :Mon May 24 23:46:43 2004

POTOK PEREDACHI DATA KILLED!!!

[spike@localhost lb6]$

Вывод:  в результате проделанной работы, познакомился с средствами межпроцесных коммуникаций , а именно каналов и сигналов а так же функциями для работы с ними.

Похожие материалы

Информация о работе

Тип:
Отчеты по лабораторным работам
Размер файла:
56 Kb
Скачали:
0