Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
18 views34 pages

Osset 4

os program

Uploaded by

Preeti Ramesh
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)
18 views34 pages

Osset 4

os program

Uploaded by

Preeti Ramesh
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/ 34

1

date + \%d/%m/%Y\>
--
#!/bin/bash

# Function to calculate factorial


factorial() {
local num=$1
local result=1
while [ $num -gt 0 ]; do
result=$((result * num))
num=$((num - 1))
done
echo $result
}

# Loop to get 5 numbers from the user


for i in {1..5}; do
read -p "Enter number $i: " number
fact=$(factorial $number)
echo "Factorial of $number is $fact"
done
--
#include <stdio.h>

typedef struct {
int processID;
int arrivalTime;
int burstTime;
int priority;
int completionTime;
int turnaroundTime;
int waitingTime;
} Process;

void sortProcessesByPriority(Process proc[], int n) {


for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (proc[j].priority > proc[j + 1].priority) {
Process temp = proc[j];
proc[j] = proc[j + 1];
proc[j + 1] = temp;
}
}
}
}
int main() {
int n,i,u,v,w,x;
printf("enter no of processes :");
scanf("%d",&n);
Process proc[n];
for (i=0;i<n;i++)
{ printf("enter details of process %d",i);
scanf("%d%d%d%d",&u,&v,&w,&x);
proc[i].processID=u;
proc[i].arrivalTime=v;
proc[i].burstTime=w;
proc[i].priority=x;}

sortProcessesByPriority(proc, n);

int currentTime = 0;
float totalWaitingTime = 0, totalTurnaroundTime = 0;

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


if (currentTime < proc[i].arrivalTime) {
currentTime = proc[i].arrivalTime;
}
proc[i].completionTime = currentTime + proc[i].burstTime;
proc[i].turnaroundTime = proc[i].completionTime -
proc[i].arrivalTime;
proc[i].waitingTime = proc[i].turnaroundTime - proc[i].burstTime;

totalWaitingTime += proc[i].waitingTime;
totalTurnaroundTime += proc[i].turnaroundTime;

currentTime = proc[i].completionTime;
}

printf("Process\tArrival Time\tBurst Time\tPriority\tCompletion Time\


tTurnaround Time\tWaiting Time\n");
for (int i = 0; i < n; i++) {
printf("P%d\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n",
proc[i].processID, proc[i].arrivalTime,
proc[i].burstTime,
proc[i].priority, proc[i].completionTime,
proc[i].turnaroundTime,
proc[i].waitingTime);
}

printf("\nAverage Waiting Time: %.2f\n", totalWaitingTime / n);


printf("Average Turnaround Time: %.2f\n", totalTurnaroundTime / n);

return 0;
}
---
2
vi filename.txt
cat filename.txt
---
#!/bin/bash

# Function to calculate the area of a square


# Prompt the user for the length of the side
read -p "Enter the length of the side of the square: " side
area=$((side*side))
echo "The area of the square with side length $side is $area."
___
#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
#include<unistd.h>
sem_t mutex;
void* thread(void* arg)
{
//wait
sem_wait(&mutex);
printf("\nEntered thread\n");

//critical section
sleep(4);
//signal
printf("\n Exit thread\n");
sem_post(&mutex);
}
void main()
{
sem_init(&mutex, 0, 1);
pthread_t t1,t2;
pthread_create(&t1,NULL,thread,NULL);
sleep(2);
pthread_create(&t2,NULL,thread,NULL);
pthread_join(t1,NULL);
pthread_join(t2,NULL);
sem_destroy(&mutex);
}
___
3
int main()
{ pid_t pid;
pid = getpid();
printf("process id:%d",pid);
return 0;
}
____
#!/bin/bash

# Function to check if a number is a perfect square


# Prompt the user for a number
read -p "Enter a number: " number
sqrt=$(awk "BEGIN {print int(sqrt($number))}")
square=$((sqrt*sqrt))
if [ "$square" -eq "$number" ]; then
echo "Yes, $number is a perfect square."
else
echo "No, $number is not a perfect square."
fi

___
#include <stdio.h>
#include <stdbool.h>
#define MAX 10 // Maximum number of processes or resources

int processes, resources; // Number of processes and resources

// Function to find if a system is in a safe state


bool isSafe(int need[MAX][MAX], int allot[MAX][MAX], int avail[MAX], int
safeSeq[MAX]) {
int work[MAX], finish[MAX];
int i, j, count = 0;
bool found;

// Initialize work and finish


for (i = 0; i < resources; i++)
work[i] = avail[i];
for (i = 0; i < processes; i++)
finish[i] = 0;

// Find a safe sequence


while (count < processes) {
found = false;
for (i = 0; i < processes; i++) {
if (!finish[i]) {
// Check if the process can be executed
int j;
for (j = 0; j < resources; j++)
if (need[i][j] > work[j])
break;

if (j == resources) {
// Add the resources allocated to the process to work
for (j = 0; j < resources; j++)
work[j] += allot[i][j];

safeSeq[count++] = i;
finish[i] = 1;
found = true;
}
}
}

// If no process was found, the system is not in a safe state


if (!found)
return false;
}

return true;
}

