0% found this document useful (0 votes)
4 views

OS MANUAL

The document contains a series of programming experiments focused on process management and scheduling in Linux. It includes various programs for operations like opening, reading, writing, and closing files, as well as implementing scheduling algorithms such as Shortest Job First, First Come First Serve, and Round Robin. Additionally, it covers concepts like process creation, synchronization, and resource allocation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

OS MANUAL

The document contains a series of programming experiments focused on process management and scheduling in Linux. It includes various programs for operations like opening, reading, writing, and closing files, as well as implementing scheduling algorithms such as Shortest Job First, First Come First Serve, and Round Robin. Additionally, it covers concepts like process creation, synchronization, and resource allocation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 39

PRACTICAL NO 1

EXPERIMENT NO 1B

PROGRAM FOR OPEN():

PROGRAM FOR READ():


PROGRAM FOR WRITE():

PROGRAM FOR CLOSE():


OUTPUT:
EXPERIMENT NO 2

LINUX SHELL SCRIPT.

PROGRAM 1

1) Display top ten processes in decending order.

echo "Top 10 processes in Descending oeder os: "


ps -ax | head -n 10;

OUTPUT:

2) Display processes with highest memory usage.

echo "Display Prcesses with highest memory usage: "


ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head;

OUTPUT:
3) Display current logged user and login name.

echo "Logged in Users are: "


who -u;
echo "Number of Logged in Users are: "
who -u | wc -l ;
echo "Logged in Users are: "
w;

OUTPUT:

4) Display current shell,home,directory,os type,current path setting and current working


directory.

echo "Current home directory is:";


whoami
echo "Current operating system type
is:"; uname
echo"Current current working directory is:";
pwd

OUTPUT:
5) Display os name,release number,kernel version.

echo "OS Name is: "


uname;
echo "Release Number is:
" uname -a;
echo "Release Number of Kernel is: "
uname -r;

OUTPUT:
EXPERIMENT NO.3

SHORTEST JOB FIRST:


PROGRAM:-
#include <stdio.h>
int main()
{
int A[100][4];
int i, j, n, total = 0, index, temp;
float avg_wt, avg_tat;
printf("Enter number of process: ");
scanf("%d", &n);
printf("Enter Burst Time:\n");
for (i = 0; i < n; i++) {
printf("P%d: ", i + 1);
scanf("%d", &A[i][1]);
A[i][0] = i + 1;
}
for (i = 0; i < n; i++) {
index = i;
for (j = i + 1; j < n; j++)
if (A[j][1] < A[index][1])
index = j;
temp = A[i][1];
A[i][1] = A[index][1];
A[index][1] = temp;
temp = A[i][0];
A[i][0] = A[index][0];
A[index][0] = temp;
}
A[0][2] = 0;
for (i = 1; i < n; i++) {
A[i][2] = 0;
for (j = 0; j < i; j++)
A[i][2] += A[j][1];
total += A[i][2];
}
avg_wt = (float)total / n;
total = 0;
printf("P BT WT TAT\n");
for (i = 0; i < n; i++) {
A[i][3] = A[i][1] + A[i][2];
total += A[i][3];
printf("P%d %d %d %d\n", A[i][0],
A[i][1], A[i][2], A[i][3]);
}
avg_tat = (float)total / n;
printf("Average Waiting Time= %f", avg_wt);
printf("\nAverage Turnaround Time= %f\n", avg_tat)
}
OUTPUT:-
FIRST COME FIRST SERVE:
PROGRAM:-
#include<stdio.h>
int main()
{
int n,bt[30],wait_t[30],turn_ar_t[30],av_wt_t=0,avturn_ar_t=0,i,j;
printf("Please enter the total number of processes(maximum 30):"); // the maximum
process that be used to calculate is specified.
scanf("%d",&n);
printf("\nEnter The Process Burst Timen");
for(i=0;i<n;i++)
{
printf("P[%d]:",i+1);
scanf("%d",&bt[i]);
}
wait_t[0]=0;
for(i=1;i<n;i++)
{
wait_t[i]=0;
for(j=0;j<i;j++)
wait_t[i]+=bt[j];
}
printf("\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{
turn_ar_t[i]=bt[i]+wait_t[i];
av_wt_t+=wait_t[i];
avturn_ar_t+=turn_ar_t[i];
printf("\nP[%d]\t\t%d\t\t\t%d\t\t\t\t%d",i+1,bt[i],wait_t[i],turn_ar_t[i]);
}
av_wt_t/=i;
avturn_ar_t/=i;
printf("\nAverage Waiting Time:%d",av_wt_t);
printf("\nAverage Turnaround Time:%d",avturn_ar_t);
return 0;
}
OUTPUT:-

