带有char数组和qsort()的fork()导致子级停止工作 [英] fork() with char array and qsort() resulting child stop working

查看:89
本文介绍了带有char数组和qsort()的fork()导致子级停止工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 qsort()函数时遇到了一些问题。这种情况是对我以前添加的帖子参考。我需要对存储成员收到的元素的数组进行排序(即卡片组)。例如:

I have encountered some problems on using qsort() function. The situation is an extension to my previously added post reference. I need to sort the arrays that store the members received elements (i.e suit of cards). For example:

,并运行以下示例-

./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保存在第3个数组中,将C4 DA C3保存在第4个数组中。然后按降序对成员元素进行排序,然后执行进一步的操作。

that is save A4 BJ A2 in an array, save B2 A3 B3 in 2nd array, save CK DT D4 in 3rd array, save C4 DA C3 in 4th array. And sort the member elements in descending order and do further operations.

但是,当我尝试使用qsort时,出现以下问题:

However, when I try to use qsort, i have the following problem:

没有子输出(即使未排序的打印语句)

there are no child output (even if the unsorted print statement)

出了什么问题? qsort的实现有什么问题吗?
请帮助我。

What is the problem? is there any problem in the qsort implementation? Please help me.

代码到目前为止:

    #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


推荐答案

是的,您如何调用 qsort

第二个参数的意思是是数组中成员的数量。 sizeof(a)返回 a 的整个大小,在这种情况下为2048个字节(256个元素* 8个字节,用于指针)。您在这里真正想要的是跟踪正在填充的a元素的多少,并使用该值。

The 2nd argument is meant to be the number of members in your array. sizeof(a) returns the entire size of a which in this case is 2048 bytes (256 elements * 8 bytes for a pointer). What you actually want here is to keep track of how many elements of a you're populating and use that value instead.

哪些会导致另一个问题,也就是说,您填充 a 的方式没有多大意义,我看不到您如何从中获得输出。您用 argv [i] 重复填充数组的前 argc 个元素,以获取不同值的 i

Which sort of leads on to the another issue, namely that the way you're populating a doesn't make much sense and I can't see how you got your output from it. You're filling the first argc elements of the array with argv[i] repeatedly for different values of i.

我想你的意思是:

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

然后给您 j 作为要传递到 qsort 的元素数。

Which then gives you j as the number of elements to pass into qsort.

这篇关于带有char数组和qsort()的fork()导致子级停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