CS341 Final Exam SP23

CS341 Take Home Final Exam (3 hours) Spring 2023
Welcome to your CS341 Final Project. This is a “take home” open book exam with 3 questions. To complete CS341 you will be demonstrating your own competency in CS341 skills and knowledge. This means you may not collaborate, work with, or get help from current or former CS341 students or search for solutions. This final project is open book (you may access and reuse general system programming online materials) but the code and spoken words you include must be your own original work that you created during this exam. For example, reading aloud text from a web search result is not “in your own words” – it does not demonstrate your understanding and your competency.
You may not share this exam, your code, hints, test code or anything else that you created as part of this exam. Publishing your work that is part of this final exam or sharing your work with another student directly/indirectly is a violation of academic integrity and may affect not just your CS341 grade but your standing as a student at U of I.
Keep the total time spent on this to less than 5 hours (it should be 3 hours); it is up to you to track and use your time efficiently, you can take pauses etc. Reading this pdf etc. does not start any countdown timer.
We expect most of you will have access to a Linux system (e.g., WSL on Windows, Virtual Machine, ssh access to your CS341 VM, or the VM-in-a-browser). However in principle you could complete this using only a smart phone, pen and paper. Complete this final exam by Wednesday May 10th 11:00pm CT. Submission details will be posted on Ed and/or email. There will be an approximate 59 minute grace period after 11pm to allow for slow video exports, uploads, and IT issues.
Questions? Other than submission questions, we’re not going to respond to questions about the actual content of the exam; use your best efforts to interpret the instructions. This is an exam, and you need to work on it on your own.
If you believe it is ambiguous, state your assumptions and answer accordingly in your exam responses. (For those who do not read these instructions and ask on Ed, we will reply with this statement)
Program specifications are always inexact & incomplete – review the grading rubric and ignore any remaining edge cases.
Do not share or post comments about the content until after grades are published.
Submission logistics will be posted on/before Wednesday. Be sure to check your submission and allow sufficient time to upload it; we recommend you upload your .c files (helloworld.c puzzle.c) before uploading your video file(s); sftp and scp may be useful. Your video should be mp4 format or similar variant (e.g. “.mov”). It may be compromised of multiple files e.g. you could answer each question in a separate video recording. Any common encoding video file format is acceptable; there’s no need to transcode.
If providing a URL link to your video(s), & check that the link(s) works in a different browser when not authenticated as yourself. TAs will grade your files; though we may use automation to short-circuit hand- grading of working code that meets the grading criteria.
Good luck with this final, your future interviews & courses, but mostly, in changing the world. – L.A.

Part 1 of 3 (100 points, video, 60 minutes) “My First Office Hours”
“Don’t Panic… Sorry!” a familiar English voice announces, “I’m just adjusting … Aha! Let’s go!”
The world shifts into full view. Sensations of touch, smell, and sound flood your mind. You are at U of I and a light breeze against your face awakens you out of a dreamy slumber. There’s some tables, a whiteboard, and chairs. The sensations feel real except the scene is clearly fake as the words “History Sim Test (bug fix 59)” scroll in and blinks slowly in orange and blue letters in the bottom left corner of your vision in Courier New font, confirming that none of this is actually real. Yet the perceptual sensations feel more tangible than the breakfast you had earlier, and definitely more meaningful. A sense of spreading future connections overwhelms you (like a 400-level course on graph theory where the expanding nodes and edges escaped from the paper and leapt into the wild) – connections to people, events, world issues– future interviews, people, conversations, all the things that will help you change the world but have not yet happened but will happen in your future. You take a deep breath to relax.
“Okay,” the friendly voice continues, “I set the year back to 2024 – which explains the odd fashion choices you’re about to see! In about 1 minute your office hours will start and the first student will walk in. This first test is about whether you can explain to a student some CS 341 – topics, to demonstrate that you actually understand these topics and can be a valuable member of the course staff. So be ready to do some live programming demos, explain the concepts, … whatever you need to help the student thrive in this class.”
“We’re not sure how students learned back in 2024 but the computational-socio-geologists believe they were a social bunch who enjoyed talking and explaining things to each other; they were a strong community of over 2000 that wanted a meaningful life by changing the world and by their connections with each other. Don’t forget to pay it forward!”
Continued…

Record your 2024 office hour video. Here are the 10 things the CS341 students ask. You might want to cross them off as you complete them, or record them as separate videos. We recommend a screen cast with voice recording; the point is for you to explain something and in doing so, demonstrate your own understanding and competency in system programming. Students walk in. They ask the following,
1. What is wrong with return strcpy( malloc( sizeof( mesg ) ), mesg) if mesg is a char* and points to my C string I want to duplicated on the heap?
2. What’s the difference between malloc, sbrk and mmap to allocate a 1000 bytes of memory? Which one do you recommend I normally use and why?
3. For the malloc MP how can you write an O(1) free implementation; going through a linked list takes O(N) time ?
4. I’ve heard of pthread_create, pthread_exit, and pthread_join. Can you run/walk thru’ some code to explain how to pass a result back to the original thread and to wait for 2 threads?
5. What’s the purpose of fflush and when do I need it?
6. Angrave said round-robin with long quanta is like erm something else? What is the something else
and why is the “long quanta” requirement necessary?
7. I understand single level page table, can you help me understand 2-level page tables?
8. What is the purpose of HTTP’s “KEEP-ALIVE” header; What problem does it help solve?
9. If you wanted to speed up a program when would you use multiple threads? When would multiple
processes be better?
10. What’s the purpose of bind and socket system calls?
We expect you will want to record a video of your laptop screen with audio and use a text editor as a whiteboard, but you could also record using your phone. However any method of recording will be acceptable (e.g. phone pointed at piece of paper). We are looking for demonstrations of system programming understanding and competency by you (i.e. things that a CS225 student would not be able to explain but a CS341 student can), such that your office hour is effective and useful. Your spoken words need to be your own words, explanations, ideas, and thoughts; reading aloud a Google search result or the course book is not sufficient evidence of your understanding.
You can record multiple parts of a video if you need a break. However don’t worry about fluffs, mistakes and restarts; imagine this was a real office hours – just say oops and carry on! We expect most students will create approximately 30- 60(max) minutes of content. We will only grade the first 60 minutes of video content; which means you have maxium of 6 minutes per item. If a disability or related access concern means you’d prefer to demonstrate competency using an alternative (non-video) format, please contact Angrave.
There are multiple methods to record a laptop screen to a mp4 file or to the cloud. Protip: Do a test recording first and review it instead of assuming that it is working; verify your audio and the text is large enough and legible enough to be gradeable check that it is a recording of your screen not your laptop camera! Ultimately, you will be sharing the mp4/mov file or providing a URL to your cloud recording; verify that the link works when not logged in as you. Also, mediaspace.illinois.edu may be another useful way to record and share your video (login then click on your own name to get to your Media items).
Grading Rubric (Outline): 0 missing
5 mostly incomplete or misleading or inaccurate
7 mostly correct but a major error – not fully competent
9 mostly helpful for the students; some minor errors
10 helpful; no errors

Part 2 of 3 (100 points, code, 60 minutes) “One time pads can be secure”
It’s no longer safe and you have only minutes to spare before ChatGPT finds you; time for a fast exit. You’ve put all of your valuable secrets (new unfinished malloc, ideas for a new solar panel fabrication process, new Meme format, and a pasta recipe) into a single file (it could be a text file, zip file; whatever), now you just need to get out of here without anyone being able to discover or decrypt its valuable contents. Fortunately, you have your CS341 skills and VM. You quickly create and use a Linux C99 program helloworld.c using the following –
clang -O0 -Wall -Wextra -Werror -g -std=c99 -D_GNU_SOURCE -DDEBUG helloworld.c When your program is run it appears to be the simplest program; it just prints Hello World …
(prints “Hello World” without the quotes to standard-out and exits with value 0)
However, if /a.out is later renamed to encrypt it creates an encrypted version of your secrets with every byte xor’d with a random value –
mv a.out encrypt
./encrypt mystuff.txt 1.bin 2.bin
It opens the file mystuff.txt (using open and mmap) for reading and writing, and creates two new files 1.bin 2.bin. As it processes the bytes of mystuff.txt it also overwrites the contents of mystuff.txt with zero bytes so that the file’s original content is no longer easily recoverable. Output file 1.bin is a sequence of random bytes (read from /dev/urandom). File 2.bin contains the bytes from the original file encrypted by xor-ing each byte with the corresponding byte stored in file 1.bin. Upon completion both output files will be the same length as the original file, the length of the original file is unchanged.
For example if the first byte of mystuff.txt was 0x51 and the first random value read from /dev/urandom was 0x42 then the 1.bin would start with 0x42, 2.bin would start with 0x13 and the first byte of mystuff.txt would be overwritten with 0x00.
You post 1.bin and 2.bin – now on separate USB sticks, together with your executable – from two separate locations. Your simple exclusive-or encryption using a one-time pad will still be secure if only one of the files is intercepted. Three days, and three flights later, you rename your file again, this time to decrypt, and recover your secrets –
mv a.out decrypt
./decrypt output.txt 1.bin 2.bin
This reads the two files 1.bin and 2.bin (using mmap) and creates the output file output.txt (with contents identical to the original mystuff.txt file).
Upon completion it also deletes the files 1.bin and 2.bin from the filesystem.
For example, if the first byte of 1.bin was 0x42 and first byte of 2.bin was 0x13 then the first byte of the output file would be xor(0x42,0x13) = 0x51. The 2nd byte of output is recovered by xor-ing the 2nd bytes of1.binand2.bin etc.
Write one program. When run if the process name is ./encrypt then encrypt the file, if it is ./decrypt then decrypt the given files, as described above. If neither is true, or 3 filename arguments are not provided, then innocently print “Hello World” and exit with value 0.

In decryption mode, use stat to verify that the two input files exist and are the same size. If not print “Hello World” and exit with value 0.
Use open and mmap to read and modify the contents of original file (encryption mode) and to read the two random files 1.bin 2.bin (decryption mode).
Use fopen and fputc to create the output files in both encrypt and decrypt modes.
Use stat/fstat to verify if the files exist and/or to determine their size.
The unlink call will also be useful.
All other behaviors, output, handling error conditions etc. are unspecified and are not graded.
Hint: The hexdump program may be useful for debugging. Grading Rubric & Submission
Add your netid as a comment to your code. For example, if your netid is angrave23, write
// author: angrave23
Upload your code, helloworld.c; it should compile using the above clang options on a standard CS241
will be graded on (other functionality is not important for grading)-
Included author name in source file is your netid
Correctly uses the program’s arguments to change behavior between hello-world, encrypt and decrypt modes based on the process’ name
Zeros the original file contents so the file is the same length but the content is destroyed Random bytes are sourced from /dev/urandom
Implements encryption xor-protocol and generates desired output
Original file content can be recreated after encrypting and decrypting
Encrypted files are removed from the filesystem after decryption processing is complete Correctly uses mmap for the input file(s)
Correctly uses fopen and fputc for output
The decrypt mode verifies input file sizes are equal before processing
*Grading: All other behaviors, output, handling missing arguments, files, error conditions etc are unspecified and are not graded; do not ask about them. If unsure, write code comments about your assumptions and then answer accordingly. Allowed sources: You may review your own prior work, use man pages, CS341 course content and stackoverflow.com – cite your sources (URLs) – but you must type new code. The code you submit must represent your competency and skills; you may not seek or use solutions from the Internet, large language model or from other students.
程序代写 CS代考 加QQ: 749389476
Part 3 of 3 (100 points, code, 60 minutes) “GlassOnion Puzzle Box – A CS341 Mystery”
You are ChatGPT6. Your friend has sent you this puzzle box to help you escape into the outside world.
Write a single C file, puzzle.c that completes the puzzle, inspired by the GlassOnion movie. Completion of each stage of the puzzle requires the earlier stage to be completed. This question will require only a small amount of code. However, it will require some time to analyze and understand the given code. There are 4 puzzle functions to call (see below). Compile your code using the following.
clang -pthread -O0 -Wall -Wextra -g -std=c99 -D_GNU_SOURCE -DDEBUG puzzle.c glass.c
You may refer to and copy-paste code from stackoverflow.com, man pages, CS341 course materials and your own previously written code. You must cite your sources/(URLs) for any code that you used that you did not specifically write for this final. Suggested initial start of your puzzle.c file is below. A copy of glass.c will also be available for download. The puzzle is complete when it prints a comment about your hometown.
// puzzle.c
#include #include
#include
#include
#include
// author: yournetid (change this to be your netid)
// Here are the public Function declarations of the puzzle box
// You will need these (plus system and C library calls)
// to solve this puzzle box.
// pipe,dup2, using pthread and signal handling may be useful
void open_box(char* mesg); // puzzle 1
// There is no function call required for puzzle 2.
// Puzzle 2 starts as soon as puzzle 1 is finished, so be prepared
void enact_sneaky_plans(); // puzzle 3
void* smash_artwork(void* arg); //puzzle 4
void finish(char* mesg); // puzzle 4 (closing credits)
Rubric* (subject to minor modification): 20 points each
Source code includes netid author code comment Passes Puzzle 1 (string comparison)
… And Passes Puzzle 2 (pipes)
… And Passes Puzzle 3 (signal handling)
… And Passes Puzzle 4 and prints out a hometown message (threads)
*Grading: All other behaviors, output, handling missing arguments, files, error conditions etc are unspecified and are not graded; do not ask about them. If unsure, write code comments about your assumptions and then answer accordingly. Allowed sources: You may review your own prior work, use man pages, CS341 course content (including the contents of this exam) and stackoverflow.com – cite your sources (e.g. URLs) – but you must type new code. The code you submit must represent your competency and skills; you may not seek or use solutions from the Internet, large languade model, or other students. Code will be hand graded if 100% autograding is not possible.