PRIORITY:
PROGRAM:-
#include <stdio.h>
int main() {
int pn = 0;
int CPU = 0;
int allTime = 0;
printf("Enter Processes Count: ");
scanf("%d", &pn);
int AT[pn];
int ATt[pn];
int NoP = pn;
int PT[pn];
int PP[pn];
int waitingTime[pn];
int turnaroundTime[pn];
for (int i = 0; i < pn; i++) {
printf("\nProcessing time for P%d: ", i + 1);
scanf("%d", &PT[i]);
printf("Priority for P%d: ", i + 1);
scanf("%d", &PP[i]);
printf("Arrival Time for P%d: ", i + 1);
scanf("%d", &AT[i]);
ATt[i] = AT[i];
}
int LAT = 0;
for (int i = 0; i < pn; i++) {
if (AT[i] > LAT) {
LAT = AT[i];
}
}
int ATv = AT[0];
int ATi = 0;
int P1 = PP[0];
int P2 = PP[0];
while (NoP > 0 && CPU <= 1000) {
for (int i = 0; i < pn; i++) {
if (ATt[i] < ATv) {
ATi = i;
ATv = ATt[i];
P1 = PP[i];
P2 = PP[i];
}
else if (ATt[i] == ATv || ATt[i] <= CPU) {
if (PP[i] != (pn + 1)) {
P2 = PP[i];
if (P2 < P1) {
ATi = i;
ATv = ATt[i];
P1 = PP[i];
P2 = PP[i];
}
}
}
}
if (CPU < ATv) {
CPU = CPU + 1;
continue;
} else {
waitingTime[ATi] = CPU - ATt[ATi];
CPU = CPU + PT[ATi];
turnaroundTime[ATi] = CPU - ATt[ATi];
ATt[ATi] = LAT + 10;
ATv = LAT + 10;
ATi = 0;
PP[ATi] = pn + 1;
P1 = PP[0];
P2 = PP[0];
NoP = NoP - 1;
}
}
printf("\nPN\tPT\tPP\tWT\tTT\n\n");
for (int i = 0; i < pn; i++) {
printf("P%d\t%d\t%d\t%d\t%d\n", i + 1, PT[i], PP[i], waitingTime[i], turnaroundTime[i]);
}
int AvgWT = 0;
int AVGTaT = 0;
for (int i = 0; i < pn; i++) {
AvgWT = waitingTime[i] + AvgWT;
AVGTaT = turnaroundTime[i] + AVGTaT;
}
printf("AvgWaitingTime: %d\nAvgTurnaroundTime: %d\n", AvgWT / pn, AVGTaT / pn);
return 0;
}
OUTPUT:-
EXPERIMENT NO 4.

PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main(void) {
pid_t pid = fork();
if(pid == 0) {
printf("Child => PPID: %d PID: %d\n", getppid(), getpid());
exit(EXIT_SUCCESS);
}
else if(pid > 0) {
printf("Parent => PID: %d\n", getpid());
printf("Waiting for child process to finish.\n");
wait(NULL);
printf("Child process finished.\n");
}
else {
printf("Unable to create child process.\n");
}
return EXIT_SUCCESS;
}
OUTPUT:

EXPERIMENT NO 5

PROGRAM
1)PRIORITY:
#include <stdio.h>
//Function to swap two variables
void swap(int *a,int *b)
{
int temp=*a;
*a=*b;
*b=temp;
}
int main()
{
int n;
printf("Enter Number of Processes: ");
scanf("%d",&n);
// b is array for burst time, p for priority and index for process id
int b[n],p[n],index[n];
for(int i=0;i<n;i++)
{
printf("Enter Burst Time and Priority Value for Process %d: ",i+1);
scanf("%d %d",&b[i],&p[i]);
index[i]=i+1;
}
for(int i=0;i<n;i++)
{
int a=p[i],m=i;
//Finding out highest priority element and placing it at its desired position
for(int j=i;j<n;j++)
{
if(p[j] > a)
{
a=p[j];
m=j;
}
}
//Swapping processes
swap(&p[i], &p[m]);
swap(&b[i], &b[m]);
swap(&index[i],&index[m]);
}
// T stores the starting time of process
int t=0;
//Printing scheduled process
printf("Order of process Execution is\n");
for(int i=0;i<n;i++)
{
printf("P%d is executed from %d to %d\n",index[i],t,t+b[i]);
t+=b[i];
}
printf("\n");
printf("Process Id Burst Time Wait Time TurnAround Time\n");
int wait_time=0;
for(int i=0;i<n;i++)
{
printf("P%d %d %d %d\n",index[i],b[i],wait_time,wait_time + b[i]);
wait_time += b[i];
}
return 0;
}
OUTPUT:

2)ROUND ROBBIN:
#include <stdio.h>
int main() {
// Input number of processes
int n;
printf("Enter Total Number of Processes: ");
scanf("%d", &n);
int wait_time = 0, ta_time = 0, arr_time[n], burst_time[n], temp_burst_time[n];
int x = n;
// Input details of processes
for (int i = 0; i < n; i++) {
printf("Enter Details of Process %d \n", i + 1);
printf("Arrival Time: ");
scanf("%d", &arr_time[i]);
printf("Burst Time: ");
scanf("%d", &burst_time[i]);
temp_burst_time[i] = burst_time[i];
}
// Input time slot
int time_slot;
printf("Enter Time Slot: ");
scanf("%d", &time_slot);
// Total indicates total time
// counter indicates which process is executed
int total = 0, i = 0;
printf("Process ID Burst Time Turnaround Time Waiting Time\n");
// Round Robin Scheduling
for (total = 0; x != 0;) {
if (temp_burst_time[i] > 0) {
// Process execution in the given time slot
if (temp_burst_time[i] <= time_slot) {
total += temp_burst_time[i]; // Add remaining burst time to total
temp_burst_time[i] = 0; // Mark process as completed
x--; // Decrement remaining processes
printf("\nProcess No %d \t\t %d\t\t\t\t %d\t\t\t %d", i + 1, burst_time[i],
total - arr_time[i], total - arr_time[i] - burst_time[i]);
wait_time += total - arr_time[i] - burst_time[i]; // Waiting time calculation
ta_time += total - arr_time[i]; // Turnaround time calculation
} else {
total += time_slot; // Process is executed for time_slot
temp_burst_time[i] -= time_slot;
}
}
// Move to next process, if needed
if (i == n - 1) {
i = 0;
} else if (arr_time[i + 1] <= total) {
i++;
} else {
i = 0; }
}
// Calculate averages
float average_wait_time = (float)wait_time / n;
float average_turnaround_time = (float)ta_time / n;
printf("\nAverage Waiting Time: %f", average_wait_time);
printf("\nAvg Turnaround Time: %f", average_turnaround_time);
return 0;
}

OUTPUT:

3)SJF
#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,totalT=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);
printf("\nEnter Burst Time:\n");
for(i=0;i<n;i++)
{
printf("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1;
}
//sorting of burst times
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;
}
wt[0]=0;
//finding the waiting time of all the processes
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
//individual WT by adding BT of all previous completed processes
wt[i]+=bt[j];
//total waiting time
total+=wt[i];
}
//average waiting time
avg_wt=(float)total/n;
printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{
//turnaround time of individual processes tat[i]=bt[i]+wt[i];

//total turnaround time totalT+=tat[i];


printf("\np%d\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}
//average turnaround time avg_tat=(float)totalT/n;
printf("\n\nAverage Waiting Time=%f",avg_wt); printf("\nAverage Turnaround Time=
%f",avg_tat);
}
OUTPUT:

EXPERIMENT NO 6
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
int mutex = 1;
int full = 0;
int empty = 10, data = 0;
void producer()
{
--mutex;
++full;
--empty;
data++;
printf("\nProducer produces item number: %d\n", data);
++mutex;
}
void consumer()
{
--mutex;
--full;
++empty;
printf("\nConsumer consumes item number: %d.\n", data);
data--;
++mutex;
}
int main()
{
int n, i;
printf("\n1. Enter 1 for Producer"
"\n2. Enter 2 for Consumer"
"\n3. Enter 3 to Exit");
for (i = 1; i > 0; i++)
{
printf("\nEnter your choice: ");
scanf("%d", &n);
switch (n)
{
case 1:
if ((mutex == 1) && (empty != 0))
{
producer();
}
else
{
printf("The Buffer is full. New data cannot be produced!");
}
break;
case 2:
if ((mutex == 1) && (full != 0))
{
consumer();
}
else
{
printf("The Buffer is empty! New data cannot be consumed!");
}
break;

case 3:
exit(0);
break;
}
}
}

OUTPUT:

EXPERIMENT NO 7

PROGRAM:
#include <stdio.h>
int main() {
/* array will store at most 5 processes with 3 resources
if your process or resources are greater than 5 and 3, then increase the size of the array
*/
int p, c, count = 0, i, j, alc[5][3], max[5][3], need[5][3], safe[5], available[3], done[5],
terminate = 0;
printf("Enter the number of processes and resources: ");
scanf("%d %d", &p, &c);
// Validate input size
if (p > 5 || c > 3) {
printf("Error: Maximum processes = 5, Maximum resources = 3. Adjust array size
accordingly.\n");
return 1;
}
// Input allocation matrix
printf("Enter allocation of resources for all processes (%dx%d matrix):\n", p, c);
for (i = 0; i < p; i++) {
for (j = 0; j < c; j++) {
scanf("%d", &alc[i][j]);
}
}
// Input max resource requirement matrix
printf("Enter the max resources required for all processes (%dx%d matrix):\n", p, c);
for (i = 0; i < p; i++) {
for (j = 0; j < c; j++) {
scanf("%d", &max[i][j]);
}
}
// Input available resources
printf("Enter the available resources (%d values):\n", c);
for (i = 0; i < c; i++) {
scanf("%d", &available[i]);
}
// Calculate the need matrix
printf("\nNeed resources matrix:\n");
for (i = 0; i < p; i++) {
for (j = 0; j < c; j++) {
need[i][j] = max[i][j] - alc[i][j];
printf("%d\t", need[i][j]);
}
printf("\n");
}
// Initialize done array
for (i = 0; i < p; i++) {
done[i] = 0;
}
return 0;
}

OUTPUT:
EXPERIMENT NO 8

PROGRAM:

1)MFT:
#include <stdio.h>
int main() {
int ms, bs, nob, ef, n, mp[10], tif = 0;
int i, p = 0;
// Input memory size and block size
printf("Enter the total memory available (in Bytes): ");
scanf("%d", &ms);
printf("Enter the block size (in Bytes): ");
scanf("%d", &bs);

// Calculate number of blocks and external fragmentation


nob = ms / bs;
ef = ms - (nob * bs);

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


scanf("%d", &n);

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


printf("Enter memory required for process %d (in Bytes): ", i + 1);
scanf("%d", &mp[i]);
}
// Display the number of available blocks
printf("\nNo. of Blocks available in memory: %d", nob);
printf("\n\nPROCESS\tMEMORY REQUIRED\tALLOCATED\tINTERNAL FRAGMENTATION");
// Process allocation
for (i = 0; i < n && p < nob; i++) {
printf("\n%d\t%d", i + 1, mp[i]);
if (mp[i] > bs) {
printf("\tNO\t---"); // Process too large for a block
} else {
printf("\tYES\t%d", bs - mp[i]); // Allocated with internal fragmentation
tif += (bs - mp[i]);
p++;
}
}
// If there are remaining processes that couldn't be allocated
if (i < n) {
printf("\nMemory is full, remaining processes cannot be accommodated.");
}
// Print total internal and external fragmentation
printf("\n\nTotal Internal Fragmentation: %d", tif);
printf("\nTotal External Fragmentation: %d\n", ef);
return 0;
}
OUTPUT:

2)MVT:

#include <stdio.h>
int main() {
int ms, mp[10], i, temp, n = 0;
char ch = 'y';
// Input total memory available
printf("\nEnter the total memory available (in Bytes): ");
scanf("%d", &ms);
temp = ms;
for (i = 0; ch == 'y' || ch == 'Y'; i++, n++) {
printf("\nEnter memory required for process %d (in Bytes): ", i + 1);
scanf("%d", &mp[i]);

if (mp[i] <= temp) {


printf("\nMemory is allocated for Process %d", i + 1);
temp -= mp[i]; // Reduce available memory
} else {
printf("\nMemory is Full, Process %d cannot be allocated.", i + 1);
break;
}
printf("\nDo you want to continue (y/n)? ");
scanf(" %c", &ch); // Space before %c to ignore newline character
}
// Output memory allocation details
printf("\n\nTotal Memory Available: %d", ms);
printf("\n\nPROCESS\tMEMORY ALLOCATED");
for (i = 0; i < n; i++)
printf("\n%d\t%d", i + 1, mp[i]);
printf("\n\nTotal Memory Allocated: %d", ms - temp);
printf("\nTotal External Fragmentation: %d\n", temp);
return 0;
}
OUTPUT:
EXPERIMENT NO 9

PROGRAM:
FIFO
#include<stdio.h>
int main()
{
int pageFaults = 0;
int frames;
int m, n, s, pages;
printf("\n Enter frame size: ");
scanf("%d",&frames);
printf("\n Enter the no of pages: ");
scanf("%d",&pages);
int incomingStream[pages];
printf("\n Enter the String: ");
for(m=0;m<pages;m++){
scanf("%d",&incomingStream[m]);
}
printf("Incoming \t frame1 \t frame2 \t frame3");
int temp[frames];
for(m=0;m<frames;m++){
temp[m] = -1;
}
for(m=0;m<pages;m++){
s = 0;
for(n=0;n<frames;n++){
if(incomingStream[m] == temp[n]){
s++;
pageFaults--;
}
}
pageFaults++;
if((pageFaults<=frames) && (s == 0)){
temp[m] = incomingStream[m];
}
else if(s == 0){
temp[(pageFaults-1)%frames] = incomingStream[m];
}
printf("\n");
printf("%d\t\t\t",incomingStream[m]);
for(n=0;n<frames;n++){
if(temp[n]!=-1){
printf(" %d\t\t\t",temp[n]);
}
else{
printf(" - \t\t\t");
}
}
}
printf("\n Total page Faults: \t%d\n",pageFaults);
printf("\n Total page Hits: \t%d\n",pages-pageFaults);
return 0;
}
OUTPUT:
LRU
#include<stdio.h>
#include<limits.h>
int checkHit(int incomingPage, int queue[], int occupied) {
for (int i = 0; i < occupied; i++) {
if (incomingPage == queue[i])
return 1;
}
return 0;
}
void printFrame(int queue[], int occupied) {
for (int i = 0; i < occupied; i++)
printf("%d\t\t\t", queue[i]);
}
int main() {
int n, frames, pagefault = 0, pageHits = 0;
printf("Enter number of frames: ");
scanf("%d", &frames);
printf("Enter the number of pages in the reference string: ");
scanf("%d", &n);
int incomingStream[n];
int queue[frames];
int distance[n];
int occupied = 0;
printf("Enter the page reference string (space-separated): ");
for (int i = 0; i < n; i++) {
scanf("%d", &incomingStream[i]);
}
printf("page\t Frame1 \t Frame 2\t Frame3\n");
for (int i = 0; i < n; i++) {
printf("%d: \t\t", incomingStream[i]);
if (checkHit(incomingStream[i], queue, occupied)) {
pageHits++;
printFrame(queue, occupied);
}
else if (occupied < frames) {
queue[occupied] = incomingStream[i];
pagefault++;
occupied++;
printFrame(queue, occupied);
}
else {
int max = INT_MIN;
int index;
for (int j = 0; j < frames; j++) {
distance[j] = 0;
for (int k = i - 1; k >= 0; k--) {
++distance[j];
if (queue[j] == incomingStream[k])
break;
}
if (distance[j] > max) {
max = distance[j];
index = j;
}
}
queue[index] = incomingStream[i];
printFrame(queue, occupied);
pagefault++;
}
printf("\n");
}

printf("Page Faults: %d\n", pagefault);


printf("Page Hits: %d\n", pageHits);
return 0;
}
OUTPUT:

You might also like