Skip to content

Commit 9ca334e

Browse files
WeChat cstutorcs CSE330 Projects
0 parents commit 9ca334e

File tree

25 files changed

+1483
-0
lines changed

25 files changed

+1483
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Assignment-4-Template
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
WeChat: cstutorcs
2+
QQ: 749389476
3+
Email: tutorcs@163.com
4+
#include <iostream>
5+
#include <vector>
6+
7+
// This is a simplified version of the Linux kernel's memory management: mm_types.h
8+
// You can find the original file at: https://elixir.bootlin.com/linux/v6.4.11/source/include/linux/mm_types.h
9+
10+
11+
// Define the Page structure to track individual pages
12+
struct page {
13+
bool pte_present; // A flag to indicate if the page is in memory or swapped
14+
bool pte_young; // A flag to indicate if the page is in use
15+
std::string data; // Name or data associated with the page
16+
};
17+
18+
// Define the vm_area_struct structure (simplified for illustration)
19+
struct vm_area_struct {
20+
unsigned long vm_start; // Start address of the VMA
21+
unsigned long vm_end; // End address of the VMA
22+
std::vector <page> pages; // A list of pages in the VMA
23+
};
24+
25+
// Define a mm_struct structure (simplified for illustration)
26+
struct mm_struct {
27+
std::vector <vm_area_struct> vma_list; // A list of VMAs
28+
};
29+
30+
struct vma_iterator {
31+
std::vector <vm_area_struct> vma_list; // A list of VMAs
32+
size_t index; // Index to track the current VMA
33+
};
34+
35+
// Define a macro to initialize the vma_iterator
36+
#define VMA_ITERATOR(name, __mm, __addr) \
37+
struct vma_iterator name = { \
38+
.vma_list = (__mm).vma_list, \
39+
.index = __addr \
40+
}
41+
42+
// Function to get the next VMA
43+
vm_area_struct *vma_next(vma_iterator *vmi) {
44+
if (vmi->index < vmi->vma_list.size()) {
45+
return &vmi->vma_list[vmi->index++];
46+
} else {
47+
return nullptr;
48+
}
49+
}
50+
51+
// Define a macro to iterate through VMAs
52+
#define for_each_vma(__vmi, __vma) \
53+
while (((__vma) = vma_next(&(__vmi))) != NULL)
54+
55+
int main() {
56+
// Example usage
57+
mm_struct mm;
58+
struct vm_area_struct *vma = NULL;
59+
60+
// Populate mm with some VMAs for demonstration
61+
vm_area_struct vma1 = {
62+
0x1000,
63+
0x3000,
64+
{
65+
{true, true, "Page 1"},
66+
{false, false, "Page 2"},
67+
{true, false, "Page 3"},
68+
{true, true, "Page 4"},
69+
{false, false, "Page 5"}
70+
}
71+
};
72+
73+
vm_area_struct vma2 = {
74+
0x4000,
75+
0x6000,
76+
{
77+
{true, false, "Page 6"},
78+
{false, false, "Page 7"},
79+
{true, true, "Page 8"},
80+
{true, false, "Page 9"},
81+
{true, true, "Page 10"}
82+
}
83+
};
84+
85+
mm.vma_list.push_back(vma1);
86+
mm.vma_list.push_back(vma2);
87+
88+
// Initialize memory statistics variables
89+
unsigned long total_rss = 0;
90+
unsigned long total_swap = 0;
91+
unsigned long total_wss = 0;
92+
93+
// TODO 1: Use mm_struct to initialize the VMA_ITERATOR
94+
// Hint: Use the VMA_ITERATOR macro, and initialize the index to 0
95+
// Hint: Use the mm_struct to access the list of VMAs
96+
// Hint: Only one line of code is needed
97+
98+
99+
// TODO 2: Replace the while loop with for_each_vma macro
100+
// Hint: Use the for_each_vma macro to iterate through the VMAs
101+
// Hint: Only one line of code is needed, then remove the while loop
102+
while (1) {
103+
// Initialize VMA statistics variables
104+
unsigned long vma_rss = 0;
105+
unsigned long vma_swap = 0;
106+
unsigned long vma_wss = 0;
107+
108+
// TODO 3: Iterate through the pages in the VMA and update the memory statistics
109+
// Hint: Use the page structure to access individual pages
110+
// Hint: Use the vma_rss, vma_swap, and vma_wss variables to track the memory statistics
111+
// Hint: if pte_present is true, the page is in memory; otherwise, it is swapped
112+
// Hint: if pte_young is true, the page is in use
113+
// Hint: Only about 10 lines of code are needed
114+
115+
116+
// Update the memory statistics for the entire process
117+
total_rss += vma_rss;
118+
total_swap += vma_swap;
119+
total_wss += vma_wss;
120+
121+
// Print memory statistics for the VMA
122+
std::cout << std::endl;
123+
std::cout << "VMA: " << vma->vm_start << " - " << vma->vm_end << std::endl;
124+
std::cout << " Resident Set Size: " << vma_rss << std::endl;
125+
std::cout << " Swapped Size: " << vma_swap << std::endl;
126+
std::cout << " Working Set Size: " << vma_wss << std::endl;
127+
}
128+
129+
// Print memory statistics for the entire process
130+
std::cout << std::endl;
131+
std::cout << "Memory Statistics:" << std::endl;
132+
std::cout << " Total Resident Set Size: " << total_rss << std::endl;
133+
std::cout << " Total Swapped Size: " << total_swap << std::endl;
134+
std::cout << " Total Working Set Size: " << total_wss << std::endl;
135+
136+
return 0;
137+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Assignment-5-Template
2+
3+
## Sample Output Screenshots:
4+
![sample_output](sample_output.png)
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
WeChat: cstutorcs
2+
QQ: 749389476
3+
Email: tutorcs@163.com
4+
#include <iostream>
5+
#include <fstream>
6+
#include <string>
7+
#include <filesystem>
8+
9+
10+
namespace fs = std::filesystem;
11+
12+
// Define a function to list all the files in the current directory
13+
void listFiles() {
14+
fs::path currentPath = fs::current_path(); // Get the current working directory
15+
16+
try {
17+
std::cout << "Current path is " << currentPath << "\n";
18+
for (const auto& entry : fs::directory_iterator(currentPath)) {
19+
if (fs::is_regular_file(entry)) {
20+
std::cout << "File: " << entry.path().filename() << "\n";
21+
} else if (fs::is_directory(entry)) {
22+
std::cout << "Directory: " << entry.path().filename() << "\n";
23+
}
24+
}
25+
} catch (const fs::filesystem_error& ex) {
26+
std::cerr << "Error: " << ex.what() << "\n";
27+
}
28+
return;
29+
}
30+
31+
32+
int main() {
33+
std::string filename = "assignment5_output.txt";
34+
std::ofstream file;
35+
36+
std::string data = "\tHello, File System!\n\t2023Fall CSE330\n\tAssignment 5\n\tLearning File System\n";
37+
38+
// TODO 1: Create and open a file for writing (use file and filename)
39+
// Note: For open() operation, if the file already exists, its content will be cleared
40+
// Hint: Only one line of code is needed
41+
42+
43+
// TODO 2: use '<<' to write the data variable to the file
44+
// Hint: Only one line of code is needed
45+
46+
47+
// TODO 3: Close the file
48+
// Hint: Only one line of code is needed
49+
50+
51+
std::cout << "File created and written successfully." << std::endl;
52+
listFiles();
53+
std::cout << std::endl;
54+
55+
// TODO 4: Use std::ifstream to open the previous file for reading and modifying
56+
// Hint: Only one line of code is needed
57+
58+
59+
std::string fileContent;
60+
std::string line;
61+
// TODO 5: Use while loop and std::getline to read the file content line by line
62+
63+
64+
// TODO 6: Close the file
65+
// Hint: Only one line of code is needed
66+
67+
68+
// Print the file content
69+
std::cout << "File content:" << std::endl;
70+
std::cout << fileContent << std::endl;
71+
std::cout << std::endl;
72+
73+
// Modify the content by finding and replacing a substring
74+
std::string searchString = "File System";
75+
76+
77+
// TODO 7: Replace "Your Name" with your name
78+
std::string replaceString = "Your Name";
79+
80+
// TODO 8: use find() to find and use replace() to replace the string
81+
// Hint: use a while loop to replace all occurrences of the searchString: e.g. while (find_result != std::string::npos)
82+
// Hint: use source_string.replace(position, length, replaceString) to replace the substring
83+
84+
85+
86+
// Print the modified content
87+
std::cout << "Modified file content:" << std::endl;
88+
std::cout << fileContent << std::endl;
89+
std::cout << std::endl;
90+
91+
// TODO 9: update the fileContent to the file, then close it
92+
// Hint: use open() again
93+
94+
95+
96+
std::string newFilename = "assignment5_renamed.txt";
97+
// TODO 10: use std::rename(oldname, newname) to rename the file
98+
// Hint: you may need to convert the filename to c_str() before passing it to std::rename()
99+
100+
101+
102+
103+
// test if the file is renamed successfully
104+
std::ifstream renamedFile(newFilename);
105+
if (!renamedFile) {
106+
std::cerr << "Failed to open the renamed file." << std::endl;
107+
return 1;
108+
}
109+
// read the rename file content and then print it
110+
std::string renamedFileContent;
111+
std::string renamedLine;
112+
while (std::getline(renamedFile, renamedLine)) {
113+
renamedFileContent += renamedLine + "\n";
114+
}
115+
renamedFile.close();
116+
std::cout << "New file name: " << newFilename << std::endl;
117+
std::cout << "Renamed file content:" << std::endl;
118+
std::cout << renamedFileContent << std::endl;
119+
std::cout << std::endl;
120+
121+
listFiles();
122+
return 0;
123+
}

Project-1-Template-main/Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
obj-m = producer_consumer.o
2+
all:
3+
#$(MAKE) -f ./process_gen/Makefile
4+
cd process_gen && $(MAKE)
5+
make -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) modules
6+
clean:
7+
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
8+
#make -f ./process_gen/Makefile clean
9+
cd process_gen && $(MAKE) clean

Project-1-Template-main/README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# CSE330: Operating Systems
2+
3+
Please follow the below steps to test your Project-1 Process Management. (Due: **17th October 2023 11:59 PM**)
4+
5+
- Download the zip of this git repository.
6+
- Unzip the repository.
7+
- Implement your kernel module code in the template (producer\_consumer.c).
8+
- Run the test.sh script with the test arguments.
9+
10+
bash test.sh < Number of processes to be spawned> < Buffer Size > < Number of Producer > < Number of Consumer > < Number of lines to be displayed from the dmesg >
11+
12+
## Test Cases:
13+
14+
| Test Case # | Test Command | Test Criteria | Total Points |
15+
| :------------- |:-------------:| :-----:|:-----:|
16+
| 1 | sudo ./test.sh 10 5 1 0 25 | The number of items produced will be equal to the buffer size 5. (3 pts) <br /> Module should exit without error. (2 pts) | 5|
17+
| 2 | sudo ./test.sh 10 5 0 1 25 | Consumer should consume nothing (2 pts) <br /> Module should exit without error (3 pts) | 5|
18+
| 3 | sudo ./test.sh 10 50 1 1 25 | The total number of produced items should be equal to 10. Each process should be produced and consumed only once. (4 pts) <br /> CPU time should match with ps command. (4 pts) <br /> Module should exit without error. (2 pts) | 10 |
19+
| 4 | sudo ./test.sh 100 50 1 1 25 | The total number of produced items should be equal to 100. Each process should be produced and consumed only once. (6 pts) <br /> CPU time should match with ps command. (6 pts)<br /> Module should exit without error. (3 pts) | 15 |
20+
| 5 | sudo ./test.sh 1000 50 1 1 100 | The total number of produced items should be equal to 1000. Each process should be produced and consumed only once. (6 pts). <br /> CPU time should match with ps command. (6 pts) <br /> Module should exit without error. (3 pts) | 15 |
21+
22+
## Bonus Points (Total Points:10)
23+
24+
a) Bonus Test Case:
25+
- If your code also passes the Bonus Test Case
26+
27+
| Test Case # | Test Command | Test Criteria | Total Points |
28+
| :------------- |:-------------:| :-----:|:-----:|
29+
| 6 | sudo ./test.sh 100 50 1 2 25 | The toal CPU time calculated by 2 consumer thread should match with ps command. Module should exit without error | 10 |
30+
31+
## Note:
32+
- Please do not make any changes in provided test code to pass the test cases.
33+
- You can use print statements in case you want to debug and understand the logic of the test code.
34+
- Please get in touch with the TAs if you face issues in using the test scripts.
35+
36+
## Sample Output Screenshots:
37+
38+
### Test Case 1
39+
![test_case_1](sample_output/test1.png)
40+
41+
### Test Case 2
42+
![test_case_2](sample_output/test2.png)
43+
44+
### Test Case 3
45+
![test_case_3](sample_output/test3.png)
46+
47+
### Test Case 4
48+
![test_case_4](sample_output/test4.png)
49+
50+
### Test Case 5
51+
![test_case_5](sample_output/test5.png)
52+
53+
### Test Case 6
54+
![test_case_6](sample_output/test6.png)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
objects = process_generator
2+
all: $(objects)
3+
4+
$(objects): %: %.c
5+
$(CC) $(CFLAGS) -o $@ $<
6+
7+
clean: $(objects)
8+
rm $(objects)

0 commit comments

Comments
 (0)