0% found this document useful (0 votes)
2 views2 pages

Process Life Cycle Simulation

This C program simulates a process life cycle with states such as NEW, READY, RUNNING, BLOCKED, and TERMINATED. Users can create processes, run them, interrupt, resume, or terminate them through a menu-driven interface. Input validation is included to ensure valid selections and actions on the processes.

Uploaded by

wyatt wallace
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Process Life Cycle Simulation

This C program simulates a process life cycle with states such as NEW, READY, RUNNING, BLOCKED, and TERMINATED. Users can create processes, run them, interrupt, resume, or terminate them through a menu-driven interface. Input validation is included to ensure valid selections and actions on the processes.

Uploaded by

wyatt wallace
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include <stdio.

h>

typedef enum { NEW, READY, RUNNING, BLOCKED, TERMINATED } ProcessState;


typedef struct { char name; ProcessState state; } Process;

void printState(Process *p) {


const char *states[] = {"NEW", "READY", "RUNNING", "BLOCKED", "TERMINATED"};
printf("Process %c is now %s\n", p->name, states[p->state]);
}

Process* selectProcess(Process *p1, Process *p2, ProcessState state, const char *action) {
char choice;
printf("Select a process to %s (A/B): ", action);
scanf(" %c", &choice);
Process *p = (choice == 'A' || choice == 'a') ? p1 : (choice == 'B' || choice == 'b') ? p2 : NULL;
return (p && p->state == state) ? p : (printf("Invalid selection or state.\n"), NULL);
}

int main() {
Process processA = {'A', NEW}, processB = {'B', NEW}, *p;
int choice;

do {
printf("\nProcess Life Cycle Simulation\n");
printf("1. Create Process A (New -> Ready)\n");
printf("2. Create Process B (New -> Ready)\n");
printf("3. Run a Ready process\n");
printf("4. Interrupt Running process (Running -> Blocked)\n");
printf("5. Resume a Blocked process (Blocked -> Running)\n");
printf("6. Terminate Running process\n");
printf("7. Exit\n");
printf("Enter your choice: ");

if (scanf("%d", &choice) != 1) { // Input validation


printf("Invalid input. Enter a number between 1 and 7.\n");
while (getchar() != '\n'); // Clear input buffer
continue;
}

switch (choice) {
case 1:
if ([Link] == NEW) [Link] = READY, printState(&processA);
else printf("Process A is already created.\n");
break;
case 2:
if ([Link] == NEW) [Link] = READY, printState(&processB);
else printf("Process B is already created.\n");
break;
case 3:
if ((p = selectProcess(&processA, &processB, READY, "run"))) p->state = RUNNING,
printState(p);
else printf("No ready process to run.\n");
break;
case 4:
if ((p = selectProcess(&processA, &processB, RUNNING, "interrupt"))) p->state =
BLOCKED, printState(p);
else printf("No running process to interrupt.\n");
break;
case 5:
if ((p = selectProcess(&processA, &processB, BLOCKED, "resume"))) p->state =
RUNNING, printState(p);
else printf("No blocked process to resume.\n");
break;
case 6:
if ((p = selectProcess(&processA, &processB, RUNNING, "terminate"))) p->state =
TERMINATED, printState(p);
else printf("No running process to terminate.\n");
break;
case 7:
printf("Exiting simulation.\n");
break;
default:
printf("Invalid choice. Try again.\n");
}
} while (choice != 7);

return 0;
}

You might also like