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

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

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

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

Описание работы:

   1. Текстовое и графическое описание функционирования программы (родительского и дочерних процессов).

Текстовое описание:

Родительский процесс создает потоки, число которых задается в командной строке. Каждый поток создает дочерний процесс. Между родительским и дочерними процессами существует по два канала для передачи информации в обоих направлениях. Родительский процесс каждую секунду передает числа от 2 до 10. Если сумма чисел, переданных дочернему процессу становится больше 10, то дочерний процесс посылает сигнал «end». Так же дочерний процесс каждую секунду передает «trash» для того, чтобы родительский процесс не останавливался, ожидая сообщения от дочерних процессов.

Графическое описание:

   2. Текст программы.

#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>

const size = 1024;

char buf1[1024], buf2[1024], msg[1024];

int n, i, sum[100], k[100];

int pid[100], status[100], fd1[100][2], fd2[100][2];

pthread_t id[100];

time_t itime;

void show_msg (int p1, int p2, char msg[size], int s)

{

time(&itime);

printf("process %d", p1);

if (s)

{

printf(" sent %s", msg);

printf(" to process %d", p2);

}

else

{

printf(" received %s", msg);

printf(" from process %d", p2);

}

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

}

void send_msg(char buf[size], int fd[2], char msg[size])

{

close(fd[0]);

strcpy(buf, msg);

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

}

void get_msg(char buf[size], int fd[2])

{

close(fd[1]);

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

}

void exchange(int i)

{

while(1)

{   

if (pid[i] == 0)

{    

time(&itime);

send_msg(buf1, fd1[i], "trash");

show_msg(i, 0, "trash", 1);   

get_msg(buf2, fd2[i]);     

show_msg(i, 0, buf2, 0);     

int b = atoi(buf2);

sum[i] += b;

printf("process %d", i);

printf(". sum = %d", sum[i]);

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

if (sum[i] > 10)

send_msg(buf1, fd1[i], "end");

show_msg(i, 0, "end", 1);

}

sleep(1);

}     

}

}

void *process(void *p)

{

int i = (int)p;

pid[i] = fork();

if (pid[i] == 0)

{

time(&itime);

printf("start process %d", i);

printf(". pid = %d", getpid());

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

sum[i] = 0;

exchange(i); 

}

}

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

{

signal(SIGINT, SIG_IGN);

n = atoi(argv[1]);

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

{

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

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

k[i] = 0;

pthread_create(&id[i], NULL, process, (void *)i);

}

while(1)     

{

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

{

double t = abs((10*(10*rand()/(RAND_MAX + 1.0)))) + 2.0;

char b[size];

gcvt(t, 2, b);

if (!k[i])

{

send_msg(buf2, fd2[i], b);     

show_msg(0, i, buf2, 1);

get_msg(buf1, fd1[i]);

show_msg(0, i, buf1, 0);

if (strcmp(buf1, "end") == 0)

{

time(&itime);

kill(pid[i], SIGKILL);

printf("end process %d", i);

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

k[i] = 1;

}

}        

sleep(1);

}

int a = 0;

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

if (!k[i]) a = 1;

if (!a) break;

}

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

{

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

pthread_join(id[i], NULL);

}

return 0;

}

Результаты программы. 

Откомпилировали программу: gcc l6.c –o l6.exe -lpthread  

Запустили программу: ./l6.exe 3

start process 1. pid = 1583. time: Thu Mar 18 22:54:57 2004

process 1 sent trash to process 0. time: Thu Mar 18 22:54:57 2004

process 1 received 6 from process 0. time: Thu Mar 18 22:54:57 2004

process 1. sum = 6. time: Thu Mar 18 22:54:57 2004

process 0 sent 6 to process 1. time: Thu Mar 18 22:54:57 2004

process 0 received trash from process 1. time: Thu Mar 18 22:54:57 2004

start process 3. pid = 1587. time: Thu Mar 18 22:54:57 2004

process 3 sent trash to process 0. time: Thu Mar 18 22:54:57 2004

start process 2. pid = 1586. time: Thu Mar 18 22:54:57 2004

