Vous êtes sur la page 1sur 51

SJF cpu scheduling

#include<stdio.h>

void main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);

printf("\nEnter Burst Time:\n");


for(i=0;i<n;i++)
{
printf("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1; //contains process number
}

//sorting burst time in ascending order using selection sort


for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}

temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;

temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}

wt[0]=0; //waiting time for first process will be zero

//calculate waiting time


for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];

total+=wt[i];
}

avg_wt=(float)total/n; //average waiting time


total=0;

printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");


for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //calculate turnaround time
total+=tat[i];
printf("\np%d\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}

avg_tat=(float)total/n; //average turnaround time


printf("\n\nAverage Waiting Time=%f",avg_wt);
printf("\nAverage Turnaround Time=%f\n",avg_tat);
}
Simulate FCFS disk scheduling algorithm.
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
char p[10][5];
int tot=0,wt[10],i,n;
float avg=0;
clrscr();
printf("enter no of processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter process%d name:\n",i+1);
scanf("%s",&p[i]);
printf("enter process time");
scanf("%d",&pt[i]);
}

wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=wt[i-1]+et[i-1];
tot=tot+wt[i];
}
avg=(float)tot/n;
printf("p_name\t P_time\t w_time\n");
for(i=0;i<n;i++)
printf("%s\t%d\t%d\n",p[i],et[i],wt[i]);
printf("total waiting time=%d\n avg waiting time=%f",tot,avg);
getch();
}
Output
enter no of processes: 5

enter process1 name: aaa


enter process time: 4
enter process2 name: bbb
enter process time: 3
enter process3 name: ccc
enter process time: 2
enter process4 name: ddd
enter process time: 5
enter process5 name: eee
enter process time: 1

p_name P_time w_time


aaa 4 0
bbb 3 4
ccc 2 7
ddd 5 9
eee 1 14
total waiting time=34
avg waiting time=6.80

Simulate SCAN disk scheduling algorithm

#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],b[20],n,i,j,temp,p,s,m,x,t=0;
clrscr();
printf("Enter head pointer position:");
scanf("%d",&a[0]);
s=a[0];
printf("\nEnter previous head position:");
scanf("%d",&p);
printf("\nEnter max track limit:");
scanf("%d",&m);
printf("\nEnter number of processes:");
scanf("%d",&n);
printf("\nEnter processes in request order");
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
a[n+1]=m;
a[n+2]=0;
for(i=n+1;i>=0;i--)
{
for(j=0;j<=i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(i=1;i<=n+1;i++)
{
if(s==a[i])
x=i;
}
j=0;
if(s<p)
{
for(i=x;i>0;i--)
{
t+=(a[i]-a[i-1]);
b[j++]=a[i];
}
t+=a[x+1]-a[0];
b[j++]=a[0];
for(i=x+1;i<n+1;i++)
{
t+=(a[i+1]-a[i]);
b[j++]=a[i];
}
b[j++]=a[i];
}
else
{
for(i=x;i<n+2;i++)
{
t+=(a[i+1]-a[i]);
b[j++]=a[i];
}
t+=a[n+2]-a[x-1];
b[j++]=a[n+2];
for(i=x-1;i>1;i--)
{
t+=(a[i]-a[i-1]);
b[j++]=a[i];
}
b[j++]=a[i];
}
printf("\nProcessing order:");
for(i=0;i<=n+1;i++)
printf("\t%d",b[i]);
printf("\nTotal Head Movement:%d",t);
getch();

C-SCAN disk scheduling

#include<iostream.h>
#include<conio.h>
void scan(int left[],int right[],int i,int head,int n)
{
int ans[20];
int a,b,c,d;
a=i-1;
d=0;
c=n-1-i;
b=i;
while(a>-1)
{ cout<<\n a is <<a;
cout<<\n left[a] is<<left[a];

ans[d]=left[a];
cout<<\n answer is<<ans[d];
a;
d++;
}
cout<<d is<<d;
while(d<n)
{
ans[d]=right[c];
cout<<\n right c is<<right[c];
cout<<\n ans[d] is <<ans[d];
c;
d++;
}
cout<<order is;
getch();
for(int j=0;j<n;j++)
{
cout<<\n<<ans[j];
}
}
void divide(int d[],int n,int head)
{
cout<<array is;
for(int q=0;q<n;q++)
{
cout<<\n <<d[q];
}
int left[10],right[10];
for(int i=0;i<n;i++)
{
if(d[i]>head)
{
cout<<breaking at <<d[i];
break;
}
}
cout<<value of i<<i;
int k,l,m;
l=1;
k=0;
m=n;
left[0]=d[0];
cout<<left is<<left[0];
while(l<i)
{
cout<<\n d[l] value <<d[l];
left[l]=d[l];
cout<<\n left is <<left[l];
l++;
cout<<l is <<l;
}
int o;
o=i;
while(o<m)
{
right[k]=d[o];
cout<<\n right is<<right[k];
cout<<\n d[i] is<<d[o];
k++;
o++;
}
scan(left,right,i,head,n);
}
void sort(int d[],int n)
{
int temp,small,pos;
for(int i=0;i<n-1;i++)
{
small=d[i];
pos=i;
for(int j=i+1;j<n;j++)
{
if(small>d[j])
{
small=d[j];
pos=j;
}
}
temp=d[pos];
d[pos]=d[i];
d[i]=temp;
}
}
void main()
{
clrscr();
int head,d[20],n;
cout<< enter number of disk schedulings you want to do;
cin>>n;
cout<<enter head;
cin>>head;
for(int i=0;i<n;i++)
{
cout<<\n enter;
cin>>d[i];
}
sort(d,n);
divide(d,n,head);
getch();
}
File Organization
Write a C program to simulate the following file organization techniques
a) Single level directory b) Two level directory c) Hierarchical
DESCRIPTION
The directory structure is the organization of files into a hierarchy of folders. In a
single-level directory system, all the files are placed in one directory. There is a root directory
which has all files. It has a simple architectureand there are no sub directories. Advantage of
single level directory system is that it is easy to find a file in thedirectory. In the two-level
directory system, each user has own user file directory (UFD). The system maintains a master
block that has one entry for each user. This master block contains the addresses of the
directory of the users. When a user job starts or a user logs in, the system's master file
directory (MFD) is searched. When a user refers to a particular file, only his own UFD is
searched. This effectively solves the name collision problem and isolates users from one
another. Hierarchical directory structure allows users to create their own subdirectories and to
organize their files accordingly. A tree is the most common directory structure. The tree has a
root directory, and every file in the system has a unique path name. A directory (or
subdirectory) contains a set of files or subdirectories.
PROGRAM
1. SINGLE LEVEL DIRECTORY ORGANIZATION

#include<stdio.h>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir;
void main()
{
int i,ch;
char f[30];
clrscr();
dir.fcnt = 0;
printf("\nEnter name of directory -- ");
scanf("%s", dir.dname);
while(1)
{
printf("\n\n 1. Create File\t2. Delete File\t3. Search File \n 4. Display Files\t5. Exit\nEnter
your choice -- ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\n Enter the name of the file -- ");
scanf("%s",dir.fname[dir.fcnt]);
dir.fcnt++;
break;
case 2: printf("\n Enter the name of the file -- ");
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f, dir.fname[i])==0)
{
printf("File %s is deleted ",f);
strcpy(dir.fname[i],dir.fname[dir.fcnt-1]);
break;
}
}
if(i==dir.fcnt)
printf("File %s not found",f);
else
dir.fcnt--;
break;
case 3: printf("\n Enter the name of the file -- ");
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f, dir.fname[i])==0)
{
printf("File %s is found ", f);
break;
}
}
if(i==dir.fcnt)
printf("File %s not found",f);
break;
case 4: if(dir.fcnt==0)
printf("\n Directory Empty");
else
{
printf("\n The Files are -- ");
for(i=0;i<dir.fcnt;i++)
printf("\t%s",dir.fname[i]);
}
break;
default: exit(0);
}
}
getch();
}
OUTPUT:

Enter name of directory -- CSE

1. Create File 2. Delete File 3. Search File


4. Display Files 5. Exit Enter your choice 1
Enter the name of the file -- A

1. Create File 2. Delete File 3. Search File


4. Display Files 5. Exit Enter your choice 1

Enter the name of the file -- B

1. Create File 2. Delete File 3. Search File


4. Display Files 5. Exit Enter your choice 1

Enter the name of the file -- C

1. Create File 2. Delete File 3. Search File


4. Display Files 5. Exit Enter your choice 4

The Files are -- A B C

1. Create File 2. Delete File 3. Search File


4. Display Files 5. Exit Enter your choice 3

Enter the name of the file ABC

File ABC not found


1. Create File 2. Delete File 3. Search File
4. Display Files 5. Exit Enter your choice 2

Enter the name of the file B


File B is deleted

1. Create File 2. Delete File 3. Search File


4. Display Files 5. Exit Enter your choice 5

2. TWO LEVEL DIRECTORY ORGANIZATION


#include<stdio.h>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir[10];
void main()
{
int i,ch,dcnt,k;
char f[30], d[30];
clrscr();
dcnt=0;
while(1)
{
printf("\n\n 1. Create Directory\t 2. Create File\t 3. Delete File");
printf("\n 4. Search File \t \t 5. Display \t 6. Exit \t Enter your choice -- ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\n Enter name of directory -- ");
scanf("%s", dir[dcnt].dname);
dir[dcnt].fcnt=0;
dcnt++;
printf("Directory created");
break;
case 2: printf("\n Enter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter name of the file -- ");
scanf("%s",dir[i].fname[dir[i].fcnt]);
dir[i].fcnt++;
printf("File created");
break;
}
if(i==dcnt)
printf("Directory %s not found",d);
break;
case 3: printf("\nEnter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
{
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter name of the file -- ");
scanf("%s",f);
for(k=0;k<dir[i].fcnt;k++)
{
if(strcmp(f, dir[i].fname[k])==0)
{
printf("File %s is deleted ",f);
dir[i].fcnt--;
strcpy(dir[i].fname[k],dir[i].fname[dir[i].fcnt]);
goto jmp;
}
}
printf("File %s not found",f);
goto jmp;
}
}
printf("Directory %s not found",d);
jmp : break;
case 4: printf("\nEnter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
{

if(strcmp(d,dir[i].dname)==0)
{
printf("Enter the name of the file -- ");
scanf("%s",f);
for(k=0;k<dir[i].fcnt;k++)
{
if(strcmp(f, dir[i].fname[k])==0)
{
printf("File %s is found ",f);
goto jmp1;
}
}
printf("File %s not found",f);
goto jmp1;
}
}
printf("Directory %s not found",d);
jmp1: break;
case 5: if(dcnt==0)
printf("\nNo Directory's ");
else
{
printf("\nDirectory\tFiles");
for(i=0;i<dcnt;i++)
{
printf("\n%s\t\t",dir[i].dname);
for(k=0;k<dir[i].fcnt;k++)
printf("\t%s",dir[i].fname[k]);
}
}
break;
default:exit(0);
}
}
getch();
}

OUTPUT:
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit Enter your choice -- 1

Enter name of directory -- DIR1


Directory created

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice -- 1

Enter name of directory -- DIR2

Directory created

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice -- 2

Enter name of the directory DIR1


Enter name of the file -- A1
File created

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice -- 2

Enter name of the directory DIR1


Enter name of the file -- A2
File created

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice -- 2

Enter name of the directory DIR2


Enter name of the file -- B1
File created

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice -- 5
Directory Files
DIR1 A1 A2
DIR2 B1

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice -- 4

Enter name of the directory DIR


Directory not found

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice -- 3
Enter name of the directory DIR1
Enter name of the file -- A2

File A2 is deleted

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice 6

3. HIERARCHICAL DIRECTORY ORGANIZATION

#include<stdio.h>
#include<graphics.h>
struct tree_element
{
char name[20];
int x,y,ftype,lx,rx,nc,level;
struct tree_element *link[5];
};
typedef struct tree_element
node; void main()
{
int gd=DETECT,gm;
node *root;
root=NULL;
clrscr();
create(&root,0,"root",0,639,320);
clrscr();
initgraph(&gd,&gm,"c:\\tc\\BGI");
display(root);
getch();
closegraph();
}
create(node **root,int lev,char *dname,int lx,int rx,int x)
{
int i,gap;
if(*root==NULL)
{
(*root)=(node *)malloc(sizeof(node));
printf("Enter name of dir/file(under %s) :",dname);
fflush(stdin);
gets((*root)->name);
printf("enter 1 for Dir/2 forfile :");
scanf("%d",&(*root)->ftype);
(*root)->level=lev;
(*root)->y=50+lev*50;
(*root)->x=x;
(*root)->lx=lx;
(*root)->rx=rx;
for(i=0;i<5;i++)
(*root)->link[i]=NULL;
if((*root)->ftype==1)
{
printf("No of sub directories/files(for %s):",(*root)->name); scanf("%d",&(*root)->nc);
if((*root)->nc==0)
gap=rx-lx;
else gap=(rx-lx)/(*root)->nc;
for(i=0;i<(*root)->nc;i++)
create(&((*root)->link[i]),lev+1,(*root)->name,lx+gap*i,lx+gap*i+gap,lx+gap*i+gap/2);
}
else (*root)->nc=0;
}
}
display(node *root)
{
int i;
settextstyle(2,0,4);
settextjustify(1,1);
setfillstyle(1,BLUE);
setcolor(14); if(root!=NULL)
{
for(i=0;i<root->nc;i++)
{
line(root->x,root->y,root->link[i]->x,root->link[i]->y);
}
if(root->ftype==1) bar3d(root->x-20,root->y-10,root->x+20,root->y+10,0,0); else
fillellipse(root->x,root->y,20,20);
outtextxy(root->x,root->y,root->name); for(i=0;i<root->nc;i++)
{
display(root->link[i]);
}
}
}

OUTPUT:
Enter Name of dir/file (under root): ROOT
Enter 1 for Dir / 2 For File : 1
No of subdirectories / files (for ROOT) :2
Enter Name of dir/file (under ROOT):USER 1
Enter 1 for Dir /2 for file:1
No of subdirectories /files (for USER 1):1
Enter Name of dir/file (under USER 1):SUBDIR
Enter 1 for Dir /2 for file:1
No of subdirectories /files (for SUBDIR):2
Enter Name of dir/file (under USER 1):
JAVA Enter 1 for Dir /2 for file:1
No of subdirectories /files (for JAVA): 0
Enter Name of dir/file (under SUBDIR):VB
Enter 1 for Dir /2 for file:1
No of subdirectories /files (for VB): 0

Enter Name of dir/file (under ROOT):USER2


Enter 1 for Dir /2 for file:1
No of subdirectories /files (for USER2):2
Enter Name of dir/file (under ROOT):A
Enter 1 for Dir /2 for file:2
Enter Name of dir/file (under USER2):SUBDIR 2
Enter 1 for Dir /2 for file:1
No of subdirectories /files (for SUBDIR 2):2

Enter Name of dir/file (under SUBDIR2):PPL


Enter 1 for Dir /2 for file:1
No of subdirectories /files (for PPL):2
Enter Name of dir/file (under PPL):B
Enter 1 for Dir /2 for file:2
Enter Name of dir/file (under PPL):C
Enter 1 for Dir /2 for file:2
Enter Name of dir/file (under SUBDIR):AI
Enter 1 for Dir /2 for file:1
No of subdirectories /files (for AI): 2
Enter Name of dir/file (under AI):D
Enter 1 for Dir /2 for file:2
Enter Name of dir/file (under AI):E

Enter 1 for Dir /2 for file:2


C program of Bankers Algorithm to avoid deadlock :

Bankers algorithm is a resource allocation and deadlock avoidance algorithm developed by


Edsger Dijkstra that tests for safety by simulating the allocation of predetermined maximum
possible amounts of all resources, and then makes a s-state check to test for possible
deadlock conditions for all other pending activities, before deciding whether allocation
should be allowed to continue.

How Bankers Algorithm works ?

The Bankers algorithm is run by the operating system whenever a process requests
resources. The algorithm avoids deadlock by denying or postponing the request if it
determines that accepting the request could put the system in an unsafe state (one where
deadlock could occur). When a new process enters a system, it must declare the maximum
number of instances of each resource type that may not exceed the total number of resources
in the system. Also, when a process gets all its requested resources it must return them in a
finite amount of time.
#include<stdio.h>
#include<conio.h>
int main()
{
int clm[7][5],req[7][5],alloc[7][5],rsrc[5],avail[5],comp[7];
int first,p,r,i,j,prc,count,t;
count=0;
for(i=1;i<=7;i++)
comp[i]=0;
printf("\t BANKERS ALGORITHM IN C \n\n");
printf("Enter the no of processes : ");
scanf("%d",&p);
printf("\n\nEnter the no of resources : ");
scanf("%d",&r);
printf("\n\nEnter the claim for each process : ");
for(i=1;i<=p;i++)
{
printf("\nFor process %d : ",i);
for(j=1;j<=r;j++)
{
scanf("%d",&clm[i][j]);
}
}
printf("\n\nEnter the allocation for each process : ");
for(i=1;i<=p;i++)
{
printf("\nFor process %d : ",i);
for(j=1;j<=r;j++)
{
scanf("%d",&alloc[i][j]);
}
}
printf("\n\nEnter total no of each resource : ");
for(j=1;j<=r;j++)
scanf("%d",&rsrc[j]);
for(j=1;j<=r;j++)
{
int total=0;
avail[j]=0;
for(i=1;i<=p;i++)
{total+=alloc[i][j];}
avail[j]=rsrc[j]-total;
}
do
{
for(i=1;i<=p;i++)
{
for(j=1;j<=r;j++)
{
req[i][j]=clm[i][j]-alloc[i][j];
}
}
printf("\n\nAvailable resources are : ");
for(j=1;j<=r;j++)
{ printf(" ",avail[j]); }
printf("\nClaim matrix:\tAllocation matrix:\n");
for(i=1;i<=p;i++)
{
for(j=1;j<=r;j++)
{
printf("%d\t",clm[i][j]);
}
printf("\t\t\t");
for(j=1;j<=r;j++)
{
printf("%d\t",alloc[i][j]);
}
printf("\n");
}
prc=0;
for(i=1;i<=p;i++)
{
if(comp[i]==0)//if not completed
{
prc=i;
for(j=1;j<=r;j++)
{
if(avail[j]=0)
{
prc=0;
break;
}
}
}
if(prc!=0)
break;
}
if(prc!=0)
{
printf("\nProcess ",prc,"runs to completion!");
count++;
for(j=1;j<=r;j++)
{
avail[j]+=alloc[prc][j];
alloc[prc][j]=0;
clm[prc][j]=0;
comp[prc]=1;
}
}
}
while(count!=p&&prc!=0);
if(count==p)
printf("\nThe system is in a safe state!!");
else
printf("\nThe system is in an unsafe state!!");
getch();
}
Producer Consumer Problem in C

#include<stdio.h>

#include<stdlib.h>

int mutex=1,full=0,empty=3,x=0;

int main()

int n;

void producer();

void consumer();

int wait(int);

int signal(int);

printf("\n1.Producer\n2.Consumer\n3.Exit");

while(1)

printf("\nEnter your choice:");

scanf("%d",&n);

switch(n)

case 1: if((mutex==1)&&(empty!=0))

producer();

else

printf("Buffer is full!!");
break;

case 2: if((mutex==1)&&(full!=0))

consumer();

else

printf("Buffer is empty!!");

break;

case 3:

exit(0);

break;

return 0;

int wait(int s)

return (--s);

int signal(int s)

return(++s);

}
void producer()

mutex=wait(mutex);

full=signal(full);

empty=wait(empty);

x++;

printf("\nProducer produces the item %d",x);

mutex=signal(mutex);

void consumer()

mutex=wait(mutex);

full=wait(full);

empty=signal(empty);

printf("\nConsumer consumes item %d",x);

x--;

mutex=signal(mutex);

Output
1.Producer

2.Consumer

3.Exit

Enter your choice:1

Producer produces the item 1

Enter your choice:2

Consumer consumes item 1

Enter your choice:2

Buffer is empty!!

Enter your choice:1

Producer produces the item 1

Enter your choice:1

Producer produces the item 2

Enter your choice:1

Producer produces the item 3

Enter your choice:1

Buffer is full!!

Enter your choice:3


Simulate the working of a Dining philosophers problem.

#include<stdio.h>

#include<semaphore.h>

#include<pthread.h>

#define N 5

#define THINKING 0

#define HUNGRY 1

