Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program in C to create N processes that form a ternary tree (i.e., each

ID: 3672702 • Letter: W

Question

Write a program in C to create N processes that form a ternary tree (i.e., each
node has exactly three child nodes) where N is the command line input to the program (N must be
greater than zero and less than 10). Each process prints out its own PID and its parent PID. Use
loop structure. Explain fully how the program works. For brevity, do not write include files. My previous lab was provided as starting point,please complete.

/**************************************************************************/
/* PROGRAM: lab2.c */
/* DESCRIPTION: This program generates a chain of processes */
/* using fork(). The number of processes n is a command line argument. */ /* Each process sleeps for a random time less than 10 seconds then prints out */
/* process ID, parent ID, and child ID */
/**************************************************************************/
# include <stdio.h>
# include <stdlib.h>
# include <sys/types.h>
# include <unistd.h>
int main(int argc, char *argv[])
{
int i, m, n, sleeptime, seed;
pid_t childpid;
if (argc !=2) {
printf(" Usage: %s processes ", argv[0]);
exit(1);
}
n = atoi(argv[1]); m = 10;
childpid = 0;
for (i=0; i<n;i++)
if (childpid = fork()) break;
if (childpid == -1) {
perror (" The fork failed ");
exit(1);
}
printf(" %d: process ID:%6ld parent ID:%6ld child
ID:%6ld",i, (long)getpid(), (long)getppid(), (long)childpid);
seed = (int)(getpid() + childpid);
8
srand(seed);
/* since each process has a different childpid, using the childpid
as the seed number will restart the random function.
Therefore, each process will have a different sleeptime
*/
sleeptime = rand()%m;
printf (" sleep = %d ", sleeptime);
sleep(sleeptime);
exit(0);
}

# include # include # include # include unistd.h? int main int argc, char *argvI) int i, m, n, sleeptime, seed pid t childpid; if (argc 2) { printf("in Usage: %s processesa", argv[0]) exit(1) n = atoi(argv[1]); m = 10; childpid = 0; for (i=0; i

Explanation / Answer

// C program to demonstrate Ternary Search Tree (TST) insert, travese

// and search operations

#include <stdio.h>

#include <stdlib.h>

#define MAX 50

// A node of ternary search tree

struct Node

{

    char data;

    // True if this character is last character of one of the words

    unsigned isEndOfString: 1;

    struct Node *left, *eq, *right;

};

// A utility function to create a new ternary search tree node

struct Node* newNode(char data)

{

    struct Node* temp = (struct Node*) malloc(sizeof( struct Node ));

    temp->data = data;

    temp->isEndOfString = 0;

    temp->left = temp->eq = temp->right = NULL;

    return temp;

}

// Function to insert a new word in a Ternary Search Tree

void insert(struct Node** root, char *word)

{

    // Base Case: Tree is empty

    if (!(*root))

        *root = newNode(*word);

    // If current character of word is smaller than root's character,

    // then insert this word in left subtree of root

    if ((*word) < (*root)->data)

        insert(&( (*root)->left ), word);

    // If current character of word is greate than root's character,

    // then insert this word in right subtree of root

    else if ((*word) > (*root)->data)

        insert(&( (*root)->right ), word);

    // If current character of word is same as root's character,

    else

    {

        if (*(word+1))

            insert(&( (*root)->eq ), word+1);

        // the last character of the word

        else

            (*root)->isEndOfString = 1;

    }

}

// A recursive function to traverse Ternary Search Tree

void traverseTSTUtil(struct Node* root, char* buffer, int depth)

{

    if (root)

    {

        // First traverse the left subtree

        traverseTSTUtil(root->left, buffer, depth);

        // Store the character of this node

        buffer[depth] = root->data;

        if (root->isEndOfString)

        {

            buffer[depth+1] = '';

            printf( "%s ", buffer);

        }

        // Traverse the subtree using equal pointer (middle subtree)

        traverseTSTUtil(root->eq, buffer, depth + 1);

        // Finally Traverse the right subtree

        traverseTSTUtil(root->right, buffer, depth);

    }

}

// The main function to traverse a Ternary Search Tree.

// It mainly uses traverseTSTUtil()

void traverseTST(struct Node* root)

{

    char buffer[MAX];

    traverseTSTUtil(root, buffer, 0);

}

// Function to search a given word in TST

int searchTST(struct Node *root, char *word)

{

    if (!root)

        return 0;

    if (*word < (root)->data)

        return searchTST(root->left, word);

    else if (*word > (root)->data)

        return searchTST(root->right, word);

    else

    {

        if (*(word+1) == '')

            return root->isEndOfString;

        return searchTST(root->eq, word+1);

    }

}

// Driver program to test above functions

int main()

{

    struct Node *root = NULL;

    insert(&root, "cat");

    insert(&root, "cats");

    insert(&root, "up");

    insert(&root, "bug");

    printf("Following is traversal of ternary search tree ");

    traverseTST(root);

    printf(" Following are search results for cats, bu and cat respectively ");

    searchTST(root, "cats")? printf("Found "): printf("Not Found ");

    searchTST(root, "bu")?   printf("Found "): printf("Not Found ");

    searchTST(root, "cat")? printf("Found "): printf("Not Found ");

    return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote