#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h> // For file control options
int main(int argc, char **argv) {
printf("About to fork\n");
int pid = fork();
if (pid > 0) {
// Parent process
printf("parent: writing\n");
// Open a temporary file
int fd = open("tempfile.txt", O_WRONLY | O_CREAT, 0666);
write(fd, "hello", 6);
close(fd);
int child_status;
printf("parent: waiting\n");
wait(&child_status);
printf("parent: done\n");
} else {
// Child process
printf("child: read\n");
sleep(1); // Wait to ensure parent has written the message
// Open the file for reading
int fd = open("tempfile.txt", O_RDONLY);
char buf[1025];
int n = read(fd, buf, 1024);
if (n >= 0) {
buf[n] = 0;
printf("child: received %d bytes: \"%s\"\n", n, buf);
} else {
perror("read");
}
close(fd);
}
printf("child: exit\n");
exit(0);
return 0;