国庆10.3

完成父子进程的通信,父进程发送—句话后,子进程接收打印然后子进程发送一句话,父进程接收后打印

#include 

int main(int argc, const char *argv[])
{
	int pfd[2] = {0};
	if(pipe(pfd) < 0)
	{
		perror("pipe");
		return -1;
	}
	char buf[128] = "";
	char info_c[]="child message";
	char info_p[]="parent message";

	while(1)
	{
		pid_t cpid = fork();
		if(cpid > 0) 	//父进程
		{                                                             
			close(pfd[0]);  
			write(pfd[1], info_c, sizeof(info_c)); 	//给子进程发送消息
			printf("%s\n",info_c);
			wait(NULL);
			read(pfd[0], buf, sizeof(buf)); 	//接收子进程的消息
			printf("%s\n",buf);
			close(pfd[1]);  
		}
		else if(0 == cpid) 	//子进程
		{
			close(pfd[1]);  
			read(pfd[0], buf, sizeof(buf)); 	//给父进程发送消息
			printf("%s\n",buf);
			write(pfd[1], info_p, sizeof(info_p)); 	//接收父进程的消息
			printf("%s\n",info_p);
			close(pfd[0]);  
		}
		else
		{
			perror("fork");
			return -1;
		}	
	}
	return 0;
}

你可能感兴趣的:(java,前端,javascript)