The output snapshot is followed by the code. Explanatory comments are added.
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
/* Structures to define the process tree */
struct processInfo
{
char processName; /* A, B, C, D */
int numChild;
char children[5];
int retValue;
};
/* Define the process tree */
struct processInfo process[] = {
{ 'A', 2, {'B', 'C'}, 5},
{ 'B', 1, {'D'}, 9 },
{ 'C', 0, { }, 8 },
{ 'D', 0, { }, 12 },
};
char myId = 'A'; /* my process id */
int retVal;
void processfunction()
{
int i, j;
int n = sizeof(process) / sizeof(struct
processInfo);
printf("Started process %c, pid=%d\n", myId, getpid());
/* Get the children details */
for(i = 0; i < n; i++)
{
if(myId ==
process[i].processName) /* process details found */
break;
}
if(i < n) /* entry found */
{
/* update return value
*/
retVal =
process[i].retValue;
/* create forked
children */
pid_t pids[5];
for(j = 0; j <
process[i].numChild; j++)
{
pids[j] = fork();
if(pids[j] < 0)
{
printf("Process %c, pid=%d: fork failed\n", myId, getpid());
}
if(pids[j] == 0)
{
/* child process */
/* 1. update id */
myId = process[i].children[j];
/* 2. call the processfunction, and return */
processfunction();
return;
}
else
{
printf("Process %c, pid=%d: Forked %c, pid=%d\n", myId, getpid(),
process[i].children[j], pids[j]);
}
}
/* Children forked, wait
for children to end */
printf("Process %c,
pid=%d: Waiting for children to end\n", myId, getpid());
for(j = 0; j <
process[i].numChild; j++)
{
int status;
if(pids[j] > 0) /* child was successfully forked */
{
waitpid(pids[j], &status, 0);
printf("Process %c, pid=%d: Child exited with status %d\n", myId,
getpid(), WEXITSTATUS(status));
}
}
}
/* Sleep for sometime */
sleep(10);
printf("Process %c, pid=%d: ending process\n",
myId, getpid());
}
int main()
{
processfunction();
return retVal;
}
In C programming language. Problem 1 (Building a given Process Tree) 10 points) In this exercise...
Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A* search algorithm. Please include pictures that the code runs and shows the different states as it reaches goal state please. 1. Objectives • To gain more experience on using pointers and linked lists in C programs. • To learn how to solve problems using state space search and A* search algorithm. 2. Background A* search and 15-puzzle problem have been introduced in the class....
Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A* search algorithm. 1. Objectives • To gain more experience on using pointers and linked lists in C programs. • To learn how to solve problems using state space search and A* search algorithm. 2. Background A* search and 15-puzzle problem have been introduced in the class. For more information, please read the wiki page of 15-puzzle problem at https://en.wikipedia.org/wiki/15_puzzle, and the wiki page of...