2014-01-28 5 views
0

커널 목록에서 모든 요소를 ​​제거하고 해당 내용을 사용자 버퍼에 복사합니다 (모든 요소가 맞으면 제거합니다). 나는 synchrone을하고있다. 그래서 나는 세마포어를 포함했다. 나는 제거 과정을 수행하는 방법이 synchrone 모드의 목록에서 제거하고 그것을 다른 목록에 삽입 한 다음 코드를 풀고 새로운 목록에서 삭제하는 것이다. 문자열 AUX로 복사 소자의 양은 오른쪽이지만 실행될 때 (단부의 근처) :커널 목록에서 요소 제거

printk(KERN_INFO "item->data: %d",item->data); 

결과 적이다 항목 -는> 데이터 : 111

발생하지 않도록 item> 데이터는 임의의 숫자이므로 제거로 인해 메모리가 실패하지만 문제를 해결하는 방법이나 문제가 무엇인지 알 수 없습니다.

list_item_t 구조는 다음이다 :

typedef struct { 
     int data; 
     struct list_head links; 
    }list_item_t; 

마이리스트 선언은 : 여기

struct list_head mylist = LIST_HEAD_INIT(mylist); 

문제를 생성하는 코드이다.

static ssize_t modtimer_read (struct file *file, char *user, size_t nbits, loff_t * offset){ 
    struct list_head* pos = mylist.next; // The position of the list 
    struct list_head* auxpos; 
    struct list_head listaux = LIST_HEAD_INIT(listaux); 
    list_item_t* item; 
    char aux[MAX_BUFFER]; 
    char aux2[10]; 
    int total =0; 
    int subt =0; 
    int done = 0; 


    printk(KERN_INFO "modtimer_read open"); 

    if (down_interruptible(&mtx)) /*Lock*/ 
     return -EINTR; 

    while (done == 0){ 
     if(pos == pos->next || list_num_items == 0){ 
      done++; 
     printk(KERN_INFO "Empty list"); 
     // Esperar 
     }else{ 
      item = list_entry(pos, list_item_t, links); //gets the item in the 'pos' position 
      subt=sprintf(aux2, "%d\n",item->data); 
      auxpos = pos->next; 
      if(subt + total > MAX_BUFFER) {   
       done++; 
       printk(KERN_INFO "string has maximum size"); 
      }else { 
       total+= sprintf(&aux[total],"%i\n",item->data); //reads the data (integer) 

       list_add_tail(&listaux,pos); //Added into the aux list     
       list_del(pos); //deleted from the list 
       list_num_items--; 
      } 
      subt = 0; 
      pos = auxpos; 
     } 
    } 
    aux[total] = '\0'; 
    up(&mtx); 
    pos = listaux.next; 
    while(&listaux != listaux.next){ 
     item = list_entry(pos, list_item_t, links); //gets the item in the 'pos' position 
     auxpos = pos->next; 
     list_del(pos); 

     printk(KERN_INFO "item->data: %d",item->data); 

     vfree(item); 
     pos = auxpos; 
    } 
    copy_to_user(user,aux,total); 

    printk(KERN_INFO "modtimer_read cerrado"); 
    return total; 
} 

답변

0
list_add_tail(&listaux,pos); 

매개 변수의 순서가 잘못이 라인은 헤더 pos 시작 목록에 목록 항목 listaux을 추가합니다.

documentation을 읽어보십시오.

+0

그래서, 무엇이 중요한가요? 나중에 제거하려면 보조 목록에 추가하십시오. 의사는 "지정된 헤드 앞에 새 항목을 삽입하면 큐를 구현하는 데 유용합니다."라고 말합니다. – Kaostias