#define EATING 2

#define LEFT (ph_num+4)%N

#define RIGHT (ph_num+1)%N

sem_t mutex;

sem_t S[N];

void * philospher(void *num);

void take_fork(int);

void put_fork(int);

void test(int);

int state[N];

int phil_num[N]={0,1,2,3,4};
int main()

int i;

pthread_t thread_id[N];

sem_init(&mutex,0,1);

for(i=0;i<N;i++)

sem_init(&S[i],0,0);

for(i=0;i<N;i++)

pthread_create(&thread_id[i],NULL,philospher,&phil_num[i]);

printf("Philosopher %d is thinkingn",i+1);

for(i=0;i<N;i++)

pthread_join(thread_id[i],NULL);

void *philospher(void *num)

while(1)

int *i = num;

sleep(1);

take_fork(*i);

sleep(0);
put_fork(*i);

void take_fork(int ph_num)

sem_wait(&mutex);

state[ph_num] = HUNGRY;

printf("Philosopher %d is Hungryn",ph_num+1);

test(ph_num);

sem_post(&mutex);

sem_wait(&S[ph_num]);

sleep(1);

void test(int ph_num)

if (state[ph_num] == HUNGRY && state[LEFT] != EATING && state[RIGHT] !=


EATING)

state[ph_num] = EATING;

sleep(2);

printf("Philosopher %d takes fork %d and %dn",ph_num+1,LEFT+1,ph_num+1);

printf("Philosopher %d is Eatingn",ph_num+1);
sem_post(&S[ph_num]);

void put_fork(int ph_num)

sem_wait(&mutex);

state[ph_num] = THINKING;

printf("Philosopher %d putting fork %d and %d downn",ph_num+1,LEFT+1,ph_num+1);

printf("Philosopher %d is thinkingn",ph_num+1);

test(LEFT);

test(RIGHT);

sem_post(&mutex);

Pass 1 of a two pass assembler

#include<stdio.h>

#include<stdlib.h>

#include<conio.h>

#include<string.h>

void main()

FILE *f1,*f2,*f3,*f4,*f5;

int lc,sa,i=0,j=0,m[10],pgmlen,len,k,len1,l=0;
char name[10],opnd[10],la[10],mne[10],s1[10],mne1[10],opnd1[10];

char lcs[10],ms[10];

char sym[10],symaddr[10],obj1[10],obj2[10],s2[10],q[10],s3[10];

clrscr();

f1=fopen("input.txt","r");

f2=fopen("optab.txt","r");

f3=fopen("symtab.txt","w+");

f4=fopen("symtab1.txt","w+");

f5=fopen("output.txt","w+");

fscanf(f1,"%s%s%s",la,mne,opnd);

if(strcmp(mne,"START")==0)

sa=atoi(opnd);

strcpy(name,la);

lc=sa;

strcpy(s1,"*");

fscanf(f1,"%s%s%s",la,mne,opnd);

while(strcmp(mne,"END")!=0)

if(strcmp(la,"-")==0)

fscanf(f2,"%s%s",mne1,opnd1);

while(!feof(f2))
{

if(strcmp(mne1,mne)==0)

m[i]=lc+1;

fprintf(f3,"%s\t%s\n",opnd,s1);

fprintf(f5,"%s\t0000\n",opnd1);

lc=lc+3;

i=i+1;

break;

else

fscanf(f2,"%s%s",mne1,opnd1);

else

fseek(f3,SEEK_SET,0);

fscanf(f3,"%s%s",sym,symaddr);

while(!feof(f3))

if(strcmp(sym,la)==0)

{
itoa(lc,lcs,10);

fprintf(f4,"%s\t%s\n",la,lcs);

itoa(m[j],ms,10);

j=j+1;

fprintf(f5,"%s\t%s\n",ms,lcs);

i=i+1;

break;

else

fscanf(f3,"%s%s",sym,symaddr);

} //f3

if(strcmp(mne,"RESW")==0)

lc=lc+3*atoi(opnd);

else if(strcmp(mne,"BYTE")==0)

strcpy(s2,"-");

len=strlen(opnd);

lc=lc+len-2;

for(k=2;k<len;k++)

q[l]=opnd[k];

l=l+1;

fprintf(f5,"%s\t%s\n",q,s2);
break;

else if(strcmp(mne,"RESB")==0)

lc=lc+atoi(opnd);

else if(strcmp(mne,"WORD")==0)

strcpy(s3,"#");

lc=lc+3;

fprintf(f5,"%s\t%s\n",opnd,s3);

break;

} // else la=-

