Скачиваний:
2
Добавлен:
08.08.2022
Размер:
1.21 Кб
Скачать
#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;
}
Соседние файлы в папке Lab8_Oreshchenko
  • #
    08.08.202265 б2compiler1
  • #
    08.08.202269 б2compiler2
  • #
    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