Sayaksome-3
Sayaksome-3
Sayaksome-3
Question 1
i) System Name
vi) uptime
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
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;
sum += (2 * i - 1);
return sum;
int sumEvenNumbers(int n) {
int sum = 0;
sum += (2 * i);
return sum;
}
int main() {
int n;
scanf("%d", &n);
if (pid < 0) {
printf("fork failed");
exit(1);
} else if (pid == 0) {
} else {
wait(NULL);
return 0;
Output: