Operating System CSP244
Operating System CSP244
Submitted By
Student Name Faizan Hassan
Student Id 2023486998
Section/Group CS-P
Programme B.Tech (CS&P)
Department Computer Science and Engineering
Session/Semester 2023-24/2303
Submitted To
Faculty Name Mr. Kanderp Narayan Mishra
SHARDA UNIVERSITY
Plot No. 32-34, Knowledge Park III,
Greater Noida, Uttar Pradesh 201310
TABLE OF CONTENT
Theory :
I. fork(): The use of the fork() system call is to create a new process by duplicating the
calling process. The fork() system call is made by the parent process, and if it is
successful, a child process is created.
OUTPUT:
II. exec(): The exec system call in Linux is used to replace the current running process with
a new process. It is often used in conjunction with process creation functions like fork,
where a child process is created.
III. getpid(): The getpid() function returns the process ID of the calling process.
IV. exit():The exit command in Linux terminates the current shell session and logs the user
out of their current session.
OUTPUT:
VI. close(): The close system call closes a file descriptor that was previously obtained by the
open system call.
Name - Faizan Hassan
Sys ID - 2023486998
PROGRAM FOR “CLOSE()” SYSTEM CALL:
1. ##include <stdio.h>
2. #include <stdlib.h>
3. #include <unistd.h>
4. #include <fcntl.h>
5.
6. int main() {
7. // Open a file for writing (create it if it doesn't exist)
8. int file_descriptor = open("example.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
9. if (file_descriptor < 0) {
10. perror("Error opening file");
11. exit(EXIT_FAILURE);
12. }
13. // Write some data to the file
14. const char *data = "Hello, World!\n";
15. ssize_t bytes_written = write(file_descriptor, data, 14); // 14 is the length of
the data
16.
17. if (bytes_written < 0) {
18. perror("Error writing to file");
19. close(file_descriptor); // Close the file before exiting
20. exit(EXIT_FAILURE);
21. }
22. printf("Data written successfully.\n");
23. // Close the file
24. if (close(file_descriptor) < 0) {
25. perror("Error closing file");
26. exit(EXIT_FAILURE);
27. }
28. printf("File closed successfully.\n");
29. return 0;
30. }
OUTPUT:
VII. stat():The stat system call in Linux is used to obtain information about a file, such as its
size, permissions, last access time, and more.
PROGRAM FOR “STAT()” SYSTEM CALL:
Name - Faizan Hassan
Sys ID - 2023486998
1. #include <stdio.h>
2. #include <stdlib.h>
3. #include <sys/stat.h>
4. #include <time.h>
5.
6. int main() {
7. struct stat fileStat;
8. // Specify the file to get the information
9. const char *filename = "example.txt";
10. // Call stat() to get file information
11. if (stat(filename, &fileStat) < 0) {
12. perror("stat");
13. return 1;
14. }
15. // Print file information
16. printf("File: %s\n", filename);
17. printf("File size: %lld bytes\n", (long long)fileStat.st_size);
18. printf("Number of links: %hu\n", fileStat.st_nlink);
19. printf("File inode: %llu\n", (unsigned long long)fileStat.st_ino);
20. printf("File permissions: ");
21. printf((S_ISDIR(fileStat.st_mode)) ? "d" : "-");
22. printf((fileStat.st_mode & S_IRUSR) ? "r" : "-");
23. printf((fileStat.st_mode & S_IWUSR) ? "w" : "-");
24. printf((fileStat.st_mode & S_IXUSR) ? "x" : "-");
25. printf((fileStat.st_mode & S_IRGRP) ? "r" : "-");
26. printf((fileStat.st_mode & S_IWGRP) ? "w" : "-");
27. printf((fileStat.st_mode & S_IXGRP) ? "x" : "-");
28. printf((fileStat.st_mode & S_IROTH) ? "r" : "-");
29. printf((fileStat.st_mode & S_IWOTH) ? "w" : "-");
30. printf((fileStat.st_mode & S_IXOTH) ? "x" : "-");
31. printf("\n");
32. // Print the last access time
33. printf("Last access time: %s", ctime(&fileStat.st_atime));
34. // Print the last modification time
35. printf("Last modification time: %s", ctime(&fileStat.st_mtime));
36. // Print the last status change time
37. printf("Last status change time: %s", ctime(&fileStat.st_ctime));
38. return 0;
39. }
OUTPUT:
VIII. opendir(): The opendir system call in Linux is used to open a directory for reading. It
returns a pointer to a DIR structure, which represents the directory stream.
OUTPUT:
IX. readdir():The readdir system call in Linux is used to read directory entries from an open
directory stream.
OUTPUT: