Index:: NAME - Yash Chowdhary ID - 201003003001 Batch - Bcs3D - Ai Subject - Operating System
Index:: NAME - Yash Chowdhary ID - 201003003001 Batch - Bcs3D - Ai Subject - Operating System
ID – 201003003001
BATCH – BCS3D_AI
SUBJECT – OPERATING SYSTEM
INDEX:
Module 1
i) Program to show the execution of fork() system call in respect to parent and child
processes.
ii) Program for showing the choice of child process and then parent process using loop.
iii) Program to call a function from the main function. The called function contains a
single fork(). iv) Program to show the execution of Parent process until the Child
process complete its execution
v) Program to show the execution of Parent process until the Child process complete its
execution. Print process ids to check.
Module 2
i) First Come First Serve
ii) Shortest Job First
iii) Priority Scheduling
iv) Round Robin
Module 3
i. Program to implement the Producer Consumer problem using Semaphore.
ii. Programming steps (Only the Algorithm) to implement the Dining Philosopher’s Problem.
Module 4
i) Details about various linux commands.
ii) Shell Script to find out the biggest number in 3 numbers – If – elif block.
iii) Shell Script to perform the Arithmetic Operations (summation, subtraction,
multiplication and division) of two numbers – using Switch.
iv) Shell Script to print the Pascal triangle – using for loop.
Module I: Process
Management using fork()
system call
i) Write a program to show the execution of fork() system call in respect to parent and child
processes.
Program :
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
main()
int f=fork();
if(f==0)
printf(“Child Process”);
else
printf(“Parent Process”);
}
}
Output :
ii) Write a Program for showing the choice of child process and then parent process using
loop.
Program :
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main()
{
int j, c;
scanf("%d", &c);
int pid;
pid = fork();
if (pid == 0) {
printf("Child Process\n");
else {
Process\n");
wait(NULL);
return 0;
}
Output:
Parent Process
Process
Parent Process
Child Process
Child Process
Child Process
Child Process
iii) Write a program to call a function from the main function. Note: the called function
contain a single fork(). Explain the output
Program :
#include <stdio.h>
#include <sys/types.h>
forkedProcess()
printf("Child Process\n");
int main()
(pid == 0)
{
forkedProcess();
} else
printf("Parent Process\n");
return 0;
Output:
Parent Process
Since only one fork() is contained when calling the required function from the main function,
the Parent Process interfers with the Child Process which creates a base for the called
function and this output is obtained.
iv) Write a Program to show the execution of Parent process until the Child
process complete its execution Program :
#include <sys/types.h>
#include <sys/types.h>
#include <stdio.h>
fork();
if (pid < 0) {
1;
}
else { wait(NULL);
Output:
Child Complete
v) Write a Program to show the execution of Parent process until the Child
process complete its execution. Print process ids to check. Program:
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
<stdlib.h>
perror("Fork failed");
}
if (pid == 0) {
process is exiting\n");
exit(0); }
}
Output :
I am: 874
returned: 0
process is exiting
Module II: Simulation of
scheduling algorithms
i) FCFS :
Algorithm:
I) Start
II) Create a queue to store tasks that need to be completed.
III) Receive a new task and add it to the end of the queue.
IV) Take the task at the front of the queue and start processing it.
V) Once the task is finished, remove it from the queue and move on to the next task. VI)
Repeat steps 3 and 4 until all tasks are completed. VII) End
Output :
1 21 0 21
2 3 21 24
3 6 24 30
4 2 30 32
Algorithm :
1. Start
2. Begin by listing all of the jobs that need to be completed.
3. Assign priority numbers to each job, with the smallest number representing the highest
priority.
4. Determine the amount of time required to complete each job.
5. Select the job with the lowest priority number and the shortest amount of time required to
complete it.
6. Execute the job and remove it from the list.
7. Assign a new priority number to the remaining jobs.
8. Repeat steps 4 - 6 until all jobs are completed.
Output:
Process Burst Time Waiting Time Turnaround Time
p1 4 0 4
p2 5 4 9
p3 6 9 15
p4 7 15 22
iii) Priority Scheduling Algorithm :
1. Start
2. Create a queue that will hold all of the processes.
3. Assign priority numbers to each process according to their importance.
4. Select the process with the highest priority and execute it.
5. Remove the process from the queue.
6. Repeat steps 3 and 4 until all processes have been executed.
7. End
Output :
Burst Time:3
Priority:1
1. Start
2. Initialize a queue with the list of processes and their respective arrival time.
3. Set a time quantum (time slice) for each process.
4. Start running the first process in the queue.
5. Decrement the time quantum of the process.
6. When the time quantum reaches 0, move the process to the end of the queue and start
running the next process in the queue.
7. Repeat steps 3-5 until all the processes are completed.
8. Calculate the turnaround time and waiting time for each process.
Output:
Enter Arrival Time and Burst Time for Process Process Number 1 :4
Enter Arrival Time and Burst Time for Process Process Number 2 :4
Enter Arrival Time and Burst Time for Process Process Number 3 :7 2
Enter Time Quantum: 5
P[2] | 6 | 1
P[3] | 5 | 3
P[1] | 9 | 3
Algorithm :
1. Create two semaphore objects, one for the producer and one for the consumer.
3. The producer thread will wait on the producer semaphore and the consumer thread will wait
on the consumer semaphore.
4. The producer thread will produce the data and post it to a shared buffer.
5. The producer thread will then signal the consumer thread by releasing the consumer
semaphore.
6. The consumer thread will then consume the data from the shared buffer.
7. The consumer thread will then signal the producer thread by releasing the producer
semaphore.
8. Steps 4-7 will repeat until there is no more data to be produced or consumed.
Program:
<stdlib.h>
int a = 1; int
b = 0;
int c = 10, s = 0;
void producer()
--a;
++b;
--c;
s++; printf("\nProducer
s);
++a;
void consumer()
--a;
--b;
++c;
"item %d",
s); s--;
++a;
int main()
int n, i;
omp critical
&n);
if ((a == 1)
}
else
{
printf("Buffer is full!");
break;
case 2:
if ((a == 1)
else {
printf("Buffer is empty!");
break;
case
3: exit(0);
break;
}
}
Output :
Producer producesitem 1
Algorithm :
ls : ls is a Linux shell command that lists directory contents of files and directories
Cat : Cat(concatenate) command is very frequently used in Linux. It reads data from the file
and gives their content as output. It helps us to create, view, concatenate files. Syntax :
$cat filename
Head : The head command, as the name implies, print the top N number of data of the given input.
By default, it prints the first 10 lines of the specified files. If more than one file name is provided
then data from each file is preceded by its file name.
Syntax :
Tail : The tail command, as the name implies, print the last N number of data of the given input. By
default it prints the last 10 lines of the specified files. If more than one file name is provided then
data from each file is precedes by its file name.
Syntax:
chmod : In Unix-like operating systems, the chmod command is used to change the access mode
of a file.
Syntax :
chown : chown command is used to change the file Owner or group. Whenever you want to
change ownership you can use chown command.
Syntax:
grep : The grep filter searches a file for a particular pattern of characters, and displays all lines that
contain that pattern. The pattern that is searched in the file is referred to as the regular expression
(grep stands for global search for regular expression and print out). Syntax:
wc : wc stands for word count. As the name implies, it is mainly used for counting purpose.
It is used to find out number of lines, word count, byte and characters count in the files specified in
the file arguments.
Syntax:
wc [OPTION]... [FILE]...
gunzip : gunzip command is used to compress or expand a file or a list of files in Linux. It accepts
all the files having extension as .gz, .z, _z, -gz, -z , .Z, .taz or.tgz and replace the compressed file
with the original file by default. The files after uncompression retain its actual extension.
Syntax:
diff : diff stands for difference. This command is used to display the differences in the files by
comparing the files line by line. Unlike its fellow members, cmp and comm, it tells us which lines in
one file have is to be changed to make the two files identical.
Syntax :
sort : SORT command is used to sort a file, arranging the records in a particular order. By default,
the sort command sorts file assuming the contents are ASCII. Using options in the sort command
can also be used to sort numerically. Syntax :
$ sort filename.txt
uniq : The uniq command in Linux is a command-line utility that reports or filters out the repeated
lines in a file.
In simple words, uniq is the tool that helps to detect the adjacent duplicate lines and also deletes
the duplicate lines. uniq filters out the adjacent matching lines from the input file(that is required
as an argument) and writes the filtered data to the output file.
Syntax :
ii) Write a Shell Script to find out the biggest number in 3 numbers – If – elif block
Algorithm :
2. If (num1 > num2) and (num1 > num3) echo value of num1
3. elif(num2 > num1) and (num2 > num3) echo value of num2
4. Otherwise,
Program :
Num3"
read num3
echo $num1
echo $num2
else
echo $num3
fi
Output :
Enter Num1
14
Enter Num2
78
Enter Num3
200
200
iii) Write a Shell Script to perform the Arithmetic Operations (summation, subtraction,
multiplication and division) of two numbers – using Switch
Algorithm :
Calculate res = a + b
if Choice equals 3
if Choice equals 4
Calculate res = a / b
a read b
echo "Enter your choice: " echo
"1. Addition"
read ch
case $ch in
1) res=`echo $a + $b | bc`
;;
2) res=`echo $a - $b | bc`
;;
3) res=`echo $a \* $b | bc`
;;
;;
esac
Output :
Result : 24
iv) Write a Shell Script to print the Pascal triangle – using for loop
Algorithm :
1 )Initialize variables
2 )rows=5
3) Use for loop to print the Pascal triangle for i in `seq 0 $rows` do
do
done
echo ""
done
Program :
#!/bin/bash
NR
typeset -A arr
`seq 0 $NR`;do
arr[$i,0]=1 #start is 1
arr[$i,$i]=1 #end is 1
p=$((i-1)) #for j in
`seq 1 $p`;do for
((j=1;j<$i;j++));do a=$
{arr[$((i-1)),$((j-1))]}
b=${arr[$((i-1)),$j]}
arr[$i,$j]=$((a+b))
done
#echo ${arr[$i]}
done
#print result
do
done
printf "\n"
done
Output :
11
121
1331
1464