From 90740df1cd2d97c6c6165ea47c3cd5390765a3c3 Mon Sep 17 00:00:00 2001 From: Saranya <144870813+Saranyaaav@users.noreply.github.com> Date: Sun, 12 May 2024 20:52:10 +0530 Subject: [PATCH] Update README.md --- README.md | 111 ++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 100 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index d38d99b..2f63316 100644 --- a/README.md +++ b/README.md @@ -20,23 +20,112 @@ Execute the C Program for the desired output. # PROGRAM: ## 1.To Write a C program that illustrates files copying - - - - - - +``` +#include +#include +#include +#include +#include // Include for debugging + +int main() { + char block[1024]; + int in, out; + int nread; + + // Open input file + in = open("filecopy.c", O_RDONLY); + if (in == -1) { + perror("Error opening input file"); + exit(1); + } + printf("Input file opened successfully\n"); + + // Open or create output file + out = open("file.out", O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR); + if (out == -1) { + perror("Error opening/creating output file"); + exit(1); + } + printf("Output file opened/created successfully\n"); + + // Read from input file and write to output file + while ((nread = read(in, block, sizeof(block))) > 0) { + printf("Read %d bytes from input file\n", nread); + if (write(out, block, nread) != nread) { + perror("Error writing to output file"); + exit(1); + } + printf("Wrote %d bytes to output file\n", nread); + } + + if (nread == -1) { + perror("Error reading from input file"); + exit(1); + } + + printf("End of input file\n"); + + // Close files + close(in); + close(out); + + printf("Files closed successfully\n"); + + exit(0); +} +``` ## 2.To Write a C program that illustrates files locking - - - +``` +//C program that illustrates files locking goes here +#include +#include +#include +#include +#include +int main (int argc, char* argv[]) +{ char* file = argv[1]; +int fd; +struct flock lock; +printf ("opening %s\n", file); +/* Open a file descriptor to the file. */ +fd = open (file, O_WRONLY); +// acquire shared lock +if (flock(fd, LOCK_SH) == -1) { +printf("error"); +}else +{printf("Acquiring shared lock using flock"); +} +getchar(); +// non-atomically upgrade to exclusive lock +// do it in non-blocking mode, i.e. fail if can't upgrade immediately +if (flock(fd, LOCK_EX | LOCK_NB) == -1) { +printf("error"); +}else +{printf("Acquiring exclusive lock using flock");} +getchar(); +// release lock +// lock is also released automatically when close() is called or process exits +if (flock(fd, LOCK_UN) == -1) { +printf("error"); +}else{ +printf("unlocking"); +} +getchar(); +close (fd); +return 0; +} +``` ## OUTPUT +### FILECOPYING +![Screenshot from 2024-05-07 13-18-53](https://github.com/gowriganeshns/Linux-File-IO-Systems-locking/assets/144870813/e540eaf9-f56a-44a0-bfc8-cb81948f6d75) +![Screenshot from 2024-05-07 13-22-21](https://github.com/gowriganeshns/Linux-File-IO-Systems-locking/assets/144870813/08c8e46d-afc4-4763-afca-01def1469d8e) +![Screenshot from 2024-05-07 13-23-08](https://github.com/gowriganeshns/Linux-File-IO-Systems-locking/assets/144870813/ca463491-3e57-4d3e-bf02-1487f7e7f4a2) - - +### FILELOCKING +![Screenshot from 2024-05-07 13-27-18](https://github.com/gowriganeshns/Linux-File-IO-Systems-locking/assets/144870813/036e0d2c-837b-4a91-841f-eff9d646d9b7) # RESULT: The programs are executed successfully.