信号通信与线程练习

1、捕获control+c信号,进行处理posix信号量

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fcntl.h>
#include<sys/sem.h>
#include<sys/shm.h>
#include<sys/ipc.h>
#include<sys/types.h>
#include<signal.h>
#include<semaphore.h>
#include<sys/stat.h>


sem_t *space=NULL;
sem_t *data=NULL;


void func(int num)
{
	//关闭posix信号量
	if(-1 == sem_close(space))
	{
		perror("close space failed:");
		exit(-1);
	}
	int cd = sem_close(data);
	if(-1 == cd)
	{
		perror("close data failed");
		exit(-1);
	}
	//删除posix信号量
	int us = sem_unlink("/space");
	if(-1 == us)
	{
		perror("unlink data failed");
		exit(-1);
	}
	int ud = sem_unlink("/data");
	if(-1 == ud)
	{
		perror("unlink data failed");
		exit(-1);
	}
	//退出程序
	exit(0);
}


int main()
{

	signal(SIGINT, func);
	//创建一个key值
	key_t key = ftok("./",0); 
	//创建一个共享空间
	int shm_id = shmget(key,1024, IPC_CREAT | 0666);
	if(-1 == shm_id)
	{
		perror("create share memory failed:");
		return -1;
	}
	//映射共享空间
	char *p = shmat(shm_id, NULL, 0);
	if(NULL == p)
	{
		perror("map share memory failed.");
		return -1;
	}
	//创建信号量	
	space = sem_open("/space",O_CREAT, 0777, 1);
	data = sem_open("/data",O_CREAT, 0777, 0);
	//写入共享内存
	while(1)

	{
		char *msg = "1234567890";
		for(int i=0; i<10; i++)
		{
			sem_wait(space);
			memcpy(p, msg+i, 1);
			sem_post(data);
		}
	}
	
}

2、通过线程与有名管道进行两个进程间的同时读写
ject的进程

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>

#include<pthread.h>
#include<fcntl.h>
#include<unistd.h>


void *j_to_r(void *);
void *r_to_j(void *);
	

int main()
{
	//创建线程
	pthread_t tid_w;
	pthread_t tid_r;
	int pthread_ret = pthread_create(&tid_r, NULL, j_to_r, NULL);
	if(pthread_ret)
	{
		perror("create write thread failed:");
		return -1;
	}
	pthread_ret = pthread_create(&tid_w, NULL, r_to_j, NULL);
	if(pthread_ret)
	{
		perror("create read thread failed:");
		return -1;
	}
	int w_ret = pthread_join(tid_w,NULL);
	int r_ret = pthread_join(tid_r,NULL);
	return 0;
}


void *r_to_j(void *argv)
{
	//创建管道
	int fifo_w = mkfifo("./r_to_j_fifo", 0777);
	//打开管道文件
	int fifo_fd = open("./r_to_j_fifo", O_WRONLY);
	//写入数据
	char *buf = malloc(101);
	while(1)
	{
		memset(buf, 0, 101);
		fgets(buf,100,stdin);
		write(fifo_fd, buf, strlen(buf));
		if(strcmp(buf,"bey\n") == 0)
			break;
	}
	return NULL;
}

void *j_to_r(void *argv)
{
	//创建管道
	int fifo_r = mkfifo("./j_to_r_fifo", 0777);
	//打开管道文件
	int fifo_fd = open("./j_to_r_fifo", O_RDONLY);
	//写入数据
	char *buf = malloc(101);
	while(1)
	{
		memset(buf, 0, 101);
		int n = read(fifo_fd, buf, 100);
		printf("%s",buf);
		if(strcmp(buf,"bey\n") == 0)
			break;
	}
	return NULL;
}

rose的进程

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>

#include<pthread.h>
#include<fcntl.h>
#include<unistd.h>


void *j_to_r(void *);
void *r_to_j(void *);
	

int main()
{
	//创建线程
	pthread_t tid_w;
	pthread_t tid_r;
	int pthread_ret = pthread_create(&tid_w, NULL, j_to_r, NULL);
	if(pthread_ret)
	{
		perror("create write thread failed:");
		return -1;
	}
	pthread_ret = pthread_create(&tid_r, NULL, r_to_j, NULL);
	if(pthread_ret)
	{
		perror("create read thread failed:");
		return -1;
	}
	int w_ret = pthread_join(tid_w,NULL);
	int r_ret = pthread_join(tid_r,NULL);
	return 0;
}


void *j_to_r(void *argv)
{
	//创建管道
	int fifo_w = mkfifo("./j_to_r_fifo", 0777);
	//打开管道文件
	int fifo_fd = open("./j_to_r_fifo", O_WRONLY);
	//写入数据
	char *buf = malloc(101);
	while(1)
	{
		memset(buf, 0, 101);
		fgets(buf,100,stdin);
		write(fifo_fd, buf, strlen(buf));
		if(strcmp(buf,"bey\n") == 0)
			break;
	}
	return NULL;
}


void *r_to_j(void *argv)
{
	//创建管道
	int fifo_r = mkfifo("./r_to_j_fifo", 0777);
	//打开管道文件
	int fifo_fd = open("./r_to_j_fifo", O_RDONLY);
	//写入数据
	char *buf = malloc(101);
	while(1)
	{
		memset(buf, 0, 101);
		read(fifo_fd, buf, 100);
		printf("%s",buf);
		if(strcmp(buf,"bey\n") == 0)
			break;
	}
	return NULL;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值