#include<stdio.h>
#include<stdlib.h>
int mem[100],reg[4],cond[]={0,0,0,0,0,1},opc,op1,op2,pc;
FILE *fp;
char fname[20];
void load()
{
printf("Enter file name:");
scanf("%s",fname);
fp = fopen(fname,"r");
if(fp==NULL)
{
printf("File %s not found.\n",fname);
exit(1);
}
while(fscanf(fp,"%d",&mem[pc])!=-1)
pc++;
fclose(fp);
}
void print()
{
int i;
for(i=0;i<pc;i++)
printf("%06d\n",mem[i]);
}
void run()
{
int i;
pc = 0;
while(1)
{
opc = mem[pc]/10000;
op1 = mem[pc]%10000/1000-1;
op2 = mem[pc]%1000;
switch(opc)
{
case 0: // STOP
return;
case 1: // ADD
reg[op1]+=mem[op2];
break;
case 2: // SUB
reg[op1]-=mem[op2];
break;
case 3: // MULT
reg[op1]*=mem[op2];
break;
case 8: // DIV
reg[op1]/=mem[op2];
break;
case 4: // MOVER
reg[op1]=mem[op2];
break;
case 5: // MOVEM
mem[op2]=reg[op1];
break;
case 6: // COMP
if(reg[op1] < mem[op2])
cond[0]=1;
if(reg[op1] <= mem[op2])
cond[1]=1;
if(reg[op1] == mem[op2])
cond[2]=1;
if(reg[op1] > mem[op2])
cond[3]=1;
if(reg[op1] >= mem[op2])
cond[4]=1;
break;
case 7: // BC
if(cond[op1]==1)
pc = op2-1;
for(i=0;i<5;i++)
cond[i]=0;
break;
case 9: // READ
scanf("%d",&mem[op2]);
break;
case 10:// PRINT
printf("%d\n",mem[op2]);
}
pc++;
}
}
int main()
{
int ch;
while(1)
{
printf("1.Load\n2.Print\n3.Run\n4.Exit\n");
printf("Enter your choice (1-4):");
scanf("%d",&ch);
switch(ch)
{
case 1:
load();
break;
case 2:
print();
break;
case 3:
run();
break;
case 4:
exit(0);
}
}
return 0;
}
#include<stdlib.h>
int mem[100],reg[4],cond[]={0,0,0,0,0,1},opc,op1,op2,pc;
FILE *fp;
char fname[20];
void load()
{
printf("Enter file name:");
scanf("%s",fname);
fp = fopen(fname,"r");
if(fp==NULL)
{
printf("File %s not found.\n",fname);
exit(1);
}
while(fscanf(fp,"%d",&mem[pc])!=-1)
pc++;
fclose(fp);
}
void print()
{
int i;
for(i=0;i<pc;i++)
printf("%06d\n",mem[i]);
}
void run()
{
int i;
pc = 0;
while(1)
{
opc = mem[pc]/10000;
op1 = mem[pc]%10000/1000-1;
op2 = mem[pc]%1000;
switch(opc)
{
case 0: // STOP
return;
case 1: // ADD
reg[op1]+=mem[op2];
break;
case 2: // SUB
reg[op1]-=mem[op2];
break;
case 3: // MULT
reg[op1]*=mem[op2];
break;
case 8: // DIV
reg[op1]/=mem[op2];
break;
case 4: // MOVER
reg[op1]=mem[op2];
break;
case 5: // MOVEM
mem[op2]=reg[op1];
break;
case 6: // COMP
if(reg[op1] < mem[op2])
cond[0]=1;
if(reg[op1] <= mem[op2])
cond[1]=1;
if(reg[op1] == mem[op2])
cond[2]=1;
if(reg[op1] > mem[op2])
cond[3]=1;
if(reg[op1] >= mem[op2])
cond[4]=1;
break;
case 7: // BC
if(cond[op1]==1)
pc = op2-1;
for(i=0;i<5;i++)
cond[i]=0;
break;
case 9: // READ
scanf("%d",&mem[op2]);
break;
case 10:// PRINT
printf("%d\n",mem[op2]);
}
pc++;
}
}
int main()
{
int ch;
while(1)
{
printf("1.Load\n2.Print\n3.Run\n4.Exit\n");
printf("Enter your choice (1-4):");
scanf("%d",&ch);
switch(ch)
{
case 1:
load();
break;
case 2:
print();
break;
case 3:
run();
break;
case 4:
exit(0);
}
}
return 0;
}
5 Comments
Thanks 👍
ReplyDeleteupload file too
ReplyDeleteyes Can i get the whole solution and questions in one pdf or any file like that
ReplyDelete
ReplyDeleteProgram/Source Code
Here is the source code of the C program for the FCFS Scheduling. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
Process Arrival Time Burst Time
P1 0 5
P2 0 11
P3 0 11
1. /*
2. * FCFS Scheduling Program in C
3. */
4.
5. #include
6. int main()
7. {
8. int pid[15];
9. int bt[15];
10. int n;
11. printf("Enter the number of processes: ");
12. scanf("%d",&n);
13.
14. printf("Enter process id of all the processes: ");
15. for(int i=0;i<n;i++)
16. {
17. scanf("%d",&pid[i]);
18. }
19.
20. printf("Enter burst time of all the processes: ");
21. for(int i=0;i<n;i++)
22. {
23. scanf("%d",&bt[i]);
24. }
25.
26. int i, wt[n];
27. wt[0]=0;
28.
29. //for calculating waiting time of each process
30. for(i=1; i<n; i++)
31. {
32. wt[i]= bt[i-1]+ wt[i-1];
33. }
34.
35. printf("Process ID Burst Time Waiting Time TurnAround Time\n");
36. float twt=0.0;
37. float tat= 0.0;
38. for(i=0; i<n; i++)
39. {
40. printf("%d\t\t", pid[i]);
41. printf("%d\t\t", bt[i]);
42. printf("%d\t\t", wt[i]);
43.
44. //calculating and printing turnaround time of each process
45. printf("%d\t\t", bt[i]+wt[i]);
46. printf("\n");
47.
48. //for calculating total waiting time
49. twt += wt[i];
50.
51. //for calculating total turnaround time
52. tat += (wt[i]+bt[i]);
53. }
54. float att,awt;
55.
56. //for calculating average waiting time
57. awt = twt/n;
58.
59. //for calculating average turnaround time
60. att = tat/n;
61. printf("Avg. waiting time= %f\n",awt);
62. printf("Avg. turnaround time= %f",att);
63. }
#include
ReplyDelete#include
#include
#include
voidbubblesort(intarr[30],intn)
{
inti,j,temp;
for(i=0;iarr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
voidinsertionsort(intarr[30],intn)
{
inti,j,temp;
for(i=1;i=0&&temp<=arr[j])
{
arr[j+1]=arr[j];
j=j-1;
}
arr[j+1]=temp;
}
}
voidfork1()
{
intarr[25],arr1[25],n,i,status;
printf("\nEnter the no of values in array :");
scanf("%d",&n);
printf("\nEnter the array elements :");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
intpid=fork();
if(pid==0)
{
sleep(10);
printf("\nchild process\n");
printf("child process id=%d\n",getpid());
insertionsort(arr,n);
printf("\nElements Sorted Using insertionsort:");
printf("\n");
for(i=0;i<n;i++)
printf("%d,",arr[i]);
printf("\b");
printf("\nparent process id=%d\n",getppid());
system("ps -x");
}
else
{
printf("\nparent process\n");
printf("\nparent process id=%d\n",getppid());
bubblesort(arr,n);
printf("Elements Sorted Using bubblesort:");
printf("\n");
for(i=0;i<n;i++)
printf("%d,",arr[i]);
printf("\n\n\n");
}
}
intmain()
{
fork1();
return0;
}