Tuesday, 22 November 2011
Monday, 14 November 2011
OPERATING SYSTEM LAB MANUAL ANNA UNIVERSITY, EVEN SEMESTER
EX.NO: 1A
DATE:
IMPLEMENTATION OF LINUX SYSTEM CALLS
[fork(),wait(),getpid()]
AIM:
To write a C program using LINUX system calls (fork(),wait(),getpid()).
ALGORITHM:
Step1: Start the process.
Step2: Create a new process (parent process) using fork() system call.
Step3: Create a identical copy of parent process(child process) using fork () system call
Step4: Process id is created for both child and parent process using getpid() system call
Step5: The Child process is made to execute.
Step6: After the exiting of the child process parent process will get exit
Step7: Stop the process.
PROGRAM:
#include<stdio.h>
#include<sys/type.h>
#include<sys/wait.h>
#include<unistd.h>
void parent ();
void child();
main ()
{
int r;
r=fork();
{
printf(“fork empty”);
}
if(r>0)
parent(r);
else
child(r);
}
void child()
{
int i;
printf(“child starts executing”);
for(i=0;i<5;i++)
{
printf(“\n child %d is executing %d time”,getpid(),i);
sleep(i);
}
printf(“child %d exit \n”,getpid());
}
void parent(pid_t child_pid)
{
printf(“\n Parent %d watiting for child %d to start the executing “, getpid(),child_pid);
printf(“\n”);
waitpid(child_pid,NULL,0);
printf(“Parent %d exit \n”,getpid());
}
OUTPUT:
Parent 269 watiting for child 70 to start the executing
Child starts executing
Child 270 is executing 1 time
Child 270 is executing 1 time
Child 270 is executing 1 time
Child 270 is executing 1 time
Child 270 is executing 1 time
Child 270 exit.
Parent 269 exit
RESULT:
Thus the given C Program is executed and output is verified.
EX.NO: 1B
DATE:
IMPLMENTATION OF LINUX SYSTEM CALLS
[opendir (),readdir(),closedir()]
AIM:
To write a C program to access directories using LINUX system calls opendir (),readdir(),closedir().
ALGORITHM:
- Start the process.
- Create a directory entry in the dirent structure and a directory variable
- Get the name of the directory to be opened
- Using the system call opendir(),open the given directory and store it in the directory variable.
- Read the contents of the directory using readdir()system call and print the list of files in the directory
- Close the directory after the read and write process
- Stop the process.
PROGRAM:
#include<Stdio.h>
#include<dirent.h>
Struct dirent *dptr;
int main(int argc,char *argv[])
{
char buff[256];
DIR *dirp;
printf(“\n \n enter directory name”);
scanf(“%s”,buff);
if ((disp=opendir(buff))==null);
{
printf(“error”);
exit(1);
} while (dptr=readir(disp))
{
printf(“%s\n”,dptr->d_name);
}
closedir(dirp);
}
OUTPUT:
Enter directory name raju
Ashwini.c
maha.c
RESULT:
Thus the given C Program is executed and output is verified.
EX.NO: 1C
DATE:
IMPLEMENTATION OF LINUX SYSTEM CALL
[stat()]
AIM:
To write a C program to know the status of the file using LINUX system call stat().
ALGORITHM:
Step1: Start the process.
Step2: Define a structure stat.
Step3: Declare two unsigned variables for storing the address and total size of the file
Step4: Specify the filename and check the status of file using stat() system call
Step5: Print the address and size of the file.
Step6: Stop the process.
PROGRAM:
#include<sys/stat.h>
#include<stdio.h>
int main()
{
Struct stat s;
unsigned long a;
unsigned long b;
if(stat(“test.c”,&s)==(-1))
{
perror(“Error:cannot stat file”);
exit(EXIT_FAILURE);
}
a=s.st_blk.size;
b=s.st_size;
printf(“Address:%u \t Total size: %u \n”,a,b);
return 0;
}
OUTPUT:
Address:4096 Total size:52
RESULT:
Thus the given C Program is executed and output is verified.
EX.NO:1 D
DATE:
TO DEMONSTRATE EXECLP ( ) FUNCTION
AIM:
To write a shell program to implement the execlp() function.
ALGORITHM:
1. Start the program
2. Obtain the pid value using fork() function.
3. If pid <0 , then print fork failed.
4. Else if pid = 0,execlp() function will invoked.
5. Else print child process complete.
6. Terminate the program.
PROGRAM:
#include<stdio.h>
main(int argc,char *argv[])
{
int pid;
pid=fork();
if(pid<0)
{
fprintf(stderr,”Fork failed \n”);
exit(-1);
}
elseif(pid==0)
{
execlp(“/bin/ls”,”ls”,NULL);
}
else
{
wait(NULL);
printf(“Child Complete”);
exit(0);
}
OUTPUT:
a.out fib.sh mov.c len.sh str.sh fact.sh copy.c cat.c even.c odd.sh child complete
RESULT
Thus the execlp functions were implemented.
EX.NO: 2
DATE:
IMPLEMENTATION OF I/O SYSTEM CALLS
[open(),read(),write()]
AIM:
To write a c program to perform operations on files using LINUX system calls open(),read(),write()
ALGORITHM:
Step1: Start the process.
Step2: Declare and initialize an array variable.
Step3: Declare a file pointer.
Step4: Using the open() system call, open a file in a write mode and store it in a file
Pointer.
Step5: Using the write()system call, write the contents of an array to the file.
Step6: The written file is read with the help of read() system call. Then Close the file.
Step7: Stop the process.
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
double d[10]={1.3,1.0,1.23,1.9,0.91,1.5,1.4,0.0,1.01,8.15};
int main( void)
{
int I;
FILE *fp1;
if((fp=fopen(“my.txt”,”wb”))==NULL)
{
printf(“Cannot open file \n”);
exit(1);
}
/* write the entire array in one step */
if(fwrite(d,sizeof(d),1,fp)!=1)
{
printf(“write error \n”);
exit(1);
}
fclose(fp);
if((fp=fopen(“my.txt”,”rb”))==NULL)
{
printf(“Cannot open file \n”);
exit(1);
}
/*clear the array */
for(i=0;i<10;i++)
d[i]=0.0;
/* read the entire array in one step */
if(fread(d,sizeof(d),1,fp)!=1)
{
printf(“read error \n”);
exit(1);
}
fclose(fp);
/*display the array */
for(i=0;i<10;i++)
printf(“%f”,d[i]);
return 0;
}
OUTPUT:
1.300001.1.000001.230001.90000000.9100001.5000001.40000000.000001.0100008.150000
RESULT:
Thus the given C Program is executed and output is verified.
EX.NO: 3A
DATE:
SIMULATION OF LS COMMAND
AIM:
To write a C program to simulate ls command.
ALGORITHM:
- Start the process.
- Create a directory entry using dirent structure.
- To define a pointer to a structure, dp predefined structure DIR and another pointer to a structure called ep.
- The directory is opened using the opendir() function.
- In the while loop read each entry using the readdir() function which returns a pointer.
- If no pointer is returned the program is exited, else it will list the files inside the directory.
- The closedir() function closes this open directory.
- Stop the process.
PROGRAM:
#include<stdio.h>
#include<dirent.h>
Struct dirent *dptr;
int main(int argc,char* argv[])
{
char buff[256];
DIR *dirp;
printf(“\n \n enter directory name”);
scanf(“%s”,buff);
if((disp=opendir(buff))==NULL)
{
printf(“error”);
exit(1);
}
while (dptr==readir(disp))
{
printf(“%s \n”,dptr->-name);
}
closedir(dirp);
}
OUTPUT:
Enter directory name ex3
a.out
ex1.c
ls
ex3a
ex1
ex3.c
RESULT:
Thus the given C Program is executed and output is verified.
EX.NO: 3B
DATE:
SIMULATION OF GREP COMMAND
AIM:
To implement the grep command in C.
ALGORITHM:
- Start the process.
- Declare the required variables.
- Obtain the filename and the required word to be searched from the user.
- Open the file and read the contents till the end of the file.
- Search the given word in the file using strstr() function, if it matches, then the word is found.
- Do this till the end of file is reached.
- Stop the process.
PROGRAM:
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
Int calc_butsize(char *filename)
{
Struct stat st;
Stat(filename,&st);
Return((int)st.st_size);
}
Int main(int argc,char *argv[])
{
If(argc<3)
Printf(“usage:\n\t %s <word><files>…..\n”,argv[0]);
Return -1;
}
FILE *fp;
Char *filename;
Int *=2;
For(x;x!=argc;x++)
Filename=argv[x];
If((fp=fopen(filename;”r”))==NULL)
{
Printf(“Failed to open file:%s \n “, argv[2]);
Return -2;
}
Int BUFSIZE=calc_bufsize(filename);
Char buf[BUFSIZE];
Fread(&buf,sizeof(char),BUFSIZE,fp);
Char *ans=strstr(buf,argv[1]);
If(ans!=NULL)
Printf(“%s \n”,filename);
Printf(“the word is found “);
Else
Printf(“ the word is not found in the given file \n”);
Fclose(fp);
}
Return 0;
}
OUTPUT:
[cs28@linux ~]$ ./3b “include” 3b.c
3b.c
The given word is found
RESULT:
Thus the given C Program is executed and output is verified.
EX: NO: 4A
DATE:
IMPLEMENTATION OF FCFS SCHEDULING
AIM:
To write a C Program to perform FCFS Scheduling in Linux.
ALGORITHM:
- Start the program.
- Read the number of requests
- Read the burst(execution) time for each process
- The processes are allowed to execute depends of the arrival time.
- The waiting time is calculated for all the process.
- The gantt chart is drawn representing the process execution.
- Calculate the waiting time and the turnaround time for all the process.
- Stop the process.
PROGRAM:
#include<stdio.h>
int main()
{
int a[10],b[10],c[10],n,i,wt=0;
float avgwt;
a[0]=b[0]=0;
printf("\nEnter the no. of processes\t");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter the burst time:");
scanf("%d",&a[i]);
}
printf("\n The entered process are");
for(i=1;i<=n;i++)
printf("\np%d %d",i, a[i]);
printf("\n The gantt chart is:\n");
for(i=1;i<=n;i++)
{
printf("\t p%d",i);
}
printf("\n");
printf("\t%d",b[0]);
for(i=1;i<=n;i++)
{
b[i]=a[i]+b[i-1];
printf("\t%d",b[i]);
}
printf("\n The waiting time is");
for(i=0;i<n;i++)
{
printf("\np%d\t%d",i+1,b[i]);
wt=b[i]+wt;
}
avgwt=wt/i;
printf("\nThe average waiting time is %f",avgwt);
printf("\nThe turnaround time is");
wt=0;
for(i=1;i<=n;i++)
{
printf("\np%d\t%d",i,b[i]);
wt=b[i]+wt;
}
avgwt=wt/i;
printf("\nThe average turnaround time is %f",avgwt);
printf("\n");
}
OUTPUT:
[root@localhost ~]# ./p5
Enter the no. of processes 4
Enter the burst time:4
Enter the burst time:3
Enter the burst time:2
Enter the burst time:5
The entered processes are
P1 4
P2 3
P3 2
P4 5
The gantt chart is:
P1 P2 P3 P4
0 4 7 9 14
The waiting time is
P1 0
P2 4
P3 7
P4 9
The average waiting time is 5.000000
The turnaround time is
P1 4
P2 7
P3 9
P4 14
The average turnaround time is 6.000000
RESULT:
Thus the program using FCFS Scheduling was performed & the output was verified.
EX: NO: 4B
DATE: IMPLEMENTATION OF SJF SCHEDULING
AIM:
To write a C program to perform SJF scheduling in Linux.
ALGORITHM:
1. Stop the program.
2. Read the number of request
3. Read the servicing time for each process
4. Sort the process by servicing time(i.e. the job having least burst time is executed first)
5. Calculate the waiting time for all the process.
6. Calculate the average waiting time.
7. Draw the gantt chart and print the waiting time, average waiting time, turnaround time and average turnaround time
8. Stop the process
PROGRAM:
#include<stdio.h>
int main()
{
int p[5],b[5],w[5],f[5],n,tb,t[5],i,j,tw,tt,temp,wt,c[10];
float aw,at;
printf("\n Enter the no. of process:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the burst time:");
scanf("%d",&b[i]);
p[i]=i+1;
}
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(b[i]>b[j])
{
temp=b[i];
b[i]=b[j];
b[j]=temp;
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
tw=w[0]=tt=t[0]=tb=0;
for(i=0;i<n;i++)
{
w[i]=tb;
tb=tb+b[i];
t[i]=b[i]+w[i];
tw=tw+w[i];
tt=tt+t[i];
}
aw=(float)tw/n;
at=(float)tt/n;
for(i=0;i<n;i++)
c[i]=w[i];
c[n]=t[n-1];
printf("\n Process Burst time Waiting time Turnaround time\n");
for(i=0;i<n;i++)
printf("\np%d\t\t%d\t\t%d\t\t%d\n",p[i],b[i],w[i],t[i]);
printf("\nGantt chart\n");
for(i=0;i<=n;i++)
for(j=i+1;j<=n;j++)
if(c[i]>c[j])
{
temp=c[i];
c[i]=c[j];
c[j]=temp;
}
for(i=0;i<n;i++)
printf("\t p%d",p[i]);
printf("\n");
for(i=0;i<=n;i++)
printf("\t%d",c[i]);
printf("\n The total waiting time=%d",tw);
printf("\n Total turnaround time is=%d",tt);
printf("\n Average waiting time=%f",aw);
printf("\n Average turnaround time=%f",at);
printf("\n");
}
}
OUTPUT:
[root@localhost ~]# ./p5
Enter the no. of process:4
Enter the burst time:3
Enter the burst time:5
Enter the burst time:6
Enter the burst time:1
Process Burst time Waiting time Turnaround time
P4 1 0 1
P1 3 1 4
P2 5 4 9
P3 6 9 15
Gantt chart
P4 P1 P2 P3
0 1 4 9 15
The total waiting time=14
Total turnaround time is=29
Average waiting time=3.500000
Average turnaround time=7.250000
RESULT:
Thus the C program to perform SJF Scheduling in linux was done and the output was verified.
EX: NO: 5A
DATE:
IMPLEMENTATION OF ROUND ROBIN ALGORITHM
AIM:
To implement the round robin algorithm.
ALGORITHM:
- Start the program.
- Read the number of process and the time slice.
- Read the burst time for each process.
- Calculate the total turn around time, average waiting time based on the time slice.
- The Gantt chart is prepared based on the times the process gets executed.
- Display the result.
- Stop the execution.
PROGRAM:
#include<stdio.h>
int enqueue(int num,int y,int q[]);
int main()
{
int queue[20],i,pro[20];
int rear=0,num,tq,bur[20],wt[20],turn[20];
int flag,j=1,temp,burst[20],wait[20];
float avgwt=0,avgturn=0;
wt[0]=0;
printf("\n ROUND ROBIN SCHEDULING\n");
printf("\n\nEnter the number of process:");
scanf("%d",&num);
printf("\nProcess\tBurst time\n");
for(i=0;i<num;i++)
{
printf("\n p%d\t\t",i+1);
scanf("%d",&bur[i]);
burst[i]=bur[i];
}
printf("\n\nEnter the time quantum:");
scanf("%d",&tq);
do
{
flag=0;
for(i=0;i<num;i++)
{
if(bur[i]>=tq)
{
rear=enqueue(i+1,rear,queue);
bur[i]-=tq;
wt[j]=tq+wt[j-1];
j++;
}
else if((bur[i]>0)&&(bur[i]<tq))
{rear=enqueue(i+1,rear,queue);
wt[j]=bur[i]+wt[j-1];
bur[i]=0;
j++;
}
}
for(i=0;i<num;i++)
if(bur[i]==0)
flag++;
}while(flag!=num);
for(i=1;i<=rear;i++)
queue[i-1]=queue[i];
printf("\n\nGANTT CHART\n");
for(i=0;i<rear;i++)
printf(" p%d\t",queue[i]);
printf("\n");
for(i=0;i<=rear;i++)
printf("%d\t",wt[i]);
for(i=0;i<num;i++)
{
wait[i]=0;
temp=0;
for(j=0;j<rear;j++)
{
if(queue[j]==i+1)
{
wait[i]+=(wt[j]-temp);
temp=wt[j+1];
}
}
}
printf("\n\n**********************************************************");
printf("\nProcess\tBurst time \tWait time \tTurnaround time\n");
printf("**********************************************************\n");
for(i=0;i<num;i++)
{
turn[i]=wait[i]+burst[i];
printf("\np%d\t\t%d\t\t%d\t\t%d",i+1,burst[i],wait[i],turn[i]);
avgwt+=wait[i];
avgturn+=turn[i];
}
printf("\n\nAverage Waiting time is %f",avgwt/num);
printf("\n\nAverage Turnaround time is %f",avgturn/num);
}
}
int enqueue(int n,int rear,int queue[])
{
rear++;
queue[rear]=n;
return rear;
}
OUTPUT:
[root@localhost ~]# ./rr
ROUND ROBIN SCHEDULING
Enter the number of process: 4
Process Burst time
P1 3
P2 6
P3 4
P4 2
Enter the time quantum: 2
GANTT CHART
P1 P2 P3 P4 P1 P2 P3 P2
0 2 4 6 8 9 11 13 15
**********************************************************
Process Burst time Wait time Turnaround time
**********************************************************
P1 3 6 9
P2 6 9 15
P3 4 9 13
P4 2 6 8
Average waiting time is 7.500000
Average Turnaround time is 11.250000
RESULT:
Thus program is written by implementing Round robin algorithm and executed successfully.
EXNO: 5B
DATE:
IMPLEMENTATION OF PRIORITY SCHEDULING ALGORITHM
AIM:
To implement the priority scheduling algorithm.
ALGORITHM:
- Start the program
- Obtain the number of processes from the user
- Obtain the CPU burst time and the priorities for each process.
- Calculate the waiting time for each process such that the process with lower priority is executed first and the process with highest priority is executed last.
- Display all the details for each process along with the average waiting time.
- Stop the execution.
PROGRAM:
#include<stdio.h>
int enqueue(int num,int y,int q[]);
int main()
{
int p[5],b[5],w[5],f[5],n,tb,t[5],i,j,tw,tt,temp,wt,pr[10],c[10];
float aw,at;
printf("\n Enter the no. of process:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the burst time & priority:");
scanf("%d",&b[i]);
scanf("%d",&pr[i]);
p[i]=i+1;
}
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(pr[i]>pr[j])
{
temp=pr[i];
pr[i]=pr[j];
pr[j]=temp;
temp=b[i];
b[i]=b[j];
b[j]=temp;
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
tw=w[0]=tt=t[0]=tb=0;
for(i=0;i<n;i++)
{
w[i]=tb;
tb=tb+b[i];
t[i]=b[i]+w[i];
tw=tw+w[i];
tt=tt+t[i];
}
aw=(float)tw/n;
at=(float)tt/n;
for(i=0;i<n;i++)
c[i]=w[i];
c[n]=t[n-1];
printf("\n Process time Burst time Waiting time Turnaround time\n");
for(i=0;i<n;i++)
printf("\n\t%d\t\t%d\t\t%d\t\t%d\n",p[i],b[i],w[i],t[i]);
printf("\nGantt chart\n");
for(i=0;i<=n;i++)
for(j=i+1;j<=n;j++)
if(c[i]>c[j])
{
temp=c[i];
c[i]=c[j];
c[j]=temp;
}
for(i=0;i<n;i++)
printf("\t p%d",p[i]);
printf("\n");
for(i=0;i<=n;i++)
printf("\t%d",c[i]);
printf("\n The total waiting time=%d",tw);
printf("\n Total turnaround time is=%d",tt);
printf("\n Average waiting time=%f",aw);
printf("\n Average turnaround time=%f",at);
printf("\n");
}
OUTPUT:
Enter the no. of process: 4
Enter the burst time for P1: 4
Enter the priority for P1: 2
Enter the burst time for P2: 5
Enter the priority for P2: 3
Enter the burst time for P3: 6
Enter the priority for P3: 1
Enter the burst time for P4: 7
Enter the priority for P4: 4
Process time Burst time Waiting time Turnaround time
3 6 0 6
1 4 6 10
2 5 10 15
4 7 15 22
Gantt chart
P3 P1 P2 P4
0 6 10 15 22
The total waiting time=31
Total turnaround time is=53
Average waiting time=7.750000
Average turnaround time=13.250000
RESULT:
Thus the program using priority scheduling was done and the output was verified.
EX.NO:6A
DATE:
IMPLEMENTATION OF INTERPROCESS COMMUNICATION USING SHARED MEMORY
AIM:
To implement interprocess communication using shared memory.
ALGORITHM:
1.Start the process.
2.Create the child process using fork().
3.Create the shared memory for parent process using shmget() system call.
4.Now allow the parent process to write in shared memory using shmptr pointer which is return type of shmat().
5.Now across and attach the same shared memory to the child process.
6.The data in the shared memory is read by the child process using the shmptr pointer.
7.Now, detach and reuse the shared memory.
8.Stop the process
PROGRAM:
#include<stdio.h>
#include<sys/shm.h>
#include<sys/ipc.h>
int main()
{
int child, shmid, I;
char *shmptr;
child=fork();
if(!child)
{
shmid=shmget(4041,32,0666| IPC_CREAT);
shmptr=shmat(shmid,0,0);
printf(“\nParent writing:\t”);
for(I=0;I<10;I++)
{
shmptr[I]=’a’+I;
putchar(shmptr[I]);
}
printf(“\n”);
wait(NULL);
}
else
{
shmid=shmget(4041,32,0666);
shmptr=shmat(shmid,0,0);
printf(“\nChild is reading:\t”);
for(I=0;I<10;I++)
putchar(shmptr[I]);
shmdt(NULL);
shmctl(shmid,IPC_RMID,NULL);
}
printf("\n");
return 0;
}
OUTPUT:
[root@localhost ~]# ./ipc
Parent writing: abcdefghij
Child is reading: abcdefghij
RESULT:
Thus a interprocess communication using shared memory was implemented in linux platform and the output was verified.
EX.NO:6B
DATE:
IMPLEMENTATION OF INTERPROCESS COMMUNICATION USING PIPES
AIM:
To implement interprocess communication using pipes.
ALGORITHM:
1. Start the process.
2. Create a child process using fork().
3. Now close the read end of the parent using close().
4. Write the data in the pipe using write().
5. Now close the read end of the child using close().
6. Read the data in the pipe using read().
7. Display the string.
8. Stop the process.
PROGRAM:
#include<stdio.h>
int main()
{
int fd[2],child;
char a[10];
printf(“Enter the string to enter into the pipe:\t”);
scanf(“%s”, a);
pipe(fd);
child=fork();
if(!child)
{
close(fd[0]);
write(fd[1],a,5);
wait(0);
}
else
{
close(fd[1]);
read(fd[0],a,5);
printf(“The string received from the pipe is %s”, a);
}
return 0;
}
OUTPUT:
[root@localhost ~]# ./pipes
Enter the string to enter into the pipe: Welcome
The string retrieved from the pipe is Welcome
RESULT:
Thus the program is executed and the output is verified.
Ex.No:7
IMPLEMENTATION OF PRODUCER CONSUMER PROBLEM
DATE:
AIM:
To write a c program to implement Producer- consumer problem.
ALGORITHM:
1. Start the program.
2. Declare the variables required.
3. If the buffer is empty, Allow the producer to produce the item and add it in the queue.
4. Note the position of the buffer.
5. Display the item added to the queue.
6. If the buffer is full,Producer can't produce the item and the consumer is allowed to
consume the item.
7. Decrement the queue and display the values.
8. Stop the program
PROGRAM:
#include<stdio.h>
#define BUFFER_SIZE 5
int in,out=0;
struct semaphore
{
int b[10],c[10],d[10];
};
struct semaphore b[BUFFER_SIZE],c[10],d[10];
int main()
{
int c,q=0;
do
{
printf("\n******** PRODUCER-CONSUMER PROBLEM **********");
printf("\n 1.Producer\n 2.Consumer\n 3.Exit\n");
printf("\nEnter your choice:");
scanf("%d",&c);
switch(c)
{
case 1:
producer();
break;
case 2:
consumer();
break;
case 3:
q=1;
}
}
while(!q);
return 0;
}
producer()
{
int i;
if(((in+1)%BUFFER_SIZE)==5)
printf("\nBUFFER IS FULL\n");
else
{
for(i=1;i<BUFFER_SIZE;i++)
{
printf("\nProducer creates the product:%d\n",i);
b[in]=c[i];
printf("The position of Buffer before production:%d\n",i);
in=(in+1)%BUFFER_SIZE;
printf("The position of the Buffer after production:%d\n",in);
}
}
}
consumer()
{
int i;
if(in==out)
printf("BUFFER IS EMPTY\n");
else
{
for(i=1;i<BUFFER_SIZE;i++)
{
printf("Consumer used the product:%d\n",i);
d[i]=b[out];
printf("The position of buffer before consumption:%d\n",out);
out=(out+1)%BUFFER_SIZE;
printf("The position of the buffer after consumption:%d\n",out);
}
}
}
OUTPUT:
[root@localhost ~]# ./pc
******** PRODUCER-CONSUMER PROBLEM **********
1.Producer
2.Consumer
3.Exit
Enter your choice:1
Producer creates the product:1
The position of Buffer before production:1
The position of the Buffer after production:1
Producer creates the product:2
The position of Buffer before production:2
The position of the Buffer after production:2
Producer creates the product:3
The position of Buffer before production:3
The position of the Buffer after production:3
Producer creates the product:4
The position of Buffer before production:4
The position of the Buffer after production:4
******** PRODUCER-CONSUMER PROBLEM **********
1.Producer
2.Consumer
3.Exit
Enter your choice:2
Consumer used the product:1
The position of buffer before consumption:0
The position of the buffer after consumption:1
Consumer used the product:2
The position of buffer before consumption:1
The position of the buffer after consumption:2
Consumer used the product:3
The position of buffer before consumption:2
The position of the buffer after consumption:3
Consumer used the product:4
The position of buffer before consumption:3
The position of the buffer after consumption:4
******** PRODUCER-CONSUMER PROBLEM **********
1.Producer
2.Consumer
3.Exit
Enter your choice:3
--------------------------------------------------------------------------------
RESULT:
Thus the program was executed and the output was verified.
EX.NO:8 IMPLEMENTATION OF MEMORY MANAGEMENT SCHEME USING FIRST FIT
AIM:
To write a program to implement dynamic memory allocation first fit.
ALGORITHM:
1. Start the program.
2. Enter the number of blocks.
3. Enter the base address and size for each block.
4. Enter the size of process.
5. Compute and allocate the first block which is large enough to hold the process request.
6. Stop the program.
PROGRAM:
#include<stdio.h>
void line()
{
int i;
printf("\n");
for(i=0;i<80;i++)
printf("-");
}
void main()
{
int bsize[20],i,n,psize,baddr[20];
line();
printf("\n\t\tFIRST FIT PLACEMENT ALGORITHM\n");
line();
printf("\nEnter the number of blocks:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter base address of block no %d:",i+1);
scanf("%d",&baddr[i]);
printf("\nEnter free block size no. %d:",i+1);
scanf("%d",&bsize[i]);
}
printf("\nEnter the size of the process to be fit:");
scanf("%d",&psize);
line();
printf("\nBEFORE ALLOCATION:\n");
printf("\nBlock No. \tBase Address \tBlock Size");
for(i=0;i<n;i++)
printf("\n%d\t\t%d\t\t%d",i+1,baddr[i],bsize[i]);
line();
for(i=0;i<n;i++)
if(bsize[i]<psize)
continue;
else
{
bsize[i]-=psize;
baddr[i]+=psize;
break;
}
if(i==n)
printf("\nThe process has not been fit");
else
{
printf("\nAFTER ALLOCATION:\n");
printf("\nBlock No. \tBase Address \t Block Size");
for(i=0;i<n;i++)
printf("\n%d\t\t%d\t\t%d",i+1,baddr[i],bsize[i]);
}
line();
printf("\n");
}
OUTPUT:
[root@localhost ~]# ./first
--------------------------------------------------------------------------------
FIRST FIT PLACEMENT ALGORITHM
--------------------------------------------------------------------------------
Enter the number of blocks:4
Enter base address of block no 1:10
Enter free block size no. 1:45
Enter base address of block no 2:60
Enter free block size no. 2:75
Enter base address of block no 3:150
Enter free block size no. 3:100
Enter base address of block no 4:300
Enter free block size no. 4:140
Enter the size of the process to be fit:50
--------------------------------------------------------------------------------
BEFORE ALLOCATION:
Block No. Base Address Block Size
1 10 45
2 60 75
3 150 100
4 300 140
--------------------------------------------------------------------------------
AFTER ALLOCATION:
Block No. Base Address Block Size
1 10 45
2 110 25
3 150 100
4 300 140
--------------------------------------------------------------------------------
RESULT:
Thus the program was executed and the output was verified.
EX.NO:9 IMPLEMENTATION OF MEMORY MANAGEMENT SCHEME USING BEST FIT
AIM:
To write a program to implement dynamic memory allocation best fit.
ALGORITHM:
1. Start the program.
2. Enter the number of blocks.
3. Enter the base address and size for each block.
4. Enter the size of process.
5. Compute and allocate the smallest block which is large enough to hold the process request.
6. Stop the program.
PROGRAM:
#include<stdio.h>
void line()
{
int i;
printf("\n");
for(i=0;i<80;i++)
printf("-");
}
void main()
{
int bsize[20],i,n,j,temp,psize,baddr[20];
line();
printf("\n\t\tBEST FIT PLACEMENT ALGORITHM\n");
line();
printf("\nEnter the number of blocks:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter base address of block no %d:",i+1);
scanf("%d",&baddr[i]);
printf("\nEnter free block size no. %d:",i+1);
scanf("%d",&bsize[i]);
}
printf("\nEnter the size of the process to be fit:");
scanf("%d",&psize);
line();
printf("\nBEFORE ALLOCATION:\n");
printf("\nBlock No. \tBase Address \tBlock Size");
for(i=0;i<n;i++)
printf("\n%d\t\t%d\t\t%d",i+1,baddr[i],bsize[i]);
line();
for(i=0;i<n;i++)
for(j=0;j<n-1;j++)
if(bsize[j]>bsize[j+1])
{
temp=bsize[j];
bsize[j]=bsize[j+1];
bsize[j+1]=temp;
temp=baddr[j];
baddr[j]=baddr[j+1];
baddr[j+1]=temp;
}
for(i=0;i<n;i++)
if(bsize[i]<psize)
continue;
else
{
bsize[i]-=psize;
baddr[i]+=psize;
break;
}
if(i==n)
printf("\nThe process has not been fit");
else
{
printf("\nAFTER ALLOCATION:\n");
printf("\nBlock No. \tBase Address \t Block Size");
for(i=0;i<n;i++)
printf("\n%d\t\t%d\t\t%d",i+1,baddr[i],bsize[i]);
}
line();
printf("\n");
}
OUTPUT:
[root@localhost ~]# ./best
--------------------------------------------------------------------------------
BEST FIT PLACEMENT ALGORITHM
--------------------------------------------------------------------------------
Enter the number of blocks:4
Enter base address of block no 1:10
Enter free block size no. 1:45
Enter base address of block no 2:60
Enter free block size no. 2:75
Enter base address of block no 3:150
Enter free block size no. 3:100
Enter base address of block no 4:300
Enter free block size no. 4:140
Enter the size of the process to be fit:50
--------------------------------------------------------------------------------
BEFORE ALLOCATION:
Block No. Base Address Block Size
1 10 45
2 60 75
3 150 100
4 300 140
--------------------------------------------------------------------------------
AFTER ALLOCATION:
Block No. Base Address Block Size
1 10 45
2 110 25
3 150 100
4 300 140
--------------------------------------------------------------------------------
[root@localhost ~]#
EXNO: 10
DATE:
IMPLEMENTATION OF FILE ALLOCATION USING LINKED LIST
AIM:
To write a program to implement file allocation using linked lists.
ALGORITHM:
Step 1: Start the process
Step 2: Create a structure named ‘LIST’ with data and *next as members .Also define some
Other pointers *ptr,*temp,*start using LIST.
Step 3: Read the choice from the user.
Step 4: If choice is 1 then perform insertion operation
Step 4.1: If list is created for the first time, then perform the following
Step 4.1.1: Allocate memory using malloc and made it to be point by ptr .
Step 4.1.2: Get the data to be inserted and assign it into data field of pointer.
Step 4.1.3: Assign the address pointed by the ptr to be pointed by start (i.e) head
pointer of list.
Step 4.2.1: Else read the item from the user and also read the item after which new
Element has to be inserted.
Step 4.2.2: Then perform insertion using insert_after() function.
Step 5: If choice is 2 then perform remove operation.
Step 5.1: If the list is not created then print ‘list is empty’.
Step 5.2: Else
Read the item to be removed and remove it using remove_item() function.
Step 6: If choice is 3 then perform display operation.
Step 6.1: If the list is not created then print ‘list is empty’.
Step 6.2: Else
Display the element using display() function.
Step 7: If the choice is 4 exit from the execution.
Step 8: Stop the process.
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
typedef struct list
{
int data;
Struct list *next;
} LIST;
LIST *ptr,*temp,*start=NULL;
void insert_after(int,int);
void remove_item(int);
void display(void);
int count=0;
int main()
{
Int item,opt,dat;
System(“clear”);
Ptr=NULL;
/* printf(“%d”,sizeof(LIST));*/
Do
{
printf(“\n #############MENU ##############\n”);
printf(“1.Insert \n 2.Remove \n 3. Display \n 4.Exit \n”);
scanf(“%d”,&opt);
Switch(opt)
{
Case 1:
Printf(“Enter the data to insert:”);
Scanf(“%d”,&item);
if(count==0)
{
ptr=(LIST*) malloc (sizeof(LIST));
Ptr-> next=NULL;
Ptr->data = item;
Start = ptr;
}
Else
{
Printf(“Enter the item after which you have to insert new element :”);
Scanf(“%d”,&dat);
Insert_after(dat,item);
}
Count++;
Break;
Case 2:
If (count==0)
{
Printf(“\n List is empty \n”);
Break;
}
Printf(“Enter the item to remove:”);
Scanf(“%d”,&item);
Remove_item(item);
Count--;
Break;
Case 3:
If (count==0)
{
Printf(“\n List is empty \n “);
Break;
}
Else
{
Printf(“List elements are \n”);
Display();
}
break;
Case 4:
break;
}
} while(opt!=4);
Return 0;
}
Void insert_after(int data, int item)
{
LIST *tmp;
Temp=(LIST*) malloc(sizeof(LIST));
Temp->data=item;
Ptr=start;
While(ptr!=NULL)
{
If(ptr->data==data)
{
Tmp=ptr->next;
Ptr->next=temp;
Temp->next=tmp;
Break;
}
Ptr=ptr->next;
}
}
Void remove_item(int item)
{
Ptr= start;
If(ptr->data==item)
{
Start = ptr-> next;
Free(ptr);
}
While(ptr->next!=NULL)
{
If((ptr->next)-> data==item)
{
Temp=ptr->next;
Ptr->next=(ptr->next)->next;
Free(temp);
Break;
}
Ptr=ptr->next;
}
}
Void display ()
{
Ptr = start;
While(ptr!=NULL)
{
Printf(“%d->”,ptr->data);
Ptr=ptr->next;
}
}
OUTPUT:
[root@localhost~]#./link
############MENU###################
- Insert
- Remove
- Display
- Exit
Enter your option : 1
Enter the data to insert 12
############MENU###################
- Insert
- Remove
- Display
- Exit
Enter your option : 1
Enter the data to insert 34
Enter the item after which you have to insert new element : 12
############MENU###################
- Insert
- Remove
- Display
- Exit
Enter your option : 3
List elements are
12->34->
############MENU###################
- Insert
- Remove
- Display
- Exit
Enter your option : 1
Enter the data to insert 56
Enter the item after which you have to insert new element : 12
############MENU###################
- Insert
- Remove
- Display
- Exit
Enter your option : 3
List elements are
12->56->34->
RESULT:
Thus the given C Program is executed and output is verified.
Subscribe to:
Posts (Atom)