0% found this document useful (0 votes)
7 views5 pages

OS LAB Systemcall

The document provides examples of various UNIX file system calls in C, including creat(), open(), close(), read(), write(), dup(), fcntl(), and unlink(). Each example demonstrates the usage of the system call, error handling, and basic file operations. The code snippets illustrate how to create, open, read, write, duplicate, check file modes, and delete files.

Uploaded by

dharineeshmv
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

OS LAB Systemcall

The document provides examples of various UNIX file system calls in C, including creat(), open(), close(), read(), write(), dup(), fcntl(), and unlink(). Each example demonstrates the usage of the system call, error handling, and basic file operations. The code snippets illustrate how to create, open, read, write, duplicate, check file modes, and delete files.

Uploaded by

dharineeshmv
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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;
}

You might also like