Скачиваний:
2
Добавлен:
08.08.2022
Размер:
1.11 Кб
Скачать
#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;
}
Соседние файлы в папке Lab8_Oreshchenko
  • #
    08.08.20220 б3file.txt
  • #
    08.08.202217.9 Кб2main
  • #
    08.08.20221.21 Кб2main.cpp
  • #
    08.08.20224.63 Кб2main.o
  • #
    08.08.202217.99 Кб2main2
  • #
    08.08.20221.11 Кб2main2.cpp
  • #
    08.08.20224.8 Кб2main2.o