0% found this document useful (0 votes)
77 views25 pages

Index:: NAME - Yash Chowdhary ID - 201003003001 Batch - Bcs3D - Ai Subject - Operating System

The document contains details about 4 modules related to Operating Systems. Module 1 discusses processes and process management using fork() system calls. Module 2 simulates different scheduling algorithms like FCFS, SJF, Priority Scheduling and Round Robin. Module 3 discusses classical process synchronization problems like Producer-Consumer using semaphores. Module 4 provides shell script examples.

Uploaded by

Piyush Kumar
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)
77 views25 pages

Index:: NAME - Yash Chowdhary ID - 201003003001 Batch - Bcs3D - Ai Subject - Operating System

The document contains details about 4 modules related to Operating Systems. Module 1 discusses processes and process management using fork() system calls. Module 2 simulates different scheduling algorithms like FCFS, SJF, Priority Scheduling and Round Robin. Module 3 discusses classical process synchronization problems like Producer-Consumer using semaphores. Module 4 provides shell script examples.

Uploaded by

Piyush Kumar
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/ 25

NAME – Yash Chowdhary

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>

#include <stdlib.h> int

main()

int f=fork();

if(f==0)

printf(“Child Process”);

else

printf(“Parent Process”);
}
}

Output :

Parent ProcessChild Process

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;

printf("Enter the number of times the loop should run: \n");

scanf("%d", &c);

int pid;

pid = fork();

