20BCS3CP
20BCS3CP
Code
Course Outcome:
Aim:
To Write a C program to implement system calls.
Procedure:
Program:
#include<stdio.h>
#include<conio.h>
#include<dir.h>
#include<dos.h>
void main()
int ch;
clrscr();
do
printf("\n\t\t\tMAIN MENU\n\t\t-------------------------\n");
printf("4.Exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
list_file();
break;
case 2:
directory();
break;
case 3:
change_dir();
break;
case 4:
exit(0);
} }while(ch<=4);
int list_file()
int l;
char e[]="*.*";
clrscr();
scanf("%d",&l);
switch(l)
{
case 1:
subfun(e);
break;
case 2:
scanf("%s",&e);
subfun(e);
break;
case 3:
scanf("%s",&e);
subfun(e);
break;
return 0;
int directory()
unsigned attrib;
int d;
char name[10],buffer[MAXPATH];
getcwd(buffer,MAXPATH);
printf("Current directory:%s\n",buffer);
if(_dos_getfileattr(name,&attrib)==0)
return 0;
else
mkdir(name);
return 0;
int change_dir()
char buffer[MAXPATH];
int d,d1;
printf("\nCurrent Directory:%s\n",getcwd(buffer,MAXPATH));
printf("\t\tChange Directory\n\t\t-----------------\n");
scanf("%d",&d);
switch(d)
{
case 1:
chdir("..");
break;
case 2:
chdir("\\");
break;
case 3:
scanf("%s",buffer);
chdir(buffer);
break;
printf("\nCurrent Directory:%s",getcwd(buffer,MAXPATH));
return 0;
int subfun(s)
char s[10];
int d,p=0,i=0;
d=findfirst(s,&ffblk,0);
while(!d)
{
printf("%s\n",ffblk.ff_name);
d=findnext(&ffblk);
i++;
p=p+1;
if(p>=22)
getch();
p=0;
printf("\nTotal File:%d",i);
return 0;
}
Output
Result:
Thus the C program to perform various system call using case is executed and output
is verified successfully.
2.C Program for File Permissions.
Aim:
To Write a C program for file permissions.
Procedure:
1.
Read the input variables and assign the value
2.
Print the values which we are going to perform
3.
Using the structure ,it performs the operation.
4.
Check the values for all the corresponding operations.
5.
Print the result and stop the execution
Program:
#include "time.h"
#include "sys\stat.h"
#include "stdio.h"
void main(){
FILE *fp;
stat("test.txt",&status);
clrscr();
getch();
struct stat {
};
Result:
Aim:
To Write a C program for file operations.
Procedure:
1.
Read the input variables and assign the value
2.
Open the existing files. if the specified file is not existing ,the corresponding
messages will be displayed
3. If the file is exist, it get input from user and the input written in the specified file
4. Finally the file will be closed
5. stop the execution
Program:
# include <stdio.h>
# include <string.h>
int main( )
FILE *fp ;
char data[50];
fp = fopen("test.c", "w") ;
if ( fp == NULL )
return 1;
{
fputs(data, fp) ;
fputs("\n", fp) ;
fclose(fp) ;
return 0;
OUTPUT:
Opening the file test.c in write mode
Result:
Aim:
To Write a C program for file copy and move.
Procedure:
1.
Read the input variables and assign the value
2.
Open the target files. if the specified file is not existing ,the corresponding messages
will be displayed
3. If the file is exist, it get input from user and the input written in the specified file
4. Finally the file will be closed
5. stop the execution
Program:
#include <stdio.h>
#include <stdlib.h>
main()
gets(source_file);
exit(EXIT_FAILURE);
}
gets(target_file);
fclose(source);
exit(EXIT_FAILURE);
fputc(ch, target);
fclose(source);
fclose(target);
return 0;
}
Result:
Aim:
To Write a C program for Dining Philosophers Problem.
Algorithm:
Step 1: Start
Eat
Step 7: Stop
Program:
#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];
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);
}
Result:
#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)
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++;
mutex=signal(mutex);
void consumer()
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
x--;
mutex=signal(mutex);
Output
1.Producer
2.Consumer
3.Exit
Enter your choice:1
Result:
Aim:
Algorithm.
Step 1: Start
Step 3: For each process in the ready Q, assign the process id and accept the CPU burst
time
Step 4: Set the waiting of the first process as 0 and its burst time as its turn around time
Waiting time for process(n)= waiting time of process (n-1) + Burst time of process(n-1)
Turn around time for Process(n)= Turn around time of Process(n-1)+ Burst time for
process(n)
Step 6: Calculate
Step 7: Stop
#include<stdio.h>
int main(){
int bt[10]={0},at[10]={0},tat[10]={0},wt[10]={0},ct[10]={0};
int n,sum=0;
float totalTAT=0,totalWT=0;
printf("Enter number of processes ");
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&at[i]);
scanf("%d",&bt[i]);
printf("\n");
for(int j=0;j<n;j++)
sum+=bt[j];
ct[j]+=sum;
tat[k]=ct[k]-at[k];
totalTAT+=tat[k];
for(int k=0;k<n;k++)
wt[k]=tat[k]-bt[k];
totalWT+=wt[k];
printf("Solution: \n\n");
for(int i=0;i<n;i++)
printf("Average WT = %f\n\n",totalWT/n);
return 0;
}
Output
Arrival time of process[3] 0
Burst time of process[3] 3
Solution:
P# AT BT CT TAT WT
P1 0 24 24 24 0
P2 0 3 27 27 24
P3 0 3 30 30 27
Result:
Aim:
Algorithm:
Step 1: Start
Step 3: For each process in the ready Q, assign the process id and accept the CPU burst
time
Step 4: Start the Ready Q according the shortest Burst time by sorting according to lowest
to highest burst time.
Step 5: Set the waiting time of the first process as „0‟ and its turnaround time as its burst
time.
Waiting time for process(n)= waiting time of process (n-1) + Burst time of process (n-1)
Turn around time for Process(n)= Turn around time of Process (n-1)+ Burst time for
process (n)
Step 6: Calculate
Step 7: Stop
Program:
#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;
scanf("%d",&n);
for(i=0;i<n;i++)
printf("p%d:",i+1);
scanf("%d",&bt[i]);
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;
for(i=1;i<n;i++)
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
total=0;
printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
total+=tat[i];
Output
Result:
Aim:
Algorithm:
Step 1: Start
Step 2: Accept the number of processes in the ready Queue and time quantum
Step 3: For each process in the ready Q, assign the process id and accept the CPU burst
time
Step 4: Calculate the no. of time slices for each process where No. of time slice for
process(n) = burst time process(n)/time slice
Step 5: If the burst time is less than the time slice then the no. of time slices =1.
Waiting time for process(n) = waiting time of process(n-1)+ burst time of process(n-1 ) +
the time difference in getting the CPU from process(n-1)
Turn around time for process (n) = waiting time of the process (n) + burst time of
process(n)+ the time difference in getting CPU from process(n).
Step 7: Calculate
Step 8: Stop
Program:
#include<stdio.h>
int main()
int count,j,n,time,remain,flag=0,time_quantum;
int wait_time=0,turnaround_time=0,at[10],bt[10],rt[10];
scanf("%d",&n);
remain=n;
for(count=0;count<n;count++)
printf("Enter Arrival Time and Burst Time for Process Process Number %d :",count+1);
scanf("%d",&at[count]);
scanf("%d",&bt[count]);
rt[count]=bt[count];
scanf("%d",&time_quantum);
for(time=0,count=0;remain!=0;)
time+=rt[count];
rt[count]=0;
flag=1;
else if(rt[count]>0)
rt[count]-=time_quantum;
time+=time_quantum;
}
remain--;
printf("P[%d]\t|\t%d\t|\t%d\n",count+1,time-at[count],time-at[count]-bt[count]);
wait_time+=time-at[count]-bt[count];
turnaround_time+=time-at[count];
flag=0;
if(count==n-1)
count=0;
else if(at[count+1]<=time)
count++;
else
count=0;
return 0;
Output
Result:
Aim:
Algorithm:
Step 1: Start
Step 3: For each process in the ready Q, assign the process id and accept the CPU
bursttime
Step 5: Set the waiting of the first process as „0‟ and its burst time as its turn around time
Step 6: For each process in the Ready Q Calculate
Waiting time for process(n)= waitingtime of process (n-1) + Burst time of process(n-1)
Turn around time for Process(n)= waiting time of Process(n)+ Burst time for process(n)
Calculate
Step 7: Stop
Program:
#include<stdio.h>
#include<conio.h>
void main()
int x,n,p[10],pp[10],pt[10],w[10],t[10],awt,atat,i;
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nProcess no %d : ",i+1);
scanf("%d %d",&pt[i],&pp[i]);
p[i]=i+1;
for(i=0;i<n-1;i++)
for(int j=i+1;j<n;j++)
if(pp[i]<pp[j])
x=pp[i];
pp[i]=pp[j];
pp[j]=x;
x=pt[i];
pt[i]=pt[j];
pt[j]=x;
x=p[i];
p[i]=p[j];
p[j]=x;
w[0]=0;
awt=0;
t[0]=pt[0];
atat=t[0];
for(i=1;i<n;i++)
w[i]=t[i-1];
awt+=w[i];
t[i]=w[i]+pt[i];
atat+=t[i];
printf("\n\n Job \t Burst Time \t Wait Time \t Turn Around Time Priority \n");
for(i=0;i<n;i++)
awt/=n;
atat/=n;
getch();
Result: