linux c++ 多进程 匿名管道 命名管道 消息队列 信号量 共享内存 内存映射

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdbool.h>
#include <thread>
#include <errno.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/msg.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/mman.h>

//多进程
//匿名管道命名管道
//消息队列
//信号量
//共享内存
//内存映射

struct mymsg
{
	long int msgtype;
	char data[BUFSIZ];
};

union semun
{
	int val;
	struct semid_ds *buf;
	unsigned short int *arry;
	struct seminfo *__buf;
};

struct shmst
{
	char name[20];
	int age;
	char str[100];
};

typedef struct shmst mmapst;

void onexit()
{
	fprintf(stderr, "%s\n", "onexit");
}

void on_exit_cb(int status, void* arg)
{
	fprintf(stderr, "on_exit_cb status = %d arg = %d \n", status, reinterpret_cast<size_t>(arg));
}


const char* buf = "hello ";

int main(int argc, char* argv[])
{
	pid_t pid;
	int fd;

	//----------------------------
	perror(argv[0]);
	//----------------------------

	//----------------------------
	atexit(onexit);
	on_exit(on_exit_cb, (void*)1);
	//----------------------------

	//----------------------------
	fd = open("a.txt", O_RDWR | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
	//----------------------------

	//----------------------------
	//管道
	int _pipe[2] = { 0 };
	int ret = pipe(_pipe);
	if (ret == -1)
	{
		printf("pipe error %d", errno, strerror(errno));

		exit(-1);
	}
	//----------------------------

	//----------------------------
	//消息队列
	std::thread t([] {
		int msgid = msgget(1234, IPC_CREAT | 0666);
		if(msgid == -1)
			perror("msgget");
		struct mymsg msg2 = { 0 };
		while (true)
		{
			ssize_t ret = msgrcv(msgid, &msg2, sizeof(mymsg) - sizeof(long int), 0, 0/*IPC_NOWAIT*/);
			if (ret == -1)
			{
				perror("msgrcv error");
			}
			else if(ret > 0)
			{
				if (strcmp(msg2.data, "end") == 0)
				{
					printf("msgrec msg:%s\n", msg2.data);
					break;
				}
			}
		}

		msgctl(msgid, IPC_RMID, 0);
	});
	
	std::this_thread::sleep_for(std::chrono::seconds(2));
	//----------------------------

	unlink("./testsem.ini");
	if (open("./testsem.ini", O_RDWR | O_CREAT | O_TRUNC, 0666) == -1)
	{
		perror("open");
	}

	//----------------------------
	//信号量
	key_t sem_key = ftok("./testsem.ini", 1);
	perror("ftok");
	int sem_id = semget(sem_key, 1, 0666 | IPC_CREAT);
	if (sem_id == -1)
	{
		perror("semget");
	}

	union semun sem_union;
	sem_union.val = 1;

	if (semctl(sem_id, 0, SETVAL, sem_union) == -1)
	{
		perror("semctl");
	}
	//----------------------------


	//----------------------------
	//共享内存

	int shm_id = shmget(key_t(12345), sizeof(struct shmst), 0666 | IPC_CREAT);
	if (shm_id == -1)
		perror("shmget");

	void* pbuf = shmat(shm_id, 0, 0);
	if (pbuf == NULL)
		perror("shmat");
	else
	{
		struct shmst* pshmst = (struct shmst*)pbuf;
		strcpy(pshmst->name, "lls");
		strcpy(pshmst->str, "hello lls");
		pshmst->age = 27;

		if (shmdt(pbuf) == -1)
			perror("shmdt");
	}
	//----------------------------


	//----------------------------
	//内存映射
	int mmap_fd = open("./mmap.temp", O_CREAT | O_RDWR | O_TRUNC, 0666);
	if (mmap_fd == -1)
		perror("open");

	write(mmap_fd, " ", sizeof(mmapst));//不加这句会报bus error

	mmapst* pmmapbuf = (mmapst*)mmap(NULL, sizeof(mmapst), PROT_READ | PROT_WRITE, MAP_SHARED, mmap_fd, 0);
	if (pmmapbuf == NULL)
		perror("mmap");

	strcpy(pmmapbuf->name, "lls");
	strcpy(pmmapbuf->str, "hello lls");
	pmmapbuf->age = 27;

	if (msync(pmmapbuf, sizeof(mmapst), MS_SYNC) == -1)
		perror("msync");

	if (munmap(pmmapbuf, sizeof(mmapst)) == -1)
		perror("munmap");

	close(mmap_fd);
	//----------------------------

	pid = fork();
	if (pid == -1)
	{		
		printf("%s", "fork error\n");
		exit(EXIT_FAILURE);
	}
	else if (pid == 0)
	{
		//----------------------------
		close(_pipe[0]);
		write(_pipe[1], "abcde", 5);
		//----------------------------

		//----------------------------
		write(fd, buf, strlen(buf));
		close(fd);
		//----------------------------

		if (-1 == execl("/home/lls/projects/testfile/bin/x64/Debug/testfile.out", "lls", "luls", NULL))
		{
			perror("execl error");
			printf("execl error: %d: %s\n", errno, strerror(errno));
		}

		//execlp("ls", "l", NULL);

		//execle("ls", "l", NULL, "/bin");

		//char* const argv[] = {"ls", NULL};
		//execv("/bin/ls", argv);

		//execvp("ls", argv);

		//char* const envp[] = { "/bin", NULL };
		//execve("ls", argv, envp);


		//printf("i'm child child_pid = %d ppid = % d!\n", getpid, getppid);	
		//exit(110);
	}

	//----------------------------
	int s = access("./temp.txt", 0);
	if (s != 0)
	{
		ret = mkfifo("./temp.txt", 0666 | __S_IFIFO);
		if (ret == -1)
		{
			perror("mkfifo error");
		}
	}

	int fifofd = open("./temp.txt", O_RDONLY);
	char* szb[10] = { 0 };
	read(fifofd, szb, 5);
	close(fifofd);

	printf("------------------------\n");
	printf("fifo:\n");
	fprintf(stderr, "read fifo : %s\n", szb);
	printf("------------------------\n");

	//----------------------------


	struct sembuf sem_b;
	sem_b.sem_num = 0;
	sem_b.sem_op = -1;
	sem_b.sem_flg = SEM_UNDO;
	if (semop(sem_id, &sem_b, 1) == -1)
	{
		perror("semop");
	}

	//----------------------------
	close(_pipe[1]);
	char szbuf[10] = { 0 };
	read(_pipe[0], szbuf, 5);

	printf("------------------------\n");
	printf("pipe:\n");
	fprintf(stderr, "read pipe : %s\n", szbuf);
	printf("------------------------\n");
	//----------------------------

	printf("i'm parent pid = %d!\n", getpid());

	//----------------------------
	int status = 0;
	waitpid(pid, &status, 0);
	//----------------------------

	//----------------------------
	lseek(fd, 0, SEEK_SET);
	char buf2[100] = { 0 };
	ssize_t res = read(fd, (void*)buf2, 100);
	(void)res;
	close(fd);
	printf("%s\n", buf2);
	//----------------------------

	//----------------------------
	t.join();
	//----------------------------

	//----------------------------
	//struct sembuf sem_b;
	sem_b.sem_num = 0;
	sem_b.sem_op = 1;
	sem_b.sem_flg = SEM_UNDO;
	if (semop(sem_id, &sem_b, 1) == -1)
	{
		perror("semop");
	}

	if (semctl(sem_id, 0, IPC_RMID, 0) == -1)
		perror("semctl");
	//----------------------------

	//----------------------------
	if (shmctl(shm_id, IPC_RMID, 0) == -1)
	{
		perror("shmctl");
	}
	//----------------------------

    return 0;
}


#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/msg.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/mman.h>

struct mymsg
{
	long int msgtype;
	char data[BUFSIZ];
};

union semun
{
	int val;
	struct semid_ds* buf;
	unsigned short int* array;
	struct seminfo *__buf;
};

typedef struct shmst
{
	char name[20];
	int age;
	char str[100];
}mmapst;

int main(int argc, char* argv[])
{
	key_t sem_key = ftok("./testsem.ini", 1);
	int sem_id = semget(sem_key, 1, 0666 | IPC_CREAT);
	if (sem_id == -1)
	{
		perror("semget");
	}

	/*union semun sem_union;
	sem_union.val = 1;
	if (semctl(sem_id, 0, SETVAL, sem_union) == -1)
	{
		perror("semctl");
	}*/

	struct sembuf sem_bf;
	sem_bf.sem_num = 0;
	sem_bf.sem_op = -1;
	sem_bf.sem_flg = SEM_UNDO;

	if (semop(sem_id, &sem_bf, 1) == -1)
	{
		perror("semop");
	}

	printf("------------------------\n");

	//----------------------------
	//共享内存

	int shm_id = shmget(key_t(12345), sizeof(struct shmst), 0666 | IPC_CREAT);
	if (shm_id == -1)
		perror("shmget");

	void* pbuf = shmat(shm_id, 0, 0);
	if (pbuf == NULL)
		perror("shmat");
	else
	{
		struct shmst* pshmst = (struct shmst*)pbuf;

		printf("------------------------\n");
		printf("shm:\n");
		printf("name:%s\n", pshmst->name);
		printf("str:%s\n", pshmst->str);
		printf("age:%d\n", pshmst->age);
		printf("------------------------\n");

		if (shmdt(pbuf) == -1)
			perror("shmdt");
	}

	//----------------------------

	//----------------------------
	//内存映射
	int mmap_fd = open("./mmap.temp", O_RDONLY);
	if (mmap_fd == -1)
		perror("open");

	mmapst* pmmapbuf = (mmapst*)mmap(NULL, sizeof(mmapst), PROT_READ, MAP_SHARED, mmap_fd, 0);
	if (pmmapbuf == NULL)
		perror("mmap");

	printf("------------------------\n");
	printf("mmap:\n");
	printf("name:%s\n", pmmapbuf->name);
	printf("str:%s\n", pmmapbuf->str);
	printf("age:%d\n", pmmapbuf->age);
	printf("------------------------\n");

	if (munmap(pmmapbuf, sizeof(mmapst)) == -1)
		perror("munmap");

	close(mmap_fd);
	//----------------------------

	key_t key = 1234;
	int msgid = msgget(key, 0666 | IPC_CREAT);
	printf("msgget msgid = %d\n", msgid);

	struct mymsg msg = { 0 };
	msg.msgtype = 1;
	strcpy(msg.data, "end");

	if (-1 == msgsnd(msgid, &msg, sizeof(mymsg) - sizeof(long int), 0))
	{
		perror("msgsend error");
	}

	printf(argv[0]);
	printf("\n");
	printf(argv[1]);
	printf("\n");
    printf("hello from testfile!\n");

	char path[1024] = { 0 };
	getcwd(path, 1024);
	printf(path);
	printf("\n");

	int s = access("/home/lls/projects/testprocess/bin/x64/Debug/temp.txt", 0);
	if (s == 0)
	{
		//unlink("/home/lls/projects/testprocess/bin/x64/Debug/temp.txt");
	}
	else
	{
		int ret = mkfifo("/home/lls/projects/testprocess/bin/x64/Debug/temp.txt", 0666 | __S_IFIFO);
		if (ret == -1)
		{
			perror("mkfifo error");
		}
	}

	int fifofd = open("./temp.txt", O_WRONLY);
	write(fifofd, "edcba", 5);
	close(fifofd);

	printf("------------------------\n");

	sem_bf.sem_num = 0;
	sem_bf.sem_op = 1;
	sem_bf.sem_flg = SEM_UNDO;

	if (semop(sem_id, &sem_bf, 1) == -1)
	{
		perror("semop");
	}


    return 0;
}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 消息队列共享内存信号量Linux 系统中的三种 IPC(进程间通信)机制。 消息队列:用于进程间的异步通信,发送和接收的进程都不需要同时存在。比如:一个进程(发送方)可以将消息发送到消息队列上,而另一个进程(接收方)可以在随后的某个时间接收这个消息。 共享内存:用于进程间的共享数据。多个进程可以访问同一个共享内存区域,而不必复制数据,从而提高了效率。比如:在生产者-消费者问题中,共享内存可以用来存储生产者生产的物品,消费者可以从共享内存获取物品并进行消费。 信号量:用于控制进程对共享资源的访问。信号量一般是一个计数器,用来记录可用资源的数量。进程在访问共享资源之前,必须获取相应的信号量,而在访问完之后,需要释放信号量。比如:在多进程并发访问共享资源时,信号量可以控制每个进程对共享资源的访问顺序,从而避免冲突。 下面是一个使用消息队列的 C 语言示例: ``` #include <sys/msg.h> // 操作消息队列的头文件 struct msgbuf { long mtype; // 消息类型 char mtext[256]; // 消息正文 }; int main() { int msqid; struct msgbuf buf; key_t key; // 创建或获取消息队列的 key key = ftok(".", 'a'); // 创建或获取消息队列 msqid = msgget(key, 0666 | IPC_CREAT); // 发送消息到消息队列 buf.mtype = 1; strcpy(buf.mtext, "Hello, world!"); msgsnd(msqid, &buf, sizeof(buf.mtext), 0); // 从消息队列接收消息 msgrcv(msqid, &buf, sizeof(buf.mtext), 0, 0); printf("Received message: %s\n", buf.mtext); // 删除消息队列 msgctl(msqid, IPC_RMID, NULL); return 0; } ``` 这个程序创建了一个消息队列,向消息队列发送了一条消息,然后从消息队列中接收了一条消息,并最终删除了消息队列

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值