How to Write Content “Hello Process” into File f.txt by Child Process? The parent process is responsible for reading content from f.txt and displaying it after the child process exits.

  linux, question
#include <sys/types.h>
 #include <sys/wait.h>
 #include <unistd.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <stdio.h>
 int main(int argc, char *argv[])
 {
 pid_t  pid;
 printf("before fork\n");
 pid = fork();
 if(pid < 0)
 {
 perror("fork error");
 }
 else if(0 == pid)
 {  //child
 char ch[] = "Hello Process";
 int fd = -1;
 fd = open("f.txt",O_WRONLY);
 while(fd > 0)
 {
 write(fd,&ch,1);
 }
 close(fd);
 }
 else
 { //parent
 char ret[] = "Hello Process";
 int fd = -1;
 fd = open("f.txt",O_RDONLY);
 while(fd > 0)
 {
 read(fd,output,1);
 }
 close(fd);
 }
 printf("pid = %s\n",pid);
 return 0;

Wait for the child process to exit using the parent process waitpid

In addition, change the code format