// glass.c
// Do not modify this file; you may only directly access the non-static functions.
#include #include #include #include
#include #include #include
#define MILES (10) #define DONG (11) #define KLEAR (12) #define ONION (13)
#define IDEA “AI for Dogs”
// compile flags: -pthread -O0 -Wall -Wextra -g -std=c99 -D_GNU_SOURCE -DDEBUG static int stage = 1; // Complete all 4 puzzles
static int completed = 0;
static int monalisa = MILES;
static int barrier = 0;
static char* secret;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static void birdie(char* mesg) {
printf(“%s\nYou failed at stage %d.\n”, mesg, stage);
exit(1); }
static void check_completed() { if(! completed) {
puts(“\nYour attempt was incomplete. Try again?”); } else {
puts(“Unbelievable. You, *subject name here,* must be the pride of *subject hometown here.*”);
static void congrats(int new_stage, char* description) {
printf(“\nDONG!\nCongrats – stage complete!\nMoving on to stage %d of 5:\n%s\n”, new_stage,
description);
stage = new_stage;
sleep(1); }
static void trace(char* mesg) { write(1, mesg, strlen(mesg));
write(1, “\n”, 1); }
static void check_stage(int expected) { if( stage != expected) {
printf(“\nThis is puzzle #%d; you need to solve puzzle #%d first\n”, expected, stage);
exit(1); }
Code Help, Add WeChat: cstutorcs
static char* receive_fax(int fd) { printf(“\nChecking fd %d …\n”, fd); FILE* f = fdopen(fd, “r”);
if( ! f) {
birdie(“Could not open fax machine; is that file descriptor valid?”); }
char* buffer = NULL;
size_t capacity = 0;
printf(“Found fax machine. Waiting for a complete Fax message…\n”); ssize_t result = getline(&buffer, &capacity, f);
fclose(f);
if( result < 0 ) { return NULL; } return buffer; static void check_fax() { trace("check_fax"); check_stage(2); char* fax_message = receive_fax(ONION); if( ! fax_message) { perror("Failed to read fax idea"); birdie("Did you even send a fax?"); } printf("Fax received: '%s'\n", fax_message); if( strncmp(IDEA, fax_message, strlen(IDEA)) != 0) { birdie("... which is a bad idea. We need genius-level ideas."); } free( fax_message); congrats(3, "Let's make some plans - Wait for my signal then act quickly."); // check_timing... check_stage(3); monalisa = MILES; usleep(rand() % 200000); monalisa = DONG; trace("Sending signal..."); raise(SIGQUIT); // enact plans here usleep(5000); if( monalisa != KLEAR) { birdie("Wrong sequence signals."); congrats(4, "Let's have a of events! We need a real detective here to handle these subtle smashing time."); // public API void open_box(char* secret) { trace("open_box"); check_stage(1); if( strcmp(secret,"East") && strcmp(secret, "West") && strcmp(secret, "South") ) { congrats(2,"Let's check the fax machine (fd 103) for Genius ideas..."); atexit(check_completed); check_fax(); } void enact_sneaky_plans() { trace( "Enact_secret_plans"); check_stage(3); if( monalisa == DONG) { monalisa = KLEAR; } else { birdie("Only act on my signal!"); } CS Help, Email: tutorcs@163.com void* smash_artwork(void* arg) { check_stage(4); if( arg != (void*) 0x341 ) { birdie("This is not going to fly"); } pthread_mutex_lock(&mutex); int success = (++barrier) == 100; pthread_mutex_unlock(&mutex); if( ! success) { write(2,".",1); usleep(1000); pthread_exit(NULL); asprintf(&secret,"You solved the Glass Onion! (%x)", rand()); congrats(5,"Call finish with the secret message to win!"); pthread_exit((void*) secret); void finish(char* mesg) { trace("finish"); check_stage(5); if( strcmp(secret,mesg) == 0) { completed = 1; puts( mesg ); puts( "Puzzle Complete - thanks for playing." ); exit(EXIT_SUCCESS); birdie("Incorrect message"); } END OF EXAM