Hi, i need help with this opreating system homework.

Part 1:

you need your C program to do the following in order to achieve this desired output.

(1) navigate the subfolders of the proc folder looking for those folders holding information about processes in the system. Remember that proc folder has such folders, named after the process pid number, and that the proc folder has other folders hosting data for other purposes.

(2) each time you locate a process folder you need to create a constant string array to hold the parameters needed to execute the proper ps command to retrieve the rss information for a process with specific pid. The command to retrieve that with ps is "ps -p pid -o rss". The array should have these entries of the ps command as its elements and that last element in the array should be the NULL.

char *const parmList[] = {"ps","-p",pid,"-o","rss",NULL};

in this case pid is a string that you need to form based on the process pid which is the name of the folder.

(3) next you need to execute the ps command. To do so, you need to create a child process for this purpose who will then execute a proper exec system call that accepts two arguments, the path of the command to execute "/bin/ps" and the array you created earlier. This will be the execvp system call

execvp("/bin/ps", parmList);

The child needs to send its output to the parent then terminates hence, a pipe should be created before forking the child and a proper close/dup should be performed in the child code to get the result of execvp written to the pipe.

(4) the parent should wait to read the output of the child from the pipe and extract from it the value of rss without the heading then add that value to total.

(5) repeat the steps (2) to (4) for all folders holding information about processes in the system.

(6) display the total rss on the screen.

Part 2:

(1) navigate the subfolders of the proc folder looking for those folders holding information about processes in the system. Remember that proc folder has such folders, named after the process pid number, and that the proc folder has other folders hosting data for other purposes.

(2) each time you locate a process folder you need to form a string hosting the path to the status file in that folder.

(3) use the path string to open the file.

(4) Remember that that the value of rss size is in the status file, open the file in gedit to understand the formats of it and to know how to locate the value of rss. Having done so, you can now code how to extract such information by read the file line by line. You can do that using a proper i/o method from those who are definded in stdio.h, for example, fscanf.

(5) each time you read a line, check if it contains the rss header which is "VmRSS:". You can do that with the use of the C string function strstr. If the line contains the substring "VmRSS:" then it is the desired line having the value needed hence, you need to read the value using proper i/o method from those who are defined in stdio.h, for example, sscanf.

(6) add the retrieved value of rss to the total.

(7) repeat the steps (2) to (6) for all folders holding information about processes in the system.

(8) display the total rss on the screen.

THANKS IN ADVANCE :D

hey did any one answer you on this?

heyy,

I need it too

Sure, I can help you with your operating system homework. It seems like you have two parts to complete. Let's go through each part and explain how to achieve the desired output.

Part 1:
1. To navigate the subfolders of the proc folder in order to find the process folders, you can use functions like opendir() and readdir() in C. These functions allow you to open a directory and read its contents.
2. Once you locate a process folder, you need to create a constant string array to hold the parameters needed to execute the ps command. You can use the sprintf() function to form the command string with the process id (pid). Here's an example:

```c
char pid[10]; // Assume you store the process id as a string
sprintf(pid, "%s", "12345"); // Replace "12345" with the actual process id
char *const parmList[] = {"ps", "-p", pid, "-o", "rss", NULL};
```

3. After creating the parameter array, you need to execute the ps command. To do this, you should create a child process using fork(), and then use execvp() to execute the ps command with the parameter array. Here's an example:

```c
pid_t child_pid = fork();
if (child_pid == 0) {
// Child process
execvp("/bin/ps", parmList);
exit(0);
} else if (child_pid > 0) {
// Parent process
// Wait for child process to terminate
wait(NULL);
} else {
// Fork failed, handle error
}
```
4. The child process should send its output to the parent process. To achieve this, you need to create a pipe before forking the child. The child process should close its standard output (STDOUT_FILENO) and duplicate the write end of the pipe (using dup2()) to it. The parent process can then read from the read end of the pipe to get the output of the child. Here's an example:

```c
int pipefd[2];
pipe(pipefd);
pid_t child_pid = fork();
if (child_pid == 0) {
// Child process
close(pipefd[0]); // Close read end of the pipe
dup2(pipefd[1], STDOUT_FILENO); // Redirect STDOUT to the write end of the pipe
execvp("/bin/ps", parmList);
exit(0);
} else if (child_pid > 0) {
// Parent process
close(pipefd[1]); // Close write end of the pipe
// Read the output from the pipe
char buffer[BUFSIZE];
ssize_t n;
while ((n = read(pipefd[0], buffer, BUFSIZE)) > 0) {
// Process the output as needed
}
close(pipefd[0]); // Close read end of the pipe
} else {
// Fork failed, handle error
}
```

5. In the parent process, you can extract the value of RSS (Resident Set Size) from the output obtained from the child process. You can parse the output using string manipulation functions like strtok() or strstr(). Once you have the RSS value as a string, you can convert it to an integer and add it to a running total.
6. Repeat steps 2-5 for all the folders holding information about processes in the system.
7. Finally, display the total RSS on the screen.

Part 2:
1. The steps involving navigating the subfolders of the proc folder and locating process folders are the same as in Part 1.
2. To form the path to the status file in each process folder, you can concatenate the folder path with the "status" filename, like "/proc/<pid>/status".
3. Use the path string obtained in the previous step to open the status file using fopen().
4. Read each line of the status file using fgets() or any other suitable I/O method. Check if the line contains the RSS header "VmRSS:" using the strstr() function.
5. If the line contains the "VmRSS:" substring, use sscanf() or similar functions to extract the value of RSS from the line.
6. Add the retrieved value of RSS to the total, similar to Part 1.
7. Repeat steps 2-6 for all the folders holding information about processes in the system.
8. Finally, display the total RSS on the screen.

Make sure to include the necessary header files (`<stdio.h>`, `<stdlib.h>`, `<sys/types.h>`, `<sys/wait.h>`, `<unistd.h>`, etc.) and handle error conditions appropriately.