Добавил:
nickolai_oreschenko
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:УТС 4 семестр / Lab8_Oreshchenko / main2
.cpp#include <pthread.h>
#include <iostream>
#include <stdio.h>
#include <signal.h>
#include <sys/msg.h>
#include <unistd.h>
#include <string.h>
struct thread_info
{
int msgid;
bool flag = true;
};
struct TMessage
{
long mtype;
char buff[256];
};
void* read(void* arg)
{
thread_info* info = (thread_info*)arg; // преобразуем v thread_info
TMessage message;
message.mtype = 1;
int result;
while(info->flag){
memset(message.buff, 0, sizeof(message.buff));
result = msgrcv(info->msgid, &message, sizeof(message.buff), message.mtype, IPC_NOWAIT);
if (result == -1){
perror("Receive error");
}
std::cout << message.buff << std::endl;
sleep(1);
}
return NULL;
}//read
int main()
{
key_t key = ftok("file.txt", 1);
std::cout << key << std::endl;
thread_info info;
info.flag = true;
info.msgid = msgget(key, 0);
if(info.msgid < 0){
info.msgid = msgget(key, 0666|IPC_CREAT);
}
pthread_t thread_read;
pthread_create(&thread_read, NULL, &read, &info);
getchar();
info.flag = false;
pthread_join(thread_read, NULL);
msgctl(info.msgid, IPC_RMID, NULL);
return 0;
}