circular linked list not working properly in c windows

痴心易碎 提交于 2019-12-13 03:16:21

问题


I tried to implement circular linked list to manage a set of tasks by a server side application. The application is multi-threaded where one thread (updater() ) reads the linked list only for read while another two (push_stream() and delete_stream()) access the linked list to add to and delete from the linked list respectively.

My problem is not all the files to be deleted (after being processed) are deleted.

 struct data_stream
{
    bool processed;
    int count;
    char filename[30];
    int TYPE_GRP;
    int task_type;
    struct data_stream *next;
};

struct data_stream *stream_head=NULL; //global variable 

main(partial code)

main()
{

_beginthread(updater, 0, NULL);

while ((new_socket = accept(srv_sock, (struct sockaddr *)&client, &c)) != INVALID_SOCKET)
    {
        _beginthreadex(0, 0, handle_client, &new_socket, 0, 0);
    }
} 

handle_client function (partial code)

 handle_client()
    {

//some where in handle_client    
    EnterCriticalSection(&stream_lock);
    stream_head = push_stream(&stream_head, TYPE_GRP, task_type);
    LeaveCriticalSection(&stream_lock);

    }

updater function (full source code)

void updater(void *data)
{
    while (1)
    {
        struct data_stream*temp = stream_head;
        struct data_stream*first = stream_head;
        struct data_stream*prev = NULL;
        if (stream_head != NULL)
        {
            struct data_stream*next = NULL;
            do
            {
                next = temp->next;
                if (temp->processed == false&&temp->task_type == 2)
                {
                   process_files(temp); 
                }
                else if (temp->processed == false&&temp->task_type == 3)
                    {
                    process_others();
                    }
                EnterCriticalSection(&stream_lock);
                temp->processed = true;
                LeaveCriticalSection(&stream_lock);
                temp = next;

            } while (temp != first);
        }
        if (stream_head != NULL)
        {
            EnterCriticalSection(&stream_lock);
            stream_head=delete_stream(&stream_head);
            LeaveCriticalSection(&stream_lock);
        }
        Sleep(6000);
    }
}

process_files

void process_files(struct data_stream*temp)
{
    int count = 0;
    char file_to_update[50] = { NULL };
    size_t name_len = strlen(temp->filename);
    memcpy_s(file_to_update, name_len, temp->filename, name_len);
    file_to_update[name_len] = '\0';
    temp->count = 0;
    FILE *list_to_update ;
    fopen_s(&list_to_update , file_to_update, "r+b");
    if (list_to_update != NULL)
        {
            char readline[100] = { '\0' };
            while (fgets(readline, sizeof(readline), list_to_update ) != NULL)              
                {
                    //read a line at a time and process the list
                    count++;
                }
            temp->count = count;
            fclose(list_to_update );
        }
        else
            printf("\nerror opening file\n");
}

process_others()

void process_others(struct data_stream*temp)
 {
    int count = 0;
    char file_to_update[50] = { NULL };
    size_t name_len = strlen(temp->filename);
    memcpy_s(file_to_update, name_len, temp->filename, name_len);
    file_to_update[name_len] = '\0';
    temp->count = 0;
    FILE *list_to_update ;
    fopen_s(&list_to_update , file_to_update, "r+b");
     if (list_to_update != NULL)
        {
            char readline[100] = { '\0' };
            while (fgets(readline, sizeof(readline), list_to_update ) != NULL)              
               {
                    //read a line at a time and process the list
                    count++;
                }
            temp->count = count;
            fclose(list_to_update );
        }
        else
         {
            printf("\nerror opening file\n");
        }
    }

}

push_stream (full source code)

struct data_stream* push_stream(struct data_data_stream**head_ref, int typ_grp, int task_type)
{
    struct data_stream*new_data_stream=
        (struct data_data_stream*)malloc(sizeof(struct data_stream));
    new_stream->processed = false;
    new_stream->task_type = task_type;
    new_stream->TYPE_GRP = typ_grp;
    new_stream->filename[0] = NULL;
    new_stream->count = 0;
    new_stream->next = NULL;
    if (task_type == 2)
        sprintf_s(new_stream->filename, "%s%03d.txt", "file_md5_list_", stream_count);
    else
        sprintf_s(new_stream->filename, "%s%03d.txt", "other_list_", stream_count);
    if (*head_ref == NULL)
    {
        *head_ref = new_stream;
        new_stream->next = new_stream;
    }
    else
    {
        struct data_stream* last = (*head_ref)->next;
        struct data_stream* prev = (*head_ref)->next;
        while (last != *head_ref)
        {   
            prev = last;
            last = last->next;          
        }   
        new_stream->next = *head_ref;
        last->next = new_stream;
    }
    if (stream_count > 998)
        stream_count = 1;
    else
        stream_count++;
    return new_stream;
}

delete_stream function (full source code)

struct data_stream* delete_stream(struct data_data_stream**head)
{
    if (head == NULL)
        return NULL;    
    struct data_stream*prev = NULL;
    struct data_stream*temp = *head;
    struct data_stream*first = *head;
    do
    {
        struct data_stream*next = temp->next;
        if (temp->processed)
        {
            if (prev == NULL)
                *head = temp->next;
            else
                prev->next = temp->next;

            char file_to_delete[50] = { NULL };
            memcpy_s(file_to_delete, strlen(temp->filename), temp->filename, strlen(temp->filename));
            DeleteFileA(file_to_delete);
            free(temp);
        }
        else
        {
            prev = temp;
        }
        temp = next;
    } while (temp != first);
    if (prev == NULL)
    {
        return NULL;
    }

    return *head;
}    

来源:https://stackoverflow.com/questions/48623528/circular-linked-list-not-working-properly-in-c-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!