C语言有名管道fifo的创建及运用

三个文件(fifo.c,write.c,read.c)
fifo.c用来创建有名管道fifo(最先编译)
write.c用来向管道写入数据

read.c用来从管道读出数据

==================================================
//文件1:fifo.c

#include <stdio.h>
#include <string.h>
#include <error.h>
#include <fcntl.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
	int result;
	if(argc!=2)			//判断是否有两个参数
	{
		printf("input error\n");
		exit(1);
	}
	else 
	{		
		result=access(argv[1],F_OK);//判断该文件是否存在
		if(result==0)				//存在
		{
			unlink(argv[1]);		//删除同名文件
		}
	}
	int ret;
	ret=mkfifo(argv[1],0644);		//创建有名管道
	if(ret!=0)
	{
		perror("mkfifo error");
		exit(1);
	}
}

==================================================
//文件2:write.c

#include <stdio.h>
#include <fcntl.h>
#include <error.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 128
int main()
{
	char buffer[SIZE];
	int write_fd;
	int ret;
	int len;
	do
	{
		write_fd=open("./swap",O_WRONLY);//打开管道(只写)
		if(write_fd<0)
		{
			perror("open error");
			exit(1);
		}
		printf("input data\n");
		fgets(buffer,SIZE,stdin);
		len=strlen(buffer);
		buffer[len-1]='\0';
		ret=write(write_fd,buffer,SIZE);
		if(ret==-1)
		{
			perror("write error");
			exit(1);
		}
		printf("send success\n");
		close(write_fd);
	}while(strcmp(buffer,"exit")!=0);
	return 0;
}

==================================================
//文件3:read.c

#include <stdio.h>
#include <fcntl.h>
#include <error.h>
#include <stdlib.h>
#define SIZE 128
int main()
{
	char buffer[SIZE];
	int read_fd;
	int ret;
	do
	{
		read_fd=open("./swap",O_RDONLY);
		if(read_fd<0)
		{
			perror("open error");
			exit(1);
		}
		ret=read(read_fd,buffer,SIZE);
		if(ret==-1)
		{
			perror("read error");
			exit(1);
		}
		printf("the data is:\n");
		printf("%s\n",buffer);
		close(read_fd);
	}while(strcmp(buffer,"exit")!=0);
	return 0;
}
编译命令


gcc fifo.c -o f
./f swap
gcc read.c -o r
gcc write.c -o w


  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
#### 介绍 基于Cython的快速国密算法Python实现,目前支持SM2, SM3, SM4(ECB、CBC) #### 安装教程 ``` pip install fastgm ``` #### 使用说明 ##### SM2 SM2是国家密码管理局发布的椭圆曲线公钥密码算法。对标RSA 使用方法: + 生成秘钥 ``` from fastgm import SM2 sk, pk = SM2.generate_key() # sk为私钥,pk为公钥,均为hex格式 ``` + 使用公钥加密 ``` from fastgm import SM2 pk = 'B9C9A6E04E9C91F7BA880429273747D7EF5DDEB0BB2FF6317EB00BEF331A83081A6994B8993F3F5D6EADDDB81872266C87C018FB4162F5AF347B483E24620207' # 公钥,Hex格式 data = b'helloworld' # 待加密内容,格式为bytes数组 sm2 = SM2() # 初始化 enc = sm2.encrypt(pk, data) # 运行加密算法 ``` + 使用私钥解密 ``` from fastgm import SM2 sk = '00B9AB0B828FF68872F21A837FC303668428DEA11DCD1B24429D0C99E24EED83D5' # 私钥 sm2 = SM2() # 初始化 dec = sm2.decrypt(sk, enc) # 运行解密算法,enc为加密内容 ``` #### SM3 SM3密码杂凑算法是中国国家密码管理局2010年公布的中国商用密码杂凑算法标准。对标MD5 使用方法: ``` from fastgm import SM3 data = b'helloworld' # 待哈希bytes数组 h = SM3().hash(data) # 哈希运算 ``` #### SM4 SM4.0(原名SMS4.0)是中华人民共和国政府采用的一种分组密码标准。对标AES。 -------- 该资源内项目源码是个人的毕设,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! <项目介绍> 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 --------

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值