OS Code sppu
OS Code sppu
#!/bin/sh
create()
{
touch addresbook.txt
insert()
{
echo " enter a name "
read name
echo " enter a address "
read address
echo " enter phone no "
read phoneno
echo " enter email id "
read email
veiw()
{
echo " show the contents of the address book "
echo " name address phoneno email "
cat addresbook.txt
}
modify()
{
echo " enter ur name which u want to modify "
read name
grep -v " $name " addresbook.txt > addresbook1.txt
cp addresbook1.txt addresbook.txt
rm addresbook1.txt
echo " enter a name "
read name
echo " enter a address "
read address
echo " enter phone no "
read phoneno
echo " enter email id "
read email
echo " $name $address $phoneno $email ">> addresbook.txt
}
delete()
{
echo " enter ur name which u want to delete "
read name
grep -v " $name " addresbook.txt > addresbook1.txt
cp addresbook1.txt addresbook.txt
rm addresbook1.txt
}
n=0
while [ $n -ne 6 ]
do
echo " enter ur choice "
echo " 1.create "
echo " 2.insert "
echo " 3.veiw "
echo " 4.modify "
echo " 5.delete "
echo " 6.exit"
read ch
case $ch in
"1") create;;
"2")insert;;
"3")veiw;;
"4")modify;;
"5")delete;;
"6")exit;;
esac
done
Assignment No.3
A. Fork – Bubble Sorting
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
pid_t pid;
pid=fork();
if(pid==0)
{
printf("Hello,I am the Child process\n");
bubble_sort(array, n);
printf("Sorted list in ascending order:\n");
for ( c = 0 ; c < n ; c++ )
printf("%d\n", array[c]);
}
else
{
printf("Hello,I am the Parent process\n");
bubble_sort_2(array, n);
printf("Sorted list in decending order:\n");
for ( c = 0 ; c < n ; c++ )
printf("%d\n", array[c]);
}
}
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
int main(int argc, char *argv[])
{
return 0;
}
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
int mid,c=0;
if(l<=u)
{
mid=(l+u)/2;
if(m==a[mid])
{
c=1;
}
else
if(m<a[mid])
{
return binary(a,n,m,l,mid-1);
}
else
return binary(a,n,m,mid+1,u);
}
else
return c;
}
int nEle=argc-1,elem,temp,pid,l,u,c,x;
char line[100];
char *args[nEle+1];
int i,j=2,a[nEle];
j=0;
for(i=1;i<=nEle;i++)
{
elem=atoi(argv[i]);
a[j]=elem;
printf("a[%d]=%d\n",j,a[j]);
j++;
}
printf("Please Enter the Element to be search using binary search\
n");
scanf("%d",&x);
l=0,u=nEle-1;
c=binary(a,nEle,x,l,u);
if(c==0)
printf("Number is not found.\n");
else
printf("Number is found.\n");
return 0;
}
Assignment No.4
A.Round Robin – Scheduling Algorithm
#include<stdio.h>
void main()
{
int i, NOP, sum=0,count=0, y, quant, wt=0, tat=0, at[10], bt[10],
temp[10];
float avg_wt, avg_tat;
printf(" Total number of process in the system: ");
scanf("%d", &NOP);
y = NOP;for(i=0; i<NOP; i++)
{
printf("\n Enter the Arrival and Burst time of the Process[%d]\n",
i+1);
printf(" Arrival time is: \t");
scanf("%d", &at[i]);
printf(" \nBurst time is: \t");
scanf("%d", &bt[i]);
temp[i] = bt[i];
}
printf("Enter the Time Quantum for the process: \t");
scanf("%d", &quant);
printf("\n Process No \t\t Burst Time \t\t TAT \t\t Waiting Time ");
for(sum=0, i = 0; y!=0; )
{
if(temp[i] <= quant && temp[i] > 0)
{
sum = sum + temp[i];
temp[i] = 0;
count=1;
}
else if(temp[i] > 0)
{
temp[i] = temp[i] - quant;
sum = sum + quant;
}
if(temp[i]==0 && count==1)
{
y--;
printf("\nP%d \t\t %d\t\t\t\t %d\t\t\t %d", i+1, bt[i], sum-at[i],
sum-at[i]-bt[i]); wt
= wt+sum-at[i]-bt[i];
tat = tat+sum-at[i];
count =0;
}
if(i==NOP-1)
{
i=0;
}
else if(at[i+1]<=sum)
{
i++;
}
else
{
i=0;
}
}
avg_wt = wt * 1.0/NOP;
avg_tat = tat * 1.0/NOP;
printf("\n Average Turn Around Time: \t%f", avg_tat);
printf("\n Average Waiting Time: \t%f", avg_wt);
}
B.Shortest Job First – Scheduling Algorithm
#include <stdio.h>
int main()
int A[100][4];
scanf("%d", &n);
scanf("%d", &A[i][1]);
A[i][0] = i + 1;
index = i;
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;
A[i][2] += A[j][1];
total += A[i][2];
avg_wt = (float)total / n;
total = 0;
printf("P BT WT TAT\n");
total += A[i][3];
}
Assignment No.5
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h> //for system calls
#include <sys/types.h>
#include <sys/syscall.h> //to print thread id
#include <stdlib.h>
#define SIZE 3
int main()
{
int prod, cons, i, j, k, l;
return 0;
}
return NULL;
pthread_exit(0);
}
return NULL;
pthread_exit(0);
}
Assignment No.6
#include <stdio.h>
#include <pthread.h>
#include <sys/syscall.h>
#include <unistd.h>
int getItemforBuff();
void readItemfromBuff(int buffer);
int buffer;
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t wrt = PTHREAD_MUTEX_INITIALIZER;
int flag = 0;
int read_count = 0;
int main()
{
pthread_t rd1_tid;
pthread_t wr_tid;
pthread_t rd2_tid;
pthread_join(wr_tid, NULL);
pthread_join(rd1_tid, NULL);
pthread_join(rd2_tid, NULL);
return 0;
}
if (read_count == 1)
{
pthread_mutex_lock(&wrt);
}
pthread_mutex_unlock(&mutex1);
if (flag == 1)
{
readItemfromBuff(buffer);
sleep(1);
flag = 0;
}
pthread_mutex_lock(&mutex1);
read_count--;
if (read_count == 0)
{
pthread_mutex_unlock(&wrt);
}
pthread_mutex_unlock(&mutex1);
}
}
int getItemforBuff()
{
int item;
return item;
}
#include<stdio.h>
int main()
{
int n,i,bt[10],wt[10],tat[10],awt,atat;
printf("How many Elements you want to enter:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter Burst time for P%d:",i);
scanf("%d",&bt[i]);
}
printf("\nProcess Details are:\n");
printf("Process\t Burst Time\n");
for(i=0;i<n;i++)
{
printf(" P%d\t %d\n",i,bt[i]);
}
printf("\n");
//Waiting Time
wt[0]=0;
for(i=0;i<n;i++)
{
wt[i+1]=wt[i]+bt[i];
printf("Waiting Time of P%d is %d\n",i,wt[i]);
}
printf("\n");
//Turn Around Time
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
printf("Turn Around Time of P%d is %d\n",i,tat[i]);
}
printf("\nProcess\t Burst Time\t Waiting Time\t Turn-Around Time\n");
for(i=0;i<n;i++){
printf(" P%d\t %d\t\t %d\t \t\t%d\n",i,bt[i],wt[i],tat[i]); }
awt=0;
atat=0;
for(i=0;i<n;i++)
{
awt=awt+wt[i];
atat+=tat[i];
}
printf("\nAverage Waiting Time: %d",(awt/n));
printf("\nAverage Turn Around Time: %d\n",(atat/n));
}
B. LRU, FIFO, OPT.
#include <stdio.h>
void FIFO(char[], char[], int, int);
void lru(char[], char[], int, int);
void opt(char[], char[], int, int);
int main()
{
int ch, YN = 1, i, l, f;
char F[10], s[25];
system("clear");
printf("\n\n\tEnter the no of empty frames: ");
scanf("%d", &f);
printf("\n\n\tEnter the length of the string: ");
scanf("%d", &l);
printf("\n\n\tEnter the string: ");
scanf("%s", s);
for (i = 0; i < f; i++)
F[i] = -1;
do
{
system("clear");
printf("\n\n\t*********** MENU ***********");
printf("\n\n\t1:FIFO\n\n\t2:LRU\n\n\t3:OPT\n\n\t4:EXIT");
printf("\n\n\tEnter your choice: ");
scanf("%d", &ch);
system("clear");
switch (ch)
{
case 1:
for (i = 0; i < f; i++)
{
F[i] = -1;
}
FIFO(s, F, l, f);
break;
case 2:
for (i = 0; i < f; i++)
{
F[i] = -1;
}
lru(s, F, l, f);
break;
case 3:
for (i = 0; i < f; i++)
{
F[i] = -1;
}
opt(s, F, l, f);
break;
case 4:
exit(0);
}
printf("\n\n\tDo u want to continue IF YES PRESS 1\n\n\tIF NO
PRESS 0 : ");
scanf("%d", &YN);
} while (YN == 1);
return (0);
}
//FIFO
void FIFO(char s[], char F[], int l, int f)
{
int i, j = 0, k, flag = 0, cnt = 0;
printf("\n\tPAGE\t FRAMES\t FAULTS");
for (i = 0; i < l; i++)
{
for (k = 0; k < f; k++)
{
if (F[k] == s[i])
flag = 1;
}
if (flag == 0)
{
printf("\n\t%c\t", s[i]);
F[j] = s[i];
j++;
else
{
flag = 0;
printf("\n\t%c\t", s[i]);
for (k = 0; k < f; k++)
{
printf(" %c", F[k]);
}
printf("\tNo page-fault");
}
if (j == f)
j = 0;
}
}
//LRU
void lru(char s[], char F[], int l, int f)
{
int i, j = 0, k, m, flag = 0, cnt = 0, top = 0;
printf("\n\tPAGE\t FRAMES\t FAULTS");
for (i = 0; i < l; i++)
{
for (k = 0; k < f; k++)
{
if (F[k] == s[i])
{
flag = 1;
break;
}
}
printf("\n\t%c\t", s[i]);
if (j != f && flag != 1)
{
F[top] = s[i];
j++;
if (j != f)
top++;
}
else
{
if (flag != 1)
{
for (k = 0; k < top; k++)
{
F[k] = F[k + 1];
}
F[top] = s[i];
}
if (flag == 1)
{
for (m = k; m < top; m++)
{
F[m] = F[m + 1];
}
F[top] = s[i];
}
}
for (k = 0; k < f; k++)
{
printf(" %c", F[k]);
}
if (flag == 0)
{
printf("\tPage-fault%d", cnt);
cnt++;
}
else
printf("\tNo page fault");
flag = 0;
}
}
//optimal
void opt(char s[], char F[], int l, int f)
{
int i, j = 0, k, m, flag = 0, cnt = 0, temp[10];
if (j != f && flag == 0)
{
F[j] = s[i];
j++;
}
else if (flag == 0)
{
for (m = 0; m < f; m++)
{
for (k = i + 1; k < l; k++)
{
if (F[m] != s[k])
{
temp[m] = temp[m] + 1;
}
else
break;
}
}
m = 0;
for (k = 0; k < f; k++)
{
if (temp[k] > temp[m])
{
m = k;
}
}
F[m] = s[i];
}
printf("\n\t%c\t", s[i]);
for (k = 0; k < f; k++)
{
printf(" %c", F[k]);
}
if (flag == 0)
{
printf("\tPage-fault %d", cnt);
cnt++;
}
else
printf("\tNo Page-fault");
flag = 0;
Bankers
#include <stdio.h>
void main()
{
int alloc[10][10], max[10][10], avail[10], tot[10], need[10][10],
pflag[10] = {0}, safe[10], flag1, flag2, p, r, i, j, k = 0, m;
printf("Enter the no of processes: ");
scanf("%d", &p);
printf("\nEnter the no of resources: ");
scanf("%d", &r);
printf("\nEnter the total instances of resources: ");
for (i = 0; i < r; i++)
{
scanf("%d", &tot[i]);
avail[i] = tot[i];
}
printf("\nEnter the allocated instances for each process ");
for (i = 0; i < p; i++)
{
printf("\nProcess%d: ", i);
for (j = 0; j < r; j++)
scanf("%d", &alloc[i][j]);
}
printf("\nEnter the max instances required for each process");
for (i = 0; i < p; i++)
{
printf("\nProcess %d: ", i);
for (j = 0; j < r; j++)
scanf("%d", &max[i][j]);
}
printf("\nThe available matrix is: ");
for (j = 0; j < r; j++)
{
for (i = 0; i < p; i++)
avail[j] = avail[j] - alloc[i][j];
printf("\t%d", avail[j]);
}
printf("\n\nThe need matrix is: ");
for (i = 0; i < p; i++)
{
printf("\nProcess %d:", i);
for (j = 0; j < r; j++)
{
need[i][j] = max[i][j] - alloc[i][j];
printf("\t%d", need[i][j]);
}
}
for (m = 0; m < p; m++)
{
for (i = 0; i < p; i++)
{
if (pflag[i] == 0)
{
flag1 = 0;
printf("\n\nFor process %d:", i);
for (j = 0; j < r; j++)
{
if (need[i][j] > avail[j])
{
flag1 = 1;
break;
}
}
if (flag1 == 0)
{
for (j = 0; j < r; j++)
avail[j] = avail[j] + alloc[i][j];
pflag[i] = 1;
printf("\nProcess %d can be granted
resources..",i);
printf("\nNew Available resources are\n");
for (j = 0; j < r; j++)
printf("\t%d", avail[j]);
safe[k] = i;
k++;
}
if (flag1 == 1)
printf("\nProcess %d cannot be granted
resources....Going to next process", i);
} //outer if
} //outer for
} //outer for
flag2 = 0;
for (i = 0; i < p; i++)
{
if (pflag[i] == 0)
{
printf("\n\nSystem is NOT in a safe state");
flag2 = 0;
break;
}
else
flag2 = 1;
}
if (flag2 == 1)
{
printf("\n\nSystem is in a SAFE STATE\nSAFE SEQUENCE is\n");
for (i = 0; i < p; i++)
printf("Process%d ", safe[i]);} }
Assignment No.9
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#define MAX_BUF 1024
int main()
{
int fd, c = 0;
char *fifo1 = "fifo1";
char *fifo2 = "fifo2";
int fd1;
int words = 1, lines = 1, chars = 0;
char buf1[MAX_BUF];
mkfifo(fifo1, 0666);
fd = open(fifo1, O_RDWR);
char str;
printf("\nEnter the String:");
while ((str = getchar()) != '#')
buf1[c++] = str;
buf1[c] = '\0';
write(fd, buf1, sizeof(buf1));
close(fd);
unlink(fifo1);
fd1 = open(fifo2, O_RDWR);
read(fd1, buf1, sizeof(buf1));
printf("\nThe contents of file are %s", buf1);
int i = 0;
while (buf1[i] != '\0')
{
if (buf1[i] == ' ' || buf1[i] == '\n')
{
words++;
}
else
{
chars++;
}
if (buf1[i] == '\n')
{
lines++;
}
i++;
}
printf("\n No of Words: %d", words);
printf("\n No of Characters: %d", chars);
printf("\n No of Lines: %d\n", lines);
close(fd1);
return 0;
}
Assignment No.10
#include <stdio.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <wait.h>
int main()
{
int x, y, ret, ret_v;
long int add;
int shmid;
int *shmptr;
key_t key;
pid_t pid;
printf("\nEnter a number:");
scanf("%d", &x);
*shmptr = x;
ret = shmdt((void *)shmptr);
if (ret == 0)
printf("\nShared Memory detached successfully\n");
pid = fork();
if (pid == 0)
{
printf("\n-----------------------------------------------\
nThis is Child Process\
n-----------------------------------------------");
shmptr = (int *)shmat(shmid, 0, 0);
add = (int)shmptr;
if (add != -1)
printf("\nShared Memory is attached at address:%u",
shmptr);
else
{
printf("\nShared Memory not attached!");
_exit(-1);
}
y = *shmptr;
printf("\nThe data read is:%d", y);
ret = shmdt((void *)shmptr);
if (ret == 0)
printf("\nShared Memory detached successfully\n");
ret_v = shmctl(shmid, IPC_RMID, 0);
if (ret_v == 0)
printf("\nShared Memory removed successfully!\n\n");
printf("-----------------------------------------------\n");
}
else
{
wait(0);
}
return 0;
}
Assignment No.11
A. SSTF Algorithm
#include <stdio.h>
#include <stdlib.h>
int main()
{
int RQ[100], i, n, TotalHeadMoment = 0, initial, count = 0;
printf("Enter the number of Requests\n");
scanf("%d", &n);
printf("Enter the Requests sequence\n");
for (i = 0; i < n; i++)
scanf("%d", &RQ[i]);
printf("Enter initial head position\n");
scanf("%d", &initial);
#include <stdio.h>
#include <stdlib.h>
int main()
{
int RQ[100], i, j, n, TotalHeadMoment = 0, initial, size, move;
printf("Enter the number of Requests\n");
scanf("%d", &n);
printf("Enter the Requests sequence\n");
for (i = 0; i < n; i++)
scanf("%d", &RQ[i]);
printf("Enter initial head position\n");
scanf("%d", &initial);
printf("Enter total disk size\n");
scanf("%d", &size);
printf("Enter the head movement direction for high 1 and for low
0\n");
scanf("%d", &move);
int index;
for (i = 0; i < n; i++)
{
if (initial < RQ[i])
{
index = i;
break;
}
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int RQ[100], i, j, n, TotalHeadMoment = 0, initial, size, move;
printf("Enter the number of Requests\n");
scanf("%d", &n);
printf("Enter the Requests sequence\n");
for (i = 0; i < n; i++)
scanf("%d", &RQ[i]);
printf("Enter initial head position\n");
scanf("%d", &initial);
printf("Enter total disk size\n");
scanf("%d", &size);
printf("Enter the head movement direction for high 1 and for low
0\n");
scanf("%d", &move);
int index;
for (i = 0; i < n; i++)
{
if (initial < RQ[i])
{
index = i;
break;
}
}