int main() {
int i, j;

// Input number of processes and resources


printf("Enter the number of processes: ");
scanf("%d", &processes);
printf("Enter the number of resources: ");
scanf("%d", &resources);

int max[MAX][MAX], allot[MAX][MAX], avail[MAX];


int need[MAX][MAX], safeSeq[MAX];

// Input the maximum resources matrix


printf("Enter the maximum resource matrix:\n");
for (i = 0; i < processes; i++) {
for (j = 0; j < resources; j++) {
printf("Max[%d][%d]: ", i, j);
scanf("%d", &max[i][j]);
}
}

// Input the allocated resources matrix


printf("Enter the allocated resource matrix:\n");
for (i = 0; i < processes; i++) {
for (j = 0; j < resources; j++) {
printf("Allot[%d][%d]: ", i, j);
scanf("%d", &allot[i][j]);
}
}

// Input the available resources


printf("Enter the available resources:\n");
for (i = 0; i < resources; i++) {
printf("Avail[%d]: ", i);
scanf("%d", &avail[i]);
}

// Calculate the need matrix


for (i = 0; i < processes; i++) {
for (j = 0; j < resources; j++) {
need[i][j] = max[i][j] - allot[i][j];
}
}

// Check system's safety


if (isSafe(need, allot, avail, safeSeq)) {
printf("System is in a safe state.\n");
printf("Safe sequence is: ");
for (i = 0; i < processes; i++)
printf("P%d ", safeSeq[i]);
printf("\n");
} else {
printf("System is not in a safe state.\n");
}

return 0;
}
____
4
cal 2024
_____

#!/bin/bash

# Function to check if the number is an even prime

# Prompt the user for a number


read -p "Enter a number: " num
if [ "$num" -eq 2 ]; then
echo "$num is an even prime number."
else
echo "$num is not an even prime number."
fi
___
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> //Header file for sleep(). man 3 sleep for details.
#include <pthread.h>

// A normal C function that is executed as a thread


// when its name is specified in pthread_create()
void *myThreadFun(void *vargp)
{
sleep(1);
printf("Printing Geeksquiz from Thread \n");
return NULL;
}

int main()
{
pthread_t thread_id;
printf("Before Thread\n");
pthread_create(&thread_id, NULL, myThreadFun, NULL);
pthread_join(thread_id, NULL);
printf("After Thread\n");
exit(0);
}
_____
5
cat filename.txt
date
______
#!/bin/bash
echo "enter the radius of the circle : "
read radius
pi=3.141
circum=$(awk "BEGIN{print "$pi*$radius*2"}")
echo "circumference:$circum"
_____
#include <stdio.h>

#define MAX 10

// Structure to hold process details


typedef struct {
char processID[3]; // Process ID like P0, P1
int size; // Size of the process
} Process;

// Structure to hold block details


typedef struct {
char blockID[3]; // Block ID like B1, B2
int size; // Size of the block
int allocated; // To keep track of whether the block is allocated
} Block;

// Function to perform Worst Fit Allocation


void worstFitAllocation(Process processes[], int pCount, Block blocks[],
int bCount) {
int i, j;

// Initialize the allocation status


for (i = 0; i < bCount; i++) {
blocks[i].allocated = 0; // Mark all blocks as unallocated
}

printf("\nWorst Fit Allocation:\n");


printf("Process\tBlock\n");

// Allocate memory to processes


for (i = 0; i < pCount; i++) {
int worstIndex = -1;
for (j = 0; j < bCount; j++) {
if (blocks[j].size >= processes[i].size && (worstIndex == -1
|| blocks[j].size > blocks[worstIndex].size)) {
worstIndex = j;
}
}

// Check if a suitable block was found


if (worstIndex != -1) {
printf("%s\t%s\n", processes[i].processID,
blocks[worstIndex].blockID);
blocks[worstIndex].size -= processes[i].size; // Reduce block
size
blocks[worstIndex].allocated = 1; // Mark block as allocated
} else {
printf("%s\tNot Allocated\n", processes[i].processID);
}
}
}

int main() {
int pCount, bCount, i;
Process processes[MAX];
Block blocks[MAX];

// Input number of processes


printf("Enter the number of processes: ");
scanf("%d", &pCount);

// Input process details


for (i = 0; i < pCount; i++) {
printf("Enter size of process %d: ", i);
scanf("%d", &processes[i].size);
snprintf(processes[i].processID, sizeof(processes[i].processID),
"P%d", i);
}

// Input number of blocks


printf("Enter the number of blocks: ");
scanf("%d", &bCount);

// Input block details


for (i = 0; i < bCount; i++) {
printf("Enter size of block %d: ", i);
scanf("%d", &blocks[i].size);
snprintf(blocks[i].blockID, sizeof(blocks[i].blockID), "B%d", i);
}

// Perform Worst Fit Allocation


worstFitAllocation(processes, pCount, blocks, bCount);

return 0;
}
____
6
#include <stdio.h>
#include <unistd.h> // For fork()

int main() {
pid_t pid = fork(); // Create a new process

if (pid < 0) {
// Error occurred
printf("Fork failed\n");
return 1;
} else if (pid == 0) {
// Child process
printf("This is the child process with PID: %d\n", getpid());
printf("Child process's parent PID: %d\n", getppid());
} else {
// Parent process
printf("This is the parent process with PID: %d\n", getpid());
printf("Parent process's child PID: %d\n", pid);
}

return 0;
}
____

#!/bin/bash
# Function to convert Celsius to Fahrenheit
# Prompt the user for the temperature in Celsius
read -p "Enter temperature in Celsius: " celsius
fahrenheit=$(awk "BEGIN{printf \"%.2f\", (9/5) * $celsius + 32}")
echo "$celsius°C is equal to $fahrenheit°F."
____
server.c
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/shm.h>
#include<string.h>
int main()
{
int i;
void* shared_memory;
char buff[100];
int shmid;
shmid=shmget((key_t)2345, 1024, 0666|IPC_CREAT);
printf("Key of shared memory is %d\n",shmid);
shared_memory=shmat(shmid,NULL,0);
printf("Process attached at %p\n",shared_memory);
printf("Enter some data to write to shared memory\n");
read(0,buff,100); //get some input from user
strcpy(shared_memory,buff);
printf("You wrote : %s\n",(char *)shared_memory);
}
--
client.c
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/shm.h>
#include<string.h>
int main()
{ int i;
void *shared_memory;
char buff[100];
int shmid;
shmid=shmget((key_t)2345, 1024, 0666);
printf("Key of shared memory is %d\n",shmid);
shared_memory=shmat(shmid,NULL,0);
printf("Process attached at %p\n",shared_memory);
printf("Data read from shared memory is : %s\n",(char *)shared_memory);
}
________
7
ls -l
ls -a
ls -lh
ls -R
ls -lt
ls -lS
_______
#!/bin/bash

# Function to calculate the area of a triangle using base and height


# Prompt the user for the base and height of the triangle
read -p "Enter the base of the triangle: " base
read -p "Enter the height of the triangle: " height
area=$(awk "BEGIN {printf \"%.2f\", 0.5 * $base * $height}")
echo "The area of the triangle with base $base and height $height is
$area."
______
#include <stdio.h>
#include <stdbool.h>

#define MAX 10

int processes, resources; // Number of processes and resources

// Function to find if a system is in a safe state


bool isDeadlock(int allocation[MAX][MAX], int request[MAX][MAX], int
available[MAX]) {
int work[MAX], finish[MAX];
bool safe = true;
int i, j, k;

// Initialize work and finish


for (i = 0; i < resources; i++)
work[i] = available[i];
for (i = 0; i < processes; i++)
finish[i] = 0;

// Try to find a safe sequence


for (i = 0; i < processes; i++) {
bool progressMade = false;
for (j = 0; j < processes; j++) {
if (!finish[j]) {
// Check if request can be satisfied
bool canBeSatisfied = true;
for (k = 0; k < resources; k++) {
if (request[j][k] > work[k]) {
canBeSatisfied = false;
break;
}
}

// If request can be satisfied, pretend to allocate


resources
if (canBeSatisfied) {
for (k = 0; k < resources; k++) {
work[k] += allocation[j][k];
}
finish[j] = 1;
progressMade = true;
break;
}
}
}

if (!progressMade) {
safe = false; // If no progress was made, system is in
deadlock
break;
}
}

return !safe; // Return true if system is in deadlock


}

int main() {
int i, j;

// Input number of processes and resources


printf("Enter the number of processes: ");
scanf("%d", &processes);
printf("Enter the number of resources: ");
scanf("%d", &resources);

int allocation[MAX][MAX], request[MAX][MAX], available[MAX];

// Input the allocation matrix


printf("Enter the allocation matrix:\n");
for (i = 0; i < processes; i++) {
for (j = 0; j < resources; j++) {
printf("Allocation[%d][%d]: ", i, j);
scanf("%d", &allocation[i][j]);
}
}

// Input the request matrix


printf("Enter the request matrix:\n");
for (i = 0; i < processes; i++) {
for (j = 0; j < resources; j++) {
printf("Request[%d][%d]: ", i, j);
scanf("%d", &request[i][j]);
}
}

// Input the available resources


printf("Enter the available resources:\n");
for (i = 0; i < resources; i++) {
printf("Available[%d]: ", i);
scanf("%d", &available[i]);
}

// Check for deadlock


if (isDeadlock(allocation, request, available)) {
printf("System is in a deadlock state.\n");
} else {
printf("System is not in a deadlock state.\n");
}

return 0;
}
_____
read -p "enter the number to find factorial" num
result=1
while [ $num -gt 0 ]; do
result=$((result * num))
num=$((num - 1))
done
echo "Total of factorial is $result"
____
#include <stdio.h>
#include <stdlib.h>

#define MAX_FRAMES 100


#define MAX_REFERENCES 1000

void calculatePageFaults(int referenceString[], int numReferences, int


numFrames) {
int frames[MAX_FRAMES];
int pageFaults = 0;
int i, j, k, found, lru, lruIndex;

// Initialize frames
for (i = 0; i < numFrames; i++) {
frames[i] = -1;
}

// Process each page reference


for (i = 0; i < numReferences; i++) {
found = 0;

// Check if page is already in frames


for (j = 0; j < numFrames; j++) {
if (frames[j] == referenceString[i]) {
found = 1;
break;
}
}

// Page fault occurs if page is not found in frames


if (!found) {
// Find the LRU page
lru = -1;
lruIndex = -1;
for (j = 0; j < numFrames; j++) {
if (frames[j] == -1) {
lruIndex = j;
break;
}
for (k = 0; k < i; k++) {
if (frames[j] == referenceString[k]) {
if (lruIndex == -1 || lru < k) {
lru = k;
lruIndex = j;
}
break;
}
}
}

// Replace LRU page with the new page


frames[lruIndex] = referenceString[i];
pageFaults++;
}
}

printf("Number of page faults: %d\n", pageFaults);


}

int main() {
int referenceString[MAX_REFERENCES];
int numReferences, numFrames;

// Input number of frames


printf("Enter the number of frames: ");
scanf("%d", &numFrames);

// Input the reference string


printf("Enter the number of references: ");
scanf("%d", &numReferences);

printf("Enter the reference string:\n");


for (int i = 0; i < numReferences; i++) {
printf("Reference[%d]: ", i);
scanf("%d", &referenceString[i]);
}

// Calculate page faults


calculatePageFaults(referenceString, numReferences, numFrames);

return 0;
}
_____
for i in {1..10}
do
echo $i
done
#!/bin/bash
**************************************************
# Display the first 10 natural number
read -p "enter number:" num
for ((i=1;i<=$num;i++))
do
echo $i
done
**********************************
_____
#include<stdio.h>
struct dir
{
char dname[15];
int no;
char fn[5][10];
};

struct us
{
char user[15];
int dn;
struct dir d[5];
}u[5];

void get(int n)
{
int i,j,k;

for(i=0;i<n;i++)
{
printf("Enter the %d user name",i+1);
scanf("%s",u[i].user);
printf("Enter the no. of directories\n");
scanf("%d",&u[i].dn);

for(k=0;k<u[i].dn;k++)
{
printf("Enter the %d directory name",k+1);
scanf("%s",u[i].d[k].dname);
printf("\nEnter the no. of files");
scanf("%d",&u[i].d[k].no);
//printf("%d",u[i].d[k].no);

for(j=0;j<u[i].d[i].no;j++)
{
printf("\nEnter %d file name",j+1);
scanf("%s",u[i].d[k].fn[j]);
// printf("\n%s",u[i].d[k].fn[j]);
}
}
}
}

void display(int n)
{
int i,j,k;
for(k=0;k<n;k++)
{
for(i=0;i<u[k].dn;i++)
{
printf("%s>%s>\n",u[k].user,u[k].d[i].dname);
// printf("%d\n",u[k].d[i].no);
for(j=0;j<u[k].d[k].no;j++)
{
printf("\t%s\n",u[k].d[i].fn[j]);
}
if(u[k].dn==0)
printf("%s>",u[k].user);
}
}
}
void main()
{
int n;
clrscr();
printf("Enter the no. of users\n");
scanf("%d",&n);
get(n);
display(n);
getch();
}
____________________________________
#include<stdio.h>
struct dir
{
char dname[15];
int no;
char fn[5][10];
};

struct us
{
char user[15];
int dn;
struct dir d[5];
}u[5];

void get(int n)
{
int i,j,k;

for(i=0;i<n;i++)
{
printf("Enter the %d user name",i+1);
scanf("%s",u[i].user);
printf("Enter the no. of directories\n");
scanf("%d",&u[i].dn);

for(k=0;k<u[i].dn;k++)
{
printf("Enter the %d directory name",k+1);
scanf("%s",u[i].d[k].dname);
printf("\nEnter the no. of files");
scanf("%d",&u[i].d[k].no);
//printf("%d",u[i].d[k].no);

for(j=0;j<u[i].d[i].no;j++)
{
printf("\nEnter %d file name",j+1);
scanf("%s",u[i].d[k].fn[j]);
// printf("\n%s",u[i].d[k].fn[j]);
}
}
}
}

void display(int n)
{
int i,j,k;
for(k=0;k<n;k++)
{
for(i=0;i<u[k].dn;i++)
{
printf("%s>%s>\n",u[k].user,u[k].d[i].dname);
// printf("%d\n",u[k].d[i].no);
for(j=0;j<u[k].d[k].no;j++)
{
printf("\t%s\n",u[k].d[i].fn[j]);
}
if(u[k].dn==0)
printf("%s>",u[k].user);
}
}
}
void main()
{
int n;
clrscr();
printf("Enter the no. of users\n");
scanf("%d",&n);
get(n);
display(n);
getch();
}
__________________________________________
#!/bin/bash
echo "current date and time:"
echo "date:$(date '+%d/%m/%Y')"
echo "time:$(date '+%H:%M:%S')"
_______________________________
#include <stdio.h>
#include <stdlib.h>

#define MAX_REQUESTS 100

// Function to compare two integers (for qsort)


int compare(const void *a, const void *b) {
return ((int)a - (int)b);
}

// Function to calculate the total head movement using LOOK scheduling


int calculateHeadMovement(int requests[], int numRequests, int head, int
diskSize) {
int totalHeadMovement = 0;
int i;

// Sort the requests


qsort(requests, numRequests, sizeof(int), compare);

// Find the direction of the head movement


int direction = head < requests[0] ? 1 : -1;

// Find the index of the first request greater than or equal to the
head
int index = 0;
while (index < numRequests && requests[index] < head) {
index++;
}

// Move towards the end or beginning based on direction


if (direction == 1) {
// Move right to the end, then go left
for (i = index; i < numRequests; i++) {
totalHeadMovement += abs(head - requests[i]);
head = requests[i];
}
// Move left to the beginning
for (i = index - 1; i >= 0; i--) {
totalHeadMovement += abs(head - requests[i]);
head = requests[i];
}
} else {
// Move left to the beginning, then go right
for (i = index - 1; i >= 0; i--) {
totalHeadMovement += abs(head - requests[i]);
head = requests[i];
}
// Move right to the end
for (i = index; i < numRequests; i++) {
totalHeadMovement += abs(head - requests[i]);
head = requests[i];
}
}

return totalHeadMovement;
}

int main() {
int requests[MAX_REQUESTS];
int numRequests, head, diskSize;
int i;

// Input number of requests


printf("Enter the number of requests: ");
scanf("%d", &numRequests);

// Input the disk size


printf("Enter the disk size (number of cylinders): ");
scanf("%d", &diskSize);

// Input the current head position


printf("Enter the current head position: ");
scanf("%d", &head);

// Input the requests


printf("Enter the requests (cylinders):\n");
for (i = 0; i < numRequests; i++) {
printf("Request[%d]: ", i);
scanf("%d", &requests[i]);
}

// Calculate and print the total head movement


int totalMovement = calculateHeadMovement(requests, numRequests,
head, diskSize);
printf("Total head movement: %d cylinders\n", totalMovement);

return 0;
}
____________________________

#!/bin/bash

# Function to generate Fibonacci series


# Prompt the user for the number of terms
read -p "Enter the number of terms: " num
a=0
b=1
fib=0
echo "Fibonacci series up to $num terms:"
for (( i=0; i<$num; i++ ))
do
echo -n "$fib "
a=$b
b=$fib
fib=$((a + b))
done
____________
#include <stdio.h>

#define MAX_PROCESSES 10

typedef struct {
int arrivalTime;
int burstTime;
int waitingTime;
int turnaroundTime;
} Process;

// Function to calculate average waiting time and turnaround time using


SJF
void calculateSJF(Process processes[], int numProcesses) {
int i, j;
Process temp;
int totalWaitingTime = 0, totalTurnaroundTime = 0;

// Sort processes based on burst time (Shortest Job First)


for (i = 0; i < numProcesses - 1; i++) {
for (j = i + 1; j < numProcesses; j++) {
if (processes[j].burstTime < processes[i].burstTime) {
// Swap processes[i] and processes[j]
temp = processes[i];
processes[i] = processes[j];
processes[j] = temp;
}
}
}

// Calculate waiting time and turnaround time


processes[0].waitingTime = 0;
processes[0].turnaroundTime = processes[0].burstTime;

for (i = 1; i < numProcesses; i++) {


processes[i].waitingTime = 0;
processes[i].turnaroundTime = processes[i].burstTime;

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


processes[i].waitingTime += processes[j].burstTime;
}

processes[i].turnaroundTime += processes[i].waitingTime;
totalWaitingTime += processes[i].waitingTime;
totalTurnaroundTime += processes[i].turnaroundTime;
}

// Print results
printf("\nProcess\tArrival Time\tBurst Time\tWaiting Time\tTurnaround
Time\n");
for (i = 0; i < numProcesses; i++) {
printf("P%d\t%d\t\t%d\t\t%d\t\t%d\n", i,
processes[i].arrivalTime, processes[i].burstTime,
processes[i].waitingTime, processes[i].turnaroundTime);
}

printf("\nAverage Waiting Time: %.2f\n", (float)totalWaitingTime /


numProcesses);
printf("Average Turnaround Time: %.2f\n",
(float)totalTurnaroundTime / numProcesses);
}
int main() {
int numProcesses, i;

// Input number of processes


printf("Enter the number of processes: ");
scanf("%d", &numProcesses);

// Ensure we don't exceed maximum allowed processes


if (numProcesses > MAX_PROCESSES) {
printf("Number of processes exceeds maximum allowed (%d).",
MAX_PROCESSES);
return 1;
}

Process processes[MAX_PROCESSES];

// Input arrival time and burst time


printf("Enter Arrival Time and Burst Time for each process:\n");
for (i = 0; i < numProcesses; i++) {
printf("Process P%d:\n", i);
printf("Arrival Time: ");
scanf("%d", &processes[i].arrivalTime);
printf("Burst Time: ");
scanf("%d", &processes[i].burstTime);
}

// Calculate and print the average waiting time and turnaround time
calculateSJF(processes, numProcesses);

return 0;
}

________________________
#!/bin/bash
read -p "enter a number" num
if [ "$num" -gt 0 ];then
echo "positive"
else
echo "negative"
fi
_________________
#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
#include<unistd.h>
sem_t mutex;
void* thread(void* arg)
{
//wait
sem_wait(&mutex);
printf("\nEntered thread\n");

//critical section
sleep(4);
//signal
printf("\n Exit thread\n");
sem_post(&mutex);
}
void main()
{
sem_init(&mutex, 0, 1);
pthread_t t1,t2;
pthread_create(&t1,NULL,thread,NULL);
sleep(2);
pthread_create(&t2,NULL,thread,NULL);
pthread_join(t1,NULL);
pthread_join(t2,NULL);
sem_destroy(&mutex);
}
__________
#!/bin/bash
# Function to check if the number is an Armstrong number
# Prompt the user for a number
read -p "Enter a number: " num
original_num=$num
sum=0
digits=${#num}
while [ $num -gt 0 ]
do
digit=$((num % 10))
power=1
for ((i = 0; i < digits; i++)); do
power=$((power * digit))
done
sum=$((sum + power))
num=$((num / 10))
done
if [ "$sum" -eq "$original_num" ]; then
echo "$original_num is an Armstrong number."
else
echo "$original_num is not an Armstrong number."
fi

___________
#include <stdio.h>
#include <stdlib.h>

#define MAX_FRAMES 100


#define MAX_REFERENCES 1000

void calculatePageFaults(int referenceString[], int numReferences, int


numFrames) {
int frames[MAX_FRAMES];
int pageFaults = 0;
int i, j, k, found, lru, lruIndex;

// Initialize frames
for (i = 0; i < numFrames; i++) {
frames[i] = -1;
}

// Process each page reference


for (i = 0; i < numReferences; i++) {
found = 0;

// Check if page is already in frames


for (j = 0; j < numFrames; j++) {
if (frames[j] == referenceString[i]) {
found = 1;
break;
}
}

// Page fault occurs if page is not found in frames


if (!found) {
// Find the LRU page
lru = -1;
lruIndex = -1;
for (j = 0; j < numFrames; j++) {
if (frames[j] == -1) {
lruIndex = j;
break;
}
for (k = 0; k < i; k++) {
if (frames[j] == referenceString[k]) {
if (lruIndex == -1 || lru < k) {
lru = k;
lruIndex = j;
}
break;
}
}
}

// Replace LRU page with the new page


frames[lruIndex] = referenceString[i];
pageFaults++;
}
}

printf("Number of page faults: %d\n", pageFaults);


}

int main() {
int referenceString[MAX_REFERENCES];
int numReferences, numFrames;

// Input number of frames


printf("Enter the number of frames: ");
scanf("%d", &numFrames);
// Input the reference string
printf("Enter the number of references: ");
scanf("%d", &numReferences);

printf("Enter the reference string:\n");


for (int i = 0; i < numReferences; i++) {
printf("Reference[%d]: ", i);
scanf("%d", &referenceString[i]);
}

// Calculate page faults


calculatePageFaults(referenceString, numReferences, numFrames);

return 0;
}
__________
#!/bin/bash

# Function to calculate the area of a triangle using base and height


# Prompt the user for the base and height of the triangle
read -p "Enter the base of the triangle: " base
read -p "Enter the height of the triangle: " height
area=$(awk "BEGIN {printf \"%.2f\", 0.5 * $base * $height}")
echo "The area of the triangle with base $base and height $height is
$area."
_________________
#include <stdio.h>
#include <stdlib.h>

#define MAX_REQUESTS 100

// Function to compare two integers (for qsort)


int compare(const void *a, const void *b) {
return ((int)a - (int)b);
}

// Function to calculate the total head movement using SCAN scheduling


int calculateSCAN(int requests[], int numRequests, int head, int
diskSize) {
int totalHeadMovement = 0;
int i;
int *sortedRequests = (int *)malloc(numRequests * sizeof(int));

// Copy requests to a new array and sort them


for (i = 0; i < numRequests; i++) {
sortedRequests[i] = requests[i];
}
qsort(sortedRequests, numRequests, sizeof(int), compare);

// Find the direction of the head movement


int direction = (head < sortedRequests[numRequests - 1]) ? 1 : -1;

// Move in the direction of increasing cylinder numbers


if (direction == 1) {
// Move right to the end of the disk
int lastIndex = numRequests - 1;
while (lastIndex >= 0 && sortedRequests[lastIndex] > head) {
totalHeadMovement += sortedRequests[lastIndex] - head;
head = sortedRequests[lastIndex];
lastIndex--;
}
// Move to the end of the disk
if (head < diskSize - 1) {
totalHeadMovement += (diskSize - 1) - head;
head = diskSize - 1;
}
// Move left to the start of the disk
for (i = 0; i <= lastIndex; i++) {
totalHeadMovement += head - sortedRequests[i];
head = sortedRequests[i];
}
} else {
// Move left to the start of the disk
int firstIndex = 0;
while (firstIndex < numRequests && sortedRequests[firstIndex] <
head) {
totalHeadMovement += head - sortedRequests[firstIndex];
head = sortedRequests[firstIndex];
firstIndex++;
}
// Move to the start of the disk
if (head > 0) {
totalHeadMovement += head;
head = 0;
}
// Move right to the end of the disk
for (i = firstIndex; i < numRequests; i++) {
totalHeadMovement += sortedRequests[i] - head;
head = sortedRequests[i];
}
}

free(sortedRequests);
return totalHeadMovement;
}

int main() {
int numRequests, head, diskSize, i;

// Input number of requests


printf("Enter the number of requests: ");
scanf("%d", &numRequests);

// Ensure we don't exceed maximum allowed requests


if (numRequests > MAX_REQUESTS) {
printf("Number of requests exceeds maximum allowed (%d).\n",
MAX_REQUESTS);
return 1;
}

int requests[MAX_REQUESTS];

// Input the disk size


printf("Enter the disk size (number of cylinders): ");
scanf("%d", &diskSize);

// Input the current head position


printf("Enter the current head position: ");
scanf("%d", &head);

// Input the requests


printf("Enter the requests (cylinders):\n");
for (i = 0; i < numRequests; i++) {
printf("Request[%d]: ", i);
scanf("%d", &requests[i]);
}

// Calculate and print the total head movement


int totalMovement = calculateSCAN(requests, numRequests, head,
diskSize);
printf("Total head movement: %d cylinders\n", totalMovement);

return 0;
}
_______________----------

#!/bin/bash
echo "enter the radius of the circle : "
read radius
pi=3.141
circum=$(awk "BEGIN{print "$pi*$radius*2"}")
echo "circumference:$circum"

______________
#include<stdio.h>
void main()
{
int frag[10],block[10],process[10],np,nb,i,j;
int p,temp;
printf("\nenter total no of blocks : \n");
scanf("%d",&nb);
printf("\nenter size of each block : \n");
for(i=1;i<=nb;i++)
{
printf("block %d \n: ",i);
scanf("%d",&block[i]);
frag[i]=block[i];
}
printf("\nenter total no of process : \n");
scanf("%d",&np);
printf("\nenter size of each process : \n");
for(i=1;i<=np;i++)
{
printf("process %d \n: ",i);
scanf("%d",&process[i]);
}
printf("\npros_no pros_sz bk_no bk_actual_sz bk_avail_sz
fragmentation\n");
for(i=1;i<=np;i++)
{
for(j=1;j<=nb;j++)
{
if(frag[j]>process[i])
{
p=j;
break;
}
}
if(frag[p]>process[i])
{
temp=frag[p];
frag[p]=frag[p]-process[i];
printf("\n %d \t %d \t %d \t %d \t\t %d \t
%d\n ",i,process[i],p,block[p],temp,frag[p]);
}
else
{
printf("\n %d \t %d \t\t process has to wait \n
",i,process[i]);
}
}
printf("\n");
}
________________
#include <stdio.h>
#include <stdlib.h>

#define MAX_REQUESTS 100

// Function to compare two integers (for qsort)


int compare(const void *a, const void *b) {
return ((int)a - (int)b);
}

// Function to calculate the total head movement using CLOOK scheduling


int calculateCLOOK(int requests[], int numRequests, int head, int
diskSize) {
int totalHeadMovement = 0;
int i;
int *sortedRequests = (int *)malloc(numRequests * sizeof(int));

// Copy requests to a new array and sort them


for (i = 0; i < numRequests; i++) {
sortedRequests[i] = requests[i];
}
qsort(sortedRequests, numRequests, sizeof(int), compare);
// Find the starting point in the sorted requests
int startIndex = 0;
while (startIndex < numRequests && sortedRequests[startIndex] < head)
{
startIndex++;
}

// Move to the end of the disk (if needed) and wrap around
if (startIndex < numRequests) {
// Move to the first request greater than or equal to head
totalHeadMovement += sortedRequests[startIndex] - head;
head = sortedRequests[startIndex];
startIndex++;
}

// Move to the end of the list and wrap around


for (i = startIndex; i < numRequests; i++) {
totalHeadMovement += sortedRequests[i] - head;
head = sortedRequests[i];
}

// Wrap around to the beginning of the list if necessary


if (startIndex > 0) {
totalHeadMovement += sortedRequests[0] + diskSize - head;
}

free(sortedRequests);
return totalHeadMovement;
}

int main() {
int numRequests, head, diskSize, i;

// Input number of requests


printf("Enter the number of requests: ");
scanf("%d", &numRequests);

// Ensure we don't exceed maximum allowed requests


if (numRequests > MAX_REQUESTS) {
printf("Number of requests exceeds maximum allowed (%d).\n",
MAX_REQUESTS);
return 1;
}

int requests[MAX_REQUESTS];

// Input the disk size


printf("Enter the disk size (number of cylinders): ");
scanf("%d", &diskSize);

// Input the current head position


printf("Enter the current head position: ");
scanf("%d", &head);
// Input the requests
printf("Enter the requests (cylinders):\n");
for (i = 0; i < numRequests; i++) {
printf("Request[%d]: ", i);
scanf("%d", &requests[i]);
}

// Calculate and print the total head movement


int totalMovement = calculateCLOOK(requests, numRequests, head,
diskSize);
printf("Total head movement: %d cylinders\n", totalMovement);

return 0;
}
______________
# Loop to get 5 numbers from the user
for i in {1..5}; do
read -p "Enter number $i: " number
fact=$(factorial $number)
echo "Factorial of $number is $fact"
done
____________________
#include<stdio.h>
#include<stdlib.h>
struct dir
{
char name[10];
int indexblock;
int ind[8];
}
main()
{
struct dir f[10];
int fa[32],tmp,n,r1,r,len,i,j;
for(i=0;i<32;i++)
fa[i]=-1;
//printf("Create Directory\n");
printf("Enter no of files to be loaded\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter file name\n");
scanf("%s",f[i].name);
printf("Enter No. of blocks\n");
scanf("%d",&len);
while(1)
{
r=rand()%32;
if(fa[r]==-1)
{
fa[r]=-10;
f[i].indexblock=r;
for(j=0;j<8;j++)
f[i].ind[j]=-1;
tmp=0;
break;
}
}
while(1)
{
r=rand()%32;
if(fa[r]==-1)
{
fa[r]=i;
f[i].ind[tmp]=r;
tmp++;
if(tmp==len)
break;
}
}
}
printf("\n\nfile\t Index block\n");
for(i=0;i<n;i++)
printf("\n%s\t%d",f[i].name,f[i].indexblock);
//printf("\n\n\n indexblock");
for(i=0;i<n;i++)
{
printf("\n\n index block%d\n",f[i].indexblock);
for(j=0;j<8;j++)
printf("\t%d",f[i].ind[j]);
}
}
____________________
#include <stdio.h>
#include <stdlib.h>

#define MAX_REQUESTS 100

// Function to compare two integers (for qsort)


int compare(const void *a, const void *b) {
return ((int)a - (int)b);
}

// Function to calculate the total head movement using C-SCAN scheduling


int calculateCSCAN(int requests[], int numRequests, int head, int
diskSize) {
int totalHeadMovement = 0;
int i;
int *sortedRequests = (int *)malloc(numRequests * sizeof(int));

// Copy requests to a new array and sort them


for (i = 0; i < numRequests; i++) {
sortedRequests[i] = requests[i];
}
qsort(sortedRequests, numRequests, sizeof(int), compare);

// Find the starting point in the sorted requests


int startIndex = 0;
while (startIndex < numRequests && sortedRequests[startIndex] < head)
{
startIndex++;
}

// Move to the end of the disk and wrap around


if (startIndex < numRequests) {
// Move to the first request greater than or equal to head
totalHeadMovement += sortedRequests[startIndex] - head;
head = sortedRequests[startIndex];
startIndex++;
} else {
// Move to the end of the disk from the current head position
totalHeadMovement += (diskSize - 1 - head);
head = 0;
}

// Move to the start of the list and wrap around


for (i = startIndex; i < numRequests; i++) {
totalHeadMovement += sortedRequests[i] - head;
head = sortedRequests[i];
}

// Wrap around to the beginning of the disk


if (startIndex > 0) {
totalHeadMovement += (diskSize - 1 - sortedRequests[numRequests -
1]);
}

free(sortedRequests);
return totalHeadMovement;
}

int main() {
int numRequests, head, diskSize, i;

// Input number of requests


printf("Enter the number of requests: ");
scanf("%d", &numRequests);

// Ensure we don't exceed maximum allowed requests


if (numRequests > MAX_REQUESTS) {
printf("Number of requests exceeds maximum allowed (%d).\n",
MAX_REQUESTS);
return 1;
}

int requests[MAX_REQUESTS];

// Input the disk size


printf("Enter the disk size (number of cylinders): ");
scanf("%d", &diskSize);
// Input the current head position
printf("Enter the current head position: ");
scanf("%d", &head);

// Input the requests


printf("Enter the requests (cylinders):\n");
for (i = 0; i < numRequests; i++) {
printf("Request[%d]: ", i);
scanf("%d", &requests[i]);
}

// Calculate and print the total head movement


int totalMovement = calculateCSCAN(requests, numRequests, head,
diskSize);
printf("Total head movement: %d cylinders\n", totalMovement);

return 0;
}
_______________
#include <stdio.h>
#include <stdbool.h>

#define MAX 10 // Maximum number of processes or resources

int processes, resources; // Number of processes and resources

// Function to find if a system is in a safe state


bool isSafe(int need[MAX][MAX], int allot[MAX][MAX], int avail[MAX], int
safeSeq[MAX]) {
int work[MAX], finish[MAX];
int i, j, count = 0;
bool found;

// Initialize work and finish


for (i = 0; i < resources; i++)
work[i] = avail[i];
for (i = 0; i < processes; i++)
finish[i] = 0;

// Find a safe sequence


while (count < processes) {
found = false;
for (i = 0; i < processes; i++) {
if (!finish[i]) {
// Check if the process can be executed
int j;
for (j = 0; j < resources; j++)
if (need[i][j] > work[j])
break;

if (j == resources) {
// Add the resources allocated to the process to work
for (j = 0; j < resources; j++)
work[j] += allot[i][j];
safeSeq[count++] = i;
finish[i] = 1;
found = true;
}
}
}

// If no process was found, the system is not in a safe state


if (!found)
return false;
}

return true;
}

int main() {
int i, j;

// Input number of processes and resources


printf("Enter the number of processes: ");
scanf("%d", &processes);
printf("Enter the number of resources: ");
scanf("%d", &resources);

int max[MAX][MAX], allot[MAX][MAX], avail[MAX];


int need[MAX][MAX], safeSeq[MAX];

// Input the maximum resources matrix


printf("Enter the maximum resource matrix:\n");
for (i = 0; i < processes; i++) {
for (j = 0; j < resources; j++) {
printf("Max[%d][%d]: ", i, j);
scanf("%d", &max[i][j]);
}
}

// Input the allocated resources matrix


printf("Enter the allocated resource matrix:\n");
for (i = 0; i < processes; i++) {
for (j = 0; j < resources; j++) {
printf("Allot[%d][%d]: ", i, j);
scanf("%d", &allot[i][j]);
}
}

// Input the available resources


printf("Enter the available resources:\n");
for (i = 0; i < resources; i++) {
printf("Avail[%d]: ", i);
scanf("%d", &avail[i]);
}

// Calculate the need matrix


for (i = 0; i < processes; i++) {
for (j = 0; j < resources; j++) {
need[i][j] = max[i][j] - allot[i][j];
}
}

// Check system's safety


if (isSafe(need, allot, avail, safeSeq)) {
printf("System is in a safe state.\n");
printf("Safe sequence is: ");
for (i = 0; i < processes; i++)
printf("P%d ", safeSeq[i]);
printf("\n");
} else {
printf("System is not in a safe state.\n");
}

return 0;
}
____________
echo "enter first number"
read a
echo "enter second number"
read b
if [ $a -gt $b ]
then
echo $a "is greatest "
else
echo $b "is greatest "
_____________________
#include<stdio.h>
struct dir
{
char dname[5];
int no;
char fn[5][10];
}d[5];

void get(int n)
{
int i,j;
for(i=0;i<n;i++)
{
printf("Enter the %d directory name : ",i+1);
scanf("%s",d[i].dname);
printf("\nEnter the no. of files in that directory : ");
scanf("%d",&d[i].no);
for(j=0;j<d[i].no;j++)
{
printf("\nEnter %d file name : ",j+1);
scanf("%s",d[i].fn[j]);
}

}
}

void display(int n)
{
int i,j;

for(i=0;i<n;i++)
{
printf("%s\n",d[i].dname);
for(j=0;j<d[i].no;j++)
{
printf("\t%s\n",d[i].fn[j]);
}
}
}

void main()
{
int n;
clrscr();
printf("Enter the no. of directories :\n");
scanf("%d",&n);
get(n);
display(n);
getch();
}

_________________
******************************************************

You might also like