+ All Categories
Home > Documents > DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... ·...

DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... ·...

Date post: 15-Aug-2021
Category:
Upload: others
View: 2 times
Download: 0 times
Share this document with a friend
56
Sikkim Manipal University Page No. 1 DBMS and OS Practical Instructor Manual Subjects: BCA 3 rd Sem. BCA3060 DBMS Practical MCA 3 rd Sem. MCA3070 Advanced DBMS Practical B.Sc.(IT) 2 nd Sem. BT0073 OS AND DBMS Practical
Transcript
Page 1: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 1

DBMS and OS Practical

Instructor Manual

Subjects: BCA – 3

rd Sem.

BCA3060 DBMS – Practical

MCA – 3rd

Sem. MCA3070 Advanced DBMS – Practical

B.Sc.(IT) – 2nd

Sem. BT0073 OS AND DBMS – Practical

Page 2: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 2

Contents

1. Introduction 3

2. Objectives 3

3. Software and Hardware Requirements 3

4. Guidelines for conducting Practical Exercises 3

5. Exercises 6

Page 3: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 3

1. Introduction

The exercises given here cover the concepts of handling the different types

of database in different ways. This lab manual is prepared with an intention

of increasing the comfort level of students in Database management system

and Operating System.

The successful completion of these exercises would enable the students to

be comfortable with the skills needed for the implementation of various

techniques of DBMS and Operating System.

2. Objectives

The objective of this manual is to make the student to get hands on

experience regarding the application of theoretical concepts

learnt in Database Management System and Operating System.

3. Software and Hardware Requirements

– Software Requirements: (Minimum)

Microsoft windows/ Linux OS

Oracle 10i

OS (Both MS windows and Linux OS, Windows 7 and Fedora 16 is

preferable, although any version of fedora, MS windows or other

Linux can be used to meet the objective)

Oracle Database 11g Express Edition.(or any other version)

Or database like MySQL(above 5.0), SQL Server

Turbo/Borland C Compiler or equivalent

– Hardware Requirements: (Minimum)

A Personal Computer with:

Pentium PIV Processor

512 MB RAM

120GB Hard disk

4. Guidelines for Conducting Practical Exercises

Guided and Unguided Exercises

Page 4: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 4

This Practical comprises of Guided and Unguided exercises sessions.

During the Guided Exercise session, faculty has to provide guidance to the

student to complete the practical exercise, arrange for demonstration

(wherever necessary), and clarify the doubts. The end semester University

Examination is the unguided session.

This practical subject has to be completed in 4 days (Table 1) (i.e.) 3 ½

days of Guided (Practice session) and on the last day University exam

(Unguided) for 3 hours will be conducted. This doesn’t include the time to be

spent by a student on self-study.

Table 1: Time allotment for practical activities

Type of Activity Day(s) Hours

Guided Exercises 3 ½ 21

Unguided ½ 3

Total 4 24

During the guided sessions a student has to complete a minimum of 70% of

the exercises listed in Table 3 (covering all parts as applicable).

Assessment:

A student is to be assessed for the award of Internal Assessment (IA) marks

(Guided) and also for the end semester exam – University Examination (UE)

marks (Unguided). For details refer below Table – 2(a) and (b)

Table – 2(a): Assessment mark distribution

Program Course Code Credit IA UE Total

BCA/BSc (IT)/ MCA

BCA3060/BT0073/MCA3070 2 70 30 100

Page 5: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 5

Table 2(b): Rubrics for award of IA and UE marks

The assessment during the Guided sessions account for

70 marks (out of a total of 100 marks for 2 credit Practical subject). The

assessment during the unguided sessions will account for 30 marks.

For the conduct of University Examination (UE), the examiner should

use the list of exercises provided in Annexure-1. A student should be

allotted (at random) with any one exercise from the list for the

Unguided session. Student should perform the following:

Write the program objective

Write the source code, compile, execute it and show the output

to the examiner.

Take print of the source code and submit it to the examiner for

evaluation

For successful completion a student has to score a combined

average of 40% (i.e., totally 40 marks) in both Guided and Unguided

parts together and a minimum of 35% in each part (i.e., 25 marks in

Guided part and 11 marks in Unguided part). In case of failure in

either or both the parts, he has to redo the concerned part(s).

Instructions to the Faculty

Faculty should ensure/perform the following

A student is allotted with the computer system.

Demonstrate the program execution.

Parameter Weightage in % Evaluation Component

Attendance 10

IA (70 Marks)

No. of Exercise completed 20

Record Work 70

Total 100

Coding Logic 30

UE (30 Marks)

Compilation 30

Output 20

Viva 20

Total 100

Page 6: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 6

Insist the student to create a folder (with RollNo as the folder

name) in the local drive and store all the exercises, program code

along with sample input and output. This will form the Student

record work.

A student submits the record work (folder created during the

session) to the faculty for the award of IA marks.

Faculty submits the student record book along with the IA marks

to the University.

Faculty should ensure that the student record contains the

following details for each of the exercise.

o Objective: It should contain a few lines of description

(preferably 2 or 3 lines) of the problem being solved and

the methods or procedures followed.

o Program code with sample input and output.

Student has to execute the programs and verify the output. If needed

the student may be allowed to do more exercises in the related

concepts.

5. Exercises

The following is the list of exercises to be completed by the students

Table 3: List of Exercises

Sl. No. Title of the Exercise

Part-A - List of exercises applicable for BCA3060, MCA3070 and BT0073

1. DDL commands-I

2. DML commands-II

3. DML commands-III

4. Functions

5. PL/SQL – Commit, Rollback and Save points

Page 7: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 7

6. PL/SQL – if – case, case expression

7. PL/SQL – Loops and exceptions

8. PL/SQL – Procedures

9. PL/SQL – Stored and Invoke functions

10. PL/SQL – Triggers

11. PL/SQL – Cursor

Part–B - In addition to the above exercises, MCA3070 & BT0073 student has to perform this set of exercises also.

12. Joins

13. View, Union

14. Exception handling

Part–c - In addition to the above exercises, BT0073 student has to perform this set of exercises also.

15. First Come First Served scheduling algorithm

16. The critical-section problem

17. Banker’s Algorithm

18. The Dining Philosopher’s Problem

Page 8: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 8

Exercise 1 DDL Commands-I

1. Write the SQL queries using Data Definition Language (DDL) statements

Objective: To perform this practical need to use the below given DDL

commands and create the following tables

DDL commands: create, alter and drop

Tables: student, student_marksheet, employee, emp_dept and emp_sal

The SQL syntax for table creation are given below

Primary key: The primary key of a relational table uniquely identifies each

