2017-02-28 54 views
0

我在使用qsort()函數時遇到了一些問題。這種情況是我之前添加的文章reference的擴展。我需要對存儲成員接收元素的數組進行排序(即適合卡片)。例如:fork()與char數組和qsort()導致孩子停止工作

用以下示例運行 -

./a.out A4 B2 CK DA BJ A3 DT C4 A2 B3 D4 C3 
Child : 1, pid 18211 : A4 BJ A2 
Child : 2, pid 18212 : B2 A3 B3 
Child : 3, pid 18213 : CK DT D4 
Child : 4, pid 18214 : C4 DA C3 
Father : 4 childs created 

期望的輸出

./a.out A4 B2 CK DA BJ A3 DT C4 A2 B3 D4 C3 
    Child : 1, pid 18211 : A4 A2 BJ 
    Child : 2, pid 18212 : A3 B3 B2 
    Child : 3, pid 18213 : CK DT D4 
    Child : 4, pid 18214 : C4 C3 DA 
    Father : 4 childs created 

即節省A4 BJ A2在陣列中,保存B2 A3 B3在第二陣列,保存CK DT D4在第三個陣列中,將C4 DA C3保存在第四個陣列中。按降序排列成員元素並進行進一步的操作。

然而,當我嘗試使用快速排序,我有以下問題:

沒有子輸出(即使未排序print語句)

問題是什麼?在qsort實現中有沒有問題? 請幫幫我。

代碼至今:

#include <sys/types.h> 
    #include <sys/wait.h> 
    #include <stdio.h> 
    #include <unistd.h> 
    #include <string.h> 

    void childFunction(char *argv[], int argc, int identify){ 
     int cmp(const void *a, const void *b){ 
      return *(char *)a - *(char *)b; 
     } 
     int childnum = identify + 1 ; 
     int i,j,r,z; 
     char *a[256]; 
     char *temp[256]; 
     printf("Child : %d, pid %d : ", childnum, getpid()); 
     for(i = childnum; i < argc; i += 4) 
     { 
      for(j = 0; j < argc; j++) 
      { 
       a[j] = argv[i]; 
       printf("%s ", a[j]) ; 
       break; 
      } 
     } 
     qsort(a,sizeof(a),sizeof(a[0]),cmp); 
     printf("\n") ; 
     for(j = 0; j < sizeof(a); j++) 
      { 
       printf("%s ", a[j]) ; 
       break; 
      } 
     // do stuff 
    } 


    int main(int argc, char *argv[]){ 
     int childLimit = 4; // number of children wanted 
     int childrenPids[childLimit]; // array to store children's PIDs if needed 
     int currentPid, i; 

     for(i=0; i<childLimit; i++){ 
      switch(currentPid = fork()){ 
       case 0: 
        // in the child 
        childFunction(argv, argc, i); 
        // exit the child normally and prevent the child 
        // from iterating again 
        return 0; 
       case -1: 
        printf("Error when forking\n"); 
        break; 
       default: 
        // in the father 
        childrenPids[i] = currentPid; // store current child pid 
        break; 
      } 

     } 



     // do stuff in the father 

     //wait for all child created to die 
     waitpid(-1, NULL, 0); 
     printf("Father : %d childs created\n", i); 
    } 


    [1]: https://stackoverflow.com/questions/42325032/c-print-and-store-command-line-argument-in-a-round-robin-manner/42325301?noredirect=1#comment72082753_42325301 
+0

那麼開始你的程序不是一個真正有效的C程序,因爲C沒有嵌套函數。 –

回答

2

是的,有與你是如何調用qsort問題。

第二個參數意味着數組中的成員數。 sizeof(a)返回a的整個大小,在這種情況下爲2048字節(指針爲256個元素* 8個字節)。你在這裏實際需要的是跟蹤你有多少元素正在填充並使用該值。

哪種導致到另一個問題,即你填充的方式a沒有多大意義,我看不出你是如何得到你的輸出。您將爲i的不同值重複填充argc陣列的第一個元素argv[i]

我想你的意思是這樣的:

for(i = childnum; i < argc; i += 4) 
    { 
    a[j++]=a[i]; 
    } 

,然後給你j爲元素的數傳進qsort

+0

問題解決了,謝謝 –

2

你需要閱讀更多有關the qsort function一點,並把它傳遞到比較函數是什麼。

qsort函數將調用比較函數將指針傳遞給數組中的元素。即在你的情況下,它會調用你的函數,如

cmp(&a[0], &a[1]); 

既然你有一個指針數組,該參數cmp是指針的指針,參數是真的char **

所以你有一個無效的轉換,並減去你的函數中兩個指針的最低字節。

如果你想比較每個字符串的第一個字符,你需要使用正確的鑄造和功能提領:

return **(char **)a - **(char **)b; 
相關問題