The title of this blog post covers pretty much everything I want to say... Our first C assignment in college was tough as hell, but I also found it amazingly rewarding. Before we were given this assignment, we had pretty much zero practice or knowledge of C, only what we learned from previous C++ assignments. We were thrown in deep and expected to float. As a software engineer I assume that's how things will work from now on as I enter this career.
My only experience with C before this assignment was using gdb to exploit simple buffer overflows or format string vulnerabilities or the likes in wargames/ctf's or challenges I may have tried. Of course I learned quite a bit from such exercises, but not enough to easily breeze through the challenge this assignment presented. It dealt with Posix threads and signals.
The problem simplified:
Write a C program that prints out all the words in a file. One thread prints out the first word and the Second thread prints out the next word, the first then prints the third word and the cycle continues until the end of the file.
There were also a few other rules added to make it that little bit extra difficult.
1. After reading a word from the file but before printing it to the screen each printing thread must sleep for a random number of microseconds (up to 10000).
2. A main thread coordinates the actions of the two printing threads Unix signals are used for inter-thread communication and to avoid busy-waiting.The main thread cancels the other threads when the last word has been printed
3. Each cancelled thread prints out a farewell message as it exits.
4. Can't use semaphores or mutexes of any kind...
This is the challenge... Thankfully the assignment had to be completed in groups of two. It was nice to bounce ideas off another wizard of a person/programmer (Vadimck). I feel it helped greatly with the learning process. We ran into many issues during the process, memory access issues, signals causing the whole program to end, or individual threads to shut down. All the wonderful segmentation faults one would expect in their first time playing with C. It was a very enjoyable/hard learning curve, I feel my C programming has come on leaps and bounds. I'm not afraid anymore to lookup docs or disassemble using gdb to find bugs. Our solution worked very well, we even tried it with a dictionary file. It ate the bastard.
Without further adieu, here is Our Amazing Assignment Solution.. It can be compiled with gcc -pthread -o assignment assignment.c You also need to pass in the text file, so run ./assignment filename.txt. In C it takes a lot longer to do things right but when you do it's so rewarding. I really want to give myself a few more projects in it to get more acquainted with this sexC beast.
In other unrelated news, I've been working hard on both college and work. I also acquired myself an internship from April 2013 - September 2013 in HEAnet.ie as a Network Engineer. Hopefully I will come out the other end a well experienced networking wizard! DELIGHTED!
HAX BRAH.