record in the table. It can either be a normal attribute that is guaranteed to

be unique (such as Social Security Number in a table with no more than one

record per person) or it can be generated by the DBMS (such as a globally

unique identifier, or GUID, in Microsoft SQL Server). Primary keys may

consist of a single attribute or multiple attributes in combination.

(i) Student table

(a) CREARE TABLE student (student number (int) (primary key),

student name (varchar), student address (varchar), student dob (date

and time), student branch (char).

Create database mydatabase

CREARE TABLE student (student_number int, student_name varchar(30),

student_address varchar(30), student_dob date, student_branch

varchar(30));

(b) CREARE TABLE student_marksheet (student number (int) (primary

key), student sem (int),student total marks (int).

CREARE TABLE table_name (column_name1 data_type, column_name2

data_type, column_name3 data_type…)

(ii) Employee table

(c) Create a table employee with emp number(int) (primary key), emp

name(varchar), emp dob(date and time), emp address(varchar),

empdeptno(int), empsal int.

CREARE TABLE employee (emp_number int, emp_name varchar(30),

emp_dob date, emp_address vrchar(30), emp_deptno int.)

Page 9: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 9

(d) Create a table empdept with Emp number (int) (primary key),

empdeptno(int), dept name(char)

CREARE TABLE emp_dept (emp_number int, empdept_name varchar(30),

emp_deptno int.)

(e) CREARE TABLE emp salary with emp number(int),doj (date),

empsal(big int).

CREARE TABLE table_name (column_name1 data_type, column_name2

data_type, column_name3 data_type…)

(f) Modify the table student mark sheet using the alter command

For modify the table syntax

ALTER TABLE table_name ADD column_name datatype

For modify the table syntax

ALTER TABLE table_name DROP COLUMN column_ name datatype

(g) Remove the table emp salary using drop command

DROP TABLE table_name

table_name

Is the name of the table to be removed.

Page 10: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 10

Exercise 2 DML Commands – II

2. Write the SQL queries using Data Manipulation Language (DML)

statements

(a) Insert values into student table with field names using insert

command

(b) Display the table student using select command

(c) Update student address in student table using update

command.

(d) Delete a row from student table.

Objective: to perform this practical need to use the following DML

commands.

DML commands: insert, select, drop and update.

(a) Insert values into student table using field names using insert

command

Insert into student ((student_number, student_name, student_address,

student_dob, student_branch) values (101, ’kishore’, ’bangalore’,

’20-12-1987’,’cse’))

Insert into student ((student_number, student_name, student_address,

student_dob, student_branch) values (102,’ramesh’,’delhi’,’21-12-1987’,

’ece’))

Insert into student ((student_number, student_name, student_address,

student_dob, student_branch) values (103, ’rahul’, ’chennai’, ’22-12-1987’,

’eee’))

Insert into student ((student_number, student_name, student_address,

student_dob, student_branch) values (104,’naresh’,’hyd’,’23-12-1987’,’cse’))

Insert into student ((student_number, student_name, student_address,

student_dob, student_branch) values (105, ’ramya’, ’chennai’, ’24-12-1987’,

’cse’))

Page 11: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 11

(b) Select the table student using select command

Select * from student

//Student table will display like below

*student_ number

student_ name

student_ address

student_ dob

student_ branch

101 Kishore bangalore 20-12-1987 Cse

102 ramesh delhi 21-12-1987 Ece

103 rahul chennai 22-12-1987 Eee

104 narsh hyd 23-12-1987 Cse

105 ramya chennai 24-12-1987 cse

(c) Update student address in student table using update command.

update student set student_address=’bangalore’ where student_number=

104.

*student_ number

student_ name

student_ address

student_ dob

student_ branch

101 kishore bangalore 20-12-1987 Cse

102 ramesh delhi 21-12-1987 Ece

103 rahul chennai 22-12-1987 Eee

104 narsh bangalore 23-12-1987 Cse

105 ramya chennai 24-12-1987 cse

(d) Delete a row from student table

DELETE FROM student WHERE student_number = "105"

//table will be change like below

*student_ number

student_ name

student_ address

student_dob student_ branch

101 kishore bangalore 20-12-1987 Cse

102 ramesh delhi 21-12-1987 Ece

103 rahul chennai 22-12-1987 Eee

Page 12: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 12

104 narsh bangalore 23-12-1987 Cse

One row deleted

Exercise 3 DML Commands – III

3. Write the SQL queries using Data Manipulation Language (DML)

statements

(a) Insert values into employee table with field names using insert

command

(b) Select the table employee using select command

(c) Update empdept numbers in employee table using update

command.

(d) Delete a row from employee table.

Objective: to perform this practical use the following DML commands.

DML commands: insert, select, drop and update.

(a) Insert values into employee table with field names using insert

command

INSERT INTO employee ((emp_number, emp_name, emp_dob,

emp_address, emp_deptno, emp_sal) values (1000,’naveen’,

’18-07-1985’,’silk board’,555,5000))

INSERT INTO employee (emp_number, emp_name, emp_dob,

emp_address, emp_deptno, emp_sal) values (1001,’sureash’,

’19-07-1985’,’kr puram’, 556, 7000))

INSERT INTO employee ((emp_number, emp_name, emp_dob,

emp_address, emp_deptno, emp_sal) values (1002,’chandu’,’17-07-1985’,’

cvramannagar’, 557, 8000))

INSERT INTO employee (emp_number, emp_name, emp_dob,

emp_address, emp_deptno, emp_sal) values (1003,’ramya’,

’16-07-1985’,’hsr layout’, 558, 5500))

INSERT INTO employee (emp_number, emp_name, emp_dob,

emp_address, emp_deptno, emp_sal) values (1004,’mohan’,

’15-07-1985’,’btm layout’, 559, 5900))

Page 13: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 13

(b) Select the table employee using select command

SELECT * from employee

*emp_ number

emp_ name

emp_ dob

emp_ address

emp_ deptno

Emp_

sal

1000 naveen 18-07-1985 silk board 555 5000

1001 sureash 19-07-1985 krpuram 556 7000

1002 chandu 17-07-1985 cvramannagar 557 8000

1003 ramya 16-07-1985 hsr layout 558 5500

1004 mohan 15-07-1985 btm layout 559 5900

(c) Update empdept numbers in employee table using update

command.

update employee set emp_address= ’chennai’ where emp_number=1004.

*emp_ number

emp_ name

emp_ dob

emp_ address

emp_ deptno

Emp_sal

1000 naveen 18-07-1985 silk board 555 5000

1001 sureash 19-07-1985 krpuram 556 7000

1002 chandu 17-07-1985 cvramannagar 557 8000

1003 ramya 16-07-1985 hsr layout 558 5500

1004 mohan 15-07-1985 chennai 559 5900

(d) Delete a row from employee table.

DELETE FROM employee WHERE emp_number = "1003"

*emp_ number

emp_ name

emp_ dob

emp_ address

emp_ deptno

Emp_sal

1000 naveen 18-07-1985 silk board 555 5000

1001 sureash 19-07-1985 krpuram 556 7000

1002 chandu 17-07-1985 cv ramannagar 557 8000

Page 14: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 14

1004 mohan 15-07-1985 btm layout 559 5900

One row deleted

Exercise 4 Functions

(4) Write the Queries using student table and employee table

(a) Conversion functions (to_char, to_number & to_date), using

student table

(b) String functions (Concatenation, lpad, rpad, ltrim, rtrim, lower,

upper, initcap, length, substr) using employee table.s

Objective: The objective of this program is to: know the use of the following

functions:

Conversion functions

String functions

Date functions

Conversion Functions: Like To_Char, To_Number, To_Date These are

functions that help us to convert a value in one form to another form.

String functions like concatenation, Lpad, Rpad, Ltrim, Rtrim, Lower, Upper,

Initcap, Length, Substr.

Date Functions Like To_Char, To_Date.

Procedure and description: Conversion functions help us to convert a

value in one form to another form. For Example: a null value into an actual

value, or a value from one data type to another data type like TO_CHAR,

TO_NUMBER, TO_DATE.

1. Numeric Functions: These are functions that accept numeric input and

Return numeric values.

2. Character or Text Functions: These are functions that accept character

Input and can return both character and number values.

String functions: string is traditionally a sequence of characters, either as

a literal constant or as some kind of variable. The latter may allow its

elements to be mutated and/or the length changed, or it may be fixed (after

Page 15: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 15

creation). A string is generally understood as a data type and is often

implemented as an array of bytes (or words) that stores a sequence of

elements, typically characters, using some character encoding.

Function Description

substr Returns a portion of starting at position and char long

upper Converts to uppercase

lower Converts to lowercase

initcap Capitalizes the first character or each word

ltrim Removes blanks at the left-end of a string

rtrim Removes blanks at the right-end of a string

concat Concatenates two strings

length Returns the length of a string

lpad Returns left-padded to lengt with the string

rpad Returns right-padded to length with the string

Table: STRING FUNCTION DESCRIPTION TABLE

Queries using Conversion functions:

Query using to_char() function

Write a query to display the student_name (first character) and

student_ number (First digit) using to_char

select to_char(column_name) from wheretable_name

Write a query to display the current system time

select to_char(sysdate,'hh24:mm:ssam')from dual;

Query using to_date() function:

Write a query to display today’s date

select current Date = GETDATE ()

Queries using string functions:

Substr()

write a query to display the sub string for the word ‘oracle’ from its

position 2 to 3

select substr('oracle',2,3) from dual;

Page 16: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 16

Output

rac

translate() function

Write a query to change the ‘or’ to ‘ab’ in the word‘corporation’ using

translate function

select translate('corportion','or','ab')from dual;

Output

Cabpabtian

initcap() function

Write a query to maintain the first letter as caps using initcap function

select initcap ('oracle') from dual;

Output

Oracle

upper() function

Write a query to display all characters is in caps using upper function

Select upper ('oracle') from dual;

Output

ORACLE

lower() function

Write a query to display the all characters in small using lower

function.

select lower ('ORACLE') from dual;

Output

oracle

lpad() function

Write a query to display ‘smudde’ after leaving 5 blank places using

lpad function

Page 17: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 17

select LPAD('smudde',11,'*')from dual;

Output

*****smudde

(where * represents here blank spaces)

rpad() function

Write a query using RPAD() function to display given string with length

of 10, if string characters is less than the length then the remaining

space will be filled with ‘#’. Thestring name is manipal.

select RPAD('manipal',10,'#')from dual;

Output

manipal####

ltrim() function

Write a query to remove blanks at the left-end of a string with abc

select LTRIM(' abc')from dual;

Output

abc

rtrim() function

write a query to remove blanks at the right-end of a string with /abc

select RTRIM('/abc ')from dual;

Output

abc

Queries using date functions

write a query to display to the number of days between any two

months using month() function.

select months_between ('23-aug-2008','16-feb-2009')from dual;

write a query to add the month with date and year using add months

function

Page 18: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 18

select add_months ('3-jan-09',4) from dual;

Exercise 5 PL/SQL –Commit, Rollback and Save Points

i) Creation of simple PL/SQL program which includes declaration

section, executable section and exception handling section.

ii) Insert data into student table and use commit rollback and save

point in PL/SQL block.

Objective: the procedure of this program is to declare, execute and

exception handling sections, using these Insert data into student table and

use commit rollback and save point in PL/SQL block.

Program:

declare

sno student.student_number%type=&n;

name student.student_name%type;

begin

savepoint dup_data

select student_name into name from student where student-number=sno;

exception when NO_DATA_FOUND then

dbms_output.put_line(‘no such student ,so insert that sno into table’);

Insert into student values (&student number, &student_name);

dbms_output.put_line(‘student number’|student.|student-number);

dbms_output.put_line(‘student name’||student. student_name );

Rollback dup_data;

end;

OUTPUT:

enter value for n:768

no such student ,so insert that sno into table

student number 768

student name chandu

Page 19: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 19

COMMIT:

commits the current transaction. All changes made by the transaction

become visible to others and are guaranteed to be durable if a crash occurs.

COMMIT – commit the current transaction

SQLCOMMIT [TABLENAME | TRANSACTION]

Example: SQL commit [student | insert]

Outputs

COMMIT

Message returned if the transaction is successfully committed.

WARNING:COMMIT: no transaction in progressif there is no transaction in

progress.

ROLLBACK:

The ROLLBACK statement in SQL cancels the proposed changes in a

pending database transaction. The transaction can be rolled back

completely by specifying the transaction name in the ROLLBACK statement.

A partial rollback can also be accomplished by specifying a save point name

in lieu of the transaction name. The alternative to rolling back a transaction

is to utilize the COMMIT command to make the proposed changes part of

the relational database.

Begin transaction

……

SQLStatement(s)

...

Begin transaction

SAVEPOINT:

Page 20: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 20

The SAVEPOINT statement names and marks the current point in the

processing of a transaction. With the ROLLBACK statement, save

pointshelp us to undo parts of a transaction instead of the whole transaction.

Exercise 6 PL/SQL – If – Case, Case Expression

Develop a program that includes the features of nested if-case and

case expression. The program can be extended using the null if and

coalesce functions.

Objective: the objective of this program is to use nested if case, case

expressions and null if.

SOURCE CODE:

NESTED IF:

DECLARE

sno NUMBER(3):=&sno;

sname VARCHAR2(20):=’&sname’;

m1 NUMBER(3):=&m1;

m2 NUMBER(3):=&m2;

m3 NUMBER(3):=&m3;

total NUMBER(4);

avg1 NUMBER(5,2);

class VARCHAR(30);

BEGIN

total:=m1+m2+m3;

avg1:=total/3;

if m1>35 AND m2>35 AND m3>35 then

if avg1>60 then

class:=’first class’;

Page 21: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 21

end if;

if avg1>50 AND avg1<60 then

class:=’second class’;

end if;

if avg1>40 AND avg1<50 then

class:=’third class’;

end if;

else

class:=’fail’;

end if;

insert into student(sno,sname,m1,m2,m3,total,avg1,class);

END;

Expected Output:

Enter value for sno:15

Old 3:sno NUMBER(3):=&sno;

New 3: sno NUMBER (3):=15;

Enter value for sname:ram

Old 4:sname VARCHAR(20):=’&sname’;

New 4:sname VARCHAR (20):=ram;

Enter value for m1:40

Old 5:m1 NUMBER(3):=&m1;

New 5:m1 NUMBER(3):=40;

Enter value for m2:55

Old 6:m2 NUMBER(3):=&m2;

New 6:m2 NUMBER(3):=55;

Enter value for m3:68

Page 22: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 22

Old 7:m3 NUMBER(3):=&m3;

New 7:m3 NUMBER(3):=68;

Enter value for sno:26

Enter value for sname:manju

Enter value for m1:56

Enter value for m2:84

Enter value for m3:62

Enter value for total:202

Enter value for avg1:67.33

Enter value for class:first class

PL/SQL procedure successfully completed.

NULLIF;

DECLARE

v_num NUMBER:=&sv_user_num;

v_remainder NUMBER;

BEGIN

---------calculate the remainder and if it is zero

return a NULL v_remainder:=NULLIF(MOD(v_num,2),0);

DBMS_OUTPUT.PUT_LINE(‘v_remainder:’||v_remainder);

END;

Expected Output:

SQL>@nullif.sql;

9

Enter value for sv_user_num:26

Old 3:v_num NUMBER:=&sv_user_num;

New 3:v_num NUMBER:=26;

Page 23: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 23

V_remainder:1

PL/SQL procedure successfully completed.

CASE:

DECLARE

v_num1 NUMBER:=&sv_num1;

v_num2 NUMBER:=&sv_num2;

v_num3 NUMBER:=&sv_num3;

v_result NUMBER;

v_result:=CASE

WHEN v_num1 IS NOT NULL THEN v_num1

ELSE

CASE

WHEN v_num2 IS NOT NULL THEN v_num2

ELSE v_num3

END

END;

DBMS_OUTPUT.PUT_LINE(‘RESULT:’||v_result);

END;

Expected Output:

SQL>@case.sql;

17

Enter value for sv_ num1:12

Old 2:v_num1 NUMBER:&sv_num1;

New 2:v_num1 NUMBER:12;

Enter value for sv_num2:13

Page 24: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 24

Old 3:v_num2 NUMBER:&sv_num2;

New 3:v_num2 NUMBER:13;

Enter value for sv_num3:15

Old 4:v_num3 NUMBER:&sv_num3;

New 4:v_num3 NUMBER:15;

RESULT:12

PL/SQL procedure successfully completed.

COALESCE:

DECLARE

v_num1 NUMBER:=&sv_num1;

v_num2 NUMBER:=&sv_num2;

v_num3 NUMBER:=&sv_num3;

v_result NUMBER;

BEGIN

v_result:=COALESCE(v_num1,v_num2,v_num3);

DBMS_OUTPUT.PUT_LINE(‘RESULT:’||v_result);

END;

Expected Output:

SQL>@coal.sql;

10

Enter value for sv_ num1:45

Old 2:v_num1 NUMBER: &sv_num1;

New 2:v_num1 NUMBER: 45;

Enter value for sv_num2:23

Old 3:v_num2 NUMBER: &sv_num2;

New 3:v_num2 NUMBER: 23;

Enter value for sv_num3:15

Page 25: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 25

Old 4:v_num3 NUMBER: &sv_num3;

New 4:v_num3 NUMBER: 15;

RESULT: 45

PL/SQL procedure successfully completed.

Exercise 7 PL/SQL – Loops & Exceptions

Write a Program using loop, while loops, for loops along with error

handling and user defined exceptions.

To generate first 10 natural numbers using loop, while and for.

Program:

/* using loop statement */

declare

I number;

begin

I:=1;

loop

dbms_output.put_line(I);

I:=I+1;

exit when I>10;

end loop;

end;

Expected Output:

1.

2.

3.

4.

5.

6.

7.

8.

9.

Page 26: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 26

10.

Program:

/* using while */

declare

I number;

begin

I:=1;

while (I<=10)

loop

dbms_output.put_line (I);

I:=I+1;

end loop;

end;

Expected Output:

1.

2.

3.

4.

5.

6.

7.

8.

9.

10.

Program:

/* using for loop*/

Begin

For I in 1...10

loop

Dbms_output.put_line (I);

End loop;

Page 27: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 27

End;

Expected Output:

1.

2.

3.

4.

5.

6.

7.

8.

9.

10.

Program using predefined exceptions.

Program:

declare

emp_numberemployee.emp_number%type=&number;

name employee.emp_name%type;

begin

select emp_name into name from employee

where emp_number=emp_number;

exception when NO_DATA_FOUND then

dbms_output.put_line(‘no such employee’);

end;

Expected Output:

number: 1001

sureash

Page 28: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 28

Exercise 8 PL/SQL – Procedures

Write a PL/SQL Program to create

i) to create procedures ii) pass parameters IN & OUT.

Objective: the objective of this program is to create procedure using

IN-OUT parameters.

create a procedure using IN parameter.

Program:

create or replace procedure p1 (eno IN number,employee_namevarchar) as

begin

update employee set employee_name=employee_name where

emp_number = eno;

end;

Expected Output:

Passing values to parameters

exec p1(1003,’ramya’);

create a procedure using OUT parameter

Program:

Create procedure p2(p2 emp_number int, p2 emp_deptno out int)

is

begin

selectdepino

intoemp_dept

from employee

whereemp_number=emp_number;

end;

Expected Output:

Functionally, the code above may be used as in this example:

Page 29: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 29

EXEC P2(1002,:emp_deptno);

PL/SQL procedure successfully completed.

PRINT emp_deptno;

Emp_deptno

----------

557

Exercise 9 PL/SQL – Stored & Invoke Functions

Write a PL/SQL Program using stored functions and invoke functions

Objective: the objective of this program is to create stored, invoke and

complex functions.

Create function which adds two given numbers. (Simple programs)

Program:

create or replace function add_fun(a number,b number)

return number as

C number;

begin

C:=a+b;

return c;

end;

Function created.

/*add_fun specification*/

Declare

Result number;

Begin

Result:=add_fun(10,20);

Dbms_output.put_line(‘the sum of 10 and 20 is’||result);

End;

Page 30: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 30

Sql>/

Expected Output:

The sum of 10 and 20 is 30

Pl/sql procedure successfully completed.

Create a function which count total no.of employees having salary less

than 6000.

Program:

/*function body*/

create or replace function count_emp(esal number)return number as

cursor vin_cur as Select emp_number,emp_sal from emplpyee;

xno employee.emp_number%type;

xsal employee.emp_sal%type;

C number;

begin

open vin_cur;

C:=0;

loop

fetch vin_cur into xno,xsal;

if(xsal<esal) then

c:=c+1;

end if;

exit when vin_cur%notfound;

end loop;

close vin_cur;

return c;

end;

/

Function created.

/*function specification*/

declare

ne number;

Page 31: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 31

xsal number;

begin

ne:=count_emp(xsal);

dbms_output.put_line(xsal);

dbma_output.put_line(‘there are ‘||ne||employees’);

end;

Expected Output:

There are 2 employees

Exercise 10 PL/SQL – Triggers

Write a PL/SQL Program using BEFORE and AFTER Triggers – with

operations like insert/update/delete which will run on Sundays and all

weekdays between 17 pm to 9 am (the following day).

Objective: the objective of this program is to maintain the trigger (BEFORE

& AFTER) in given time using insert/update/delete operations.

Program:

create trigger trig1

before insert or update or delete on employee

for each row

declare

ctime number:=to_char(sysdate,’hh24’);

cday varchar2(10):=to_char(sysdate,’day’);

begin

ifctime<9 or ctime>17 then

raise_application_error(-20002,’cannot access data’);

end if;

if cday=’sunday’ orcday=’SUNDAY’ then

raise_application_error(-20002,’cannot access data’);

end if;

Page 32: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 32

end;

Expected Output:

Insert into employee (emp_name, emp_number) values (‘gopi’,’1008’);

1 row created.

Exercise 11 PL/SQL – Cursor

Write a PL/SQL cursor program to fetch first three records of an

employee table

Objective: the objective of this program is to maintain the cursorto fetch first

three employees list from employee table.

Program:

declare

cursor c2 is select * from employee;

erec employee % rowtype;

begin

open c2;

loop

fetch c2 into erec;

exit when(c2%ROWCOUNT>3);

dbms_output.put_line(‘----------------‘);

dbms_output.put_line(‘emp_number:’||erec.emp_number);

dbms_output.put_line(‘emp_name:’||erec.emp_name);

end loop;

dbms_output.put_line(‘no. of employees:’||c2%rowcount);

close c2;

end;

Expected Output:

------------

emp_number: 1000

emp_name: naveen

Page 33: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 33

------------

emp_number: 1001

emp_name: sureash

------------

emp_number: 1002

emp_name: chandu

------------

no. of employees: 3

Page 34: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 34

Exercise 12 Joins

Consider the following database of student enrollment in courses &

books adopted for each course:

STUDENT (regno: string, name: string, major: string, bdate:date)

COURSE (course #:int, cname:string, dept:string)

ENROLL ( regno:string, course#:int, sem:int, marks:int)

BOOK _ ADOPTION (course# :int, sem:int, book-ISBN:int)

TEXT (book-ISBN:int, book-title:string, publisher:string, author:string)

Demonstrate with appropriate SQL Statement for the following:

1. Create the above tables by properly specifying the primary keys and the

foreign keys.

2. Enter at least five tuples for each relation.

3. Demonstrate how you add a new text book to the database and make

this book be adopted by some department.

4. List the students who have been enrolled.

5. List the students who have registered but not enrolled.

6. List the books which have been adopted.

7. List any department that has all its adopted books published by a

specific publisher.

8. Illustrate inner join, Left join, right join, full join by joining STUDENT and

ENROLL table.

Objective:

The objective of this exercise is to enable you to understand and use SQL

relational concepts along with the concepts of Joins.

Procedure and description:

Page 35: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 35

By getting the knowledge of Primary Key and Foreign Key the relationship

between two tables can be matched and developed. There are 3 types of

relations between tables – One-To-Many, Many-To-Many and One-To-One.

We use the process called Normalization for removing redundant data

between tables when the relations are built.

SQL joins are used to query data from two or more tables, based on a

relationship between certain columns in these tables. The INNER JOIN

keyword return rows when there is at least one match in both tables (say

table_name1 & table_name2). The LEFT JOIN keyword returns all rows

from the left table (table_name1) even if there are no matches in the right

table (table_name2). The RIGHT JOIN keyword returns all the rows from

the right table (table_name2), even if there are no matches in the left table.

The FULL JOIN keyword return rows when there is a match in one of the

tables.

Algorithm: The steps for this exercise are given below:

Step – 1: Start

Step – 2: Create Database, tables using CREATE commands with its

essential attributes.

Step – 3: Insert the values using INSERT INTO statements. (Insert the

suitable values (tuples) so that queries are executed correctly.)

Step – 4: Execute different Commands and extract information from the

table. (Hint: use commands like FROM, WHERE, DISTINCT, INNER JOIN,

LEFT JOIN, RIGHT JOIN, FULL JOIN, GROUP BY, ORDER BY etc. You

can use suitable operators like AND & OR for certain conditions to meet)

Expected Output: Creation of tables, use of normalization and its

modification through SQL commands.

Page 36: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 36

Exercise 13 View, Union

Consider the following tables maintained by a book dealer:

AUTHOR (author-id:int, name:string, city:string, country:string)

PUBLISHER (publisher-id:int, name:string, city:string, country:string)

CATALOG (book-id:int, title:string, author-id:int, publisher-id:int, category-

id:int, year:int, price:int)

CATEGORY (category-id:int, description:string)

ORDER-DETAILS (order-no:int, book-id:int, quantity:int)

Demonstrate with appropriate SQL Statement for the following:

1. Create the above tables by properly specifying the primary keys and the

foreign keys.

2. Enter at least five tuples for each relation.

3. Give the details of the authors who have 2 or more books in the catalog

and the price of the books is greater than the average price of the books

in the catalog and the year of publication is after certain year (say 2000).

4. Find the author of the book which has maximum sales.

5. Demonstrate how you increase the price of books published by a

specific publisher by 10%.

6. Illustrate the use of UNION operator between the table AUTHOR and CATALOG table.

Objective:

The objective of this exercise is to enable you to understand Transaction

processing and create VIEW. Its objective is also to get the concept of

UNION operator.

Procedure and description:

In SQL, a view is a virtual table based on the result-set of an SQL

statement. A view contains rows and columns, just like a real table. The

fields in a view are fields from one or more real tables in the database. You

can add SQL functions, WHERE, and JOIN statements to a view and

present the data as if the data were coming from one single table. A view

Page 37: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 37

always shows up-to-date data. The database engine recreates the data,

using the view's SQL statement, every time a user queries a view.

The UNION operator is used to combine the result-set of two or more

SELECT statements. Notice that each SELECT statement within the UNION

must have the same number of columns. The columns must also have

similar data types. Also, the columns in each SELECT statement must be in

the same order.

Algorithm: The steps for this exercise are given below:

Step – 1: Start

Step – 2: Create tables using CREATE commands with its essential

attributes.

Step – 3: Insert the values using INSERT INTO statements. (Insert the

suitable values (tuples) so that queries are executed correctly.)

Step – 4: you can create View for the details of the sales and then execute

different commands and extract information from the table. (Hint: use

keywords like VIEW, UNION etc. You can use suitable operators like AND &

OR for certain conditions to meet various other SQL clauses.)

Step – 5: Execute and check.

Expected Output: Creation of tables, use of view and its modification

through SQL commands.

Page 38: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 38

Exercise 14 Exception handling

Write a PL/SQL block to illustrate the programmer-defined exceptions.

Get the salary of an employee and check it with the job’s salary range.

If the salary is below the range, raise exception

BELOW_SALARY_RANGE. If the salary is above the range, raise

exception ABOVE_SALARY_RANGE.

Objective:

The objective of this exercise is to enable you to make use of exception

handling in PL/SQL.

Procedure and description:

The EXCEPTION section is an optional part of any PL/SQL block. If this

section is omitted and errors are encountered, the block will be terminated.

Some errors that are encountered may not justify the immediate termination

of a block, so the EXCEPTION section can be used to handle specified

errors or user-defined exceptions in an orderly manner. Exceptions can be

user-defined.

Exceptions are raised in a block by using the command RAISE. Exceptions

can be raised explicitly by the programmer, whereas internal database

errors are automatically, or implicitly, raised by the database server.

The General Syntax for coding the exception section

DECLARE

Declaration section

BEGIN

Exception section

EXCEPTION

WHEN ex_name1 THEN

-Error handling statements

WHEN ex_name2 THEN

Page 39: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 39

-Error handling statements

WHEN Others THEN

-Error handling statements

END;

Example Exception code:

SET SERVEROUTPUT ON SIZE 100000;

DECLARE

-- define exceptions

BELOW_SALARY_RANGE EXCEPTION;

ABOVE_SALARY_RANGE EXCEPTION;

-- salary variables

n_salary employees.salary%TYPE;

n_min_salary employees.salary%TYPE;

n_max_salary employees.salary%TYPE;

-- input employee id

n_emp_id employees.employee_id%TYPE := &emp_id;

BEGIN

SELECT salary,

min_salary,

max_salary

INTO n_salary,

n_min_salary,

n_max_salary

FROM employees

INNER JOIN jobs ON jobs.job_id = employees.job_id

WHERE employee_id = n_emp_id;

IF n_salary < n_min_salary THEN

RAISE BELOW_SALARY_RANGE;

ELSIF n_salary > n_max_salary THEN

RAISE ABOVE_SALARY_RANGE;

END IF;

DBMS_OUTPUT.put_line('Employee ' || n_emp_id ||

Page 40: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 40

' has salary $' || n_salary );

EXCEPTION

WHEN BELOW_SALARY_RANGE THEN

DBMS_OUTPUT.put_line('Employee ' || n_emp_id ||

' has salary below the salary range');

WHEN ABOVE_SALARY_RANGE THEN

DBMS_OUTPUT.put_line('Employee ' || n_emp_id ||

' has salary above the salary range');

WHEN NO_DATA_FOUND THEN

DBMS_OUTPUT.PUT_LINE('Employee ' || n_emp_id || ' not found');

END;

/

Algorithm: The steps for this exercise are given below:

Step – 1: Write the PL/SQL code to create table that accept employee ID

from user and display employee.

Step – 2: include the condition for the exception raised.

Step – 3: Execute.

Expected Output:

PL/SQL exception, raise and handle it in exception handler section of

PL/SQL block.

Downloading and installing software

You can download the latest Oracle Database 11g Express Edition from

http://www.oracle.com/technetwork/products/express-

edition/overview/index.html. You can also download and use the other

database like MySQL (version 5.0 and above) from

http://www.mysql.com/downloads/. You can install Oracle or MySQL in

Linux Operation System also. This SLM of practical exercises has been

followed through the use of Windows 7 Enterprise edition or Fedora 16 (in

some case). The preferable version of MS Windows Operating System

would be Windows 7, although you can install any other version of windows

operating system. The preferable version of Linux Operating system would

Page 41: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 41

be Fedora although you can install any other version of Linux operating

system.

Note: New versions or updates may be available. You can download a new

version or an update version of both Oracle or MySQL or other database

applicable.

Page 42: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 42

Exercise 15 First Come First Served scheduling

(Refer to Unit No. 3 titled “CPU Scheduling Algorithms”)

Problem Statement: Implement the FCFS Problem using C language

constructs.

Algorithm:

This is one of the very brute force algorithms. A process that requests

for the CPU first is allocated the CPU first. Hence, the name first

come first serve.

The FCFS algorithm is implemented by using a first-in-first-out (FIFO)

queue structure for the ready queue. This queue has a head and a

tail. When a process joins the ready queue its PCB is linked to the tail

of the FIFO queue. When the CPU is idle, the process at the head of

the FIFO queue is allocated the CPU and deleted from the queue.

Even though the algorithm is simple, the average waiting is often

quite long and varies substantially if the CPU burst times vary greatly,

as seen in the following example.

Numerical Example: Consider a set of three processes P1, P2 and

P3 arriving at time instant 0 and having CPU burst times as shown

below:

Process Burst time (msecs)

P1 24

P2 3

P3 3

Page 43: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 43

The Gantt chart below shows the result.

0 24 27 30

Average waiting time and average turnaround time are calculated as

follows:

The waiting time for process P1 = 0 msecs

P2 = 24 msecs

P3 = 27 msecs

Average waiting time = (0 + 24 + 27) / 3 = 51 / 3 = 17 msecs.

P1 completes at the end of 24 msecs, P2 at the end of 27 msecs and

P3 at the end of 30 msecs. Average turnaround time = (24 + 27 + 30)

/ 3 = 81 / 3 = 27 msecs.

Exercise 16 The Critical – Section problem

(Refer to Unit No. 4 titled “Process Synchronization”)

Problem Statement: Implement the Critical Section problem using

C Language as per the algorithm given below.

Algorithm

A critical-section is a part of program that accesses a shared

resource (data structure or device) that must not be concurrently

accessed by more than one process of execution. Consider a system

consisting of n processes { p0,p1,……pn-1}. Each process has a

segment of code called a critical-section. The important feature of the

P1 P2 P3

Page 44: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 44

system is that, when one process is executing in its critical-section,

no other process is to be allowed to execute in its critical-section.

Thus the execution of critical-sections by the processes is mutually

exclusive in time. The critical-section problem is to design a protocol

that the processes can use to co-operate. Each process must request

permission to enter its critical-section. The section of the code

implementing this request is the entry section. The critical-section

may be followed by an exit section. The remaining code is the

remainder section.

A solution to the critical-section problem must satisfy the following

three requirements:

1. Mutual Exclusion: If process pi is executing in its critical-section,

then no other processes can be executing in their critical-sections.

2. Progress: If no process is executing in its critical-section and

there exist some processes that are not executing in their remainder

section can participate in the decision of which will enter in its critical-

section next, and this selection cannot be postponed indefinitely.

3. Bounded Waiting: There exist a bound on the number of times

that other processes are allowed to enter their critical-sections after a

process has made a request to enter its critical-section and before

that request is granted.

When presenting an algorithm, we define only the variables used for

synchronization purposes, and describe only a typical process pi

whose general structure is shown in figure below: The entry section

Page 45: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 45

and exit section are enclosed in boxes to highlight these important

segments of code.

Repeat

Critical-section

Remainder section

until false;

Figure: General structure of a typical process pi

The processes are numbered p0 and p1. For convenience, when

presenting pi, we use pj to denote the other process; that is j=1-i.

Algorithm Implementation

Let the processes share a common variable turn initialized to 0 (or 1).

If turn =i, then process pi is allowed to execute in its critical-section.

The structure of process pi is shown in figure below.

This solution ensures that only one process at a time can be in its

critical-section. But it does not satisfy the progress requirement, since

it requires strict alternation of processes in the execution of the

critical-section. For example, if turn=0 and p1 is ready to enter its

critical-section, p1 cannot do so, even though p0 may be in its

remainder section.

Repeat

entry section

exit section

While turn ≠ i do no-op;

Page 46: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 46

Critical-section

Remainder section

until false;

Figure: The structure of process pi in the algorithm

Exercise 17 Banker’s Algorithm

(Refer to Unit No. 5 titled “ Introduction to Deadlocks “)

Problem Statement: Implement Banker’s algorithm using C

Language as per the algorithm given below.

A deadlock avoidance algorithm requires each process to make

known in advance the maximum number of resources of each type

that it may need. Also known is the maximum number of resources of

each type available. Using both the above a priori knowledge,

deadlock avoidance algorithm ensures that a circular wait condition

never occurs.

Safe State: A system is said to be in a safe state if it can allocate

resources upto the maximum available and is not in a state of

deadlock. A safe sequence of processes always ensures a safe state.

A sequence of processes < P1, P2, ....., Pn > is safe for the current

allocation of resources to processes if resource requests from each Pi

can be satisfied from the currently available resources and the

resources held by all Pj where j < i. If the state is safe then Pi

turn= j;

Page 47: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 47

requesting for resources can wait till Pj’s have completed. If such a

safe sequence does not exist, then the system is in an unsafe state.

The resource allocation graph algorithm is not applicable where

resources have multiple instances. In such a case Banker’s algorithm

is used.

A new process entering the system must make known a priori the

maximum instances of each resource that it needs subject to the

maximum available for each type. As execution proceeds and

requests are made, the system checks to see if the allocation of the

requested resources ensures a safe state. If so only are the

allocations made, else processes must wait for resources.

The following are the data structures maintained to implement the

Banker’s algorithm:

1. n: Number of processes in the system.

2. m: Number of resource types in the system.

3. Available: is a vector of length m. Each entry in this vector gives

maximum instances of a resource type that are available at the

instant. Available[j] = k means to say there are k instances of the

jth resource type Rj.

4. Max: is a demand vector of size n x m. It defines the maximum

needs of each resource by the process. Max[i][j] = k says the ith

process Pi can request for atmost k instances of the jth resource

type Rj.

5. Allocation: is a n x m vector which at any instant defines the

number of resources of each type currently allocated to each of

Page 48: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 48

the m processes. If Allocation[i][j] = k then ith process Pi is

currently holding k instances of the jth resource type Rj.

6. Need: is also a n x m vector which gives the remaining needs of

the processes. Need[i][j] = k means the ith process Pi still needs k

more instances of the jth resource type Rj. Thus Need[i][j] =

Max[i][j] – Allocation[i][j].

Safety Algorithm

Using the above defined data structures, the Banker’s algorithm to

find out if a system is in a safe state or not is described below:

1. Define a vector Work of length m and a vector Finish of length n.

2. Initialize Work = Available and Finish[i] = false for i = 1, 2, ., n.

3. Find an i such that

a. Finish[i] = false and

b. Needi <= Work (Needi represents the ith row of the vector

Need).

If such an i does not exist , go to step 5.

4. Work = Work + Allocationi

Go to step 3.

5. If finish[i] = true for all i, then the system is in a safe state.

Resource-Request Algorithm

Let Requesti be the vector representing the requests from a process

Pi. Requesti[j] = k shows that process Pi wants k instances of the

resource type Rj. The following is the algorithm to find out if a request

by a process can immediately be granted:

1. If Requesti <= Needi, go to step 2.

Page 49: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 49

else Error “request of Pi exceeds Maxi”.

2. If Requesti <= Availablei, go to step 3.

else Pi must wait for resources to be released.

3. An assumed allocation is made as follows:

Available = Available – Requesti

Allocationi = Allocationi + Requesti

Needi = Needi – Requesti

If the resulting state is safe, then process Pi is allocated the resources

and the above changes are made permanent. If the new state is

unsafe, then Pi must wait and the old status of the data structures is

restored.

Exercise 18 The Dining Philosopher’s Problem

(Refer to Unit No. 4 titled “Process Synchronization “)

Problem Statement: Implement the Dining Philosopher’s problem

using

C Language as per the algorithm given below.

Algorithm

The dining philosophers problem is summarized as five philosophers

sitting at a table doing one of two things: eating or thinking. While

eating, they are not thinking, and while thinking, they are not eating.

The five philosophers sit at a circular table with a large bowl of

spaghetti in the center. A fork is placed in between each philosopher,

and as such, each philosopher has one fork to his or her left and one

Page 50: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 50

fork to his or her right. As spaghetti is difficult to serve and eat with a

single fork, it is assumed that a philosopher must eat with two forks.

The philosopher can only use the fork on his or her immediate left or

right.

Illustration:

The dining philosophers problem is sometimes explained using rice

and chopsticks rather than spaghetti and forks, as it is more intuitively

obvious that two chopsticks are required to begin eating.

The philosophers never speak to each other, which creates a

dangerous possibility of deadlock when every philosopher holds a left

fork and waits perpetually for a right fork (or vice versa).

Originally used as a means of illustrating the problem of deadlock,

this system reaches deadlock when there is a 'cycle of unwarranted

requests'. In this case philosopher P1 waits for the fork grabbed by

philosopher P2 who is waiting for the fork of philosopher P3 and so

forth, making a circular chain.

Starvation: (and the pun was intended in the original problem

description) might also occur independently of deadlock if a

philosopher is unable to acquire both forks due to a timing issue. For

example there might be a rule that the philosophers put down a fork

after waiting five minutes for the other fork to become available and

wait a further five minutes before making their next attempt. This

Page 51: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 51

scheme eliminates the possibility of deadlock (the system can always

advance to a different state) but still suffers from the problem of

livelock. If all five philosophers appear in the dining room at exactly

the same time and each picks up their left fork at the same time the

philosophers will wait five minutes until they all put their forks down

and then wait a further five minutes before they all pick them up

again.

The lack of available forks is an analogy to the lacking of shared

resources in real computer programming, a situation known as

concurrency. Locking a resource is a common technique to ensure

the resource is accessed by only one program or chunk of code at a

time. When the resource a program is interested in is already locked

by another one, the program waits until it is unlocked. When several

programs are involved in locking resources, deadlock might happen,

depending on the circumstances. For example, one program needs

two files to process. When two such programs lock one file each, both

programs wait for the other one to unlock the other file, which will

never happen.

In general the dining philosophers problem is a generic and abstract

problem used for explaining various issues which arise in problems

which hold mutual exclusion as a core idea. For example, as in the

above case deadlock/livelock is well explained with the dining

philosophers problem.

Waiter solution

A simple solution is achieved by introducing a waiter at the table.

Philosophers must ask his permission before taking up any forks. Because

Page 52: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 52

the waiter is aware of which forks are in use, he is able to arbitrate and

prevent deadlock. When four of the forks are in use, the next philosopher to

request one has to wait for the waiter's permission, which is not given until a

fork has been released. The logic is kept simple by specifying that

philosophers always seek to pick up their left hand fork before their right

hand fork (or vice versa).

To illustrate how this works, consider the philosophers are labeled clockwise

from A to E. If A and C are eating, four forks are in use. B sits between A

and C so has neither fork available, whereas D and E have one unused fork

between them. Suppose D wants to eat. Were he to take up the fifth fork,

deadlock becomes likely. If instead he asks the waiter and is told to wait, we

can be sure that next time two forks are released there will certainly be at

least one philosopher who could successfully request a pair of forks.

Therefore deadlock cannot happen.

Page 53: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 53

Annexure 1: : List of Exercises for Unguided session

S.No. Title of the Exercise

List of exercises for BCA3060

1.

Create the table employee with the following the following fields

S.No Name Designation Branch

Perform the following

a. Add the new column called salary

b. Rename the column “Name” to Emp_Name”

c. Describe the table employee

d. Copy the table employee to emp

e. List the employee details whose salary is greater than

5000

f. Delete the second row of the table

g. Drop the table employee

2.

Create the table Book with the following the following fields

S.No B_Name Author Price Publisher

Perform the following

a. Commit the table Book

b. Create a save point for the table Book as B

c. Roll back the table Book after inserting the 4 & 5 row

d. Define Grant & Revoke

3. Write a PL/SQL code to display employee number, name and

basic of 5 highest paid employees using cursor and loop having

Page 54: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 54

the employee and salary table as follows

Employee Table

Emp_No Name City

Salary Table

Emp_No Basic Deductions Net Salary

4.

Write a trigger which ensures that Department table does not

contain duplicate or null values in Dept_No column. If it

encounters null or duplicate value it should generate the

message to the user

List of exercises for MCA3070

1.

Create the table employee with the following the following fields

S.No Name Designation Branch

Perform the following

a. Add the new column called salary

b. Rename the column “Name” to Emp_Name”

c. Describe the table employee

d. Copy the table employee to emp

e. List the employee details whose salary is greater than

5000

f. Delete the second row of the table

Drop the table employee

2.

Write a PL/SQL code to display employee number, name and

basic of 5 highest paid employees using cursor and loop having

the employee and salary table as follows

Page 55: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 55

Employee Table

Emp_No Name City

Salary Table

Emp_No Basic Deductions Net Salary

3.

Write a trigger which ensures that Department table does not

contain duplicate or null values in Dept_No column. If it

encounters null or duplicate value it should generate the

message to the user

4.

Write a PL/SQL program to

a. implement user design exceptions

b. demonstrate ‘raise_application_error’

List of Exercises for BT0073

1.

Create the table employee with the following the following fields

S.No Name Designation Branch

Perform the following

a. Add the new column called salary

b. Rename the column “Name” to Emp_Name”

c. Describe the table employee

d. Copy the table employee to emp

e. List the employee details whose salary is greater than

5000

f. Delete the second row of the table

Drop the table employee

Page 56: DBMS and OS Practical Instructor Manual203.192.247.83/cse/pdf/manuals/odd-sem/BCA3060-MCA3070... · 2019. 7. 30. · This lab manual is prepared with an intention of increasing the

Sikkim Manipal University Page No. 56

2.

Write a PL/SQL code to display employee number, name and

basic of 5 highest paid employees using cursor and loop having

the employee and salary table as follows

Employee Table

Emp_No Name City

Salary Table

Emp_No Basic Deductions Net Salary

3.

Write a trigger which ensures that Department table does not

contain duplicate or null values in Dept_No column. If it

encounters null or duplicate value it should generate the

message to the user

4. Write a C program to implement First Come and First Serve job

Scheduling algorithm


Recommended