Friday 8 May 2020

Using Turn Variable for Critical Section Problem





PRODUCER CONSUMER PROBLEM




Critical Section








Process Synchronization






CPU Scheduling
















Introduction and Evolution of Operating Systems











Sunday 19 April 2020

Data Types in Python- Lecture 6- range




range Data Type:

range Data Type represents a sequence of numbers. 
The elements present in range Data type are not modifiable. i.e range Data type is immutable.

Form-1:

 range(10) generate numbers from 0 to 9



Form-2: range(10,20)

generate numbers from 10 to 19

 

Form-3: range(10,20,2)

2 means increment value by 2




 

We can access elements present in the range Data Type by using 
  • loops
  • index.
Using loops:



Using Index:



We cannot modify the values of range data type
Example:  
r[0]=100 
TypeError: 'range' object does not support item assignment


We can create a list of values with range data type
Example:
1) >>> l = list(range(10))
2) >>> l 
3) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Friday 17 April 2020

Data types in Python - Lecture 5- bytes and bytearray




bytes Data Type:

bytes data type represents a group of byte numbers just like an array.
The only allowed values in bytes data type are in range of 0- 256

example:
x=[10,20,30,40,50]
b=bytes(x)
type(b)----> bytes


 
You can save numbers in bytes datatype object but only in a particular range(0,256)
If you try to save a number out of this range you will get ValueError:
 


In Python if you talk about range(0,256) you mean the allowed numbers are 0 to 255 , Therefore even 256 is not allowed in bytes


bytes is IMMUTABLE
therefore item assignment is not allowed
If you try so, you will get TypeError




bytearray Data type:

bytearray is exactly same as bytes data type except that its elements can be modified.



Tuesday 14 April 2020

Immutability in Python




Immutability:

Once we create an object, we cannot perform any changes in that object.
If we try to make changes then with those changes a new object will be created.

This non changeable behaviour is called as Immutability.

suppose i have a variable x
x=10 ---> i want to store 10 in x
now this object is made with 10 in it..
Further if I want to change the data to 20 
x=20
I cannot do that in the original object

A new object will be created in the memory and data with required changes will be stored in that new object.

We can prove this thing by using id() function
id() function tells us the address of the object

If the changes are made in the same object then 
id(x) before changes and after changes should have remained same
But that's not the case
id(x) before changes and after changes comes to be different.
This proves that if we try to make changes in an immutable object, then with all those changes a new object will be created and new data will be stored in that new object.
See the below examples:




All Fundamental Data types are immutable. i.e once we creates an object,we cannot perform any changes in that object. If we are trying to change then with those changes a new object will be created. 
This non-changeable behavior is called immutability.
   

In Python if a new object is required, then PVM wont create object immediately. First it will check is there any object available with the required content or not. If available then existing object will be reused. If it is not available then only a new object will be created. 
The advantage of this approach is memory utilization and performance will be improved.

But the problem in this approach is,several references pointing to the same object,by using one reference if we are allowed to change the content in the existing object then the remaining references will be effected. 

To prevent this immutability concept is required. According to this once we create an object then we are not allowed to change content. If we are trying to change with those changes a new object will be created.

Immutability is a beautiful concept to save program from data inconsistency.
In Python if you want to store similar data then object sharing happens internally
(only in case of fundamental data types) 
Suppose i want to save subject of student s1 as Python
s="Python"
For this data 'Python' an object will be created in the memory.
Now onwards, if anywhere in the program you want to save the string 'Python'  again then in that case, you will be asked to share the earlier object only. Means for similar string no new object will be created, you will be asked to go to earlier object using address of that object
This happens with all objects of fundamental datatype.
In the above example:
s1,s2,s3, and s4 are sharing a single object.

Now suppose student no. 4 wants to change subject
s4='Java'
if s4 will try to make changes in the original object then there will be inconsistency as the other students s1 s2 and s3 are also pointing to the same object but they are not changing there subject.
How can you ask them to suffer because of s4??
If we allow s4 to make changes in the original object then the other three students will definitely suffer.
So, we introduce IMMUTABILITY
We wont allow s4 to change the data in original object

If s4 wants to change data then its better we create a new object and ask s4 to refer that.
observe the below diagram:



 Few key points: