Paper

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Using C programming, write a semaphore based solution to Dining Philosopher’s problem and explain the program

#include<stdio.h>

#define n 4

int compltedPhilo = 0,i;

struct fork{
int taken;
}ForkAvil[n];

struct philosp{
int left;
int right;
}Philostatus[n];

void goForDinner(int philID){ //same like threads concept here cases implemented
if(Philostatus[philID].left==10 && Philostatus[philID].right==10)
printf("Philosopher %d completed his dinner\n",philID+1);
//if already completed dinner
else if(Philostatus[philID].left==1 && Philostatus[philID].right==1){
//if just taken two forks
printf("Philosopher %d completed his dinner\n",philID+1);

Philostatus[philID].left = Philostatus[philID].right = 10; //remembering that he completed dinner by assigning value 10


int otherFork = philID-1;

if(otherFork== -1)
otherFork=(n-1);

ForkAvil[philID].taken = ForkAvil[otherFork].taken = 0; //releasing forks


printf("Philosopher %d released fork %d and fork %d\n",philID+1,philID+1,otherFork+1);
compltedPhilo++;
}
else if(Philostatus[philID].left==1 && Philostatus[philID].right==0){ //left already taken, trying for right fork
if(philID==(n-1)){
if(ForkAvil[philID].taken==0){ //KEY POINT OF THIS PROBLEM, THAT LAST PHILOSOPHER TRYING IN reverse
DIRECTION
ForkAvil[philID].taken = Philostatus[philID].right = 1;
printf("Fork %d taken by philosopher %d\n",philID+1,philID+1);
}else{
printf("Philosopher %d is waiting for fork %d\n",philID+1,philID+1);
}
}else{ //except last philosopher case
int dupphilID = philID;
philID-=1;

if(philID== -1)
philID=(n-1);

if(ForkAvil[philID].taken == 0){
ForkAvil[philID].taken = Philostatus[dupphilID].right = 1;
printf("Fork %d taken by Philosopher %d\n",philID+1,dupphilID+1);
}else{
printf("Philosopher %d is waiting for Fork %d\n",dupphilID+1,philID+1);
}
}
}
else if(Philostatus[philID].left==0){ //nothing taken yet
if(philID==(n-1)){
if(ForkAvil[philID-1].taken==0){ //KEY POINT OF THIS PROBLEM, THAT LAST PHILOSOPHER TRYING IN
reverse DIRECTION
ForkAvil[philID-1].taken = Philostatus[philID].left = 1;
printf("Fork %d taken by philosopher %d\n",philID,philID+1);
}else{
printf("Philosopher %d is waiting for fork %d\n",philID+1,philID);
}
}else{ //except last philosopher case
if(ForkAvil[philID].taken == 0){
ForkAvil[philID].taken = Philostatus[philID].left = 1;
printf("Fork %d taken by Philosopher %d\n",philID+1,philID+1);
}else{
printf("Philosopher %d is waiting for Fork %d\n",philID+1,philID+1);
}
}
}else{}
}

int main(){
for(i=0;i<n;i++)
ForkAvil[i].taken=Philostatus[i].left=Philostatus[i].right=0;

while(compltedPhilo<n){
/* Observe here carefully, while loop will run until all philosophers complete dinner
Actually problem of deadlock occur only thy try to take at same time
This for loop will say that they are trying at same time. And remaining status will print by go for dinner function
*/
for(i=0;i<n;i++)
goForDinner(i);
printf("\nTill now num of philosophers completed dinner are %d\n\n",compltedPhilo);
}

return 0;
}

3i
Segmentation

 A Memory Management technique in which memory is divided into variable sized chunks which
can be allocated to processes. Each chunk is called a Segment.
 The mapping of the logical address to the physical address is done with the help of the segment
table.
 Segment table is divided into three part : Segment Number, Base Address and Segment Limit.
o Segment Number - Used as an index into a segment table which contains Base Address and
Limit of each segment in physical memory.
o Base Address - Starting address of the corresponding segment in main memory.
o Segment Limit - The length of the segment.
3ii
Paging

Paging is a memory management technique that permits the physical address space of a process to be non-
contiguous.

 Physical memory is divided into fixed size blocks called frames.


 Logical memory is divided into blocks of the same size called pages.
 A frame has the same size as a page and it is a place where a(logical) page can be (physically)
placed.
 The size of a page is always power of 2, and vary between 512 bytes to 8192 bytes per page.
 Every address generated by the CPU is divided into two parts: Page number (p) and Page offset (d)
o Page number - Used as an index into a page table which contains base address of each page
in physical memory.
o Page offset - Combined with base address to define the physical memory address that is
sent to the memory unit.
3iii
Combined segmentation and paging

Pure segmentation is not very popular and not being used in many of the operating systems. However,
Segmentation can be combined with Paging to get the best features out of both the techniques.

In Segmented Paging, the main memory is divided into variable size segments which are further divided
into fixed size pages.

 Pages are smaller than segments.


 Each Segment has a page table which means every program has multiple page tables.
 The logical address is represented as Segment Number (base address), Page Number and Page
Offset.
o Segment Number - It points to the appropriate Segment Number.
o Page Number - It Points to the exact page within the segment.
o Page Offset - Used as an offset within the page frame.
Q3. (b) Compare direct file with indexed sequential file organization
Direct File Organization

 Direct file organization is also known as random or relative file organization.


 In direct file, all records are stored in direct access storage device, such as hard disk. The records
are randomly placed throughout the file.
 The records does not need to be in sequence because they are updated directly and rewritten back
in the same location.
 This file organization is useful for immediate access to large amount of information. It is used in
accessing large databases.
 It is also called as hashing.

Advantages of Direct File Organization

 Direct file helps in online transaction processing system (OLTP) like online railway reservation
system.
 In direct file, sorting of the records are not required.
 It accesses the desired records immediately.
 It updates several files quickly.
 It has better control over record allocation.

Disadvantages of Direct File Organization

 Direct file does not provide back up facility.


 It is expensive.

Indexed Sequential File Organization

 Indexed sequential file combines both sequential file and direct file organization.
 In indexed sequential file, records are stored randomly on a direct access device such as magnetic
disk by a primary key.
 This file have multiple keys. These keys can be alphanumeric in which the records are ordered is
called primary key.
 The data can be access either sequentially or randomly using the index. The index is stored in a file
and read into memory when the file is opened.

Advantages of Indexed sequential file organization

 In indexed sequential file, sequential file and random file access is possible.
 It accesses the records very fast if the index table is properly organized.
 The records can be inserted in the middle of the file.
 It provides quick access for sequential and direct processing.
 It reduces the degree of the sequential search.

Disadvantages of Indexed sequential file organization

 Indexed sequential file requires unique keys and periodic reorganization.


 Indexed sequential file takes longer time to search the index for the data access or retrieval.
 It requires more storage space.
 It is expensive because it requires special software.
 Q4. (a) Explain take-grant model for operating system security
with an example. Also explain the mechanisms of security in WIN
2000 operating system ?
Answer : - The Take-Grant System is a model that helps in determining the protection rights (e.g., read or
write) in a computer system. The Take-Grant system was introduced by Jones, Lipton, and Snyder to show
that it is possible to decide on the safety of a computer system even when the number of subjects and
objects are very large, or unbound. This can be accomplished in linear time based on the initial size of the
system.

The take-grant system models a protection system which consists of a set of states and state transitions. A
directed graph shows the connections between the nodes of this system. These nodes are representative of
the subjects or objects of the model. The directed edges between the nodes represent the rights that one
node has over the linked node.

There are a total of four such rules :

 take rule allows a subject to take rights of another object


 grant rule allows a subject to grant own rights to another object

 create rule allows a subject to create new objects

 remove rule allows a subject to remove rights it has over on another object

Example
Q4. (b) Explain Bell and La-Padula Model for security and protection.
Why is security a critical issue in a distributed OS environment ?
er : - The Bell-Lapadula Model of protection systems deals with the control of information flow. It is a
linear non-discretionary model. This model of protection consists of the following components :
 A set of subjects, a set of objects, and an access control matrix.
 Several ordered security levels. Each subject has a clearance and each object has a classification
which attaches it to a security level. Each subject also has a current clearance level which does not
exceed its clearance level. Thus a subject can only change to a clearance level below its assigned
clearance level.

The set of access rights given to a subject are the following :

 Read-Only (The subject can only read the object.)


 Append (The subject can only write to the object but it cannot read.)
 Execute (The subject can execute the object but can neither read nor write.)
 Read-Write (The subject has both read and write permissions to the object.)

Control Attribute - This is an attribute given to the subject that creates an object. Due to this, the creator
of an object can pass any of the above four access rights of that object to any subject. However, it cannot
pass the control attribute itself. The creator of an object is also known as the controller of that object.

The following restrictions are imposed by the Bell-Lapadula Model :

 Reading Down - A subject has only read access to objects whose security level is below the
subject's current clearance level. This prevents a subject from getting access to information
available in security levels higher than its current clearance level.
 Writing Up - A subject has append access to objects whose security level is higher than its current
clearance level. This prevents a subject from passing information to levels lower than its current
level.

You might also like