Skip to content

Commit

Permalink
Create reader_writer_process.c - atharv
Browse files Browse the repository at this point in the history
  • Loading branch information
atharv020304 authored Feb 4, 2025
1 parent cc28f4b commit b96f7ca
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions reader_writer_process.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>

#define BUF_SIZE 512

void process_A() {
int fd;
char buf[BUF_SIZE];

fd = open("testfile.txt", O_RDONLY);
if (fd < 0) {
perror("Error opening file in A");
exit(1);
}

read(fd, buf, sizeof(buf));
printf("Process A First read: %s\n", buf);

read(fd, buf, sizeof(buf));
printf("Process A Second read: %s\n", buf);

close(fd);
}

void process_B() {
int fd;
char buf[BUF_SIZE];


for (int i = 0; i < sizeof(buf); i++) {
buf[i] = 'a';
}


fd = open("testfile.txt", O_WRONLY | O_CREAT, 0644);
if (fd < 0) {
perror("Error opening file in B");
exit(1);
}


write(fd, buf, sizeof(buf));
printf("Process B First write done\n");

write(fd, buf, sizeof(buf));
printf("Process B Second write done\n");

close(fd);
}

int main() {
int pid = fork();

if (pid == 0) {
process_A();
} else if (pid > 0) {
process_B();
} else {
perror("Fork failed");
exit(1);
}

return 0;
}

0 comments on commit b96f7ca

Please sign in to comment.