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
相关推荐
Rose sait4 小时前
【环境配置】Linux配置虚拟环境pytorch
linux·人工智能·python
叶之香5 小时前
CentOS/RHEL 7、8安装exfat和ntfs文件系统
linux·运维·centos
一世琉璃白_Y5 小时前
pg配置国内数据源安装
linux·python·postgresql·centos
不过普通话一乙不改名5 小时前
Linux 内核开发入门:从环境配置到 Hello World 实战
linux·运维
乖乖是干饭王5 小时前
Linux 内核 Kbuild 中的 ld 调用机制
linux·c·makefile
Trouvaille ~5 小时前
【Linux】理解“一切皆文件“与缓冲区机制:Linux文件系统的设计哲学
linux·运维·服务器·操作系统·进程·文件·缓冲区
ITKEY_5 小时前
archlinux 通过wpa_supplicant 连接wifi固定ip设置方法
linux·固定ip
幸存者letp6 小时前
Python 常用方法分类大全
linux·服务器·python
知识分享小能手7 小时前
Ubuntu入门学习教程,从入门到精通,Linux操作系统概述(1)
linux·学习·ubuntu