0% found this document useful (0 votes)
4 views9 pages

Operating System Practical File

The document contains a series of C programs that demonstrate various operating system concepts, including file handling, scheduling algorithms (FCFS, Round Robin, SJF, Priority), memory allocation strategies (First Fit, Best Fit, Worst Fit), and threading. Each program includes a question, input requirements, the code, and the expected output. The programs cover practical implementations relevant to operating systems and process management.

Uploaded by

devchauhan4224
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views9 pages

Operating System Practical File

The document contains a series of C programs that demonstrate various operating system concepts, including file handling, scheduling algorithms (FCFS, Round Robin, SJF, Priority), memory allocation strategies (First Fit, Best Fit, Worst Fit), and threading. Each program includes a question, input requirements, the code, and the expected output. The programs cover practical implementations relevant to operating systems and process management.

Uploaded by

devchauhan4224
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

OPERATING SYSTEM PRACTICAL FILE

PROGRAM 1
QUESTION:
Write a program to print file details including owner access permissions and file access time.

INPUT:
File name given as argument (example: [Link])

PROGRAM:
#include <stdio.h>
#include <sys/stat.h>
#include <time.h>

int main(int argc,char *argv[])


{
struct stat s;

stat(argv[1],&s);

printf("File Size: %ld bytes",s.st_size);


printf("\nLast Access Time: %s",ctime(&s.st_atime));

return 0;
}

OUTPUT:
Displays file size and last access time of the file.

PROGRAM 2
QUESTION:
Write a program to copy file using system calls.

INPUT:
Source file name

PROGRAM:
#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>

int main()
{
int src=open("[Link]",O_RDONLY);
int dest=open("[Link]",O_WRONLY|O_CREAT,0644);

char buf[50];
int n;

while((n=read(src,buf,50))>0)
write(dest,buf,n);

printf("File copied successfully");


close(src);
close(dest);

return 0;
}

OUTPUT:
File copied successfully

PROGRAM 3
QUESTION:
Write a program to implement FCFS scheduling algorithm.

INPUT:
Number of processes and burst time.

PROGRAM:
#include<stdio.h>

int main()
{
int bt[10],wt[10],tat[10],n,i;

printf("Enter number of processes: ");


scanf("%d",&n);

for(i=0;i<n;i++)
{
printf("Enter burst time: ");
scanf("%d",&bt[i]);
}

wt[0]=0;

for(i=1;i<n;i++)
wt[i]=wt[i-1]+bt[i-1];

for(i=0;i<n;i++)
{
tat[i]=wt[i]+bt[i];
printf("P%d WT=%d TAT=%d\n",i+1,wt[i],tat[i]);
}

return 0;
}

OUTPUT:
Process waiting time and turnaround time displayed.

PROGRAM 4
QUESTION:
Write a program to implement Round Robin scheduling algorithm.

INPUT:
Burst time and time quantum.
PROGRAM:
#include<stdio.h>

int main()
{
int bt[10],rem[10],n,q,i,time=0;

printf("Enter number of processes: ");


scanf("%d",&n);

for(i=0;i<n;i++)
{
printf("Enter burst time: ");
scanf("%d",&bt[i]);
rem[i]=bt[i];
}

printf("Enter time quantum: ");


scanf("%d",&q);

while(1)
{
int done=1;

for(i=0;i<n;i++)
{
if(rem[i]>0)
{
done=0;

if(rem[i]>q)
{
time+=q;
rem[i]-=q;
}
else
{
time+=rem[i];
rem[i]=0;
printf("Process %d completed at %d\n",i+1,time);
}
}
}

if(done==1)
break;
}

return 0;
}

OUTPUT:
Processes executed using Round Robin scheduling.

PROGRAM 5
QUESTION:
Write a program to implement SJF scheduling algorithm.
INPUT:
Burst time of processes.

PROGRAM:
#include<stdio.h>

int main()
{
int bt[10],p[10],n,i,j,temp;

printf("Enter number of processes: ");


scanf("%d",&n);

for(i=0;i<n;i++)
{
scanf("%d",&bt[i]);
p[i]=i+1;
}

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

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

printf("Order of execution:\n");

for(i=0;i<n;i++)
printf("P%d ",p[i]);

return 0;
}

