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
相关推荐
多弗朗皮卡丘9 小时前
网络基础:数据链路层与其他协议或技术
linux·网络·tcp/ip
dingdingfish15 小时前
TLPI 第52章 读书笔记:POSIX Message Queues
linux·mq·ipc·posix·message·tlpi
wacsii16 小时前
【Linux进程(五)】进程控制全解析:从创建、终止、等待到程序替换
linux·服务器
七夜zippoe17 小时前
Nexent Linux 部署:用 deploy.sh
linux·服务器·docker·部署·ai智能体·nexent
去码头整点薯条9817 小时前
NFS服务器
linux·运维·服务器
蜗牛一步步往上爬18 小时前
第一章 初识Linux shell
linux·运维·服务器
拂拉氏18 小时前
【知识讲解】 Linux线程中锁的认识
linux·互斥锁
无足鸟ICT18 小时前
【RHCA+】系统调优配置文件
linux·服务器
时间的拾荒人20 小时前
Shell 脚本核心语法详解(下篇):流程控制、函数、重定向与文件包含
linux·运维·服务器·bash·脚本编程