***Typeline.
c*** }
#include <sys/types.h> if (n < 0)
#include <sys/stat.h> {
#include <fcntl.h> i = 0;
#include <stdio.h> while (read(fd, &c, 1) > 0)
#include <stdlib.h> {
#include <string.h> if (c == '\n')
#include <unistd.h> i++;
#include <sys/wait.h> }
void make_toks(char *s, char *tok[]) lseek(fd, 0, SEEK_SET);
{ j = 0;
int i = 0; while (read(fd, &c, 1) > 0)
char *p; {
p = strtok(s, " \t"); if (c == '\n')
while (p != NULL) j++;
{ if (j == i + n)
tok[i++] = p; break;
p = strtok(NULL, " \t"); }
} while (read(fd, &c, 1) > 0)
tok[i] = NULL; {
} printf("%c", c);
void typeline(char *filename, char *op) }}
{ close(fd);
int fd, i, j, n; }
char c; int main()
fd = open(filename, O_RDONLY); {
if (fd == -1) char buff[80], *args[10];
{ int pid;
printf("File %s not found.\n", filename); while (1)
return; {
} printf("myshell$");
if (strcmp(op, "a") == 0) fflush(stdin);
{ fgets(buff, 80, stdin);
while (read(fd, &c, 1) > 0) buff[strlen(buff) - 1] = '\0';
printf("%c", c); make_toks(buff, args);
close(fd); if (strcmp(args[0], "typeline") == 0)
return; typeline(args[2], args[1]);
} else
n = atoi(op); {
if (n > 0) pid = fork();
{ if (pid > 0)
i = 0; wait(NULL);
while (read(fd, &c, 1) > 0) else
{ {
printf("%c", c); if (execvp(args[0], args) == -1)
if (c == '\n') printf("Bad command.\n"); } }}
i++; return 0;
if (i == n) }
break;
}
****FCFS**** printf("| p%d ", pro[i].p);
#include <stdio.h> }
struct pro { printf("|\n\t");
int p; printf("%d", pro[0].ST);
int AT; for (i = 0; i < n; i++) {
int BT; printf(" %d", pro[i].FT);
int ST; }
int FT; for (i = 0; i < n - 1; i++) {
int WT; for (j = i + 1; j < n; j++) {
int TAT; if (pro[i].p > pro[j].p) {
} pro[10]; temp = pro[i];
int main() { pro[i] = pro[j];
int i, j, n; pro[j] = temp;
struct pro temp; }
printf("Enter how many processes: "); }
scanf("%d", &n); }
printf("\nEnter Arrival Times:\n"); printf("\n\n********* FCFS Scheduling Result
for (i = 0; i < n; i++) { *********\n");
pro[i].p = i; printf("-------------------------------------------------
printf("p%d AT: ", i); ------------\n");
scanf("%d", &pro[i].AT);
} printf("Process\tAT\tBT\tST\tFT\tWT\tTAT\n");
printf("\nEnter Burst Times:\n"); printf("-------------------------------------------------
for (i = 0; i < n; i++) { ------------\n");
printf("p%d BT: ", i); for (i = 0; i < n; i++) {
scanf("%d", &pro[i].BT); printf("p%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
} pro[i].p, pro[i].AT, pro[i].BT, pro[i].ST,
for (i = 0; i < n - 1; i++) { pro[i].FT, pro[i].WT, pro[i].TAT);
for (j = i + 1; j < n; j++) { }
if (pro[i].AT > pro[j].AT) { printf("-------------------------------------------------
temp = pro[i]; ------------\n");
pro[i] = pro[j];
pro[j] = temp; return 0;
} }
}
}
pro[0].ST = pro[0].AT;
for (i = 1; i < n; i++) {
pro[i].ST = (pro[i - 1].ST + pro[i - 1].BT >
pro[i].AT) ?
(pro[i - 1].ST + pro[i - 1].BT) : pro[i].AT;
}
for (i = 0; i < n; i++) {
pro[i].FT = pro[i].ST + pro[i].BT;
pro[i].WT = pro[i].ST - pro[i].AT;
pro[i].TAT = pro[i].FT - pro[i].AT;
}
printf("\n\n********* Gantt Chart
*********\n\n\t");
for (i = 0; i < n; i++) {
****SJF**** current_time = Pro[min_bt_index].FT;
#include <stdio.h> completed++;
struct Pro { }
int P; }
int BT; for (i = 0; i < n - 1; i++) {
int AT; for (j = i + 1; j < n; j++) {
int ST; if (Pro[i].P > Pro[j].P) {
int FT; struct Pro temp = Pro[i];
int WT; Pro[i] = Pro[j];
int TAT; Pro[j] = temp;
int visited; }
} Pro[100]; }
int main() { }
int i, j, n, completed = 0, current_time = 0, printf("\n\n\t**Gantt Chart*****\n\n\t\t");
min_bt_index; for (i = 0; i < n; i++) {
printf("\n\t Enter How many process : "); printf("| P%d ", Pro[i].P);
scanf("%d", &n); }
printf("\n\tEnter AT:\n\n"); printf("|\n\t\t");
for (i = 0; i < n; i++) { printf("%d", Pro[0].ST);
Pro[i].P = i + 1; for (i = 0; i < n; i++) {
printf("\t P%d AT: ", i + 1); printf(" %d", Pro[i].FT);
scanf("%d", &Pro[i].AT); }
Pro[i].visited = 0; printf("\n\n\t\t**SJF
} (Non-Preemptive)********\n");
printf("\n\tEnter BT:\n\n"); printf("________________________");
for (i = 0; i < n; i++) { printf("\nPro\tAT\tBT\tST\tFT\tWT\tTAT\n");
printf("\t P%d BT: ", i + 1); for (i = 0; i < n; i++) {
scanf("%d", &Pro[i].BT); printf("P%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
} Pro[i].P, Pro[i].AT, Pro[i].BT,
while (completed < n) { Pro[i].ST, Pro[i].FT, Pro[i].WT, Pro[i].TAT);
int min_bt = 1e9; }
min_bt_index = -1; printf("________________\n\n");
for (i = 0; i < n; i++) { return 0;}
if (!Pro[i].visited && Pro[i].AT <= current_time
&& Pro[i].BT < min_bt) {
min_bt = Pro[i].BT;
min_bt_index = i;
}
}
if (min_bt_index == -1) {
current_time++;
} else {
Pro[min_bt_index].ST = current_time;
Pro[min_bt_index].FT = Pro[min_bt_index].ST
+ Pro[min_bt_index].BT;
Pro[min_bt_index].WT =
Pro[min_bt_index].ST - Pro[min_bt_index].AT;
Pro[min_bt_index].TAT =
Pro[min_bt_index].FT - Pro[min_bt_index].AT;
Pro[min_bt_index].visited = 1;
******Priority****** for (i = 1; i < n; i++) {
#include <stdio.h> int minPriorityIndex = i;
struct pro { for (j = i; j < n; j++) {
int P; // Process ID if (pro[j].AT <= pro[i - 1].FT) { // Arrived
int BT; // Burst Time till now
int AT; // Arrival Time if (pro[j].Priority <
int ST; // Start Time pro[minPriorityIndex].Priority) {
int FT; // Finish Time minPriorityIndex = j; }}}
int WT; // Waiting Time if (minPriorityIndex != i) {
int TAT; // Turnaround Time temp = pro[i];
int Priority; // Priority (lower = higher) pro[i] = pro[minPriorityIndex];
} pro[100]; pro[minPriorityIndex] = temp;}
int main() { if (pro[i].AT > pro[i - 1].FT)
int i, j, n; pro[i].ST = pro[i].AT;
struct pro temp; else
printf("\n\t Enter how many processes: "); pro[i].ST = pro[i - 1].FT;
scanf("%d", &n); pro[i].FT = pro[i].ST + pro[i].BT;
printf("\n\t Enter Arrival Time (AT):\n"); pro[i].TAT = pro[i].FT - pro[i].AT;
for (i = 0; i < n; i++) { pro[i].WT = pro[i].TAT - pro[i].BT;
pro[i].P = i; } // Gantt Chart
printf("\tP%d AT: ", i); printf("\n\t ** GANTT CHART **\n\n");
scanf("%d", &pro[i].AT); printf("%d", pro[0].ST);
} for (i = 0; i < n; i++) {
printf("\n\t Enter Burst Time (BT):\n"); printf(" | P%d | %d", pro[i].P, pro[i].FT);
for (i = 0; i < n; i++) { }
printf("\tP%d BT: ", i); // Sort back by process ID for table output
scanf("%d", &pro[i].BT); for (i = 0; i < n - 1; i++) {
} for (j = i + 1; j < n; j++) {
printf("\n\t Enter Priority (lower value = higher if (pro[i].P > pro[j].P) {
priority):\n"); temp = pro[i];
for (i = 0; i < n; i++) { pro[i] = pro[j];
printf("\tP%d Priority: ", i); pro[j] = temp;}} }
scanf("%d", &pro[i].Priority); } printf("\n\nProcess\tAT\tBT\t
// Sort processes by Arrival Time Priority\tST\tFT\tTAT\tWT\n");
for (i = 0; i < n - 1; i++) { for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) { printf("P%d\t%d\t%d\t%d\t\t%d\t%d\t%d\t%d\n",
if (pro[i].AT > pro[j].AT) { pro[i].P, pro[i].AT, pro[i].BT, pro[i].Priority,
temp = pro[i]; pro[i].ST, pro[i].FT, pro[i].TAT, pro[i].WT);
pro[i] = pro[j]; } return 0;}
pro[j] = temp;
}} }
// First process
pro[0].ST = pro[0].AT;
pro[0].FT = pro[0].ST + pro[0].BT;
pro[0].TAT = pro[0].FT - pro[0].AT;
pro[0].WT = pro[0].TAT - pro[0].BT;
// Remaining processes (Priority wise)
*****Bankers***** for(pp=0;pp<xp;pp++)
#include<stdio.h> {
int n,m; scanf("%d",&avail[pp]);
int alloc[10][10],max[10][10],need[10][10]; }
int }
avail[10],total[10],work[10],finish[10],seq[10],req[10]; void calc_need()
int pno; {
void accept() int i,j;
{ for(i=0;i<n;i++)
int i,j; {
printf("\n Enter the number of processes :"); for(j=0;j<m;j++)
scanf("%d",&n); need[i][j]=max[i][j]-alloc[i][j];
printf("\n Enter the resources of processes :"); }
scanf("%d",&m); }
printf("\n Enter total instances of each resources type void print()
:"); {
for(j=0;j<n;j++) int i,j;
{ printf("\n--------------------------------------");
printf("%c:",65+j); printf("\n\tallocation\t\tMax\t\tNeed\n\t");
scanf("%d",&total[j]); for(i=0;i<3;i++)
} {
printf("\n Enter allocation of each resource type by for(j=0;j<m;j++)
each process :\n"); printf("%3c",65+j);
for(i=0;i<n;i++) printf("\t\t");
{ }
printf("P%d:\n",i); for(i=0;i<n;i++)
for(j=0;j<m;j++) {
{ printf("\nP%d\t",i);
printf("%c:",65+j); for(j=0;j<m;j++)
scanf("%d",&alloc[i][j]); printf("%3d",alloc[i][j]);
} printf("\t\t");
} for(j=0;j<m;j++)
printf("\n Enter maximum resource type needed by printf("%3d",max[i][j]);
each process"); printf("\t\t");
for(i=0;i<n;i++) for(j=0;j<m;j++)
{ printf("%3d",need[i][j]);
printf("P%d:\n",i); }
for(j=0;j<m;j++) printf("\n Available\n");
{ for(j=0;j<m;j++)
printf("%c:",65+j); printf("%3c:",65+j);
scanf("%d",&max[i][j]); printf("\n");
}}} for(j=0;j<m;j++)
void calc_avail() printf("%3d",avail[j]);
{ }
int xp,pp; int find()
printf("\n How many element"); {
scanf("%d",&xp); int i,j;
printf("\n Enter element"); for(i=0;i<n;i++)
{
if(!finish[i])
{ ******RoundRobing******
for(j=0;j<m;j++) #include <stdio.h>
if(need[i][j]>work[j]) typedef struct {
break; int pid;
if(j==m) int at;
return i; int bt;
} int bt_left;
} int st;
return -1; int ft;
} int wt;
int tat;
void bankers() int started;
{ } process;
int i,j,k=0; typedef struct {
for(j=0;j<m;j++) int pid;
work[j]=avail[j]; int start;
for(i=0;i<n;i++) int end;
finish[i]=0; } Gantt;
while((i=find())!=-1) int main() {
{ int n, tq, time = 0, done = 0, i;
finish[i]=1; printf("Enter number of processes: ");
for(j=0;j<m;j++) scanf("%d", &n);
work[j]=work[j]+alloc[i][j]; process p[n];
seq[k++]=i; for (i = 0; i < n; i++) {
} p[i].pid = i + 1;
if(k==n) printf("Enter Arrival Time of p%d: ", i + 1);
{ scanf("%d", &p[i].at);
printf("\n System is in safe state"); printf("Enter Burst Time of p%d: ", i + 1);
printf("\n safe sequence:"); scanf("%d", &p[i].bt);
for(j=0;j<n;j++) p[i].bt_left = p[i].bt;
printf("P%d,",seq[j]); p[i].started = 0;
} }
else printf("Enter Time Quantum: ");
{ scanf("%d", &tq);
printf("\n System is in unsafe state:"); int queue[100], front = 0, rear = 0;
} int visited[n];
} for (i = 0; i < n; i++) visited[i] = 0;
main() Gantt gantt[200];
{ int g_count = 0;
int i,j; for (i = 0; i < n; i++) {
system("clear"); if (p[i].at == 0) {
accept(); queue[rear++] = i;
calc_avail(); visited[i] = 1;} }
calc_need(); if (rear == 0) {
print(); int earliest = 0;
bankers(); for (i = 1; i < n; i++) {
} if (p[i].at < p[earliest].at) earliest = i;
}
time = p[earliest].at; min_at = p[i].at;
queue[rear++] = earliest; next = i;}}
visited[earliest] = 1; time = p[next].at;
} queue[rear++] = next;
while (done < n) { visited[next] = 1; }
if (front == rear) { int idx = queue[front++];
int next = -1; if (!p[idx].started) {
int min_at = 1e9; p[idx].st = (time > p[idx].at) ? time :
for (i = 0; i < n; i++) { p[idx].at;
if (!visited[i] && p[i].bt_left > 0 && p[i].at < time = p[idx].st;
min_at) { p[idx].started = 1; }
min_at = p[i].at; int exec_time = (p[idx].bt_left > tq) ? tq :
next = i; p[idx].bt_left;
} gantt[g_count].pid = p[idx].pid;
} gantt[g_count].start = time;
time = p[next].at; gantt[g_count].end = time + exec_time;
queue[rear++] = next; g_count++;
visited[next] = 1; time += exec_time;
} p[idx].bt_left -= exec_time;
int idx = queue[front++]; for (i = 0; i < n; i++) {
if (!p[idx].started) { if (!visited[i] && p[i].at <= time &&
p[idx].st = (time > p[idx].at) ? time : p[idx].at; p[i].bt_left > 0) {
time = p[idx].st; queue[rear++] = i;
p[idx].started = 1; visited[i] = 1;}}
} if (p[idx].bt_left > 0) {
int exec_time = (p[idx].bt_left > tq) ? tq : queue[rear++] = idx;} else {
p[idx].bt_left; p[idx].ft = time;
gantt[g_count].pid = p[idx].pid; p[idx].tat = p[idx].ft - p[idx].at;
gantt[g_count].start = time; p[idx].wt = p[idx].tat - p[idx].bt;
gantt[g_count].end = time + exec_time; done++;}}
g_count++; printf("\n--- Gantt Chart ---\n");
for (i = 0; i < g_count; i++) {
time += exec_time; printf("| P%d ", gantt[i].pid);
p[idx].bt_left -= exec_time; }
for (i = 0; i < n; i++) { printf("|\n");
if (!visited[i] && p[i].at <= time && p[i].bt_left for (i = 0; i < g_count; i++) {
> 0) { printf("%d ", gantt[i].start);
queue[rear++] = i; }
visited[i] = 1;
}
}
if (p[idx].bt_left > 0) {
queue[rear++] = idx;