Untitled
unknown
plain_text
a month ago
1.6 kB
12
Indexable
#include <stdio.h> #include <stdlib.h> #define n 10 #define ts 5 struct process { int pid; int cpu_time; }; struct process cqueue[n]; int front = 0, rear = 0, count = 0; // Function to add void addqueue(int pid, int cpu_time) { if (count == n) { printf("Queue is full. Cannot add process.\n"); return; } cqueue[rear].pid = pid; cqueue[rear].cpu_time = cpu_time; rear = (rear + 1) % n; count++; } // Function to delete struct process dequeue() { if (count == 0) { printf("Queue is empty. Cannot delete process.\n"); exit(1); } struct process temp = cqueue[front]; front = (front + 1) % n; count--; return temp; } // Function to simulate process execution void pprint() { while (count > 0) { struct process temp = dequeue(); int t = (temp.cpu_time < ts) ? temp.cpu_time : ts; printf("Process %d is running for %d nanoseconds.\n", temp.pid, t); temp.cpu_time -= t; if (temp.cpu_time > 0) { addqueue(temp.pid, temp.cpu_time); } } } int main() { int num_processes; printf("Enter the number of processes: "); scanf("%d", &num_processes); for (int i = 0; i < num_processes; i++) { int pid, cpu_time; printf("Enter process ID for process %d: ", i + 1); scanf("%d", &pid); printf("Enter CPU time for process %d: ", i + 1); scanf("%d", &cpu_time); addqueue(pid, cpu_time); } pprint(); return 0; }
Editor is loading...
Leave a Comment