#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

main()
{
	pid_t pid;
	int fd;
	char buf[40];
	if((fd=open("cooperate",O_CREAT|O_RDWR|O_TRUNC,0644))<0)
	{
		perror("open");
		exit(1);
	}
	pid=fork();
	if(pid<0)
	{
		perror("fork");
		exit(1);
	}
	else if(pid==0)
	{
		printf("Child process PID=%d\n",getpid());
		if(write(fd,"child process say: hello\n",25)<0)
		{
			perror("child");
			exit(1);
		}
		close(fd);
	}
	else
	{
		sleep(2);
		printf("parent process PID=%d",getpid());
		if(write(fd,"parent process say: bye\n",24)<0)
		{
			perror("parent");
			exit(1);
		}
		close(fd);
		wait(NULL);
	}
}
