CP Phase2
CP Phase2
Roll No : 58
Course Project Phase 2
Q. Design and implementation of Operating System: Stage II
i. Paging
ii. Error Handling
iii. Interrupt Generation and Servicing
iv. Process Data Structure
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
void execute();
void init();
void loadInstructionSetIntoMemory();
void checkErrors();
void printMemory(){
for(int i=0; i<300; i++){
printf("%d ", i);
for(int j=0; j<4; j++){
printf("%c ", M[j][i]);
}
printf("\n");
}
}
void printBuffer(){
for(int i=0; i<10; i++){
printf("%d ", i);
for(int j=0; j<4; j++){
printf("%c ", BUFF[j][i]);
}
printf("\n");
}
}
void init(){
for(int i=0; i<4; i++){
for(int j=0; j<300; j++){
M[i][j] = '-';
toggle[j] = 0;
}
}
for(int i=0; i<4; i++){
IR[i] = '-';
R[i] = '-';
for(int j=0; j<10; j++){
BUFF[j][i] = '-';
}
}
m=0, SI=0, IC=0, TTL=0, TLL=0, TTC=0, PI=0;
LLC=0;
ptr=1;
PTR = randAd[0] * 10;
errors = 0;
ICR=0;
ptl = 0;
pe=0;
}
void execute(){
checkErrors();
if(errors == 1)return;
for(int i=0; i<4; i++){
IR[i] = M[i][m];
}
switch(IR[0]){
case 'G':
//GD
if(IR[1] != 'D'){
PI=1;
break;
}
ICR++;
TTC+=2;
IC = (IR[2] - '0')*10 + (IR[3] - '0');
toggle[IC] = 1;
M[0][PTR+ptl] = IC/10 + '0';
M[1][PTR+ptl] = IC%10 + '0';
int t = randAd[ptr] * 10;
M[2][PTR+ptl] = randAd[ptr]/10 + '0';
M[3][PTR+ptl] = randAd[ptr]%10 + '0';
ptr++;
ptl++;
if(IC<0 || IC>99){
PI=2;
break;
}
if(BUFF[0][bp] == '-'){
PI=4;
break;
}
case 'P':
//PD
if(IR[1] != 'D'){
PI=1;
break;
}
ICR++;
TTC++;
IC = (IR[2] - '0')*10 + (IR[3] - '0');
t = map(IC)*10;
if(toggle[IC] == 0){
PI=3;
break;
}
if(IC<0 || IC>99){
PI=2;
break;
}
break;
case 'L':
//LR
if(IR[1] != 'R'){
PI=1;
break;
}
ICR++;
TTC++;
IC = (IR[2] - '0')*10 + (IR[3] - '0');
if(toggle[IC] == 0){
PI=3;
break;
}
t = map(IC/10 * 10)*10 + IC%10;
if(IC<0 || IC>99){
PI=2;
break;
}
for(int i=0; i<4; i++){
R[i] = M[i][t];
}
break;
case 'S':
//SR
if(IR[1] != 'R'){
PI=1;
break;
}
ICR++;
TTC+=2;
IC = (IR[2] - '0')*10 + (IR[3] - '0');
toggle[IC] = 1;
if(IC<0 || IC>99){
PI=2;
break;
}
t = map(IC/10 * 10)*10 + IC%10;
for(int i=0; i<4; i++){
M[i][t] = R[i];
}
break;
case 'C':
//CR
if(IR[1] != 'R'){
PI=1;
break;
}
ICR++;
TTC++;
IC = (IR[2] - '0')*10 + (IR[3] - '0');
if(toggle[IC] == 0){
PI=3;
break;
}
t = map(IC/10 * 10)*10 + IC%10;
if(IC<0 || IC>99){
PI=2;
break;
}
for(int i=0; i<4; i++){
if(M[i][t] != R[i]){
c = 0;
}
}
break;
case 'B':
//BT
if(IR[1] != 'T'){
PI=1;
break;
}
ICR++;
TTC++;
IC = (IR[2] - '0')*10 + (IR[3] - '0');
if(toggle[IC] == 0){
PI=3;
break;
}
if(IC<0 || IC>99){
PI=2;
break;
}
if(c == 1){
m = IC-1;
}
c=1;
break;
case 'H':
ICR++;
TTC++;
return;
break;
default:
PI=1;
break;
}
m++;
if(v<10){
v++;
execute();
}
}
void read_file(){
input = fopen("input_phase2.txt", "r");
while (fgets(line, sizeof(line), input) != NULL) {
if(PCI == 1){
x=0;
for(int i=0; i<strlen(line); i++){
if(line[i] != ' '){
line2[x] = line[i];
x++;
}
}
loadInstructionToBuffer(line2, strlen(line2)/40,
strlen(line2)/40);
PCI=0;
}
if(DCI == 1){
if(line[0] == '$'){
DCI = 0;
bp=0;
m=((M[2][PTR] - '0') * 10 + (M[3][PTR] - '0')) * 10;
v=0;
execute();
}else{
int j=0;
for(int i=0; i<strlen(line); i++){
if(j>3){
j=0;
bp++;
}
BUFF[j][bp] = line[i];
j++;
}
bp++;
}
}
if(line[0] == '$'){
switch(line[1]){
case 'A':
init();
for(int i=0; i<4; i++){
job[i] = line[5+i];
ttl[i] = line[10+i];
tll[i] = line[15+i];
}
for(int i=0; i<4; i++){
TTL *= 10;
TTL += ttl[i] - '0';
TLL *= 10;
TLL += tll[i] - '0';
}
PCI=1;
break;
case 'D':
DCI=1;
bp=0;
break;
default:
output = fopen("output_phase2.txt", "a");
fprintf(output, "\n\n");
fclose(output);
PCI=0, DCI=0;
}
}
}
}
void loadInstructionSetIntoMemory(){
m=randAd[ptr] * 10;
M[2][PTR+ptl] = (randAd[ptr]/10) + '0';
M[3][PTR+ptl] = (randAd[ptr]%10) + '0';
ptr++;
ptl++;
int y=0;
void main(){
read_file();
printMemory();
fclose(input);
}
Input:
$AMJ000100030001
GD10PD10H
$DTA
Hello
$END0001
$AMJ000100050001
GD10PD10GD20PD20H
$DTA
Hello
SY D Students
$END0001
Output: