0% found this document useful (0 votes)
25 views25 pages

Osprac 43

Uploaded by

NANCY
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)
25 views25 pages

Osprac 43

Uploaded by

NANCY
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/ 25

DATA STRUCTURES

PRACTICAL ASSINGMENT
NAME :- Nancy Kaim
ROLL NO. :- 22/CS/43
SEM :- IIIrd
SUBMITTING TO :- Shikha Maam 
1. Execute various Linux commands for:

i. Information Maintenance: wc, clear, cal, who, date, pwd

ii. File Management: cat, cp, rm, mv, cmp, comm, diff, find, grep, awk
iii. Directory Management : cd, mkdir, rmdir, ls
2. Execute various Linux commands for:

i. Process Control: fork, getpid, ps, kill, sleep


ii. Communication: Input-output redirection, Pipe
iii. Protection Management: chmod, chown, chgrp

3. Write a programme (using fork() and/or exec() commands) where parent and child execute:

i. same program, same code.

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
pid_t pid , p;
p = fork();
pid = getpid();
if(p<0){
fprintf(stderr ,"FORK FAILED");
return 1;
}
printf("Process id of child process : %d \n" ,p);
printf("Process id of parent process : %d \n" ,pid);
return 0;
}

Output:
ii. same program, different code.

#include<stdio.h>
#include<stdlib.h>
int main(){
system("clear");
printf("First version \nCPU model \n");
system("cat /proc/cpuinfo |awk 'NR==5{print}' ");
printf("Kernel version \n");
system("cat /proc/version");
printf("\nAmount of time last booted \n");
system("cat /proc/uptime");
printf("Amount of memory configured in the system.\n");
system("cat /proc/meminfo |awk 'NR==4{print}'");
printf("Amount of memory currently available.\n");
system("cat /proc/meminfo |awk 'NR==2{print}'");
return 0;
}

OUTPUT

iii. Before terminating, the parent waits for the child to finish its task.

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
int main()
{
int pid;
pid = fork();
if (pid < 0)
{
printf("\nERROR");
exit(1);
}
else if (pid == 0)
{
printf("\nHELLO I AM CHILD PROCESS");
printf("\nMY pid is %d", getpid());
exit(0);
}
else if (pid > 0)
{
wait(NULL);
printf("\nHELLO I AM PARENT PROCESS");
printf("\nMy pid is %d \n", getpid());
exit(1);
}
}

OUTPUT:
NK@LAPTOP-L3CF71I ~
$ gcc ans1_c.c –o res

NK@LAPTOP-L3CF71I ~
$ ./res

HELLO I AM CHILD PROCESS


MY pid is 1203
HELLO I AM PARENT PROCESS
My pid is 2451

4. Write a program to to report behaviour of Linux kernel including kernel version, CPU type and
model. (CPU information)

AND

5. Write a program to report behaviour of Linux kernel including information on 19 configured


memory, amount of free and used memory. (Memory information)

#include <stdio.h>
#include <stdlib.h>
int main()
{
system("clear");
printf("First version \nCPU model \n");
system("cat /proc/cpuinfo |awk 'NR==5{print}' ");
printf("Kernel version \n");
system("cat /proc/version");
printf("\nAmount of time last booted \n");
system("cat /proc/uptime");
printf("Amount of memory configured in the system.\n");
system("cat /proc/meminfo |awk 'NR==4{print}'");
printf("Amount of memory currently available.\n");
system("cat /proc/meminfo |awk 'NR==2{print}'");
return 0;
}

OUPUT

6. Write a program to copy files using system calls.

#include <stdio.h>
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
using namespace std;
void copy(int old, int new1);
int main(int argc, char *(argv[]))
{
int fdold, fdnew;
if (argc != 3)
{
printf("Needs two arguments\n");
exit(0);
}
fdold = open(argv[1], 0);
if (fdold == -1)
{
printf("Unable to open file\n");
exit(0);
}
fdnew = creat(argv[2], 6666);
if (fdnew == -1)
{
printf("Unable to create file\n");
exit(0);
}
copy(fdold, fdnew);
return 0;
}
void copy(int old, int new1)
{
int count;
char buffer[512];
while ((count = read(old, buffer, sizeof(buffer))) > 0)
{
write(new1, buffer, count);
}
}

OUTPUT
NK@LAPTOP-L3CF71I ~
$ touch file1.txtx file2.txt

NK@LAPTOP-L3CF71I ~
$ cat>file1.txt
Namastae Duniya!!

NK@LAPTOP-L3CF71I ~
$ g++ ans5.cpp –o res

NK@LAPTOP-L3CF71I ~
$ ./res file1.txt

NK@LAPTOP-L3CF71I ~
$ ./res file2.txt

NK@LAPTOP-L3CF71I ~
$ cat file2.txt
Namastae Duniya!!
7. Use an operating system simulator to simulate operating system tasks.
8. Write a program to implement scheduling algorithms FCFS/ SJF/ SRTF/ non preemptive
scheduling algorithms.

FIRST-COME-FIRST-SERVE ALGORITHM:

#include <iostream>
using namespace std;
int main()
{
int n, bt[20], wt[20], tat[20], avwt = 0, avtat = 0, i, j;
cout << "Enter total number of processes(maximun 20): ";
cin >> n;
cout << "\nEnter Process Burst Time: " << endl;
for (i = 0; i < n; i++)
{
cout << "P[" << i + 1 << "]: ";
cin >> bt[i];
}
wt[0] = 0;
for (i = 1; i < n; i++)
{
wt[i] = 0;
for (j = 0; j < i; j++)
{
wt[i] += bt[j];
}
}
cout << "\nProcess Burst Time Waiting Time Turnaround Time";
for (i = 0; i < n; i++)
{
tat[i] = bt[i] + wt[i];
avwt += wt[i];
avtat += tat[i];
cout << endl
<< "P[" << i + 1 << "]"
<< " " << bt[i] << " " << wt[i] << " " << tat[i];
}
avwt /= i;
avtat /= i;
cout << endl;
cout << "\nAverage Waiting Time : " << avwt;
cout << endl;
cout << "Average Turnaround Time : " << avtat;
cout << endl;
return 0;
}

OUPUT:

SHORTEST-JOB-FIRST ALGORITHM

#include <iostream>
using namespace std;
int main()
{
int burst_time[20], process[20], waiting_time[20], tat[2], i, j, n, total
= 0, pos, temp;
float wait_avg, TAT_avg;
cout << "Enter number of process (maximum 20) : ";
cin >> n;
cout << "\nEnter Burst time : \n";
for (i = 0; i < n; i++)
{
cout << "Process[" << i + 1 << "] : ";
cin >> burst_time[i];
process[i] = i + 1;
}
for (i = 0; i < n; i++)
{
pos = i;
for (j = i + 1; j < n; j++)
{
if (burst_time[j] < burst_time[pos])
{
pos = j;
}
}
temp = burst_time[i];
burst_time[i] = burst_time[pos];
burst_time[pos] = temp;
temp = process[i];
process[i] = process[pos];
process[pos] = temp;
}
waiting_time[0] = 0;
for (i = 1; i < n; i++)
{
waiting_time[i] = 0;
for (j = 0; j < i; j++)
{
waiting_time[i] += burst_time[j];
}
total += waiting_time[i];
}
wait_avg = (float)total / n;
total = 0;
cout << "\nProcess\t Burst Time \t Waiting Time \tTurnaround Time ";
for (i = 0; i < n; i++)
{
tat[i] = burst_time[i] + waiting_time[i];
total += tat[i];
cout << "\n"
<< process[i] << "\t\t" << burst_time[i] << "\t\t" <<
waiting_time[i] << "\t\t" << tat[i];
}
TAT_avg = (float)total / n;
cout << "\n\nAverage Waiting Time : " << wait_avg;
cout << "\nAverage Turnaround Time : " << TAT_avg;
return 0;
}

OUPUT:
SRTF ALGORITHM
OUPUT:
NON-PREEMPTIVE PRIORITY BASED ALGORITHM
9. Write a program to calculate the sum of n numbers using Pthreads. A list of n numbers is
divided into two smaller lists of equal size, and two separate threads are used to sum the sublists.
10. Write a program to implement first-fit, best-fit and worst-fit allocation strategies.

FIRST-FIT:

#include <stdio.h>
#define max 25
void main()
{
int frag[max], b[max], f[max], i, j, nb, nf, temp;
static int bf[max], ff[max];
printf("\n\tMemory Management Scheme - First 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; i++)
{
printf("Block %d:", i);
scanf("%d", &b[i]);
}
printf("Enter the size of the files :-\n");
for (i = 1; i <= nf; i++)
{
printf("File %d:", i);
scanf("%d", &f[i]);
}
for (i = 1; i <= nf; i++)
{
for (j = 1; j <= nb; j++)
{
if (bf[j] != 1)
{
temp = b[j] - f[i];
if (temp >= 0)
{
ff[i] = j;
break;
}
}
}
frag[i] = temp;
bf[ff[i]] = 1;
}
printf("\nFile No\tFile Size \tBlock No.\tBlock Size\tFragment");
for (i = 1; i <= nf; i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", i, f[i], ff[i], b[ff[i]],
frag[i]);
}

OUTPUT:
BEST-FIT:

#include <stdio.h>
#define max 25
void main()
{
int frag[max], b[max], f[max], i, j, nb, nf, temp, lowest = 10000;
static int bf[max], ff[max];
printf("\n\tMemory 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; i++)
{
printf("Block %d:", i);
scanf("%d", &b[i]);
}
printf("Enter the size of the files :-\n");
for (i = 1; i <= nf; i++)
{
printf("File %d:", i);
scanf("%d", &f[i]);
}
for (i = 1; i <= nf; i++)
{
for (j = 1; j <= nb; j++)
{
if (bf[j] != 1)
{
temp = b[j] - f[i];
if (temp >= 0)
{
if (lowest > temp)
{
ff[i] = j;
lowest = temp;
}
}
}
}
frag[i] = lowest;
bf[ff[i]] = 1;
lowest = 10000;
}
printf("\nFile No \tFile Size \tBlock No.\tBlock Size\tFragment");
for (i = 1; i <= nf && ff[i] != 0; i++)
printf("\n %d\t\t %d\t\t %d\t\t %d\t\t %d", i, f[i], ff[i], b[ff[i]],
frag[i]);
}

OUTPUT:

WORST-FIT

#include <stdio.h>
#define max 25
int main()
{
int frag[max], b[max], f[max], i, j, nb, nf, temp = 0, highest = 0;
static int bf[max], ff[max];
printf("\n\tMemory 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; i++)
{
printf("Block %d:", i);
scanf("%d", &b[i]);
}
printf("\nEnter the size of the files :-\n");
for (i = 1; i <= nf; i++)
{
printf("File %d:", i);
scanf("%d", &f[i]);
}
for (i = 1; i <= nf; i++)
{
for (j = 1; j <= nb; j++)
{
if (bf[j] != 1)
{
temp = b[j] - f[i];
if (temp >= 0)
{
if (highest < temp)
{
ff[i] = j;
highest = temp;
}
}
}
}
frag[i] = highest;
bf[ff[i]] = 1;
highest = 0;
}
printf("\nFile No\tFile Size\tBlock No.\tBlock Size\tFragement");
for (i = 1; i <= nf; i++)
{
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", i, f[i], ff[i], b[ff[i]],
frag[i]);
}
return 0;
}

OUPUT:

You might also like