*SYSTEM CALLS PROGRAMS*
Write()
#include<unistd.h>
int main()
{
write(1,”Hello”,5)
}
read()
#include<unistd.h>
int main()
{
Char b[30];
read(0,b,10);
write(1,b,10);
}
Open
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
int main() {
int fd;
char *filename = "example.txt";
// Open the file for reading and writing, create if it doesn't exist
fd = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
printf("File opened successfully!\n");
// Close the file
close(fd);
printf("File closed successfully!\n");
return 0;
}
lseek
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
int main() {
int fd;
off_t offset;
// Open the file for reading
fd = open("example.txt", O_RDONLY);
// Move the file offset to a specific position (e.g., 100 bytes from the beginning)
offset = lseek(fd, 100, SEEK_SET);
// Close the file
close(fd);
return 0;
}
stat. This information includes details such as file size, permissions, inode number, timestamps, and more.
#include <stdio.h>
#include <sys/stat.h>
int main() {
const char *filename = "example.txt";
struct stat file_stat;
// Call the stat system call to retrieve information about the file
stat(filename, &file_stat);
// Display file information
printf("File Size: %ld bytes\n", file_stat.st_size);
printf("File Permissions: %o\n", file_stat.st_mode & 0777);
printf("Inode Number: %ld\n", file_stat.st_ino);
return 0;
}
Open the current directory, Read directory entries
#include <stdio.h>
#include <dirent.h>
int main() {
DIR *dir;
struct dirent *entry;
// Open the current directory
dir = opendir(".");
if (dir == NULL) {
perror("opendir");
return 1;
}
// Read directory entries
while ((entry = readdir(dir)) != NULL) {
printf("%s\n", entry->d_name);
}
// Close the directory
closedir(dir);
return 0;
}