• Stars
    star
    382
  • Rank 108,532 (Top 3 %)
  • Language
    C
  • License
    Other
  • Created over 5 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

C Programming Lab: Assessing Your C Programming Skills

lab0-c

Assessing Your C Programming Skills

This lab will give you practice in the style of programming you will need to be able to do proficiently, especially for the later assignments in the class. The material covered should all be review for you. Some of the skills tested are:

  • Explicit memory management, as required in C.
  • Creating and manipulating pointer-based data structures.
  • Working with strings.
  • Enhancing the performance of key operations by storing redundant information in data structures.
  • Implementing robust code that operates correctly with invalid arguments, including NULL pointers.

The lab involves implementing a queue, supporting both last-in, first-out (LIFO) and first-in-first-out (FIFO) queueing disciplines. The underlying data structure is a circular doubly-linked list, enhanced to make some of the operations more efficient.

Prerequisites

There are a few prerequisites which must be installed on your machine before you will be able to build and run the autograders.

The following command will install all required and optional dependencies on Ubuntu Linux 20.04 or later:

$ sudo apt install build-essential git clang-format cppcheck aspell colordiff valgrind

Some distros like Arch Linux won't install aspell-en with aspell, and you must install it explicitly:

$ sudo pacman -S aspell-en

Note: Cppcheck version must be at least 1.90, otherwise it might report errors with false positives. You can get its version by executing $ cppcheck --version. Check Developer Info for building Cppcheck from source.

Integrate clang-format to vim

If you want to run clang-format automatically after saving with vim, clang-format supports integration for vim according to Clang documentation.

By adding the following into $HOME/.vimrc

function! Formatonsave()
  let l:formatdiff = 1
  py3f <path-to-clang-format.py>/clang-format.py
endfunction
autocmd BufWritePre *.h,*.hpp,*.c,*.cc,*.cpp call Formatonsave()

Then, it has zero-effort integration into the coding workflow since it can handle formatting changes while saving a file. Note: on Ubuntu Linux 18.04, the path to clang-format.py is /usr/share/vim/addons/syntax/.

Running the autograders

Before running the autograders, compile your code to create the testing program qtest

$ make

Check the correctness of your code, i.e. autograders:

$ make test

Check the example usage of qtest:

$ make check

Each step about command invocation will be shown accordingly.

Check the memory issue of your code:

$ make valgrind
  • Modify ./.valgrindrc to customize arguments of Valgrind
  • Use $ make clean or $ rm /tmp/qtest.* to clean the temporary files created by target valgrind

Extra options can be recognized by make:

  • VERBOSE: control the build verbosity. If VERBOSE=1, echo eacho command in build process.
  • SANITIZER: enable sanitizer(s) directed build. At the moment, AddressSanitizer is supported.

Using qtest

qtest provides a command interpreter that can create and manipulate queues.

Run $ ./qtest -h to see the list of command-line options

When you execute $ ./qtest, it will give a command prompt cmd> . Type help to see a list of available commands.

Files

You will handing in these two files

  • queue.h : Modified version of declarations including new fields you want to introduce
  • queue.c : Modified version of queue code to fix deficiencies of original code

Tools for evaluating your queue code

  • Makefile : Builds the evaluation program qtest
  • README.md : This file
  • scripts/driver.py : The driver program, runs qtest on a standard set of traces
  • scripts/debug.py : The helper program for GDB, executes qtest without SIGALRM and/or analyzes generated core dump file.

Helper files

  • console.{c,h} : Implements command-line interpreter for qtest
  • report.{c,h} : Implements printing of information at different levels of verbosity
  • harness.{c,h} : Customized version of malloc/free/strdup to provide rigorous testing framework
  • qtest.c : Code for qtest

Trace files

  • traces/trace-XX-CAT.cmd : Trace files used by the driver. These are input files for qtest.
    • They are short and simple.
    • We encourage to study them to see what tests are being performed.
    • XX is the trace number (1-17). CAT describes the general nature of the test.
  • traces/trace-eg.cmd : A simple, documented trace file to demonstrate the operation of qtest

Debugging Facilities

Before using GDB debug qtest, there are some routine instructions need to do. The script scripts/debug.py covers these instructions and provides basic debug function.

