0% found this document useful (0 votes)
11 views6 pages

Os pgm-2 - r22

The document provides multiple examples of using UNIX/LINUX system calls for file and directory operations, including opening, reading, writing, and closing files, as well as manipulating file descriptors with fcntl, seeking within files, retrieving file information with stat, and reading directory contents with opendir and readdir. Each example includes C code snippets demonstrating the functionality of these system calls. The examples illustrate basic error handling and proper resource management.

Uploaded by

23h51a05v6
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)
11 views6 pages

Os pgm-2 - r22

The document provides multiple examples of using UNIX/LINUX system calls for file and directory operations, including opening, reading, writing, and closing files, as well as manipulating file descriptors with fcntl, seeking within files, retrieving file information with stat, and reading directory contents with opendir and readdir. Each example includes C code snippets demonstrating the functionality of these system calls. The examples illustrate basic error handling and proper resource management.

Uploaded by

23h51a05v6
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
You are on page 1/ 6

Program 2:

Write programs using the I/O system calls of UNIX/LINUX operating system
(open, read, write, close, fcntl, seek, stat, opendir, readdir).

Open:
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

int main() {
// Open the file for writing (create if doesn't exist)
int fd = open("example.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd == -1) {
perror("open error");
return 1;
}

// Write data to the file


const char *text = "Hello, UNIX system calls!";
ssize_t bytes_written = write(fd, text, 24); //SSIZE gives the size of data for
both s and us
if (bytes_written == -1) {
perror("error to write data");
close(fd);
return 1;
}
// Close the file
close(fd);
return 0;
}

2. open(), read(), and close() example:

#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

int main() {
// Open the file for reading
int fd = open("example.txt", O_RDONLY);
if (fd == -1) {
perror("open");
return 1;
}

// Read data from the file


char buffer[256];
ssize_t bytes_read = read(fd, buffer, sizeof(buffer) - 1);
if (bytes_read == -1) {
perror("read");
close(fd);
return 1;
}
buffer[bytes_read] = '\0'; // Null-terminate the string
printf("Read data: %s\n", buffer);

// Close the file


close(fd);
return 0;
}

3. fcntl() example (for file descriptor manipulation):

#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

int main() {
// Open the file for reading
int fd = open("example.txt", O_RDONLY);
if (fd == -1) {
perror("open");
return 1;
}

// Get the current flags for the file descriptor


int flags = fcntl(fd, F_GETFL);
if (flags == -1) {
perror("fcntl");
close(fd);
return 1;
}
printf("Current file descriptor flags: %d\n", flags);

// Set the file descriptor to non-blocking


If(fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
perror("fcntl");
close(fd);
return 1;
}
printf("Set the file descriptor to non-blocking mode.\n");
// Close the file
close(fd);
return 0;
}

4. seek() example (for moving the file pointer):

#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

int main() {
// Open the file for reading and writing
int fd = open("example.txt", O_RDWR);
if (fd == -1) {
perror("open");
return 1;
}

// Move the file pointer to the 10th byte


off_t offset = lseek(fd, 10, SEEK_SET);
if (offset == -1) {
perror("lseek");
close(fd);
return 1;
}
printf("Moved file pointer to position: %ld\n", offset);

// Read from the new position


char buffer[256];
ssize_t bytes_read = read(fd, buffer, sizeof(buffer) - 1);
if (bytes_read == -1) {
perror("read");
close(fd);
return 1;
}
buffer[bytes_read] = '\0';
printf("Read data: %s\n", buffer);

// Close the file


close(fd);
return 0;
}

5. stat() example (for retrieving file information):

#include <sys/stat.h>
#include <stdio.h>

int main() {
struct stat file_info;
// Get information about the file
if (stat("example.txt", &file_info) == -1) {
perror("stat");
return 1;
}

// Print file information


printf("File size: %ld bytes\n", file_info.st_size);
printf("Last modified: %ld\n", file_info.st_mtime);
printf("File permissions: %o\n", file_info.st_mode & 0777);

return 0;
}

6. opendir(), readdir(), and closedir() example:

#include <dirent.h>
#include <stdio.h>

int main() {
DIR *dir = opendir(".");
if (dir == NULL) {
perror("opendir");
return 1;
}

// Read directory contents


struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
printf("Name: %s\n", entry->d_name);
}
// Close the directory
closedir(dir);
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy