CS3103_Project_Instruction_Doc_Opt_A (1)
CS3103_Project_Instruction_Doc_Opt_A (1)
Version 1.0
Note: All following steps are based on the given virtual machine which has been deployed with
all needed utilities. Issues caused by other development environments may exceed the scope of TA
support. If any questions, please contat Mr. SONG Ziwei via [email protected]
CS3103 Project Instruction Doc Option A
1 Supplement information
Before making introduce about Project (Option A), the basic usages of development environment
is shown as follows:
After correct import and startup of the given virtual machine (Please follows the guide
document provided), You may see the desktop view like the following figure. The marked folder
contains all needed files for Project (Option A).
It’s highly recommended to use VSCode as development platform, which has been already
installed in the virtual machine. Use VSCode to open the marked folder and start a new terminal
as the following figure depicts.
1
CS3103 Project Instruction Doc Option A
2
CS3103 Project Instruction Doc Option A
• Learn how to synchronize the execution of processes and coordinate the use of shared
memory segment with POSIX Semaphores.
• Learn how to coordinate the execution of multiple threads in multi-threaded programming
with POSIX Thread.
• Learn to understand the file system and related directory/file operations.
Project (Option A) has two problems: Problem 1 and Problem 2. You will implement two C
programs to solve Problem 1 and Problem 2, respectively. For both problems, your implementa-
tion should comply with the proposed requirements. Since completing former problem will help
you with the later one more smoothly, it is highly recommended that you implement the two
programs for Problem 1 and Problem 2 in order.
To help you get started, some C program files in a compressed file named project.zip are avail-
able on the Google Drive, which is in /public/cs3103/project/, including the following files/di-
rectories:
Project
| - - test_case / # The t e s t c a s e d i r e c t o r y f o r p r o b l e m 2 .
| - - helpers . c # Useful functions
| - - helpers . h # Useful functions
| - - problem1 . c # The s o u r c e c o d e f i l e f o r p r o b l e m 1 .
| - - problem2 . c # The s o u r c e c o d e f i l e f o r p r o b l e m 2 .
‘-- Makefile # I t can be u s e d t o c o m p i l e s o u r c e c o d e .
In helper.h file, there are some helper functions, which are implemented in helper.c file,
that may be used for your programs. Please read the comments carefully to understand the
functionalities of the functions before using them. The provided Makefile can be directly used to
compile problem1.c. If you want to use it to compile other source files, please modify Makefile
before using it. For Problem 1, we provide the sample code in problem1.c about how to create
process and use shared memory segment and semaphores. Please implement your program for
Problem 2 in the file named problem2.c, where we have provided the necessary coding logic.
Tips: You can also learn how the shared memory and POSIX semaphore work and how to
use them in your program from online pages. Here are some useful links:
• Shared memory: https://man7.org/linux/man-pages/man7/shm overview.7.html
• Semaphore: https://man7.org/linux/man-pages/man7/sem overview.7.html
The following table lists some functions with brief description that may be used in this project,
you can click the name of functions for detail descriptions:
3
CS3103 Project Instruction Doc Option A
4
CS3103 Project Instruction Doc Option A
3 Problem 1
3.1 Introduction
Problem 1 aims to help you understand how the shared memory and semaphores are used among
processes (or threads). Please implement your program in problem1.c, run it and answer the
questions about Problem 1.
In problem1.c, we use three types of variables, i.e., the global variable global param (line
17), the local variable local param (line 29), and the shared memory variables shared param p,
shared param c (line 30). The values of all variables are initialized to 0 (the shared memory is
automatically initialized).
We use the fork() system call to create a new child process. After that, the parent process and
child process will execute their own code independently and concurrently. The parent process
catches an input parameter in the command line from terminal (see 3.2 How to run Problem
1). The input parameter should be one of your group member’s CityU student
ID number, aka an eight-digit decimal number. Then the parent process assigns the
value of the input parameter to the three variables. The child process tries to get the input
parameter by reading the values of these variables and print them in terminal. We use the
semaphore PARAM ACCESS SEMAPHORE coordinate the operations on the variables between
the processes. In this way, we can ensure that only one process can access the variables at any
time, i.e., mutual exclusion.
5
CS3103 Project Instruction Doc Option A
6
CS3103 Project Instruction Doc Option A
adjacent digits among the eight digits of the eight-digit decimal number. See 3.5 Explanation
for more detail about the thread-to-digit mapping rule.
c)This function must create eight semaphores, each of which should be associated with
one digit of the eight-digit decimal number. The semaphore is used to ensure that only one
thread can access the corresponding digit at any time. When a thread tries to access one digit,
it must first be able to lock the corresponding semaphore.
d)You should design a strategy to ensure mutual exclusion when the threads access the
digits. Specifically, only when a thread can lock two semaphores at the same time, it can access
the two corresponding digits and further proceed to execute operations. Please use the semaphore
to achieve this. Any other mechanisms are NOT allowed and will be graded with zero points.
e)Once a thread can access the two digits, it can read their values and increase each by 1
respectively. If a digit’s value reaches ten after adding 1, the thread should reassign modulo 10
of the value to it, since the number is decimal. DON’T do carry in operation. Once the thread
finishes the above operation, we say it executes an addition operation.
f )Once a thread is created or it finishes an addition operation, it must try to lock the
semaphores immediately until it finishes all the addition operations (depending on num of operations).
g)When all eight child threads finish their addition operations, the master thread should
write the modified eight-digit decimal number into a text file by calling the saveResult() function,
which is in helper.h. The function has two input parameters, i.e., the name of the text file (must
be p1 result.txt for Problem 1) and a long int type number, such as the modified eight-digit
decimal number for Problem 1.
Warnings!
• Please define the name of a semaphore as ”xxx GROUP NAME”, where GROUP
is your group number, NAME is your full name, and ”xxx” can be any char-
acters for your own usage.
• Please close and delete your semaphores in the parent process by calling sem close()
and sem unlink() functions. Read the code in problem1.c to learn how to do
this.
3.5 Explanation
Here we introduce the detail about the thread-to-digit mapping rule (as stated in 3.4 Problem
1 Requirements b)). For example, the chosen student ID is 56781234. We can assign an
index to each of the eight digits from the most significant digit to the least significant digit (i.e.,
from left to right):
• digit1 = 5
• digit1 = 6
7
CS3103 Project Instruction Doc Option A
• digit1 = 7
• digit1 = 8
• digit1 = 1
• digit1 = 2
• digit1 = 3
• digit1 = 4
Suppose that each thread has an index corresponding to the order in which they were created.
For example, the first thread created can be named thread1 , the second thread created can be
named thread2 , and so on until the eighth thread created is named thread8 .
Then, the thread-to-digit mapping rule is shown in the following figure. The first thread1
should access the digit1 and digit2 , the second thread2 should access the digit2 and digit3 , and
so on. Note that the ninth thread8 should access the digit8 and digit1 .
8
CS3103 Project Instruction Doc Option A
4 Problem 2
4.1 Introduction
In Problem 2, you are required to implement a word counter to automatically count the number
of words in text files (with suffix.txt). You should achieve this with a C program using two
processes (named parent process and child process respectively). The parent process reads text
files (with suffix.txt) under the given directory (which can be obtained from the input parameter
of this program). Then the child process counts the number of words in those text files by
calling the wordCount() function in helpers.h (implemented in helpers.c). Two processes
communicate with each other using a shared memory. You should write your code in the provided
problem2.c file.
The following table lists some functions may be used for your program implementation:
9
CS3103 Project Instruction Doc Option A
g)In addition, the child process should correctly find a txt file, where the last number of
words in the file is equal to the last number of the chosen student ID (A different ID
number from problem 1), and the child process needs to print the name of the txt file. For
example, the chosen student ID is 56781234, the txt file “CityU.txt” has 274 words, then
the child process should print “CityU.txt” on screen.
Warnings!
• Please detach your processes (including the parent process and child process)
from the shared memory by calling shmdt() if it will not be used.
• Please delete the shared memory in the parent process by calling shmctl() if
it will not be used. Read the code in problem1.c carefully to learn how to do
this.
10
CS3103 Project Instruction Doc Option A
11
CS3103 Project Instruction Doc Option A
5 Grading
We will grade your work with the following three considerations:
• Design (Project Report) 30%
• Implementation (Code) 30%
12
CS3103 Project Instruction Doc Option A
6 Submission
Please submit the following files:
• Source code files (with suffixes “.h” and “.c”), (at least) including problem1.c, problem2.c,
helpers.h, helpers.c, and Makefile (if any).
• The compiled executable files. The executable files should be named as “problem1” and
“problem2” for two problems, respectively.
• Project report in PDF format based on report template for Project A. It should be named
as “XX-report.pdf ”, where XX represents your group number. For example, if you are in
group 12, then your submitted report should be named as “12-report.pdf”. Please list the
names, student numbers, and emails of all group members at the beginning of the project
report and describe each team member’s contribution.
Please organize and compress the above listed files in a zip format file (named as “XX-
project.zip”, where XX is your group number):
/ XX - project
| - - helpers . c
| - - helpers . h
| - - ... # Any o t h e r s o u r c e c o d e f i l e s .
| - - Makefile # I f any .
| - - problem1 # The c o m p i l e d e x e c u t a b l e f o r p r o b l e m 1 .
| - - problem1 . c # The s o u r c e c o d e f i l e f o r p r o b l e m 1 .
| - - problem2 # The c o m p i l e d e x e c u t a b l e f o r p r o b l e m 2 .
| - - problem2 . c # The s o u r c e c o d e f i l e f o r p r o b l e m 2 .
| - - XX - report . pdf # The p r o j e c t r e p o r t f i l e .
Submit to CANVAS Self-sign-up is enabled for groups. Instead of all students having to
submit a solution to the project, Canvas allows one person from each group to submit on behalf
of their team. If you work with partner(s), both you and your partner(s) will receive the same
grade for the project.
When you’re ready to hand in your work, go to the course site in Canvas, choose the ”As-
signments” section ¿ ”Project” item ¿ ”Start Assignment” button, and upload your compressed
zip file named as “XX-project.zip”, where XX is your group number.
13
CS3103 Project Instruction Doc Option A
7 Academic Honesty
All work must be developed by each group independently. Please write your own code. All
submitted source code will be scanned by anti-plagiarism software. We will both test
your code and carefully check your source code. If your submitted code does not work,
please indicate it in the report clearly.
14