信号量操作

1.POSIX信号量Q,用信号捕捉ctrl+c,去执行关闭信号量,删除信号

接收端:

#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <signal.h>
#include <fcntl.h>
#include <pthread.h>

//POXIS操作 捕获ctrl+c
sem_t *sem_ret;
//定义函数 写对捕获到的信号进行什么操作 关闭删除信号量

void func(int signim)
{
	int cl_ret = sem_close(sem_ret);
	if(-1==cl_ret)
	{
		perror("close sem fail");
	}
	int ul_ret = sem_unlink("/mysem1");
	if(-1==ul_ret)
	{
		perror("unlink sem fail");
	}
	exit(0);//退出程序
}

int main(int argc, const char *argv[])
{
	//signal信号 收
	//挖坑 即注册信号函数
	signal(SIGINT,func);
	
	//创建有名信号量
	sem_ret = sem_open("/mysem1",O_CREAT,0666,1);
	if(sem_ret == SEM_FAILED)
	{
		perror("sem open fail");
		exit(1);
	}
	printf("ID:%d\n",getpid());	
	int i=0;
	while(1)
	{
		sleep(1);
		printf("%d\n",i);
		i++;
	}
	return 0;
}

发送端:

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

int main()
{
	//kill信号 发
	int a;
	scanf("%d",&a);
	//向指定进程发送指定信号

	int ret = kill((pid_t)a,SIGINT);
	if(-1==ret)
	{
		perror("send sig fail");
	}
	return 0;
}


2.用线程改写jack和rose管道,做到同时双向通信Q

jack_rose:

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

void *pthread(void *arg) 
{
	if (access("./myfifo1",F_OK))
	{
		mkfifo("./myfifo1",0666);
	}

	int fd = open("./myfifo1",O_WRONLY);

	char buf[128];

	while(1)
	{
		memset(buf,0,sizeof(buf));
		printf("说:\n");
		fgets(buf,sizeof(buf),stdin);
		write(fd,buf,strlen(buf));
		if ( strcmp(buf,"bye\n") == 0 )
		{
			close(fd);
			
			break;
		}
	}
	
}

int main()
{
	pthread_t tid;
	
	pthread_create(&tid,NULL,pthread,NULL);
	{
		mkfifo("./myfifo",0666);
	}

	int fd = open("./myfifo",O_RDONLY);

	char buf[128];

	while(1)
	{
		memset(buf,0,sizeof(buf));
		read(fd,buf,sizeof(buf));
		if ( strcmp(buf,"bye\n")==0 )
		{
			printf("ok,bye\n");
			close(fd);
			break;
		}
		printf("听到:%s\n",buf);
	}
	return 0;
}

rose_jack:

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

void *pthread(void *arg) 
{
	if (access("./myfifo1",F_OK))
	{
		mkfifo("./myfifo1",0666);
	}

	int fd = open("./myfifo1",O_RDONLY);

	char buf[128];

	while(1)
	{
		memset(buf,0,sizeof(buf));
		read(fd,buf,sizeof(buf));
		if ( strcmp(buf,"bye\n")==0 )
		{
			printf("ok,bye\n");
			close(fd);
			break;
		}
		printf("听到:%s\n",buf);
	}
	
}


int main()
{
	pthread_t tid;

	pthread_create(&tid,NULL,pthread,NULL);
	if (access("./myfifo",F_OK))
	{
		mkfifo("./myfifo",0666);
	}

	int fd = open("./myfifo",O_WRONLY);

	char buf[128];

	while(1)
	{
		memset(buf,0,sizeof(buf));
		printf("说:\n");
		fgets(buf,sizeof(buf),stdin);
		write(fd,buf,strlen(buf));
		if ( strcmp(buf,"bye\n") == 0 )
		{
			close(fd);
			break;
		}
	}
	return 0;
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值