c语言进程直接的管道

无名管道

c 复制代码
#include<myhead.h>	
int main(int argc, const char *argv[])
{
	int pipfd[2];
	char buff[1024]="hello world";
	char s[1024];
	//创建无名管道
	
	if(pipe(pipfd)==-1){
		perror("pipe");
		return -1;
	}
	int pid=fork();
	if(pid==-1){
		perror("fork");
		return -1;
	}
	if(pid == 0){
		//子读
		close(pipfd[1]);
		sleep(1);
		while(1){
			int res=read(pipfd[0],s,sizeof(s));
			if(strcmp("#",s)==0){
				puts("程序结束");
				return -1;
			}
			printf("父亲数据为:%s\n",s);
		}
		close(pipfd[0]);
	}else if(pid>0){
		//父写
		close(pipfd[0]);
	
		int i=0;
		while(1){
			usleep(100);
			printf("父亲发送数据>>");
		scanf("%s",buff);

			write(pipfd[1],buff,sizeof buff);
					if(strcmp(buff,"#")==0){
			puts("退出");
			break;
		}
		}
		close(pipfd[1]);
		wait(&pid);
	}
	
	return 0;
}  

斗地主改良版

c 复制代码
//斗地主
#include<myhead.h>
//定义信号量
void handler(int sig){
	if(sig==SIGALRM){
		printf("系统自动帮你出牌\n");
		printf("请输入你要出的牌\n");
		alarm(5);
	}
}
int main(int argc, const char *argv[])
{
	//函数绑
	if(signal(SIGALRM,handler)==SIG_ERR){
		perror("signal");
		return -1;
	}
	//出牌
	char ch;
	while(1){
	
		//启动定时器
		alarm(5);
		//输入
		printf("输入你要出的牌");
		scanf("%c",&ch);
		getchar();
		printf("你出的是%c\n",ch);
	}
	
	return 0;
}
ubu
相关推荐
爱丶狸7 小时前
Grafana_Zabbix_ImageRenderer_部署与前端操作手册
linux·前端·zabbix·grafana·kylin
k4m7v2pz8 小时前
把 Bevy 0.14 游戏移植到 R36S 掌机:一场与“无窗口系统“的搏斗
linux·rust·bevy·r36s·rk3326·开源掌机
木子欢儿9 小时前
Intel 架构的 MacBook Pro 运行 Linux 发热量高解决
linux·运维·服务器
会编程的土豆12 小时前
Go 模板初识
linux·运维·服务器
regret~14 小时前
【记录】网络路由、转发与网卡隔离
linux·服务器·网络
aFakeProgramer15 小时前
Linux版本兼容问题及RTA-VRTE环境配置笔记
linux·笔记
Zhu75815 小时前
在Ubuntu24快速配置专用SFTP
linux·sftp
码农客栈15 小时前
Linux 内核顶层 Makefile 详解
linux
Embedded-Xin15 小时前
Rust学习——Cargo工具
linux·学习·架构·rust·嵌入式
weixin_4357761116 小时前
2026年长沙托育vi设计从风格定位到元素运用的设计方法
linux·运维·服务器·python