OS LAB
UNIX FILES SYSTEM CALLS
NAME : Dharineesh M V
[Link] : [Link].U4ELC24105
1. creat() System Call
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
int main() {
int fd;
fd = creat("[Link]", 0777);
if (fd == -1)
printf("File creation failed\n");
else
printf("File created successfully, FD = %d\n", fd);
close(fd);
return 0;
}
2. open() System Call
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
int main() {
int fd;
fd = open("[Link]", O_RDONLY | O_CREAT, 0777);
if (fd < 0)
printf("Open failed\n");
else
printf("File opened successfully, FD = %d\n", fd);
close(fd);
return 0;
}
3. close() System Call
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
int main() {
int fd, ret;
fd = open("[Link]", O_CREAT | O_RDONLY, 0777);
ret = close(fd);
printf("Close return value = %d\n", ret);
return 0;
}
4. read() System Call
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
int main() {
int fd;
char c;
fd = open("[Link]", O_RDONLY);
read(fd, &c, 1);
printf("Character read = %c\n", c);
close(fd);
return 0;
}
5. write() System Call
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
int main() {
int fd;
char buf[] = "Hello";
fd = creat("[Link]", 0777);
write(fd, buf, sizeof(buf));
printf("Data written successfully\n");
close(fd);
return 0;
6. dup() System Call
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
int main() {
int fd1, fd2;
fd1 = open("[Link]", O_CREAT | O_RDONLY, 0777);
fd2 = dup(fd1);
printf("Original FD = %d, Duplicated FD = %d\n", fd1, fd2);
return 0;
7. fcntl() System Call
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
int main() {
int fd, mode;
fd = open("[Link]", O_RDONLY | O_CREAT, 0777);
mode = fcntl(fd, F_GETFL);
if (mode & O_RDONLY)
printf("File opened in READ ONLY mode\n");
close(fd);
return 0;
8. unlink() System Call
#include <stdio.h>
#include <unistd.h>
int main() {
int ret;
ret = unlink("[Link]");
if (ret == 0)
printf("File deleted successfully\n");
else
printf("File deletion failed\n");
return 0;
}