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
相关推荐
s***4538 小时前
Linux 下安装 Golang环境
linux·运维·golang
J***516810 小时前
Linux安装Redis以及Redis三种启动方式
linux·redis·bootstrap
4***175410 小时前
Linux 下安装 Golang环境
linux·运维·golang
Lenyiin10 小时前
《 Linux 修炼全景指南: 七 》 指尖下的利刃:深入理解 Vim 的高效世界
linux·运维·服务器·vim·lenyiin
sulikey12 小时前
Linux基础指令与权限管理深度解析:从入门到精通
linux·运维·服务器·ubuntu·centos·linux命令·linux权限
s***469812 小时前
linux 设置tomcat开机启动
linux·运维·tomcat
xu_yule12 小时前
网络和Linux网络-3(套接字编程)TCP网络通信代码
linux·网络·tcp/ip
HalvmånEver14 小时前
Linux:进程的切换与调度(进程四)
linux·运维·服务器
LCG元14 小时前
终端里的"黑客帝国":htop, glances, bpytop 系统监控工具横评
linux
L***B56814 小时前
如何安装linux版本的node.js
linux·运维·node.js