$ scripts/debug.py -h
usage: debug.py [-h] [-d | -a]

optional arguments:
  -h, --help     show this help message and exit
  -d, --debug    Enter gdb shell
  -a, --analyze  Analyze the core dump file
  • Enter GDB without interruption by SIGALRM.
$ scripts/debug.py -d
Reading symbols from lab0-c/qtest...done.
Signal        Stop	Print	Pass to program	Description
SIGALRM       No	No	No		Alarm clock
Starting program: lab0-c/qtest 
cmd> 
  • When qtest encountered Segmentation fault while it ran outside GDB, we could invoke GDB in the post-mortem debugging mode to figure out the bug.

    The core dump file was created in the working directory of the qtest.

    • Allow the core dumps by using shell built-in command ulimit to set core file size.
    $ ulimit -c unlimited
    $ ulimit -c
    unlimited
    
    • Enter GDB and analyze
    $ scripts/debug.py -a
    Reading symbols from lab0-c/qtest...done.
    [New LWP 9424]
    Core was generated by `lab0-c/qtest'.
    Program terminated with signal SIGSEGV, Segmentation fault.
    #0 ...
    #1 ... (backtrace information)
    #2 ...
    (gdb) 
    

User-friendly command line

linenoise was integrated into qtest, providing the following user-friendly features:

  • Move cursor by Left and Right key
  • Jump the cursor over words by Ctrl-Left and Ctrl-Right key
  • Get previous or next command typed before by up and down key
  • Auto completion by TAB

Built-in web server

A small web server is already integrated within the qtest command line interpreter, and you may use it by running the web command in its prompt.

$ ./qtest
cmd> web
listen on port 9999, fd is 3

Run the following commands in another terminal after the built-in web server is ready.

$ curl http://localhost:9999/new
$ curl http://localhost:9999/ih/1
$ curl http://localhost:9999/ih/2
$ curl http://localhost:9999/ih/3
$ curl http://localhost:9999/sort
$ curl http://localhost:9999/quit

License

lab0-c is released under the BSD 2 clause license. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

External source code:

More Repositories

1

lkmpg

The Linux Kernel Module Programming Guide (updated for 5.0+ kernels)
TeX
6,998
star
2

shecc

A self-hosting and educational C optimizing compiler
C
1,042
star
3

rv32emu

Compact and Efficient RISC-V RV32I[MAFC] emulator
C
327
star
4

simplefs

A simple native file system for Linux kernel
C
308
star
5

concurrent-programs

Complementary Concurrency Programs for course "Linux Kernel Internals"
C
301
star
6

jitboy

A Game Boy emulator with dynamic recompilation (JIT)
C
288
star
7

cpumemory-zhtw

Traditional Chinese translation of "What Every Programmer Should Know About Memory"
CSS
216
star
8

semu

A minimalist RISC-V system emulator capable of running Linux kernel
C
214
star
9

vwifi

A virtual wireless device driver for Linux
C
174
star
10

kvm-host

A minimalist type 2 hypervisor using Linux Kernel Virtual Machine (KVM)
C
137
star
11

pitifulvm

A shabby implementation of Java virtual machine in C
C
137
star
12

vcam

Virtual camera device driver for Linux
C
89
star
13

sehttpd

A small and efficient web server with 1K lines of C code
C
82
star
14

cserv

An event-driven and non-blocking web server
C
70
star
15

concurrent-ll

concurrent linked list implementation
C
68
star
16

khttpd

An experimental HTTP server implemented as Linux kernel module
C
60
star
17

rv32emu-legacy

RISC-V RV32I[MA] emulator with ELF support
C
47
star
18

raycaster

Wolfenstein 3D-style raycasting implementation
C
42
star
19

linux-list

Linux-like doubly-linked list
C
39
star
20

fibdrv

Linux kernel module that calculates Fibonacci numbers
Shell
37
star
21

gameboy-emu

An efficient and portable Game Boy emulator
C
36
star
22

lkm-hidden

A Linux kernel module which hides itself
C
29
star
23

rubi

Ruby-like high-performance script programming language with JIT compilation
C
25
star
24

kecho

A lightweight echo server implementation in Linux kernel mode
C
25
star
25

threaded-logger

Threaded Logger
C
21
star
26

threadkit

A collection of lightweight threading utilities
C
20
star
27

vinput

A collection of virtual input device drivers for Linux
C
20
star
28

linux-cfs-sim

Simulate Linux Completely Fair Scheduler (CFS) using POSIX Threads
C
18
star
29

rnnoise

A noise suppression library based on a recurrent neural network
C
17
star
30

kcalc

Math expression evaluation as Linux kernel module
C
16
star
31

dict

Ternary Search Tree + Bloom filter
C
15
star
32

jitcalc

A simple integer calculator using JIT compilation
C
15
star
33

y86_64-tools

Y86-64 Tools: assembler, simulator, Verilog designs
C
14
star
34

fastcat

A faster "cat" implementation using splice and sendfile system calls
C
13
star
35

neocon

A simple serial console utility
C
13
star
36

bignum

An incomplete arbitrary-precision integer arithmetic library
C
13
star
37

fiber

A User Space Threading Library
C
13
star
38

compute-pi

Leibniz formula for π
C
12
star
39

ca2023-lab3

Lab3: Construct a single-cycle CPU with Chisel
Scala
12
star
40

mapreduce

A simple C Thread pool implementation
C
12
star
41

prefix-search

Implement prefix search using ternary search tree
C
12
star
42

jit-construct

JIT compiler from scratch, derived from Nick Desaulniers' great work
Lua
11
star
43

datalab

Improved CS:APP Data Lab
C
9
star
44

buddy

Buddy Memory Allocator
C
8
star
45

moxiebox

A secure, sandboxed execution mechanism that enables deterministic input, processing and output
C
8
star
46

phonebook

sample phonebook program to illustrate the impact of cache miss
Shell
8
star
47

kilo

A text editor in less than 1000 LoC with syntax highlight and search
C
8
star
48

raytracing

Small ray tracing program for performance evaluation
C
8
star
49

intrusive-ds

A collection of intrusive data-structures for C
C
8
star
50

concurrency-primer

Concurrency Primer
TeX
8
star
51

gecos

GECOS: A lock-free synchronization mechanism
C
7
star
52

nyancat

Nyancat rendered in your terminal
C
6
star
53

matrix_oo

Sample matrix implementation illustrating object-oriented techniques in C99
Shell
6
star
54

dont-trace

A simple Linux kernel module that kills ptrace tracer and its tracees
C
6
star
55

kfifo-examples

Linux kernel module examples about kfifo
C
5
star
56

mergesort-concurrent

merge sort on singly-linked list utilzing POSIX Thread
C
5
star
57

tinymembench

Measure peak bandwidth of sequential memory accesses and the latency of random memory accesses
C
5
star
58

align-bench

Microbenchmark for unaligned memory access
C
4
star
59

cirbuf

Circular Buffer implementation with mmap(2) *incomplete*
C
4
star
60

kcalc-fixed

Math expression evaluation as Linux kernel module, fixed-point implementation
C
4
star
61

prefetcher

Evaluate the effects of prefetching
Shell
3
star
62

malloc-test-concurrent

concurrent malloc benchmark
C
3
star
63

sched-plugin

A Linux kernel module to allow user processes being handed out with LKM based scheduler
C
3
star
64

phonebook-concurrent

build a phonebook program by concurrent linked list
C
3
star
65

simrupt

A Linux device driver that simulates interrupts
C
3
star
66

tco-test

Test the ability of C compilers performing Tail Call Optimization
C
2
star
67

balanced-ternary

Ilustrate how balanced ternary works
Shell
2
star
68

vsnd

Virtual Linux soundcard driver
C
2
star
69

bf-runtime

Brainf*ck runtime engine
C
1
star
70

quotient-filter

(Incomplete) in-memory quotient filter
C
1
star
71

ksort

Linux kernel module that implements and validates sorting algorithms
C
1
star
72

arm-assembler-latex-listings

Arm Assembler language definition for the LaTeX listings package
TeX
1
star
73

clz-tests

Evaluate implementations of count leading zero
C
1
star
74

rv32emu-demo

HTML
1
star