⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 piped.c

📁 这是linux下的基础C编程代码。都是已经调试成功的
💻 C
字号:
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <limits.h>

#include "piped.h"

#define PIPE_BUFFER_SIZE 128

int collection_pipe[2],guard_control_pipe[2],gate_control_pipe[2];

char collection_buf[PIPE_BUFFER_SIZE],guard_control_buf[PIPE_BUFFER_SIZE],gate_control_buf[PIPE_BUFFER_SIZE];

pid_t collection_tenor,guard_control_tenor,gate_control_tenor;


void pipedSetTaskFunc(void (*task)(int len))
{

}

int pipedInit()
{
    int r_num;

	memset(collection_buf,0,sizeof(collection_buf));
	memset(guard_control_buf,0,sizeof(guard_control_buf));
	memset(gate_control_buf,0,sizeof(gate_control_buf));

	if(pipe(collection_pipe)<0)
	{
		printf("creat collection_pipe error\n");
		return 0;
	}

	if(pipe(guard_control_pipe)<0)
	{
		printf("creat guard_control_pipe error\n");
		return 0;
	}

	if(pipe(gate_control_pipe)<0)
	{
		printf("creat gate_control_pipe error\n");
		return 0;
	}


	if((collection_tenor=fork())==0)
	{
		close(collection_pipe[1]);
		sleep(2);

		while((r_num=read(collection_pipe[0],collection_buf,PIPE_BUFFER_SIZE))>0)
		{   
			if(!fork())
			{   
	           printf("father %d child %d read %d numbers from pipe is %s\n",getppid(),getpid(),r_num,collection_buf);
			   exit(0);
			}
        }
	}

	else if((guard_control_tenor=fork())==0)
	{
		close(guard_control_pipe[1]);
		sleep(2);

		while((r_num=read(guard_control_pipe[0],guard_control_buf,PIPE_BUFFER_SIZE))>0)
		{    
			if(!fork())
			{   
	           printf("father %d child %d read %d numbers from pipe is %s\n",getppid(),getpid(),r_num,guard_control_buf);
			   exit(0);
			}
        }
	}

	else if((gate_control_tenor=fork())==0)
	{
		close(gate_control_pipe[1]);
		sleep(2);

	    while((r_num=read(gate_control_pipe[0],gate_control_buf,PIPE_BUFFER_SIZE))>0)
		{    
			if(!fork())
			{   
	           printf("father %d child %d read %d numbers from pipe is %s\n",getppid(),getpid(),r_num,gate_control_buf);
			   exit(0);
			}
        }
	}

	return 1;
}

void write_pipe(char *buffer)
{
	    int len;
        
	    close(collection_pipe[0]);
		close(guard_control_pipe[0]);
		close(gate_control_pipe[0]);

		len=strlen(buffer);
		
	    if(write(collection_pipe[1],buffer,len)!=-1)
		{
		    printf("write data to pipe success\n");
		}

	    if(write(guard_control_pipe[1],buffer,len)!=-1)
		{
		    printf("write data to pipe success\n");
		}

	    if(write(gate_control_pipe[1],buffer,len)!=-1)
		{
		    printf("write data to pipe success\n");
		}
}

/*int main()
{
	char buffer[100];
	pipedInit();
	while(1)
	{
		scanf("%s",buffer);
		write_pipe(buffer);
	}

}*/



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -