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
相关推荐
小王C语言1 天前
Linux基础开发工具----yum、vim和gcc/g++
linux·运维·服务器
_w_z_j_1 天前
Linux----文件系统
linux·运维·服务器
努力努力再努力wz1 天前
【Linux进阶系列】:线程(上)
java·linux·运维·服务器·数据结构·c++·redis
java 乐山1 天前
蓝牙网关(备份)
linux·网络·算法
2301_803554521 天前
面试后查缺补漏--cmake,makefiles,g++,gcc(自写精华版)
linux·运维·服务器
煤球王子1 天前
浅学任务调度
linux
102400241 天前
ubuntu系统中 jupyter Kernel 频繁崩溃原因
linux·运维·服务器
jzhwolp1 天前
nginx epoll 里黑科技位运算+指针复用
linux·nginx·架构
NON-JUDGMENTAL1 天前
在 Ubuntu 上安装 Ollama 并通过 Open WebUI 运行本地大语言模型
linux·ubuntu·语言模型
ZzzZZzzzZZZzzzz…1 天前
RHCSA---权限管理
linux·运维·权限管理·特殊权限·rhcsa·acl权限·权限掩码原理