diff --git a/README.md b/README.md index d38d99b5..a9b29f68 100644 --- a/README.md +++ b/README.md @@ -22,17 +22,134 @@ Execute the C Program for the desired output. ## 1.To Write a C program that illustrates files copying +~~~ +#include +#include +#include +#include +#include + +int main(int argc, char *argv[]) { + if (argc != 3) { + fprintf(stderr, "Usage: %s \n", argv[0]); + exit(EXIT_FAILURE); + } + + char block[1024]; + int in, out; + ssize_t nread; + + // Open source file + in = open(argv[1], O_RDONLY); + if (in == -1) { + perror("Error opening source file"); + exit(EXIT_FAILURE); + } + + // Open destination file + out = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); + if (out == -1) { + perror("Error opening destination file"); + close(in); + exit(EXIT_FAILURE); + } + + // Copy contents + while ((nread = read(in, block, sizeof(block))) > 0) { + if (write(out, block, nread) != nread) { + perror("Error writing to destination file"); + close(in); + close(out); + exit(EXIT_FAILURE); + } + } + + if (nread == -1) { + perror("Error reading source file"); + } + + close(in); + close(out); + return EXIT_SUCCESS; +} + +~~~ ## 2.To Write a C program that illustrates files locking - - +~~~ +#include +#include +#include +#include +#include + +void display_lslocks() { + printf("\nCurrent `lslocks` output:\n"); + fflush(stdout); + system("lslocks"); +} + +int main(int argc, char *argv[]) { + if (argc < 2) { + fprintf(stderr, "Usage: %s \n", argv[0]); + exit(EXIT_FAILURE); + } + + char *file = argv[1]; + int fd; + + printf("Opening %s\n", file); + + fd = open(file, O_WRONLY); + if (fd == -1) { + perror("Error opening file"); + exit(EXIT_FAILURE); + } + + // Acquire shared lock + if (flock(fd, LOCK_SH) == -1) { + perror("Error acquiring shared lock"); + close(fd); + exit(EXIT_FAILURE); + } + printf("Acquired shared lock using flock\n"); + display_lslocks(); + + sleep(1); // Simulate waiting before upgrading + + // Try to upgrade to exclusive lock (non-blocking) + if (flock(fd, LOCK_EX | LOCK_NB) == -1) { + perror("Error upgrading to exclusive lock"); + flock(fd, LOCK_UN); // Release shared lock if upgrade fails + close(fd); + exit(EXIT_FAILURE); + } + printf("Acquired exclusive lock using flock\n"); + display_lslocks(); + + sleep(1); // Simulate waiting before unlocking + + // Release lock + if (flock(fd, LOCK_UN) == -1) { + perror("Error unlocking"); + close(fd); + exit(EXIT_FAILURE); + } + printf("Unlocked\n"); + display_lslocks(); + + close(fd); + return 0; +} +~~~ ## OUTPUT +Screenshot 2026-03-17 222345