Due: Wednesday, April 23, 2025 11:59 pm
For this task, you will create examples of signal handling in C. This task should be part of the C semantics project. But the semantics project is very long already. So, this error-handling part has been moved to this project.
First, read the man pages on the signal package. In the terminal, you can use man to get the signal man pages.
man 3 signal
Write a program that can respond to a ctrl-C
(interrupt). Your main function should use the signal function to
set up a handler for the SIGINT signal and then enter an infinite
loop. Your handler function just needs to print out a message (e.g.
"Interrupted!") and then call exit();
Include a screenshot of your output to your repot.
Write a program that can handle a floating point exception. Your main function should use the signal function to set up a handler for the SIGFPE signal then do something inappropriate with floating point values, such as dividing by zero. See if you can continue the program's execution after the exception takes place.
Include a screenshot of your output to your repot.
Write a program that can handle a segmentation fault error. Your main function should set up a handler for the SIGSEGV signal then execute code that results in a segmentation fault (access illegal memory). Your handler should print something appropriate and exit.
Include a screenshot of your output to your repot.
The purpose of this task is to let you have some experience on File I/O in the C language. You are expected to implement a word counter that is able to count the number of occurrences of every word in a text file. The basic requirements are as follows.
the 17
of 7
and 6
was 5
in 4
with 4
a 3
windows 3
broken 2
wings 2
central 2
portion 2
had 2
been 2
but 2
up 2
were 2
these 1
blocked 1
wooden 1
It's okay if words with the same frequency are displayed in a different order.
In your report, include a screenshot of your output and illustrate how you implement the above requirements a, b, c, and d.
This task is to try and estimate the time cost of memory management in C.
What is the average time per call to allocate a certain amount of memory?
Try timing several hundred/thousand calls to make the estimate, and try the experiment with at least three different memory sizes (small, e.g. 100 ints, medium e.g. 10K ints, large e.g. 1 million ints). Note that the first time malloc is called, there are start-up costs, and it takes extra time. Do not include the first call to malloc in your timing.
You are expected to observe the time it takes to allocate a e range of amounts of memory. What are the trends relating the allocation time to the size of thh allocation? Is it more efficient to allocate all the bytes at once, or in smaller chunks? Answer these questions in your report, using at least one graph with your data to back up your explanation.
There are many ways to time code. You can pick the way that works for you best. One is to use the ftime library call, which returns a time stamp you can use to estimate total run time reasonably well. Don't try to use it to time small things. But it works ok to use for stuff that takes at least a quarter-second.
The second way to time code is to use a profiler. On a Mac, you can use Activity Monitor, which you can find in the Instrument. On a linux machine, you can use a tool called gprof. You can use gprof on any of the dwarves (linux machines) in the robotics lab.
Write C code to familiarize yourself with the process of a Mark and Sweep Garbage Collector. In an effort to keep this as short, but informative, as possible, we are going to represent the stack and heap with structs, and write main code that manipulates the stack and heap as if there were calls to malloc and assign. Your job will be to implement a Mark and Sweep algorithm. But first, we need to describe the setup.
For example, consider the main code
At Drawing 1, we draw the stack with the first entry containing the name of the variable and the reference to a HeapChunk. The heap has just one HeapChunk. The array to manage allocated HeapChunks points to the one HeapChunk. Its entry starts with 0 (it is unmarked), then has a second 0 (there are no other HeapChunks referenced), and ends with a null pointer (there are no other HeapChunks referenced by this one).

At Drawing 2, we have a second Var on the Stack and a second HeapChunk on the heap.

At Drawing 3, we have changed the value of the reference associated with "a"

After the mark phase, we have one HeapChunk marked (the first zero is now a 1). The HeapChunk with a zero for the marked value is garbage and should be cleaned up.

In a more complicated example, we have code that adds references to HeapChunks, creating a circular links. Consider the main code
Drawing 5 shows the memory after all the code has been run.
Your next job is to add code to construct a test example of your own, run the code, and draw an image for it, similar to the final image above. The code should simulate at least 3 stack variables, at least 4 allocations on the heap, and should have a reference cycle that can't be reached from the stack (i.e. it should be a good example of why reference counting isn't enough). Add a description of your example (a commented code snippet) and your drawing to your report.
This part is to do some research on memory management in your selected language.
Repeat the word-frequency task from part 1 in your selected language. Make use of built-in data types (like a dictionary), if possible, to make the task easier. Include a screenshot of your testing result for the word counter in your report.
On Google Sites, show how the selected language is able to read from and write to a file. If possible, you can use the above word counter in the selected language as an example. Otherwise, please give a short sample code to demonstrate.
Please note that it is your responsibility to explicitly indicate the extensions you have taken. If you take the extension in C, please indicate explicitly in the codes and README file. If you take the extensions in the selected languages, please indicate explicitly in your Google Sites report or source codes and README.
The submission of this project has two components:
Required Files::
.md filename extension. Each project should have a README file along with all your source codes. The README file will contain:
tree command to generate this.You can follow the instructions for the README file from Project 1.
To check whether you've made your report well organized, please follow the Organization part in the Google Sites section in this writing page. Remember to publish all your changes and include the homepage link of your CS333 Google Sites in the README file.
© 2025 Ying Li. Page last modified: .