process 2 sent trash to process 0. time: Thu Mar 18 22:54:57 2004

process 1 sent trash to process 0. time: Thu Mar 18 22:54:58 2004

process 0 sent 2 to process 2. time: Thu Mar 18 22:54:58 2004

process 0 received trash from process 2. time: Thu Mar 18 22:54:58 2004

process 2 received 2 from process 0. time: Thu Mar 18 22:54:58 2004

process 2. sum = 2. time: Thu Mar 18 22:54:58 2004

process 0 sent 3 to process 3. time: Thu Mar 18 22:54:59 2004

process 0 received trash from process 3. time: Thu Mar 18 22:54:59 2004

process 2 sent trash to process 0. time: Thu Mar 18 22:54:59 2004

process 3 received 3 from process 0. time: Thu Mar 18 22:54:59 2004

process 3. sum = 3. time: Thu Mar 18 22:54:59 2004

process 0 sent 2 to process 1. time: Thu Mar 18 22:55:00 2004

process 0 received trash from process 1. time: Thu Mar 18 22:55:00 2004

process 3 sent trash to process 0. time: Thu Mar 18 22:55:00 2004

process 1 received 2 from process 0. time: Thu Mar 18 22:55:00 2004

process 1. sum = 8. time: Thu Mar 18 22:55:00 2004

process 0 sent 10 to process 2. time: Thu Mar 18 22:55:01 2004

process 0 received trash from process 2. time: Thu Mar 18 22:55:01 2004

process 1 sent trash to process 0. time: Thu Mar 18 22:55:01 2004

process 2 received 10 from process 0. time: Thu Mar 18 22:55:01 2004

process 2. sum = 12. time: Thu Mar 18 22:55:01 2004

process 2 sent end to process 0. time: Thu Mar 18 22:55:01 2004

process 0 sent 2 to process 3. time: Thu Mar 18 22:55:02 2004

process 0 received trash from process 3. time: Thu Mar 18 22:55:02 2004

process 2 sent trash to process 0. time: Thu Mar 18 22:55:02 2004

process 3 received 2 from process 0. time: Thu Mar 18 22:55:02 2004

process 3. sum = 5. time: Thu Mar 18 22:55:02 2004

process 0 sent 8 to process 1. time: Thu Mar 18 22:55:03 2004

process 0 received trash from process 1. time: Thu Mar 18 22:55:03 2004

process 3 sent trash to process 0. time: Thu Mar 18 22:55:03 2004

process 1 received 8 from process 0. time: Thu Mar 18 22:55:03 2004

process 1. sum = 16. time: Thu Mar 18 22:55:03 2004

process 1 sent end to process 0. time: Thu Mar 18 22:55:03 2004

process 0 sent 5 to process 2. time: Thu Mar 18 22:55:04 2004

process 0 received end from process 2. time: Thu Mar 18 22:55:04 2004

end process 2. time: Thu Mar 18 22:55:04 2004

process 1 sent trash to process 0. time: Thu Mar 18 22:55:04 2004

process 0 sent 9 to process 3. time: Thu Mar 18 22:55:05 2004

process 0 received trash from process 3. time: Thu Mar 18 22:55:05 2004

process 3 received 9 from process 0. time: Thu Mar 18 22:55:05 2004

process 3. sum = 14. time: Thu Mar 18 22:55:05 2004

process 3 sent end to process 0. time: Thu Mar 18 22:55:05 2004

process 0 sent 6 to process 1. time: Thu Mar 18 22:55:06 2004

process 0 received end from process 1. time: Thu Mar 18 22:55:06 2004

end process 1. time: Thu Mar 18 22:55:06 2004

process 3 sent trash to process 0. time: Thu Mar 18 22:55:06 2004

process 0 sent 4 to process 3. time: Thu Mar 18 22:55:08 2004

process 0 received end from process 3. time: Thu Mar 18 22:55:08 2004

end process 3. time: Thu Mar 18 22:55:08 2004

Вывод:

Рассмотрели два средства межпроцессных коммуникаций, предоставляемых ОС Linux: неименованные каналы и сигналы.

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

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

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