简单的实现,在无名管道里父子进程间的通信(大小写的转换)

#include 
#include 
#include 
#include 
#include 


int main()
{
	int fd[2];
	int fd1[2];
	pid_t pid;
	char r_buf[100];
	char w_buf[100];

	int i;

	if(pipe(fd) < 0)
	{
		perror("pipe error!\n");
		exit(-1);
	}
	if(pipe(fd1) < 0)
	{
		perror("pipe error!\n");
		exit(-1);
	}
	
	memset(r_buf,0,sizeof(r_buf));
	memset(w_buf,0,sizeof(w_buf));

	pid = fork();

	if(pid == 0)
	{
		close(fd[1]);
		close(fd1[0]);
		
		sleep(10);
		read(fd[0],r_buf,sizeof(r_buf));
		printf("get string from father:%s\n",r_buf);

		for(i = 0; i < strlen(r_buf); i++)
		{
			if(r_buf[i] >= 'A' && r_buf[i] <= 'Z')
			{
				r_buf[i] = r_buf[i] - '0'+ 32 + '0';
			}
		}

		write(fd1[1],r_buf,strlen(r_buf)+1);
		printf("wait father read!\n");

		close(fd1[1]);
		close(fd[0]);
		exit(-1);

	}
	else if(pid > 0)
	{
		close(fd[0]);
		close(fd1[1]);
		printf("please enter string:");
		scanf("%s",w_buf);

		write(fd[1],w_buf,strlen(w_buf)+1);
		printf("wait son read!\n");
		sleep(15);

		memset(w_buf,0,sizeof(w_buf));
		read(fd1[0],w_buf,sizeof(w_buf));
		printf("get string from son:%s\n",w_buf);
		
		close(fd1[0]);
		close(fd[1]);
	}

    return 0;
}

你可能感兴趣的:(简单的实现,在无名管道里父子进程间的通信(大小写的转换))