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
70 changes: 66 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,81 @@ Execute the C Program for the desired output.

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

```
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
int main()
{
char block[1024];
int in, out;
int nread;
in = open("filecopy.c", O_RDONLY);
out = open("file.out", O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);
while((nread = read(in,block,sizeof(block))) > 0)
write(out,block,nread);
exit(0);}
```

## 2.To Write a C program that illustrates files locking

```
#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);
fd = open (file, O_WRONLY);
if (flock(fd, LOCK_SH) == -1)
{
printf("error");
}
else
{
printf("Acquiring shared lock using flock");
}
getchar();
if (flock(fd, LOCK_EX | LOCK_NB) == -1)
{
printf("error");
}
else
{
printf("Acquiring exclusive lock using flock");
}
getchar();
if (flock(fd, LOCK_UN) == -1)
{
printf("error");
}
else
{
printf("unlocking");
}
getchar();
close (fd);
return 0;
}
```



## OUTPUT

## 2.To Write a C program that illustrates files locking


### 01

![image](https://github.com/Priyanghaofficial/Linux-File-IO-Systems-locking/assets/147121154/a14ff10d-b16b-4403-ab3a-639485382e02)

## OUTPUT
### 02

![image](https://github.com/Priyanghaofficial/Linux-File-IO-Systems-locking/assets/147121154/6a9674ab-333d-4bb8-be25-8f3e8bef5530)



Expand Down