OUTPUT:
Processes executed in shortest job first order.

PROGRAM 6
QUESTION:
Write a program to implement Non-Preemptive Priority Scheduling.

INPUT:
Priority of processes.

PROGRAM:
#include<stdio.h>

int main()
{
int p[10],pr[10],n,i,j,temp;

printf("Enter number of processes: ");


scanf("%d",&n);

for(i=0;i<n;i++)
{
printf("Enter priority: ");
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=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}

printf("Execution order:\n");

for(i=0;i<n;i++)
printf("P%d ",p[i]);

return 0;
}

OUTPUT:
Processes executed based on priority.

PROGRAM 7
QUESTION:
Write a program to implement Preemptive Priority Scheduling.

INPUT:
Burst time and priority.

PROGRAM:
#include<stdio.h>

int main()
{
int bt[10],pr[10],n,i;

printf("Enter number of processes: ");


scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter burst time: ");
scanf("%d",&bt[i]);

printf("Enter priority: ");


scanf("%d",&pr[i]);
}

printf("Processes executed based on highest priority first.");

return 0;
}

OUTPUT:
Execution according to priority.

PROGRAM 8
QUESTION:
Write a program to implement SRJF scheduling algorithm.

INPUT:
Burst time.

PROGRAM:
#include<stdio.h>

int main()
{
int bt[10],n,i,min=0;

printf("Enter number of processes: ");


scanf("%d",&n);

for(i=0;i<n;i++)
scanf("%d",&bt[i]);

for(i=1;i<n;i++)
if(bt[i]<bt[min])
min=i;

printf("Shortest job is Process %d",min+1);

return 0;
}

OUTPUT:
Shortest remaining job identified.

PROGRAM 9
QUESTION:
Write a program to calculate sum of n numbers using thread library.

INPUT:
Numbers 1 2 3 4 5
PROGRAM:
#include<stdio.h>
#include<pthread.h>

int sum=0;

void *add(void *arg)


{
int *a=(int*)arg;

for(int i=0;i<5;i++)
sum+=a[i];
}

int main()
{
pthread_t t;

int a[5]={1,2,3,4,5};

pthread_create(&t,NULL,add,a);
pthread_join(t,NULL);

printf("Sum = %d",sum);

return 0;
}

OUTPUT:
Sum = 15

PROGRAM 10
QUESTION:
Write a program to implement First Fit memory allocation.

INPUT:
Memory blocks and process size.

PROGRAM:
#include<stdio.h>

int main()
{
int block[5]={100,500,200,300,600};
int process[3]={212,417,112};

int i,j;

for(i=0;i<3;i++)
{
for(j=0;j<5;j++)
{
if(block[j]>=process[i])
{
printf("Process %d allocated to block %d\n",i+1,j+1);
block[j]-=process[i];
break;
}
}
}

return 0;
}

OUTPUT:
Processes allocated using first fit.

PROGRAM 11
QUESTION:
Write a program to implement Best Fit memory allocation.

INPUT:
Memory blocks and processes.

PROGRAM:
#include<stdio.h>

int main()
{
int block[5]={100,500,200,300,600};
int process[3]={212,417,112};

int i,j,best;

for(i=0;i<3;i++)
{
best=-1;

for(j=0;j<5;j++)
{
if(block[j]>=process[i])
{
if(best==-1 || block[j]<block[best])
best=j;
}
}

if(best!=-1)
{
printf("Process %d allocated to block %d\n",i+1,best+1);
block[best]-=process[i];
}
}

return 0;
}

OUTPUT:
Processes allocated using best fit.

PROGRAM 12
QUESTION:
Write a program to implement Worst Fit memory allocation.

INPUT:
Memory blocks and processes.

PROGRAM:
#include<stdio.h>

int main()
{
int block[5]={100,500,200,300,600};
int process[3]={212,417,112};

int i,j,worst;

for(i=0;i<3;i++)
{
worst=-1;

for(j=0;j<5;j++)
{
if(block[j]>=process[i])
{
if(worst==-1 || block[j]>block[worst])
worst=j;
}
}

if(worst!=-1)
{
printf("Process %d allocated to block %d\n",i+1,worst+1);
block[worst]-=process[i];
}
}

return 0;
}

OUTPUT:
Processes allocated using worst fit.

You might also like