home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The C Users' Group Library 1994 August
/
wc-cdrom-cusersgrouplibrary-1994-08.iso
/
listings
/
v_09_12
/
9n12027a
< prev
next >
Wrap
Text File
|
1991-10-15
|
2KB
|
91 lines
/* --------------------------------------------------------------
FUNCTION ADD_NODE: The steps to add a new node to the list in
ascending alphabetic order are:
Note that process_name_table is a lookup table that allows a
user-entered process name to be translated to the corresponding
processing function.
A. Get the process name and priority from the user.
B. Get a free node from free node list. If no more, complain and
get out.
C. Find place to insert new node. The pointer returned from
locate_node is the address of the node after which the new
node should be inserted.
D. Insert new node and initialize its members. Since the list is
maintained in decreasing order of priority, we need to update
the root pointer when a new higher priority process node
arrives. If the new node's priority equals to the current
highest priority, the new node is inserted ahead of that
duplicate.
-------------------------------------------------------------- */
void add_node(void)
{
int i;
Node *pnew_node; /* ptr to new node */
Node *ploc_node; /* ptr to located node */
char process_name[21]; /* selected process name */
unsigned priority; /* function priority */
static const struct {
char *ppname;
void (*pprocess)(unsigned);
} process_name_table[] = {
{"pro1", pro1},
{"pro2", pro2},
{"pro3", pro3}
};
static unsigned highest_priority = 0;
/*A*/ while (1) {
printf("\n Enter process name: ");
scanf("%20s", process_name);
for (i = 0; i < NUMELEM(process_name_table); ++i) {
if (strcmp(process_name_table[i].ppname,
process_name) == 0)
goto found;
}
printf("invalid process name\n");
}
found: while (1) {
printf("\n Enter process priority: ");
scanf("%3u", &priority);
if (priority > 0 && priority <= 100) {
break;
}
printf("1<=priority<=100\n");
}
/*B*/ pnew_node = get_free_node();
if (pnew_node == NULL) {
printf("\n No nodes available\n");
return;
}
/*C*/ ploc_node = locate_node(priority, INEXACT);
/*D*/ pnew_node->pfwd = ploc_node->pfwd;
pnew_node->pbwd = ploc_node;
ploc_node->pfwd = pnew_node;
pnew_node->pfwd->pbwd = pnew_node;
pnew_node->process = process_name_table[i].pprocess;
pnew_node->priority = priority;
if (priority >= highest_priority) {
proot_node = pnew_node;
highest_priority = priority;
}
printf(" Node added\n");
}