操作系统:进程的管道通信实验

#include
#include
#include
#include
#include
int pid1,pid2;

int main()
{
	int fd[2];
	char outpipe[100],inpipe[100];
	while((pid1 = fork()) == -1);
	if(pid1 == 0)
	{
		lockf(fd[1], 1, 0);
		sprintf(outpipe,"\n child process 1 is sending message!\n");
		write(fd[1], outpipe, 50);
		sleep(5);
		lockf(fd[1], 0, 0);
		exit(0);
	}
	else
	{
		while((pid2 = fork()) == -1);
		if(pid2 == 0)
		{
			lockf(fd[1], 1, 0);
			sprintf(outpipe,"\n child process 2 is sending message !\n");
			write(fd[1], outpipe, 50);
			sleep(5);
			lockf(fd[1], 0, 0);
			exit(0);
		}
		else
		{
			wait(0);
			read(fd[0], inpipe, 50);
			printf("%s\n",inpipe);
			wait(0);
			read(fd[0], inpipe, 50);
			printf("%s\n",inpipe);
			exit(0);
		}
	}
}

你可能感兴趣的:(操作系统,Linux/Unix,C/C++)