if (pid == 0) {

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

printf("Child Process\n");

else {

for (j = 0; j < c; j++) { printf("Parent

Process\n");

wait(NULL);

return 0;
}

Output:

Enter the number of times the loop should run:

Parent Process

Parent Process Parent

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>

#include <unistd.h> void

forkedProcess()

printf("Child Process\n");

int main()

int pid = fork(); if

(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>

#include <unistd.h> #include

<sys/wait.h> int main(int argc,

char *argv[]) { pid_t pid =

fork();

if (pid < 0) {

fprintf(stderr, "Fork Failed"); return

1;
}

else if (pid == 0) { printf("I'm

the child \n");

else { wait(NULL);

printf("Child Complete \n");

Output:

I'm the child

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>

#include <sys/wait.h> #include

<stdlib.h>

int main(int argc, char *argv[])

printf("I am: %d\n", (int) getpid());

pid_t pid = fork(); printf("fork

returned: %d\n", (int) pid); if (pid < 0)

perror("Fork failed");
}

if (pid == 0) {

printf("child with pid %d\n", (int) getpid()); printf("Child

process is exiting\n");

exit(0); }

printf("Parent waiting for the child process to end\n"); wait(NULL);

printf("parent process is exiting\n"); return(0);

}
Output :

I am: 874

fork returned: 878

Parent waiting for the child process to end fork

returned: 0

child with pid 878

Child process is exiting parent

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 :

Processes Burst time Waiting time Turn around time

1 21 0 21
2 3 21 24

3 6 24 30

4 2 30 32

ii) Shortest Job First :

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 :

Enter Total Number of Process:4

Enter Burst Time and Priority


P[1]

Burst Time:3

Priority:1

Process Burst Time Waiting Time Turnaround Time

P[32636] 32636 0 32636

P[1] 3 32636 32639

P[32766] -32952246 32639 -32919607

P[-436326864] 2105812480 -32919607 2072892873

Average Waiting Time=-8213583

Average Turnaround Time=510009635


iv) Round Robin Algorithm :

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 Total Process: 3

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

Process |Turnaround Time|Waiting Time

P[2] | 6 | 1

P[3] | 5 | 3

P[1] | 9 | 3

Average Waiting Time= 2.333333

Avg Turnaround Time = 6.666667

Module III: Simulation on


Classical Process
Synchronization Algorithm
i) Write a program to implement the Producer Consumer problem using Semaphore. Show
the programming steps, input and output parameters.

Algorithm :

1. Create two semaphore objects, one for the producer and one for the consumer.

2. Initialize the producer semaphore to 1 and the consumer semaphore to 0.

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:

#include <stdio.h> #include

<stdlib.h>

int a = 1; int

b = 0;

int c = 10, s = 0;
void producer()

--a;

++b;
--c;

s++; printf("\nProducer

produces" "item %d",

s);

++a;

void consumer()

--a;

--b;

++c;

printf("\nConsumer consumes "

"item %d",

s); s--;

++a;

int main()

int n, i;

printf("\n1. Press 1 for Producer" "\n2.

Press 2 for Consumer"

"\n3. Press 3 for Exit"); #pragma

omp critical

for (i = 1; i > 0; i++) {


printf("\nEnter your choice:"); scanf("%d",

&n);

switch (n) {case 1:

if ((a == 1)

&& (c != 0)) { producer();

}
else
{

printf("Buffer is full!");

break;
case 2:

if ((a == 1)

&& (b != 0)) { consumer();

else {

printf("Buffer is empty!");

break;
case
3: exit(0);

break;
}

}
Output :

1. Press 1 for Producer

2. Press 2 for Consumer

3. Press 3 for Exit

Enter your choice:1

Producer producesitem 1

Enter your choice:2

Consumer consumes item 1

Enter your choice:3


ii) Show the programming steps (Only the Algorithm) to implement the Dining
Philosopher’s Problem

Algorithm :

1. Create an array of dining philosophers (each philosopher will have an ID).

2. Create a semaphore with an initial value of n - 1 where n is the number of philosophers.

3. For each philosopher:

a. Pick up the left fork.

b. Wait until the semaphore is available.

c. When the semaphore is available, pick up the right fork.

d. Eat for a specified amount of time.

e. Put down the left and right forks.

f. Release the semaphore.

4. Repeat from step 3 until all philosophers have eaten.


Module IV: Basic Linux
Commands and Shell
Programming
i) Using suitable example list the details about following linux commands

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 :

head [OPTION]... [FILE]...

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:

tail [OPTION]... [FILE]...

chmod : In Unix-like operating systems, the chmod command is used to change the access mode
of a file.

The name is an abbreviation of change mode.

Syntax :

chmod [reference][operator][mode] file...

chown : chown command is used to change the file Owner or group. Whenever you want to
change ownership you can use chown command.

Syntax:

chown [OPTION]… [OWNER][:[GROUP]] FILE…


chown [OPTION]… –reference=RFILE FILE…

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:

grep [options] pattern [files]

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:

gunzip [Option] [archive name/file name]

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 :

diff [options] File1 File2

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 :

$uniq [OPTION] [INPUT[OUTPUT]]

ii) Write a Shell Script to find out the biggest number in 3 numbers – If – elif block
Algorithm :

1. Get three numbers. Say num1, num2, num2

2. If (num1 > num2) and (num1 > num3) echo value of num1

3. elif(num2 > num1) and (num2 > num3) echo value of num2

4. Otherwise,

echo value of num3

Program :

echo "Enter Num1"

read num1 echo

"Enter Num2" read

num2 echo "Enter

Num3"

read num3

if [ $num1 -gt $num2 ] && [ $num1 -gt $num3 ]


then

echo $num1

elif [ $num2 -gt $num1 ] && [ $num2 -gt $num3 ] then

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 :

1. Read Two Numbers

2. Input Choice (1-Addition, 2-Subtraction, 3-Multiplication, 4-Division)

3.if Choice equals 1

Calculate res = a + b

else If Choice equals 2

Calculate res = a - b else

if Choice equals 3

Calculate res = a * b else

if Choice equals 4

Calculate res = a / b

4. Output Result, res

Program : #!/bin/bash echo

"Enter two numbers: " read

a read b
echo "Enter your choice: " echo

"1. Addition"

echo "2. Subtraction"

echo "3. Multiplication"

echo "4. Division"

read ch
case $ch in

1) res=`echo $a + $b | bc`

;;

2) res=`echo $a - $b | bc`

;;

3) res=`echo $a \* $b | bc`

;;

4) res=`echo "scale=2; $a / $b" | bc`

;;

esac

echo "Result : $res"

Output :

Enter two numbers : 3

Enter your choice : 3

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

for j in `seq 0 $i`

do

4) Calculate the value of each element


c=`expr $i \* \( $i - 1 \) / 2 + $j`

5) Print the elements of the triangle


echo -n "$c "

done

echo ""

done

Program :

#!/bin/bash

#generate pascal triangle

echo -n "Enter the number of Row " read

NR

typeset -A arr

#declare -a arr for i in

`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

for ((i=0;i<=$NR;i++));do for((j=0;j<=$i;j++))

do

echo -n ${arr[$i,$j]} " "

done

printf "\n"

done

Output :

11

121

1331

1464

You might also like