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
相关推荐
轻松Ai享生活1 天前
5 节课深入学习Linux Cgroups
linux
christine-rr1 天前
linux常用命令(4)——压缩命令
linux·服务器·redis
三坛海会大神5551 天前
LVS与Keepalived详解(二)LVS负载均衡实现实操
linux·负载均衡·lvs
東雪蓮☆1 天前
深入理解 LVS-DR 模式与 Keepalived 高可用集群
linux·运维·服务器·lvs
乌萨奇也要立志学C++1 天前
【Linux】进程概念(二):进程查看与 fork 初探
linux·运维·服务器
獭.獭.1 天前
Linux -- 信号【上】
linux·运维·服务器
hashiqimiya1 天前
centos配置环境变量jdk
linux·运维·centos
hashiqimiya1 天前
权限更改centos中系统文件无法创建文件夹,使用命令让普通用户具备操作文件夹
linux
逆小舟2 天前
【Linux】人事档案——用户及组管理
linux·c++
青草地溪水旁2 天前
pthread_mutex_lock函数深度解析
linux·多线程·pthread