Sayaksome-3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

DAY 3

Question 1

3.1Write a program using C in Linux that displays the following information:

i) System Name

ii) OS version & release

iii) Machine Architecture

iv) Total/Used/Free Physical memory

v) Total/Used/Free Virtual memory

vi) uptime

vii) Total disk space of the partition you are using.

Source Code:

#include <stdio.h>
#include <stdlib.h>
#include <sys/sysinfo.h>
#include <sys/utsname.h>
#include <unistd.h>
#include <sys/statvfs.h>
void print_system_info() {
struct utsname uname_info;
if (uname(&uname_info) == 0) {
printf("System Name: %s\n", uname_info.sysname);
} else {
perror("uname");
}
printf("OS Version: %s\n", uname_info.version);
printf("OS Release: %s\n", uname_info.release);
printf("Machine Architecture: %s\n", uname_info.machine);
struct sysinfo sys_info;
if (sysinfo(&sys_info) == 0) {
printf("Total Physical Memory: %ld MB\n", sys_info.totalram / (1024 * 1024));
printf("Free Physical Memory: %ld MB\n", sys_info.freeram / (1024 * 1024));
printf("Used Physical Memory: %ld MB\n", (sys_info.totalram - sys_info.freeram) / (1024 * 1024));
} else {
printf("sysinfo");
}
printf("Total Virtual Memory: %ld MB\n", sys_info.totalram / (1024 * 1024));
printf("Free Virtual Memory: %ld MB\n", sys_info.freeram / (1024 * 1024));
printf("Used Virtual Memory: %ld MB\n", (sys_info.totalram - sys_info.freeram) / (1024 * 1024));
long uptime_seconds = sys_info.uptime;
long uptime_minutes = uptime_seconds / 60;
printf("Uptime: %ld seconds (%ld minutes)\n", uptime_seconds, uptime_minutes);
struct statvfs fs_info;
if (statvfs("/", &fs_info) == 0) {
printf("Total Disk Space: %ld GB\n", fs_info.f_blocks * fs_info.f_frsize / (1024 * 1024 * 1024));
} else {
printf("statvfs");
}
}
int main() {
print_system_info();
return 0;
}
Output:

Question 2

3.2Write program using C in Linux -

A. That will illustrate the creation of child process using fork() system call.

B. To create a process that creates a child process. Print PID & PPID in each process. Also,
verify those PID & PPID values using ps command. 1 file of screenshots of output needs to
be uploaded for evaluation.

Source Code:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
void print_process_info(const char *who) {
printf("%s PID: %d, PPID: %d\n", who, getpid(), getppid());
}
int main() {
pid_t pid;
print_process_info("Parent");
pid = fork();
if (pid < 0) {
printf("Fork");
exit(EXIT_FAILURE);
}
if (pid == 0) {
print_process_info("Child");
exit(EXIT_SUCCESS);
} else {
wait(NULL);
print_process_info("Parent after child");
}
return 0;
}
Output:

Question 3

3.3 Write program using C in Linux in which the child program prints the sum of 1st n odd numbers
and the child program prints the sum of 1st n even numbers.

Source Code:

#include <stdio.h>

#include <stdlib.h>

#include <sys/types.h>

#include <sys/wait.h>

#include <unistd.h>

int sumOddNumbers(int n) {

int sum = 0;

for (int i = 1; i <= n; i++) {

sum += (2 * i - 1);

return sum;

int sumEvenNumbers(int n) {

int sum = 0;

for (int i = 1; i <= n; i++) {

sum += (2 * i);

return sum;

}
int main() {

int n;

printf("Enter the value of n: ");

scanf("%d", &n);

pid_t pid = fork();

if (pid < 0) {

printf("fork failed");

exit(1);

} else if (pid == 0) {

int sum = sumOddNumbers(n);

printf("Child process: Sum of the first %d odd numbers is %d\n", n, sum);

exit(0); // Exit child process

} else {

wait(NULL);

int sum = sumEvenNumbers(n);

printf("Parent process: Sum of the first %d even numbers is %d\n", n, sum);

return 0;

Output:

You might also like