Skip to content

Commit

Permalink
Add reader writer process demo
Browse files Browse the repository at this point in the history
  • Loading branch information
unmeshvrije committed Feb 4, 2025
1 parent b96f7ca commit 1520e0e
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
*.out
*.bin
*.exe
*.txt
ashwathama
bash_gitignore.sh
endian
garibsh
ior
myps
pid
reader
run_writer_readers.sh
sync
threads
writer
1 change: 1 addition & 0 deletions ext_ignore.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
*.out
*.bin
*.exe
*.txt
40 changes: 40 additions & 0 deletions reader.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/uio.h>
#include<fcntl.h>
#include<unistd.h>

int main(void) {

int fd;
int bytes_read;
char buf[51200];
fd = open("./delme.txt", O_RDONLY);

printf("fd = %d\n", fd);
bytes_read = read(fd, buf, sizeof(buf));
printf("%d bytes read: %s\n", bytes_read, buf);
printf("________\n");

// 1 sec = 1000000
//usleep(500000);
sleep(5);
bytes_read = read(fd, buf, sizeof(buf));
printf("%d bytes read: %s\n", bytes_read, buf);
bytes_read = read(fd, buf, sizeof(buf));
printf("%d bytes read: %s\n", bytes_read, buf);
printf("________\n");
bytes_read = read(fd, buf, sizeof(buf));
printf("%d bytes read: %s\n", bytes_read, buf);
bytes_read = read(fd, buf, sizeof(buf));
printf("%d bytes read: %s\n", bytes_read, buf);
printf("________\n");
bytes_read = read(fd, buf, sizeof(buf));
printf("%d bytes read: %s\n", bytes_read, buf);
bytes_read = read(fd, buf, sizeof(buf));
printf("%d bytes read: %s\n", bytes_read, buf);
printf("________\n");
bytes_read = read(fd, buf, sizeof(buf));
printf("%d bytes read: %s\n", bytes_read, buf);
}
36 changes: 36 additions & 0 deletions writer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>

int main(void) {
int fd, i;
int bytes_written;
char buf[51200];
for (i = 0; i < sizeof(buf); i++) {
buf[i] = 'a';
}
fd = open("./delme.txt", O_WRONLY | O_CREAT, 0777);
bytes_written = write(fd, buf, sizeof(buf));
printf("%d bytes written\n", bytes_written);
printf("_______\n");
bytes_written = write(fd, buf, sizeof(buf));
printf("%d bytes written\n", bytes_written);
bytes_written = write(fd, buf, sizeof(buf));
printf("%d bytes written\n", bytes_written);
printf("_______\n");
bytes_written = write(fd, buf, sizeof(buf));
printf("%d bytes written\n", bytes_written);
bytes_written = write(fd, buf, sizeof(buf));
printf("%d bytes written\n", bytes_written);
printf("_______\n");
bytes_written = write(fd, buf, sizeof(buf));
printf("%d bytes written\n", bytes_written);
bytes_written = write(fd, buf, sizeof(buf));
printf("%d bytes written\n", bytes_written);
printf("_______\n");
bytes_written = write(fd, buf, sizeof(buf));
printf("%d bytes written\n", bytes_written);
close(fd);
}

0 comments on commit 1520e0e

Please sign in to comment.