Linux C实现父进程写,子进程读操作

#include 
#include 
#include 
#include 
#include 
#include 
#include 

int main(void)
{
	int fd = open("temp", O_CREAT | O_RDWR, 0644);
	if(fd == -1)
	{
		perror("open error");
		exit(1);
	}

	pid_t pid = fork();
	if(pid == -1)
	{
		perror("fork error");
		exit(1);
	}

	if(pid > 0)
	{
		char *p = "test123456789";
		write(fd, p, strlen(p)+1);
		close(fd);
	}

	if(pid == 0)
	{
		sleep(1);
		char buf[1024];
		lseek(fd, 0, SEEK_SET);
		int len = read(fd, buf, sizeof(buf));
		printf("%s\n", buf);
		close(fd);
	}


}

 

你可能感兴趣的:(算法)