0% found this document useful (0 votes)
16 views19 pages

Os Lap Print 1 To 4

Operating system lab programs to workout

Uploaded by

adilshanu23
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)
16 views19 pages

Os Lap Print 1 To 4

Operating system lab programs to workout

Uploaded by

adilshanu23
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
You are on page 1/ 19

EXPERIMENT 2

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;

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

char buff[100];
DIR "dirp:

printf("enterthe directory name");


scanf("%s", buff);

if(dirp=opendir(buff)==NULL)

printf("the given directory does not exist");

exit(1 );

while(dptr=readdir(dirp))

printf("%sln",dptr->d_name);

closedir(dirp);

OUTPUT

simat@simat-Veriton-Series:~$ gcc 1.c

simat@simat-Veriton-Series:~$ .Ja.out

enter the directory name sreepathy

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():

printf("the parent process id is %d",pid 1);

}
else

pid2-getpid():
printf(" the child processid is %d",pid2);

OUTPUT

simat@simat-Veriton-Series:-$ gcc 2.c


simat@simat-Veriton-Series:~$ Ja.out

the parent process id is 4731 the child process id is 4732

2.3

PROGRAM

#include <unistd.h>

int main(void) {
char "binaryPath ="/bin/ls";
char *arg1 ="-I";

char "arg2 ="home/simat";


execl(binaryPath, binaryPath, arg1,arg2, NULL);

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

-W-f--f-- 1 simat simat 44730 Feb 2 15:45 calc.tab.c

-W-f--f-- 1 simat simat 2050 Feb 2 15:45 calc.tab.h

2.4

PROGRAM

#include<stdio.h>

#include<sys/stat.h>
int main()

{
l/pointerto stat struct
struct stat sfile;

l/stat system call


stat("sreepathy", &sfile);

laccessing st_mode (data member of stat struct)


printf("st_mode =%o", sfile.st_mode);
return 0;

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("HP: hello from parentln");


wait(NULL):

printf("CT: child has terminatedn");


}

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

The System Calls to manage the process are:

• fork ():Used to create a new process


• exec() :Execute a new program
• wait() : Wait until the process finishes execution
• exit(): Exit fromthe process

And the System Calls used to get Process ID are:

getpid():- getthe unique process id of the process

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

PROGRAM : First Fit


#include <stdio.h>

#define max 25
int i, j, k=0, nb, nf, temp =0, high =0,flag =0;
void firstfit(int b[), int f[)

for (i= 1; i <= nf; it+)

for (j = 1; j <= nb; j++)


{
temp =
bi] -f0:
if (temp >= 0)

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+)

printf("Block %d:", i):


scanf("%d", &b[I]);

printf("Enter the size of the files :-In");

for (i = 1; i<= nf; it+)

printf("File %d: ", i);

scanf("%d", &f[(]);

firstfit(b, f);

return 0;

Output: First Fit

Memory Management Scheme -First Fit

Enter the number of blocks:5


Enter the number of files :4
Enter the size of the blocks :
Block 1 :100
Block 2:500
Block3:200
Block :300 4

Block 5 :600

Enter the size of the


1: 212
files :
File

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

File Size 426 must Wait

b) Worst Fit

ALGORITHM

PROGRAM :Worst Fit

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)

for (i =1; i<= nf; it+)


{
for =
(j 1;j<= nb; j++)

=
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

printf("nFile Size %d must wiat", f[]);


b[k] = highest;
highest =0;

int main()

int b[max], f[max]:


printf("nMemory Management Scheme -Worst Fit");
printf("nEnter the number of blocks :");
scanf("%d", &nb):

printf("Enter the number of files:");


scanf("%d", &nf);
printf("nEnter the size of the blocks: \n");

for (i =1; i<= nb; it+)


printf("Block %d:", i):

scanf("%d", &b[|]):

printf("Enter the size of the files :-\n");

17/68
for (i=1; i<= nf; it+)
{
printf("File %d: ", i);

scanf("%d", &f[]):

Worstfit(b, f);

return 0;

Output:Worst Fit

Memory Management Scheme -Worst Fit


Enter the number of blocks :5
Enter the number files :4 of

Enter the size of the blocks


Block 1:100

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

File Size 212 is put in 600 partition


File Size 417 is put in 500 partition
File Size 112 is put in 388 partition
File Size 426 must wait

c) Bestfit

ALGORITHM

PROGRAM : Best Fit

#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])

for (i =1; i<< nf; it+)


{

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

printf("nFile Size %d must wiat", fÜ);

b[k] =lowest;
lowest =999;

int main()

int b[max],. f[max]:

printf("nMemory Management Scheme -Best Fit");

printf("\nEnter the number of blocks:");


scanf("%d", &nb);
printf("Enter the number of files:");

scanf("%d", &nf);
printf("\nEnter the size of the blocks: n");

for (i=1; i<= nb; it+)

printf("Block %d:", i);

scanf("%d", &b[]);

printf("Enter the size of the files :-\n");

for (i =1; ii<= nf; it+)

printf("File %d: ", i);

scanf("%d", &f(]);

bestfit(b, f);

return 0;

19/68
Output :Best Fit
Memory Management Scheme -Best Fit

Enter the number of blocks:5


Enter the number of files :4
Enter the size of the blocks

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

File Size 417 is put in 500 partition

File Size 112 is put in 200 partition


File Size 426 is put in 600 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

responsible for managing, handling, and coordinating overall activities and


sharing of computer resources. It acts as an intermediary among users of
computer and computer hardware.

What are the different types of OS?

• Batched 0S (Example: Payroll System, Transactions Process, etc.)


Multi- Programmed 0S (Example: Windows 0/S, UNIX O/S, etc.)
Time Sharing 0S (Example: Multics, etc.)
• OS (LOCUS,
Distributed etc.)

• Real-Time OS (PSOS,VRTX, etc.)

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

2. Input the processes along with their burst time (bt).

3. Find turnaround time for all processes

4.. Find waiting time (wt)for all processes.

a. wt[0] =0 (
Process 1 need not to wait)

b. Find waiting time for all other processes

wt= tat-bt

5. average waiting time = total_waiting_time /no_of_processes.


6. average turnaround time = total_turn_around_time/no_of processes.

7. Display waiting time, turnaround time of each process

8. Display average waiting time,average turnaround time


9. End

PROGRAM :FCFS
#include <stdio.h>
struct process

int pid, bt, ct, wt, tat;


} p[20]:
int main()

int n, i;

printf("\nEnter the Number of Processes: ");

scanf("%d", &n);
for (i = 0; i<n; itt)

P[]-pid = i;

printf("nEnter the Burst time for Process %d: ",i);


scanf("%d", &pi].bt);

22/68
P[0].ct = 0;

for (i =0; i<n; it+)

p[].ct = p[i - 1].ct + p[].bt;


P].tat = pli].ct:

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("\nltPID\ttBurst Timelt Turnaround Timelt Waiting Timeln");


for (i =0; i<n; it+)
printf("nit%dtt%dt\tt%d\tit%d",plil.pid, pli].bt, pli].tat, p[i],wt);

printf("n"):

float avgtat =0, avgwt =0;


for (i =0; ii<n; itt)
avgtat += p[i].tat;

avgwt += p[i].wt;

printf("Average =
Turn Around Time %.2fn", avgtat / n);

printf("Average Waiting Time =%.2fn", avgwt / n);

return 0;

OUTPUT: FCFS

Enter theNumber of Processes :4


Enter the Burst time for Process 0:21
Enter the Burst time for Process 1:3
Enter the Burst time for Process 2 :6
Enter the Burst time for Process 3:2
Process execution order:
P0->P1->P2->P3->

PID Burst Time Turnaround Time Waiting Time

23/68
2 21
1 3 24 21
2 30 24
3 2 32 30

4.2 SJF

ALGORITHM
1. Begin

2. Inputthe processes along with their burst time (bt).


3. Sort the processes acCording to burst time, lowest to highest

4. Find turnaround time for all processes

5. Find waiting time (wt)for all processes.


a. wt[0] =0(Process 1 in the sorted Iist need not to wait)

b. Find waiting time for all other processes wt= tat-bt

6. average waiting time = total_waiting time/no_of_processes.


7. average turnaround time = total_turn_around_time/no_of_processes.
8. Sort the processes according to the process id

9. Displaywaiting time, turnaround time of each process

10.Displayaverage waiting time,average turnaround time


11. End
PROGRAM:SJF
#include <stdio.h>
struct process

int pid, bt, ct, wt, tat;

}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;

printf("nEnter the burst time for Process %d:", );


scanf("%d",&p[i].bt);

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;

printf("nProcess execution order:\n");

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


printf("P%d->",p[l.pid);
for (i = 0; i<n-1; it+)

for = 0; j <n-i-1;j++)
(j
if (Pü].pid > pj + 1].pid)
s= pll:
Pi] pü = 1]: +
PÜ + 1] = s;

printf("nitPIDtBurst Timelt Turnaround Timelt Waiting Timeln");


for (i = 0; < n; it+)
i
printf("n\t%ditit%dlt\t%ditit%d", p]-pid, p[j].bt, p[j].tat, p[i].wt);

printf("n"):
return 0;

Output :SJF

Enter the Number Processes :4


of
Enter the Burst time for Process 0:21
Enter the Burst time for Process 1:3
Enter the Burst time for Process 2:6
Enter the Burst time for Process 3:2

Process execution order:


P3->P1->P2->P0->

PID Burst Time TurnaroundTime Waiting Time

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;

printf("n Enter the Priority for Process %d:", i);

scanf("%d", &p[]-priority);
printf("n Enter the burst time for Process %d:", i);

scanf("%d", &p[].bt);

for (i =0; i<n-1; it+)


for j = 0;j<n-i-1:j++)
if (Pül.priority > plj + 1],priority)

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);

for (i =0; i <n-1;itt)


for (j =0;j<n-i-1;jt+)
if (Pül-pid > pj + 1]-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");

float avgtat =0, avgwt =0;


for(i =0; i<n; it+)
avgtat += p[i].tat;

avgwt + p[i].wt;

printf("Average Turn Around Time =%.2fn", avgtat / n);

printf("Average Waiting Time =%.2n", avgwt / n);

return 0;

Output : Priority

Enter the no. of processes :4


Enterthe Priority for Process 0:3
Enter the burst time for Process 0 ::21
Enter the Priority forProcess 1:1
Enter the burst time for Process 1 :6
Enter the Priority for Process 2:2

Enter theburst time for Process 2 :3


Enter the Priorityfor Process 3:4
Enter the burst time for Process 3:2
Process execution order:

27/68
P1->P2->P0->P3->

PID Priority Burst Time Turnaround Time Waiting Time

3 21 30
1 6 6
1
2 2 3 9 6
3 4 2 32 30

4.4 Round Robin


Algorithm:

PROGRAM : Round Robin


#include <stdio.h>
#define MAX 20
int ra[20], front =0, rear =1;
struct process
{
int pid, bt, btcopy,wt, tat;
} P[20], s;

void enqueue(int pid)

if (rear> MAX - 1)
return;
else

rear =rear +1;


rq[rear] = pid;
}
int dequeue()

int ele, i;

if (rear==1)
return -1;

else

28/68
ele= rq[O];
for =0; <= rear; i++)
(i i

= + rq[i] rq[i 1]:

rear--;
return ele:

int main()

int n, j, sum =0, ts, id, ct =0;


i,
printf("\nEnter the no. of processes: ");

scanf("%d",&n):
printf("\nEnter theTime Slice:");

scanf("%d",&ts);
for (i = 0;i<n; it+)
{
Pl].pid =i;

printf("nEnter the burst time for Process %d: ", i);

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

{
enqueue();

id =dequeue();
printf("nExecution order: ");

printf(" P%d->", id);


while (id =1)
if (p[id].bt > ts)

ct =ct +ts;
p[id].tat = ct;

p[id].bt =p[id].bt - ts;

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)

printf(" P%d->", id);

for(i =0;ii<n;it+)
p[].wt =p[].tat - p[i].btcopy:

printf("\nninltPIDItltBurst Timeltlt Turnaround Timelt Waiting Timeln");

for(i = 0;i<n; it+)


printf("n\t%dtlt\t%ditt%dititit%d", pl].pid, p[].btcopy, pli].tat, pli].wt):

printf("\n");

return 0;

Output: Round Robin


Enter theno. of processes:4
Enter the Time Slice:5
Enter the burst time for Process 0: 21

Enter the burst time for Process 1:3


Enter the burst time for Process 2 :6
Enter the burst time for Process 3:2

Execution order: P0-> P1-> P2-> P3-> P0-> P2-> P0-> PO-> P0->

PID Burst Time Turnaround Time Waiting Time

0 21 32 11

1 3 8 5
2 6 21 15
3 2 15 13

30/68

You might also like