Добавил:
nickolai_oreschenko
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:УТС 4 семестр / Lab8_Oreshchenko / main
.cpp#include <pthread.h>
#include <iostream>
#include <stdio.h>
#include <signal.h>
#include <sys/msg.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
struct TMessage
{
long mtype;
char buff[256];
};
struct thread_info // структура описывающая параметры потока
{
int msgid;
bool flag = true;
};
void* write(void* arg)
{
thread_info* info = (thread_info*)arg; // преобразуем v thread_info
TMessage message;
message.mtype = 1;
int count = 1;
int len;
int result;
while(info->flag){
len = sprintf(message.buff, "messag, %d", count); // пишем в буфер
result = msgsnd(info->msgid, &message, len, IPC_NOWAIT);
if(result == -1){
perror("Send error");
}
sleep(1);
count++;
}
return NULL;
}//write
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_write;
pthread_create(&thread_write, NULL, &write, &info);
getchar();
info.flag = false;
pthread_join(thread_write, NULL);
msgctl(info.msgid, IPC_RMID, NULL);
return 0;
}