이중 링크 된 목록을 두 부분으로 구현하려고합니다 : 첫 번째는 목록을 만드는 실제 함수입니다. 두 번째는 시뮬레이터입니다. 몇 개의 스레드, 독자 및 작성자 (각각 while 루프에서 이중 링크 목록으로 이동하고 푸시), 목록이 너무 커지면 정리하는 가비지 수집기 스레드 (argv에 따라) . 목록은 스레드 안전을 위해 mutex와 조건부 변수를 사용합니다. 그러나 언제든지 실행할 때마다 이중 자유/메모리 손상 (빠른) 오류가 발생하고 이유를 알 수 없습니다. 나는 약간의 도움에 감사드립니다.더블 링크 된 목록 - 메모리 손상
#include <pthread.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#define USAGE_ERR "Usage: ./hw3 <WNUM> <RNUM> <MAX> <TIME>\n"
/* Argc parameter indexing consts */
#define INPUT_WNUM_IDX 1
#define INPUT_RNUM_IDX 2
#define OUTPUT_MAX_IDX 3
#define OUTPUT_TIME_IDX 4
typedef struct node {
int value;
struct node * previous;
struct node * next;
}
node;
typedef struct {
int length;
node * head;
node * tail;
pthread_mutex_t mutex;
}
list;
list * global_list;
int stop_threads = 0;
int MAX_LIST_SIZE;
pthread_cond_t gc_cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t gc_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t read_mutex = PTHREAD_MUTEX_INITIALIZER;
list * initlist_create();
void initlist_destroy(list * target_list);
list * initlist_push_head(list * target_list, int value);
int initlist_pop_tail(list * target_list);
void initlist_remove_last_k(list * target_list, int k);
int initlist_size(list * target_list);
pthread_mutex_t initlist_get_mutex(list * target_list);
void writer_thread();
void reader_thread();
void garbage_collect_thread();
list * initlist_create() {
size_t list_size = sizeof(list);
list * new_list = (list *) malloc(list_size);
new_list->head = NULL;
new_list->tail = NULL;
new_list->length = 0;
if (0 != pthread_mutex_init(& (new_list->mutex), NULL)) {
exit(errno);
}
return new_list;
}
void initlist_destroy(list * target_list) {
node * current_node;
node * temp_node;
if (0 != pthread_mutex_lock(& (target_list->mutex))) {
exit(errno);
}
if (0 != target_list->length) {
current_node = target_list->head;
while (current_node != NULL) {
temp_node = current_node->next;
free(current_node);
current_node = temp_node;
}
}
if (0 != pthread_mutex_unlock(& (target_list->mutex))) {
exit(errno);
}
if (0 != pthread_mutex_destroy(& (target_list->mutex))) {
exit(errno);
}
free(target_list);
}
list * initlist_push_head(list * target_list, int value) {
node * new_node;
size_t node_size = sizeof(node);
if (0 != pthread_mutex_lock(& (target_list->mutex))) {
exit(errno);
}
new_node = (node *) malloc(node_size);
if(NULL == new_node)
{
printf("Malloc failed\n");
exit(errno);
}
new_node->value = value;
if (NULL != target_list->head) {
new_node->next = target_list->head;
new_node->previous = target_list->tail;
target_list->head->previous = new_node;
target_list->tail->previous = new_node;
}
else{
target_list->tail = new_node;
}
target_list->head = new_node;
target_list->length++;
if(0 < target_list->length){
pthread_cond_signal(&read_cond);
}
if (0 != pthread_mutex_unlock(& (target_list->mutex))) {
exit(errno);
}
return target_list;
}
int initlist_pop_tail(list * target_list) {
int deleted_node_value;
if (0 != pthread_mutex_lock(& (target_list->mutex))) {
exit(errno);
}
while(0 == target_list->length){
pthread_cond_wait(&read_cond, &target_list->mutex);
}
node * last_node = target_list->tail;
if (1 == target_list->length) {
last_node = target_list->head;
target_list->head = NULL;
target_list->tail = NULL;
} else {
target_list->tail = last_node->previous;
target_list->tail->next = target_list->head;
}
deleted_node_value = last_node->value;
// IMPORTANT
// If i uncomment this, segfault and memory corruption,no idea why
//free(last_node);
target_list->length--;
if (0 != pthread_mutex_unlock(& (target_list->mutex))) {
exit(errno);
}
// if (0 != pthread_mutex_unlock(& (read_mutex))) {
// exit(errno);
// }
return deleted_node_value;
}
void initlist_remove_last_k(list * target_list, int k) {
int remove_size = k;
if (0 != pthread_mutex_lock(& (target_list->mutex))) {
exit(errno);
}
if (k > target_list->length)
{
remove_size = target_list->length;
}
if (0 != pthread_mutex_unlock(& (target_list->mutex))) {
exit(errno);
}
for (int i = 0; i < remove_size; ++i) {
initlist_pop_tail(target_list);
}
}
int initlist_size(list * target_list) {
int list_size;
if (0 != pthread_mutex_lock(& (target_list->mutex))) {
exit(errno);
}
list_size = target_list->length;
if (0 != pthread_mutex_unlock(& (target_list->mutex))) {
exit(errno);
}
return list_size;
}
pthread_mutex_t initlist_get_mutex(list * target_list) {
pthread_mutex_t list_mutex;
if (0 != pthread_mutex_lock(& (target_list->mutex))) {
exit(errno);
}
list_mutex = target_list->mutex;
if (0 != pthread_mutex_unlock(& (target_list->mutex))) {
exit(errno);
}
return list_mutex;
}
void writer_thread()
{
while (1)
{
if(stop_threads){
return;
}
if (MAX_LIST_SIZE < initlist_size(global_list))
{
pthread_cond_signal(&gc_cond);
}
initlist_push_head(global_list, rand());
}
}
void reader_thread()
{
while (1)
{
if(stop_threads){
return;
}
if (MAX_LIST_SIZE < initlist_size(global_list))
{
pthread_cond_signal(&gc_cond);
}
initlist_pop_tail(global_list);
}
}
void garbage_collect_thread()
{
while(1){
if(stop_threads){
return;
}
pthread_cond_wait(&gc_cond, &gc_mutex);
int remove_count = (initlist_size(global_list)/2);
initlist_remove_last_k(global_list, remove_count);
printf("GC – %d items removed from the list\r\n", remove_count);
if (0 != pthread_mutex_unlock(& (gc_mutex))) {
exit(errno);
}
}
}
int main(int argc, char * * argv) {
/* Validate arguments */
if (5 != argc) {
printf(USAGE_ERR);
exit(errno);
}
int writers_count = atoi(argv[INPUT_WNUM_IDX]);
int readers_count = atoi(argv[INPUT_RNUM_IDX]);
int max_run_time = atoi(argv[OUTPUT_TIME_IDX]);
MAX_LIST_SIZE = atoi(argv[OUTPUT_MAX_IDX]);
global_list = initlist_create();
pthread_t garbage_collector_thread;
pthread_t writer_threads[writers_count];
pthread_t reader_threads[readers_count];
if (0 != pthread_create(&garbage_collector_thread, NULL, garbage_collect_thread, NULL))
{ exit(errno);
}
for (int i = 0; i < writers_count; ++i)
{
if (0 != pthread_create(&writer_threads[i], NULL, writer_thread, NULL))
{
exit(errno);
}
}
for (int x = 0; x < readers_count; ++x)
{
if (0 != pthread_create(&reader_threads[x], NULL, reader_thread, NULL))
{
exit(errno);
}
}
sleep(max_run_time);
//IMPORTANT
//threads should die after this is set,but all threads wait for the mutex to free
//in gcc - see "info threads" command in gdb
stop_threads = 1;
int list_size = initlist_size(global_list);
printf("List size: %d", list_size);
for (int i = 0; i < list_size; ++i)
{
continue;
printf("num is %d", initlist_pop_tail(global_list));
}
//this gets stuck also when waiting for mutex obviously...
initlist_destroy(global_list);
}
참고 : '... 1보다 큰 주석'주석의 흥미로운 유니 코드 문자 제거 문제를 해결할 것을 제안합니다. 문제의 일부가 아닌 것으로 가정합니다. – chux
제거 - 그러나 주석은 컴파일하는 동안 스트라이프가됩니다 ... – lippy1234
아마도'void reader_thread()'->'void * reader_thread (void * x_void_ptr)'입니다. 모든 경고를 활성화하여 발견 - 디버그 시간을 절약하고 사용해보십시오. [참조] (http://timmurphy.org/2010/05/04/pthreads-in-ca-minimal-working-example/) – chux