linux2021
contributed by <Billy4195
>
web
. Notice: qtest
should be able to receive other commands when the web server is running.
FORK_COUNT
to 0 and use coroutine to replace the original system call.qtest
(Create an new GitHub repository)queue_t
For the extra attributes that I added is an pointer tail
to point the tail element of the queue, and size
to store the number of elements in queue.
These two attributes are needed for the constant execution time for q_insert_tail
and q_size
q_new()
Allocate the memory for queue_t
, if the allocation fail then return NULL as SPEC required. If success, initialize the attributes
q_free()
q_free
is used to free all the allocated memory space inside the queue struct. It is better to free the allocated memory from the inner of struct to outer. Otherwise, if we free the outer struct without recording the inner allocated memory, then we may cause memory leak because doesn't free the inner one, or read from the freed outer memory is also an dangerous operation.
As a result, we just traverse every element inside of the queue and using next
to store the next element. Firstly, free the value memory, then free the element itself. After freed all the elements in the queue, we could finally free the queue_t
itself.
q_insert_head()
The point of this function is to allocate the space for new element. Because all the allocated memory by malloc
will be tracked, we can't use strdup
function to allocate memory for the given string.
The tail
pointer should also be setup if it has not been set.
I should take carefully of malloc
failure, as line 9 and line 14 are two check for the malloc
. Especially for lineine 14, don't forget to freed the allcated space for element
q_insert_tail
The SPEC required us to implement an O(1) time complexity solution. As mentioned above, we have added a tail
pointer to take care of this requirement. To achieve it, we just add element after the tail
element and update the tail
pointer.
The head
pointer should also be check and set, if it has not been set.
I should also take care the malloc
failure as we did in q_insert_head
.
q_remove_head
As the requirement, we store the string of the head element in given sp
pointer in L8~L13. At line 14 check if the head is the only element in the queue, if yes then we reset the tail
pointer, too.
q_size
To achieve the O(1) time complexity, I use the added size attribute to record the size of the queue.
Check the corner case when the given queue is NULL.
q_reverse
I use to pointer left
and right
to traverse the queue and make the next
pointer of each element point to the previous element to finish the reverse
q_sort
I choose quick sort as the implementation ofq_sort
. However, the worse case for quick sort may cause O(N^2) time complexity.
After finish the q_sort
, some related test passed, but some are still failed.
As the SPEC mentioned, we need to fix the problem of address sanitizer, and and we can see the error message show at Line 15 that it is related to echo
in console.c
Check console.c:307 in do_help_cmd()
, we can see the problem happens at calling report
function. While the error said that it's related to the global variableecho
in console.c:59. So we could guess it has something to do with param_ptr
.
Then we check the definition of param_ptr
for plist
and the call to add echo
command in console.c
.
We can see that the echo
is declared as a bool type and in add_param()
using an int
pointer to store it in param_ele
that is the cause for the address sanitizer error. To fix this problem we can just change the type from bool
to int
for the echo
variable.
simulation
didn't cause the same error as echo
, since it it also declared as bool
typeNotes: sorting without problem https://hackmd.io/@jasonmatobo/2021q1_homweork_lab0
merge sort without error https://hackmd.io/@gyes00205/2021q1_lab0