2017-02-28 51 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; 
相关问题