Homework 2

Computer Architecture I ShanghaiTech University
HW1 (in Autolab)    HW2 HW3

Task 1: Ring Buffer implementation in C

In this task, you are required to implement a ring buffer. We have provided a template for you here. You should implement all functions in ringbuffer.h. We also provide you with a main.c, which contains several simple testcases.

Make sure that the compilation of the source code generates no errors/warnings and conformity to C89 standard. We have provided a Makefile, you can run make to compile your code. You can run make memcheck to check whether there is any memory leak (you will need to install valgrind first). You can also run make clean to delete all compiled files.

Consider Learning Before you start

Implementation Details

You are required to implement a ring buffer with the following rules:

Other Requirements

Task 2: Libraries

For all tasks assume that ringbuffer.h will be provided in the working directory and your ringbuffer.c will be copied to the current working directory.

Task 2.1: Static Library

Instructions

Write a shell script named staticlib.sh that when invoked, produces a static library named libringbuffer.a from the source files ringbuffer.c and ringbuffer.h.

Then create a static-linked executable staticringbuffer from libringbuffer.a and test.c. Do not execute the program in the shell script.

You have to use the command ar to create the static library. Then use the command ld to create the executable file.

Task 2.2 Dynamic-link Library

Instructions

Write a shell script named dynamiclib.sh that when invoked, produces a dynamic-linked library named libringbuffer.so from ringbuffer.c.

You should use gcc to create a test.o from test.c, and use ld to link test.o and libringbuffer.so to create an executable called dynamicringbuffer. But that might be a little difficult. So it is also just fine to just use gcc for this task (2.2). Do not execute the program in the shell script.

Submission

You should submit a compressed file named as hw2.tar to autolab.

The directory tree of your submission should look like the following :

├── ringbuffer.c
├── dynamiclib.sh
└── staticlib.sh

You can create your submission with make submit, and it will create hw2.tar for you, including all three files needed. DO NOT include main.c in your submission.

Test Environment

The test environment on autolab is Ubuntu 16.04 with gcc 5.x