Os Lap Print 1 To 4
Os Lap Print 1 To 4
System calls
System calls of Linux operating system:* fork, exec, getpid, exit, wait, close, stat, opendir,
readdir
2.1
PROGRAM
#include<stdio.h>
#include<dirent.h>
#include<stdlib.h>
struct dirent "dptr;
char buff[100];
DIR "dirp:
if(dirp=opendir(buff)==NULL)
exit(1 );
while(dptr=readdir(dirp))
printf("%sln",dptr->d_name);
closedir(dirp);
OUTPUT
simat@simat-Veriton-Series:~$ .Ja.out
1.c
simat@simat-Veriton-Series:-$ la.out
enter the directory name welcome
10/68
the given directory does not exist
2.2
PROGRAM
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
void main()
{
int pid,pid1,pid2;
pid=fork();
if(pid==-1 )
printf("error in process creation");
exit(1 );
}
if(pid!=0)
pid1=getpid():
}
else
pid2-getpid():
printf(" the child processid is %d",pid2);
OUTPUT
2.3
PROGRAM
#include <unistd.h>
int main(void) {
char "binaryPath ="/bin/ls";
char *arg1 ="-I";
return 0;
OUTPUT
simat@simat-Veriton-Series:~$ Ja.out
total 11484
drwxr-xr-x 3 simat simat 4096 Dec 16 12:05006
-W-rw-f-- 1 simat simat 360 Feb 13 10:09 1.c
2 simat simat
drwxr-xr-x 4096 Dec 20 15:0721
-W-W--- 1 simat simat 307 Feb 13 10:17 2.c
-WXrwxr-x simat simat 1 8296 Feb 13 11:35a.out
-W--- 1 simat simat 173 Nov 16 14:43 arth.I
-w-f--f-- 1 simat simat 2070 Nov 16 14:53arth.tab.h
-w-f--- 1 simat simat 507 Nov 16 14:53 arth.y
-W-f--f-- simat simat
1 642 Jan 3 10:48 bc.c
-W-W-f-- simat simat
1 184 Feb 2 15:41 calc.l
2.4
PROGRAM
#include<stdio.h>
#include<sys/stat.h>
int main()
{
l/pointerto stat struct
struct stat sfile;
12/68
OUTPUT
simat@simat-Veriton-Series:~$ gcc stat.c
simat@simat-Veriton-Series:~$ Ja.out
st mode= 40775
2.5
PROGRAM
#include<stdio.h>
#include<sys/wait.h>
#include<unistd.h>
int main()
if (fork()== 0)
printf("HC: hello from childln");
else
printf("Byen");
return 0;
OUTPUT
simat@simat-Veriton-Series:~$ gcc wait.c
simat@simat-Veriton-Series:~$ /a.out
HP: hello from parent
HC: hello from child
Bye
CT: child has terminated
Bye
VIVA
Question 1: Explain Process Management System Calls in Linux
13/68
EXP 3
Memory Allocation Methods
AIM
To Implement Memory Allocation Methods a) First Fit b) Worst Fit c) Best Fit
a) First Fit
ALGORITHM
#define max 25
int i, j, k=0, nb, nf, temp =0, high =0,flag =0;
void firstfit(int b[), int f[)
k= j;
printf("nFile Size %d is put in %d partition", f[], b[K]);
b[k] = temp:
flag =1;
break;
}
if (flag == 0)
printf("nFile Size %d must Wait", f0):
flag =0;
}
int main()
intb[max, f[max]:
printf("nMemory Management Scheme -First Fit");
printf("nEnter the number of blocks:");
scanf("%d", &nb);
printf("Enter the number files:");
of
15/68
scanf("%d", &nf);
printf("\nEnter the size of theblocks:\n");
for (i = 1; i<= nb; it+)
scanf("%d", &f[(]);
firstfit(b, f);
return 0;
Block 5 :600
File 2:417
File 3: 112
File 4: 426
File Size 212 is put in 500 partition
File Size 417 is put in 600 partition
File Size 112 is put in 288 partition
b) Worst Fit
ALGORITHM
16/68
#include <stdio.h>
#define max 25
int j, k = 0, nb, nf, temp =0, highest =0, flag =0;
i,
void worstfit(int bl, int f1)
=
temp b[ü] - f(j]:
(temp >= 0)
if
if (highest<temp)
k=j;
highest =temp:
if (highest !=0)
printf("nFile Size %d is put in %d partition", f[i], b[k):
else
int main()
scanf("%d", &b[|]):
17/68
for (i=1; i<= nf; it+)
{
printf("File %d: ", i);
scanf("%d", &f[]):
Worstfit(b, f);
return 0;
Output:Worst Fit
Block 2:500
3:200
Block
Block4 :300
Block 5 :600
Enter the size ofthefiles:
File 1: 212
File 2: 417
File 3: 112
File 4: 426
c) Bestfit
ALGORITHM
#include <stdio.h>
#define max 25
int i, j, k =0, nb, nf, temp =0, lowest = 999, flag= 0;
void bestfit(int b[], int f])
18/68
for (j = 1; j<= nb; j++)
=
temp bi] - fi]:
if (temp >= 0)
{
if (lowest > temp)
k=j:
lowest =temp;
(lowest != 999)
if
printf("nFile Size %d is put in %d partition", fü), b[k]);
else
b[k] =lowest;
lowest =999;
int main()
scanf("%d", &nf);
printf("\nEnter the size of the blocks: n");
scanf("%d", &b[]);
scanf("%d", &f(]);
bestfit(b, f);
return 0;
19/68
Output :Best Fit
Memory Management Scheme -Best Fit
Block1:100
Block 2:500
Block 3 :200
Block 4:300
Block 5:600
Enter the size of the files :
File 1:212
File 2: 417
File 3: 112
File 4: 426
File Size 212 is put in 300 partition
VIVA
What do you mean by an operating system? What are its basic functions?
Operating System (0S) is basically a software program that manages and
handles all resources of a computer such as hardware and software.An OS is
RESULT
Implementation of the Memory Allocation Methods for fixed partition is done successfully
20/68
Experiment 4
CPU scheduling algorithms
AIM
To implement the CPU scheduling algorithms.a) FCFS b) SJF c) Priority d) Round Robin
4.1 FCFS
ALGORITHM
1. Begin
a. wt[0] =0 (
Process 1 need not to wait)
wt= tat-bt
PROGRAM :FCFS
#include <stdio.h>
struct process
int n, i;
scanf("%d", &n);
for (i = 0; i<n; itt)
P[]-pid = i;
22/68
P[0].ct = 0;
P[O].wt =0;
for (i =0; i <n; it+)
p[].wt =p[].tat - p[].bt;
printf("nProcess execution order:n");
for (i = 0;i<n;it+)
printf("P%d->", plil.pid);
printf("n"):
avgwt += p[i].wt;
printf("Average =
Turn Around Time %.2fn", avgtat / n);
return 0;
OUTPUT: FCFS
23/68
2 21
1 3 24 21
2 30 24
3 2 32 30
4.2 SJF
ALGORITHM
1. Begin
}p[20], s;
int main()
int n, j;
i,
printf("\n Enter the no. of processes: "):
scanf("%d",&n);
for (i =0; i <n; it+)
Plü.pid =i;
24/68
for (i =0;i<n-1; i++)
for (j = 0;j<n-i-1;jt+)
(PÜ].bt > PÜ + 1].b)
if
S= pO:
PÜ] = plü + 1];:
Plj + 1] = s;
p[-1].ct = 0;
for = 0; i<n; it+)
(i
p].ct = pi - 1].ct + p].bt;
P].tat = p[i].ct;
p[0].wt = 0;
for (i = 1;i<n;it+)
Pl[].wt = pi].tat - pli].bt;
for = 0; j <n-i-1;j++)
(j
if (Pü].pid > pj + 1].pid)
s= pll:
Pi] pü = 1]: +
PÜ + 1] = s;
printf("n"):
return 0;
Output :SJF
21 32 11
3 5 2
11 5
3 2 2
25/68
4.3 Priority
Algorithm:
PROGRAM : Priority
#include <stdio.h>
struct process
{
int pid, bt, ct, wt, tat, priority;
}P[20], s;
int main()
{
int n, j;
i,
printf("\n Enter the no. of processes: ");
scanf("%d",&n);
for (i =0; i<n; it+)
plil-pid = i;
scanf("%d", &p[]-priority);
printf("n Enter the burst time for Process %d:", i);
scanf("%d", &p[].bt);
S= plil:
P] = pü + 1]:
pli +1] =S;
p[-1].ct =0;
for (i =0; i<n; it+)
P[].ct = p[i - 1].ct + p[].bt;
Pl].tat = pl].ct;
p[0].wt =0;
for (i = 1;i<n; it+)
plij.wt = - p[i].tat p[j].bt;
printf("nProcessexecution order:n");
26/68
for(i =0; i<n; itt)
printf("P%d->", plil.pid);
s= pi];
Pli] =pü + 1];
plj + 1] =s;
printf("n\tPID\tPriority\tBurst Timelt Turnaround Timelt Waiting Timeln");
for(i =0; i<n; itt)
printf("nt%d\t%ditt%ditltt%dltltt%d",
p[il.pid, p[i].priority, p[j].bt, pi].tat, p[].wt);
printf("n");
avgwt + p[i].wt;
return 0;
Output : Priority
27/68
P1->P2->P0->P3->
3 21 30
1 6 6
1
2 2 3 9 6
3 4 2 32 30
if (rear> MAX - 1)
return;
else
int ele, i;
if (rear==1)
return -1;
else
28/68
ele= rq[O];
for =0; <= rear; i++)
(i i
rear--;
return ele:
int main()
scanf("%d",&n):
printf("\nEnter theTime Slice:");
scanf("%d",&ts);
for (i = 0;i<n; it+)
{
Pl].pid =i;
scanf("%d",&p[].bt);
p[].btcopy =p[].bt;
for (i 0; i<n; = it+)
{
enqueue();
id =dequeue();
printf("nExecution order: ");
ct =ct +ts;
p[id].tat = ct;
if (p[id].bt!= 0)
enqueue(p[id].pid);
}
else
29/68
ct =ct + p[id].bt;
p[id].tat = ct;
p[id].bt = 0;
id =dequeue():
if (id != -1)
for(i =0;ii<n;it+)
p[].wt =p[].tat - p[i].btcopy:
printf("\n");
return 0;
Execution order: P0-> P1-> P2-> P3-> P0-> P2-> P0-> PO-> P0->
0 21 32 11
1 3 8 5
2 6 21 15
3 2 15 13
30/68