Name Atharva Tatkare Class TE Div B Roll No:307b006
Name Atharva Tatkare Class TE Div B Roll No:307b006
Class: TE Div: B
Roll no:307B006
` Experiment :
A. Study of Basic Linux Commands: echo, ls, read, cat, touch, test, loops, arithmetic comparison,
conditional loops, grep, sed etc.
B. Write a program to implement an address book with options given below: a) Create address
record. f) Exit
`
opt=1
while [ "$opt" -lt 6 ] do
echo -e "Choose one of the Following\n1. Create a New Address Book\n2. View Records\n3.
Insert new Record\n4. Delete a Record\n5. Modify a Record\n6. Exit"
read opt
case $opt in
B. Implement the C program in which main program accepts an array. Main program uses
theFORK system call to create a new process called a child process. Parent process sorts an
array and passes the sorted array to child process through the command line arguments of
EXECVE system call. The child process uses EXECVE system call to load new program that display
array in reverse order.
Code -
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
int n, i;
printf("Enter the number of integers to be sorted:
"); scanf("%d", &n); int arr[n];
printf("Enter the integers to be sorted: ");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
pid_t pid = fork();
if (pid == 0) { //
Child process printf("Child process sorting the integers using bubble sort
algorithm...\n");
bubbleSort(arr, n);
printf("Sorted integers by child process: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
system("ps -x");
printf("\n"); } else
if (pid > 0) { //
Parent process printf("Parent process waiting for child process to
complete...\n");
wait(NULL); // Wait for the child process to complete printf("Sorted
integers by parent process: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n"); } else {
// fork failed
printf("Fork failed!\n");
return 1;
}
return 0;
}
Name: Atharva Tatkare
Class: TE Div: B
Roll no:307B006
Experiment 2 :
A. Implement the C program in which main program accepts the integers to be sorted.
Mainprogram uses the FORK system call to create a new process called a child process. Parent
process sorts the integers using sorting algorithm and waits for child process using WAIT system
call to sort the integers using any sorting algorithm. Also, demonstrate zombie and orphan
states.
B. Implement the C program in which main program accepts an array. Main program uses
theFORK system call to create a new process called a child process. Parent process sorts an
array and passes the sorted array to child process through the command line arguments of
EXECVE system call. The child process uses EXECVE system call to load new program that display
array in reverse order.
Name: Atharva Tatkare
Class: TE Div: B
Roll no:307B006
Experiment 2 :
A. Implement the C program in which main program accepts the integers to be sorted.
Mainprogram uses the FORK system call to create a new process called a child process. Parent
process sorts the integers using sorting algorithm and waits for child process using WAIT system
call to sort the integers using any sorting algorithm. Also, demonstrate zombie and orphan
states.
B. Implement the C program in which main program accepts an array. Main program uses
theFORK system call to create a new process called a child process. Parent process sorts an
array and passes the sorted array to child process through the command line arguments of
EXECVE system call. The child process uses EXECVE system call to load new program that display
array in reverse order.
Code - Parent
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
int n, i; printf("Enter the number of integers to
be sorted:
"); scanf("%d", &n); int arr[n];
printf("Enter the integers to be sorted: ");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
perror("execve failed");
return 1; } else { printf("Parent process (PID: %d) sorting the
integers...\n", getpid());
bubbleSort(arr, n);
wait(NULL);
}
return 0;
}
Child -
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
printf("Child process (PID: %d) displaying integers in reverse order: ", getpid());
for (int i = n-1; i >= 0; i--) { printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Name: Atharva Tatkare
Class: TE Div: B
Roll no:307B006
Experiment 2 :
A. Implement the C program in which main program accepts the integers to be sorted.
Mainprogram uses the FORK system call to create a new process called a child process. Parent
process sorts the integers using sorting algorithm and waits for child process using WAIT system
call to sort the integers using any sorting algorithm. Also, demonstrate zombie and orphan
states.
B. Implement the C program in which main program accepts an array. Main program uses
theFORK system call to create a new process called a child process. Parent process sorts an
array and passes the sorted array to child process through the command line arguments of
EXECVE system call. The child process uses EXECVE system call to load new program that display
array in reverse order.
Name: Atharva Tatkare
Class: TE Div: B
Roll no:307B006
Experiment - Implement the C program for CPU Scheduling Algorithms: Shortest Job First
Code -
struct Process
{
int pid;
int at; int
bt;
int ct, tt, wt, rt, st;
};
int main()
{ int size = 0; printf("Enter number of
processes: "); scanf("%d", &size);
struct Process ps[size];
while (completed != n)
{
if (miniI == -1)
{ currentTime++;
}
else
{
if (burstTimeR[miniI] == ps[miniI].bt)
{ ps[miniI].st = currentTime;
}
burstTimeR[miniI] -= 1; currentTime++;
if (burstTimeR[miniI] == 0)
{
avgWT += ps[miniI].wt;
avgTT += ps[miniI].tt; avgRT
+= ps[miniI].rt;
completed++;
iscompleted[miniI] = true;
}
}
}
printf("PID \t AT \t BT \t CT \t TT \t WT \t RT \t\n"); for
(int i = 0; i < n; i++)
{
printf("%d \t %d \t %d \t %d \t %d \t %d \t %d \t\n", ps[i].pid, ps[i].at, ps[i].bt,
ps[i].ct, ps[i].tt, ps[i].wt, ps[i].rt);
}
printf("\n\
n===================================================================
=================\n");
Experiment - Implement the C program for CPU Scheduling Algorithms: Shortest Job First
(Preemptive) and Round Robin with different arrival time.
Name: Atharva Tatkare
Class: TE Div: B
Roll no:307B006
Experiment - Implement the C program for CPU Scheduling Algorithms: Shortest Job First
(Preemptive) and Round Robin with different arrival time.
Code -
#include<stdio.h>
int main()
{
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("\nProcess No[%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;
}
}
Experiment - Implement the C program for CPU Scheduling Algorithms: Shortest Job First
(Preemptive) and Round Robin with different arrival time.
Name: Atharva Tatkare
Class: TE Div: B
Roll no:307B006
#include<stdio.h>
#include<sys/types.h>
#include<semaphore.h>
#include<pthread.h>
#include<unistd.h>
char avbuff[10]; sem_t
mutex,empty,full;
int main()
{ pthread_t tid1,tid2;
sem_init(&empty,0,10);
sem_init(&full,0,0);
sem_init(&mutex,1,1);
void *status;
pthread_create(&tid1,NULL,produce,NULL);
pthread_create(&tid2,NULL,consumer,NULL);
pthread_join(tid1,&status);
printf("\nThe exited status %s \n",(char *)status);
pthread_join(tid2,&status);
printf("\nThe exited status %s \n",(char *)status);
return 1;
}
Name: Atharva Tatkare
Class: TE Div: B
Roll no:307B006
Code-
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#include<stdlib.h>
#define READERS 3
#define WRITERS 2
pthread_mutex_t
mutex; sem_t
rw_mutex; int
readers_count = 0;
int shared_resource = 0;
pthread_mutex_init(&mutex, NULL);
sem_init(&rw_mutex, 0, 1);
pthread_mutex_destroy(&mutex);
sem_destroy(&rw_mutex);
return 0;
}
Name: Atharva Tatkare
Class: TE Div: B
Roll no:307B006
Output -
Name: Atharva Tatkare
Class: TE Div: B
Roll no:307B006
`
`
Code - #include<stdio.h>
int main(){
int i;
printf("\nEnter No of processes: ");
scanf("%d",&n);
printf("Enter no of resources: ");
scanf("%d",&m);
int available[m]; int safesequence[n];
printf("\n*****Enter details of process*****");
input(available); display(); if(isSafestate(available,safesequence))
{ printf("\n\tSYSTEM IS IN SAFE STATE...");
printf("\nsafesequence is: "); for(i=0;i<n;i++)
printf("P%d -> ",safesequence[i]);
}
else
printf("\nSYSTEM IS IN UNSAFE STATE!!!");
return 0;
}
void input (int available[]){
int i,j;
for(i=0;i<n;i++){
for(i=0;i<n;i++)
finish[i]=0;
int proceed=1,k=0;
while(proceed)
{ proceed=0;
for(i=0;i<n;i++)
{
int flag=1; if(finish[i]==0)
{ for(j=0;j<m;j++)
{ if(p[i].need[j]<=work[j])
{
continue;
}
else
{ flag=0;
break;
}
} if(flag==0)
continue;
for(j=0;j<m;j++)
{
work[j]+=p[i].allocate[j];
}
finish[i]=1;
safesequence[k++]=i;
proceed=1;
}
}
}
for(i=0;i<n&&finish[i]==1;i++)
continue;
if(i==n)
return 1;
return 0;
}
Name: Atharva Tatkare
Class: TE Div: B
Roll no:307B006
`
Experiment : Implement the C program for Deadlock Avoidance Algorithm: Bankers
Algorithm.
`
Output -
Name: Atharva Tatkare
Class: TE Div: B
Roll no:307B006
Experiment 6 : Implement the C program for Page Replacement Algorithms: FCFS, LRU, and Optimal for
frame size as minimum three.
Code -
#include<stdio.h>
typedef struct {
char
data[20][2]; int
end; }queue;
int frameSize,count,cnt,ch;
char string[50];
printf("Enter the string: ");
count=0; do{
scanf("%c",&string[count]);
count++;
}
while(string[count-1]!='\n'); count-
-; printf("\nEnter the size of the frame:
"); scanf("%d",&frameSize);
do{
printf("\nMENU\n====\n1.FIFO\n2.Least Recently Used (LRU)\n3.Optimal\n4.Exit\ n\nYour
Choice:"); scanf("%d",&ch); switch(ch){ case 1:
fifo(string,frameSize,count);
break;
case 2:
lru(string,frameSize,count);
break; case 3:
optimal(string,frameSize,count);
break; case 4:
//exit(0);
break;
default:
printf("\nInvalid choice! Please try again!");
continue;
}
while(ch!=4);
>data[position][0]=data;
}
char dequeue(queue *q,int position){
char value;
value=q->data[position][0];
return(value);
}
}
if(flag==0){
faults++;
if(q.end<frameSize) {
enqueue(&q,string[cnt],q.end);
q.end++;
}
else{
dequeue(&q,firstin);
firstin=(firstin+1)%(q.end);
enqueue(&q,string[cnt],firstin);
} printf("\t ");
for(cnt2=0;cnt2<q.end;cnt2++) {
printf("%c ",q.data[cnt2][0]);
}
printf("\t\tY");
}
else{
printf("\t ");
for(cnt2=0;cnt2<q.end;cnt2++) {
printf("%c ",q.data[cnt2][0]);
}
printf("\t\tN");
}
} printf("\n\n==============================================\n");
printf("\nTotal no. of Page Faults: %d\n\n",faults);
}
void optimal(char string[],int frameSize,int count){
int
cnt,cnt2,selector,flag,max,faults=0;
int distance[20]; queue q;
q.end=0;
printf("\nData Requested\tFrame contents\t Page Fault\
n==============================================");
for(cnt=0;cnt<count;cnt+=2){ printf("\n\n\t%c",string[cnt]);
flag=0; for(cnt2=0;cnt2<q.end;cnt2++) {
if(string[cnt]==q.data[cnt2][0]) {
flag=1; break;
if(flag==0) {
faults++;
if(q.end<frameSize) {
enqueue(&q,string[cnt],q.end);
q.data[q.end][1]=cnt;
q.end++;
} else {
for(cnt2=0;cnt2<q.end;cnt2++) {
distance[cnt2]=0;
}
for(selector=0;selector<q.end;selector++) {
for(cnt2=cnt;cnt2<count;cnt2+=2){
if(string[cnt2]==q.data[selector][0]) {
distance[selector]=cnt2/2;
break;
}
if(distance[selector]==0) {
distance[selector]=99-q.data[selector][1]; }
} }
max=0;
for(cnt2=0;cnt2<q.end;cnt2++) {
if(distance[cnt2]>max) {
max=distance[cnt2];
selector=cnt2;
}
}
dequeue(&q,selector);
enqueue(&q,string[cnt],selector);
q.data[selector][1]=cnt;
} printf("\t ");
for(cnt2=0;cnt2<q.end;cnt2++) {
printf("%c ",q.data[cnt2][0]);
}
printf("\t\tY");
}
else {
printf("\t ");
for(cnt2=0;cnt2<q.end;cnt2++) {
printf("%c ",q.data[cnt2][0]);
}
printf("\t\tN");
}
}
printf("\n\n==============================================\n");
printf("\nTotal no. of Page Faults: %d\n\n",faults);
} if(flag==0) {
faults++; if(q.end<frameSize) {
enqueue(&q,string[cnt],q.end);
q.data[q.end][1]=(cnt/2)+1;
q.end++;
} else {
min=99;
for(cnt2=0;cnt2<q.end;cnt2++) {
if(q.data[cnt2][1]<min) {
min=q.data[cnt2][1]; selector=cnt2;
}
}
dequeue(&q,selector);
enqueue(&q,string[cnt],selector);
q.data[selector][1]=(cnt/2)+1;
}
printf("\t ");
for(cnt2=0;cnt2<q.end;cnt2++) {
printf("%c ",q.data[cnt2][0]);
}
printf("\t\tY");
else {
printf("\t ");
for(cnt2=0;cnt2<q.end;cnt2++) { printf("%c
",q.data[cnt2][0]);
}
printf("\t\tN");
}
}
printf("\n\n==============================================\n");
printf("\nTotal no. of Page Faults: %d\n\n",faults);
}
Name: Atharva Tatkare
Class: TE Div: B
Roll no:307B006
Experiment 6 : Implement the C program for Page Replacement Algorithms: FCFS, LRU, and
Optimal for frame size as minimum three.
Output -