fseek(f2,SEEK_SET,0);

fscanf(f1,"%s%s%s",la,mne,opnd);

fseek(f5,SEEK_SET,0);

pgmlen=lc-sa;

printf("H^%s^%d^0%x\n",name,sa,pgmlen);

printf("T^");

printf("00%d^0%x",sa,pgmlen);

fscanf(f5,"%s%s",obj1,obj2);

while(!feof(f5))
{

if(strcmp(obj2,"0000")==0)

printf("^%s%s",obj1,obj2);

else if(strcmp(obj2,"-")==0)

printf("^");

len1=strlen(obj1);

for(k=0;k<len1;k++)

printf("%d",obj1[k]);

else if(strcmp(obj2,"#")==0)

printf("^");

printf("%s",obj1);

fscanf(f5,"%s%s",obj1,obj2);

fseek(f5,SEEK_SET,0);

fscanf(f5,"%s%s",obj1,obj2);

while(!feof(f5))

if(strcmp(obj2,"0000")!=0)

if(strcmp(obj2,"-")!=0)
{

if(strcmp(obj2,"#")!=0)

printf("\n");

printf("T^%s^02^%s",obj1,obj2);

fscanf(f5,"%s%s",obj1,obj2);

printf("\nE^00%d",sa);

getch();

Input:

input.txt

COPY START 1000

- LDA ALPHA

- STA BETA

ALPHA RESW 1

BETA RESW 1

- END - optab.txt
optab.txt

LDA 00

STA 23

LDCH 15

STCH 18

Output:

symtab.txt

ALPHA *

BETA * symtab1.txt

ALPHA 1006

BETA 1009

Output.txt

00 0000

23 0000

1001 1006

1004 1009

result.txt:
H^COPY^1000^0c

T^001000^0c^000000^230000

T^1001^02^1006

T^1004^02^1009

E^001000

Pass 2 of two pass assembler

#include<stdio.h>

#include<conio.h>

#include<string.h>

#include<ctype.h>

main()

FILE *fint,*ftab,*flen,*fsym;

int op1[10],txtlen,txtlen1,i,j=0,len;

char
add[5],symadd[5],op[5],start[10],temp[30],line[20],label[20],mne[10],operand[10],symtab[1
0],opmne[10];

clrscr();

fint=fopen("input.txt","r");

flen=fopen("length.txt","r");

ftab=fopen("optab.txt","r");

fsym=fopen("symbol.txt","r");

fscanf(fint,"%s%s%s%s",add,label,mne,operand);

if(strcmp(mne,"START")==0)
{

strcpy(start,operand);

fscanf(flen,"%d",&len);

printf("H^%s^%s^%d\nT^00%s^",label,start,len,start);

fscanf(fint,"%s%s%s%s",add,label,mne,operand);

while(strcmp(mne,"END")!=0)

fscanf(ftab,"%s%s",opmne,op);

while(!feof(ftab))

if(strcmp(mne,opmne)==0)

fclose(ftab);

fscanf(fsym,"%s%s",symadd,symtab);

while(!feof(fsym))

if(strcmp(operand,symtab)==0)

printf("%s%s^",op,symadd);

break;

else

fscanf(fsym,"%s%s",symadd,symtab);
}

break;

else

fscanf(ftab,"%s%s",opmne,op);

if((strcmp(mne,"BYTE")==0)||(strcmp(mne,"WORD")==0))

if(strcmp(mne,"WORD")==0)

printf("0000%s^",operand);

else

len=strlen(operand);

for(i=2;i<len;i++)

printf("%d",operand[i]);

printf("^");

fscanf(fint,"%s%s%s%s",add,label,mne,operand);

ftab=fopen("optab.txt","r");

fseek(ftab,SEEK_SET,0);

}
printf("\nE^00%s",start);

fclose(fint);

fclose(ftab);

fclose(fsym);

fclose(flen);

fclose(fout);

getch();

input.txt:

- COPY START 1000

1000 - LDA ALPHA

1003 - ADD ONE

1006 - SUB TWO

1009 - STA BETA

1012 ALPHA BYTE C'KLNCE

1017 ONE RESB 2

1019 TWO WORD 5

1022 BETA RESW 1

1025 - END -

optab.txt:

LDA 00
STA 23

ADD 01

SUB 05

length.txt:25

symbol.txt:

1012 ALPHA

1017 ONE

1019 TWO

1022 BETA

PASS ONE OF TWO PASS ASSEMBLER

AIM:

To implement pass one of a two pass assembler in C language.

ALGORITHM:

1.Open and Read the input file

2.If the input line has the opcode START do the following

2.1Find if there is any operand field after START, initialize the LOCCTR to the operand
value

2.2Otherwise if there is no value in the operand field then LOCCTR is set to 0

3.Write the input line to the intermediate file

4.Do the following steps until the opcode is END


4.1Check the Symbol table, if the symbol is not available then enter that symbol intothe
SYMTAB, along with the memory address in which it is stored.Otherwise, the error message
should be displayed

4.2If there is a opcode

4.2.1If opcode is present in the OPTAB, then increment the LOCCTR by3

4.2.2If opcode is WORD, then increment LOCCTR by 3;

4.2.3If opcode is BYTE, then increment LOCCTR by 1;

4.2.4If opcode is RESW then increment LOCCTR by the integer equivalent of the operand
value * 3;

4.2.5If opcode is RESB, then increment LOCCTR by the integer equivalent of the operand
value

4.3Write the processed lines in the intermediate file along with their location counters

5.To find the length of the program, Subtract the starting address of the program from the
final value of the LOCCTR

6.Close all the files and exit

PROGRAM:

/*C Program to implement

PASS ONE OF TWO PASS ASSEMBLER

*/#include<stdio.h>

#include<conio.h>

#include<string.h>

#include<stdlib.h>

Void main()

char opcode[10],mnemonic[10],operand[10],label[10],code[10];
int locctr,start,length;

FILE*fp1,*fp2,*fp3,*fp4;

clrscr();

fp1=fopen("input.dat","r");

fp2=fopen("symtab.dat","w");

fp3=fopen("out.dat","w");

fp4=fopen("optab.dat","r");

fscanf(fp1,"%s%s%s",label,opcode,operand);

if(strcmp(opcode,"START")==0)

start=atoi(operand);

locctr=start;

fprintf(fp3,"%s\t%s\t%s\n",label,opcode,operand);

fscanf(fp1,"%s%s%s",label,opcode,operand);

Else

locctr=0;

while(strcmp(opcode,"END")!=0)

fprintf(fp3,"%d\t",locctr);

if(strcmp(label,"**")!=0)

fprintf(fp2,"%s\t%d\n",label,locctr);

rewind(fp4);

fscanf(fp4,"%s",mnemonic);
while(strcmp(mnemonic,"END")!=0)

if(strcmp(opcode,mnemonic)==0)

locctr+=3;

break;

fscanf(fp4,"%s",mnemonic);

if(strcmp(opcode,"WORD")==0)

locctr+=3;

else if(strcmp(opcode,"RESW")==0)

locctr+=(3*(atoi(operand)));

else if(strcmp(opcode,"RESB")==0)

locctr+=(atoi(operand));

else if(strcmp(opcode,"BYTE")==0)

++locctr;

fprintf(fp3,"%s\t%s\t%s\n",label,opcode,operand);

fscanf(fp1,"%s%s%s",label,opcode,operand);

fprintf(fp3,"%d\t%s\t%s\t%s\n",locctr,label,opcode,operand);

length=locctr-start;

printf("\nThe length of the program is %d",length);

fclose(fp1);
fclose(fp2);

fclose(fp3);

fclose(fp4);

getch();

INPUT.DAT:

MAIN START 2000

BEGIN

LDA NUM1

** ADD NUM2

** LDCH CHAR1

** STCH CHAR2

NUM1 WORD 5

NUM2 RESW 1

CHAR1 BYTE C'A'

CHAR2 RESB 1

** END BEGIN
RESULT:

Thus pass one of a two pass assembler is implemented in C language.

Vous aimerez peut-être aussi