OS Lab File
OS Lab File
AIM: To Write a Shell program to check the given number is even or odd.
ALGORITHM:
SEPT 1: Start the program.
STEP 2: Read the value of n.
STEP 3: Calculate „r=expr $n%2‟.
STEP 4: If the value of r equals 0 then print the number is even
STEP 5: If the value of r not equal to 0 then print the number is odd.
PROGRAM:
echo "Enter the Number"
read n
r=`expr $n % 2`
if [ $r -eq 0 ]
then
echo "$n is Even number"
else
echo "$n is Odd number"
fi
Output:
EXPERIMENT NO-3
AIM: Write a Shell program to swap the two integers.
ALGORITHM:
SEPT 1: Start the program.
STEP 2: Read the value of a,b.
STEP 3: Calculate the swapping of two values by using a temporary variable temp.
STEP 4: Print the value of a and b.
PROGRAM:
echo "Enter Two Numbers"
read a b
temp=$a
a=$b
b=$temp
echo "after swapping"
echo $a $b
Output:
EXPERIMENT NO-4
Aim: Write a Shell program to find the factorial of a number
ALGORITHM:
PROGRAM:
echo "Enter a Number"
read n i=`expr $n - 1`
p=1
while [ $i -ge 1 ]
do
n=`expr $n \* $i`
i=`expr $i - 1`
done
echo "The Factorial of the given Number is $n"
Output:
EXPERIMENT NO-5
Aim: write a shell program to generate fibonacci series
Algorithm:
Step1: Define how many terms (n) of the Fibonacci series you want to generate.
Step2: Set first = 0 and second = 1 as the initial two Fibonacci numbers.
Step3:
Print the current Fibonacci number.
Calculate the next number: next = first + second.
Update values: first = second, second = next.
Step4: Repeat until the required n terms are generated.
Program:
first=0
second=1
echo
Output:
EXPERIMENT NO-6
Aim: Write a C program using the following system calls (fork, exec).
Algorithm:
1. Initialize the program and define pid_t variable for process management.
2. Invoke fork() to create a child process.
o If fork() fails, print an error.
o If fork() succeeds, identify whether the process is child or parent.
3. In the child process:
o Print a message indicating execution inside WSL.
o Call execl() to replace the child process with ls -l.
o Handle errors if exec() fails.
4. In the parent process:
o Print a message indicating process tracking.
o Use wait() to synchronize execution.
o Print completion status.
Program:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
pid_t pid;
if (pid < 0) {
// Error handling
perror("Fork failed");
return 1;
}
else if (pid == 0) {
// Child process
printf("Child process running in WSL with PID %d\n", getpid());
// If exec fails
perror("Exec failed");
return 1;
}
else {
// Parent process
printf("Parent process running in WSL with PID %d, waiting for child process to finish\n",
getpid());
return 0;
}
Output:
EXPERIMENT NO-7
Aim: Write a C program using the following system calls (get_pid, exit).
Algorithm:
1. Start
2. Retrieve Process ID
o Use the system call getpid() to obtain the current process's PID.
o Print the process ID.
3. Prepare for termination
o Print a message indicating that the process is exiting.
4. Terminate the process
o Use the system call exit(0) to exit immediately.
5. End
Program:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main() {
printf("Process running with PID: %d\n", getpid());
Output:
EXPERIMENT NO-8
Aim: Write a C program using the I/O system calls (open, read, write, etc).
Program:
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
int main() {
int source_fd, dest_fd;
char buffer[BUFFER_SIZE];
ssize_t bytes_read;
// Close files
close(source_fd);
close(dest_fd);
return 0;
}
Output:
EXPERIMENT NO- 9
Aim: Write a C program to simulate CPU scheduling algorithms: FCFS, SJF, and Round
Robin
a)FCFS:
Program:
#include <stdio.h>
void findTurnaroundTime(int processes[], int n, int bt[], int wt[], int tat[]) {
for (int i = 0; i < n; i++) {
tat[i] = bt[i] + wt[i];
}
}
int main() {
int processes[] = {1, 2, 3}; // Process IDs
int n = sizeof(processes) / sizeof(processes[0]);
int bt[] = {5, 8, 6}; // Burst time of processes
printf("FCFS Output\n");
findAvgTime(processes, n, bt);
return 0;
}
Output:
b)SJF:
Program:
#include <stdio.h>
int main() {
int processes[] = {1, 2, 3}; // Process IDs
int n = sizeof(processes) / sizeof(processes[0]);
int bt[] = {8, 5, 6}; // Burst time of processes (unsorted)
printf("SJF Output\n");
findAvgTime(n, processes, bt);
return 0;
}
Output:
c)Round Robin:
Program:
#include <stdio.h>
while (1) {
int done = 1;
for (int i = 0; i < n; i++) {
if (rem_bt[i] > 0) {
done = 0;
if (rem_bt[i] > quantum) {
t += quantum;
rem_bt[i] -= quantum;
} else {
t += rem_bt[i];
wt[i] = t - bt[i];
rem_bt[i] = 0;
}
}
}
if (done) break;
}
}
int main() {
int processes[] = {1, 2, 3}; // Process IDs
int n = sizeof(processes) / sizeof(processes[0]);
int bt[] = {5, 8, 6}; // Burst time of processes
int quantum = 3; // Time Quantum
return 0;
}
Output:
EXPERIMENT NO-10
Aim: Implement the Producer-Consumer problem using semaphores.
Program:
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
int buffer[BUFFER_SIZE];
int count = 0; // Number of items in buffer
buffer[count++] = i;
printf("Producer produced: %d\n", i);
pthread_mutex_unlock(&mutex);
sem_post(&full); // Signal consumer
sleep(1);
}
return NULL;
}
pthread_mutex_unlock(&mutex);
sem_post(&empty); // Signal producer
sleep(2);
}
return NULL;
}
int main() {
pthread_t prod, cons;
sem_init(&empty, 0, BUFFER_SIZE);
sem_init(&full, 0, 0);
pthread_mutex_init(&mutex, NULL);
pthread_join(prod, NULL);
pthread_join(cons, NULL);
sem_destroy(&empty);
sem_destroy(&full);
pthread_mutex_destroy(&mutex);
return 0;
}
Output:
EXPERIMENT NO-11
Aim: Implement the Dining Philospher problem using semaphores.
Program:
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
while (1) {
printf("Philosopher %d is thinking\n", id);
sleep(1);
// Pick up forks
sem_wait(&mutex); // Prevent deadlock
sem_wait(&forks[id]); // Pick left fork
sem_wait(&forks[(id + 1) % N]); // Pick right fork
sem_post(&mutex);
int main() {
pthread_t philosophers[N];
int ids[N];
sem_init(&mutex, 0, 1);
for (int i = 0; i < N; i++)
sem_init(&forks[i], 0, 1);
return 0;
}
Output: