Os Lab2
Os Lab2
Ass-3 Write a program to report behavior of Linux kernel including information on configured
memory, amount of free and used memory. (memory information)
Ans:
Here’s a C program that reports the behavior of the Linux kernel, including information on
configured memory, amount of free and used memory. This program reads the
/proc/meminfo file and extracts the necessary memory details.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void report_memory_behavior() {
FILE *file = fopen("/proc/meminfo", "r");
if (file == NULL) {
perror("Could not open /proc/meminfo");
return;
}
char line[256];
unsigned long total_memory = 0;
unsigned long free_memory = 0;
unsigned long available_memory = 0;
fclose(file);
int main() {
report_memory_behavior();
return 0;
}
Explanation:
1. File Opening: The program opens the /proc/meminfo file to read memory statistics.
2. Memory Variables: Variables like total_memory, free_memory, and
available_memory are defined to store values read from the file.
3. Reading and Parsing: The fgets function is used to read the file line by line. Using
sscanf, the program extracts memory information for MemTotal, MemFree, and
MemAvailable.
4. Memory Calculation: The used memory is calculated as the difference between total and
free memory.
5. Output: The memory values are printed in MB, as they are initially reported in kB in
/proc/meminfo.
Output Example:
Total Configured Memory: 8192 MB
Free Memory: 2048 MB
Used Memory: 6144 MB
Available Memory: 3072 MB
Notes:
This program is designed to run on Linux systems that have the /proc/meminfo file.
It assumes the system reports memory in kilobytes (kB) as is typical in /proc/meminfo.
The program converts these values to megabytes (MB) for readability.
Ass-4 Write a program to print file details including owner access permissions, file access
time, where file name is given as argument.
Ans:
Here’s a C program that prints file details, including the owner’s access permissions and the last
file access time. The program accepts a file name as an argument and uses system calls like
stat to get the file information.
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
#include <unistd.h>
return EXIT_SUCCESS;
}
Explanation:
1. stat() System Call: The program uses stat() to obtain file information such as
permissions, owner, and access times.
2. Permissions: The print_permissions() function decodes the file mode (permissions)
from the stat structure and prints it in a human-readable format.
3. Owner and Group: The program uses getpwuid() and getgrgid() to obtain the
username and group name corresponding to the file's user ID (st_uid) and group ID
(st_gid).
4. Last Access Time: The program uses ctime() to convert the last access time
(st_atime) into a human-readable string.
Here’s a C program that copies files using system calls like open(), read(), write(), and
close().
Program:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
if (bytes_read == -1) {
perror("Error reading source file");
}
copy_file(argv[1], argv[2]);
return EXIT_SUCCESS;
}
Explanation: