Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 100 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,112 @@ Execute the C Program for the desired output.
# PROGRAM:

## 1.To Write a C program that illustrates files copying






```
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h> // 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 <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/file.h>
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.