コード例 #1
0
    /**
     * @brief Sorts list using mergesort algorithms.
     * @param[in] i_head Pointer to the head of list.
     */
    Node * p_mergesort(Node * i_head)
    {
        // list is empty or has only one node
        if (i_head == nullptr || i_head->next == nullptr)
        {
            return i_head;
        }
        // split list into two halfs
        Node * half = p_split(i_head);

        // recursively sort two halfs
        i_head = p_mergesort(i_head);
        half = p_mergesort(half);

        // merge sorted lists
        return p_merge(i_head, half);
    }
コード例 #2
0
ファイル: pchild.c プロジェクト: Distrotech/yodl
/*
    This function specifies readPipe and writePipe, but in the prototype
    the names are reversed: there writePipe and readPipe are used.
    The prototype is given from the point of view of the parent code,
    which first specifies the writePipe.
    Here, we're in the child-process itself, and the parent's writePipe
    becomes the child's readPipe.
*/
int p_child(Process *pp, int *readPipe, int *writePipe)
{
    char **argv = p_split(pp);              /* construct argv               */

    close(readPipe[WRITE]);                 /* Close unused pipe-ends       */
    close(writePipe[READ]);

    if                                      /* Use our descriptors for      */
    (                                       /* standard streams             */
        dup2(readPipe[READ], STDIN_FILENO) < 0
        ||
        dup2(writePipe[WRITE], STDOUT_FILENO) < 0
    )
        if (message_show(MSG_ALERT))
            message("%s: Can't redirect STDIN and/or STDOUT");

    close(readPipe[READ]);                  /* close superfluous original  */
    close(writePipe[WRITE]);                /* parts                        */

    return execvp(*argv, argv);             /* exec the program             */
                                            /* (program exits or -1 is      */
                                            /* returned when exec fails     */
}