Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi i have a C programming question could you help me Write a program that veri e

ID: 3736017 • Letter: H

Question

Hi i have a C programming question could you help me

Write a program that veri es that a parent and child process share the same le pointer
and le pointer o set. Here are the steps you need to perform:
The parent should open a text le and fork a child process.
The child process should read from the text le and display what it has read. Use the
read() system call to read. use the open() system call to open a le. Do man 2 open"
to see the use of open().
When the child terminates, the parent process should then read from the same le and
display what it has read. At this stage, you may need to use the sleep system call to
synchronize (e.g, to make the child read before the parent) le access between the parent
and child processes.

Explanation / Answer

#include<stdio.h>
#include<sys/types.h>
#include<fcntl.h>


int main(){

   pid_t id;
   int fd;

   fd = open("Input179.txt", O_RDONLY);
   if (fd < 0){
      printf("Error opening file ");
      return 0;
   }
   id = fork();
   if (id == 0){
      printf("Child: ");
      char buffer[100];
      read(fd,buffer,100);
      printf("%s ",buffer);
     
   }
   else {
       sleep(5);
       printf("Parent: ");
       char buffer1[100];
       read(fd,buffer1,100);
       printf("%s ",buffer1);
       close(fd);
   }
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote