Program for mimicking ps command
Write a program to read the /proc filesystem and print the following for all the processes listed there:
PID, PPID, STATE, COMMAND, No. of Files open
Since for all the root or privileged processes, you are not allowed to read the no. of files open, print NA for those. For all user processes, print the no. of files open.
Below are some Hints for the assignment.
- Use the systems calls - opendir, chdir, readdir for opening, changing to and reading the contents of a directory. If you do not change to the directory you are opening, make sure the path is correctly set by concatenating the path to the file/directory you are trying to open.
- Remember to check the return value of every system call you are using. This is most important for system calls. In fact, without this, you will find yourselves in a lot of trouble to debug or understand what is happening.
- Typically, system calls only return -1 when there is an error. They set a global error number variable called errno which is defined in errno.h. Since it is difficult to understand the number itself, Unix/Linux provides the function strerror(errno) to print a human-readable string which gives the error encountered. Use this to debug your code.
- Use fopen and fgets calls to read the contents of a directory. Then use either strchr, strtok or sscanf to parse the contents of the file and read the pid, ppid etc. values.
- Make sure your code is modular with different functions doing different logical pieces of work and unit test these functions as you build them to reduce bugs and help in debugging the code. If you write the complete program and only then try to debug, you may find yourself bogged down. For example, write the code to parse the file which contains the PID, PPID etc. first and write a main() stub which gives the file name as the command line prompt. Call this function from such a driver program and test the parsing. Once that is correct, write a function to navigate directories and get the file names. Now, you can call your previous function from this function with the current file name.
Implementation of Banker's Algorithm
In this assignment, you need to implement the Banker's algorithm. The input to the program is the name of a file (given as command line argument) which will contain all the necessary information about the resource types, instances and maximum needs and current allocation of processes. Another file will contain the various requests which you need to check and determine if they will leave the system in a safe state or the process needs to be blocked for now. For each request, print whether it resulted in an unsafe state or a safe state. Make sure your output is meaningful instead of just printing "safe"/"unsafe".
The format of the first file is as follows:
-----------------------------------------------
#Number of resource types, Number of processes
M, N
# Total instances of each resource type
R1, R2, R3, ..., Rm
# Each line below gives the maximum requirement of a process
P1R1, P1R2, P1R3, ..., P1Rm
P1R1, P1R2, P1R3, ..., P1Rm
...
PnR1, PnR2, PnR3, ..., PnRm
# Each line below gives the current allocation to a process
P1R1, P1R2, P1R3, ..., P1Rm
P1R1, P1R2, P1R3, ..., P1Rm
...
PnR1, PnR2, PnR3, ..., PnRm
-----------------------------------------------
The format of the second file is as follows:
-----------------------------------------------
Pi: R1, R2, R3, ..., Rm
Pj: R1, R2, R3, ..., Rm
Pi: R1, R2, R3, ..., Rm
Pk: R1, R2, R3, ..., Rm
-----------------------------------------------
The Data file for the example in the book and the Two requests given for the same in the book can be downloaded for basic testing of your code. Please note that any line in the file that starts with a "#" in the first column is a comment and has to be ignored by the parser you write.
Memory Allocation Algorithms -- First, Best and Worst Fit
Contiguous Memory Allocation Algorithms
A file will be given as input (name taken as a command line parameter). The file consists of the following information:
# Total Memory of the system in KB
4096
# Total number of holes
5
# Hole information in the form of start offset, hole size in KB
500,100
1024,256
2048,1024
# Process memory reqts
600
200
50
100
1000
You need to read this information from the file and allocate the memory to the processes in the order in which they appear. For the example above, first you have to allocate 600KB, then 200KB and so on. Remember that you will have to update the hole information as you allocate. This gives the initial hole information only. You need to use the updated hole information for allocating to subsequent processes.
Output: The output should consist of starting address where the process is allocated, the amount of memory lost to external fragmentation for each of first fit, best fit and worst fit algorithms.
NOTE: If something is not clear, do NOT make assumptions. Get clarification on the specification BEFORE implementation.
Paging Algorithm
Assume a 32-bit processor. The permissions are given as "r,w,-" or "r,-,x" or "r,-,-". They represent RW, RX and R permissions on those pages. The operation in the format below is one of r, w, x. If the corresponding page has the permission, it is a valid operation; otherwise, it is not. Similarly, if the page no extracted from the virtualAddr is more than the last page for that process, it is an invalid operation.
The output of the program should be as follows:
PID virtualAddr operation pageno phyAddr
The operation is same as input if it is allowed; otherwise it should be "Invalid op". If the page no. is valid, print the page no; otherwise, NA. Finally, give the phyAddr for all valid cases.
The input file has the following format:
numProcesses pagesize
# PID
pageno frameBaseAddr perms
...
pageno frameBaseAddr perms
...
# PID
pageno frameBaseAddr perms
...
pageno frameBaseAddr perms
# VA
PID virtualAddr operation
PID virtualAddr operation
...
PID virtualAddr operation
An Example File may be downloaded to see the format of the file.