UrbanPro
true

Take BSc Tuition from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Learn BSc Computer Science with Free Lessons & Tips

Ask a Question

Post a Lesson

All

All

Lessons

Discussion

Answered on 16/03/2019 Learn BSc Computer Science

Ruhinaz

Tutor for all studying n needing students

Clear the basic studies already done in 1st n 2nd year of bsc subject then go through the syllabus and note down the things u know then u cann make ur own notes and google will help u in that as per a tutor take help of best IT tutor for hard topics who can make you understand better than 1st read more

Clear the basic studies already done in 1st n 2nd year of bsc subject then go through the syllabus and note down the things u know then u cann make ur own notes  and google will help u in that as per a tutor take help of best IT tutor for hard topics  who can make you understand better than 1st

read less
Answers 1 Comments
Dislike Bookmark

Lesson Posted on 23/12/2017 Learn BSc Computer Science

C++: Passing A Function As an Argument

Ashutosh Singh

Subject matter expert (Computer Science & Engineering) at Chegg India since June 2019. Teaching programming...

#include using namespace std; int sum(int a,int b) { return a+b; } void f2(int (*f)(int,int),int a,int b) //'*f' is a POINTER TO A FUNCTION whose RETURN TYPE is //'int' and arg type (int,int) . Here, '*f' is FORMAL PARAMETER { ... read more

#include
using namespace std;

int sum(int a,int b)
{
   return a+b;
}
void f2(int (*f)(int,int),int a,int b) //'*f' is a POINTER TO A FUNCTION whose RETURN TYPE is                                                           //'int' and arg type (int,int)  . Here, '*f' is FORMAL PARAMETER
{                                                                
  cout<<sum(a,b)<<endl;
}
int main()
{   int (*p1)(int,int)=∑          //Here, '*pf1' is ACTUAL PARAMETER
    f2(sum,10,20);                         //NAME OF A FUNCTION IS ITSELF A POINTER TO ITSELF
    f2(p1,20,79);
    return 0;

}

read less
Comments
Dislike Bookmark

Lesson Posted on 09/11/2017 Learn BSc Computer Science +4 MTech Tuition BTech Computer Science Engineering BCA Tuition Class XI-XII Tuition (PUC)

Java 9 , the new beginning

GCC

Java 9 is here! A major feature release in the Java Platform Standard Edition is Java 9 Lets see what more it offers more than its previous versions Java platform module JEP 223 : New version string scheme Moreover it includes enhancements for microsoft windows and MAcOS OS platforms ... read more

Java 9 is here! A major feature release in the Java Platform Standard Edition is Java 9

 

Lets see what more it offers more than its previous versions

 

  • Java platform module
  • JEP 223 : New version string scheme

Moreover it includes enhancements for microsoft windows and MAcOS OS platforms

 

 

read less
Comments
Dislike Bookmark

Take BSc Tuition from the Best Tutors

  • Affordable fees
  • Flexible Timings
  • Choose between 1-1 and Group class
  • Verified Tutors

Lesson Posted on 23/08/2017 Learn BSc Computer Science +1 BTech Computer Science Engineering

Quick Sort Algorithm

SR-IT Academy

SR - IT Academy is one of the leading tutorial point providing services like tutoring and computer training...

Choose the pivot (using the "median-of-three" technique); also, put the smallest of the 3 values in A, put the largest of the 3 values in A, and swap the pivot with the value in A. (Putting the smallest value in A prevents right from falling off the end of the array in the following steps.) Initialize:... read more
  1. Choose the pivot (using the "median-of-three" technique); also, put the smallest of the 3 values in A[low], put the largest of the 3 values in A[high], and swap the pivot with the value in A[high-1]. (Putting the smallest value in A[low] prevents right from falling off the end of the array in the following steps.)
  2. Initialize: left = low+1; right = high-2
  3. Use a loop with the condition:

while (left <= right)

The loop invariant is:

all items in A[low] to A[left-1] are <= the pivot
all items in A[right+1] to A[high] are >= the pivot

Each time around the loop:

left is incremented until it "points" to a value > the pivot
right is decremented until it "points" to a value < the pivot
if left and right have not crossed each other,
then swap the items they "point" to.

  1. Put the pivot into its final place.
read less
Comments 1
Dislike Bookmark

Lesson Posted on 07/08/2017 Learn BSc Computer Science +1 BTech Computer Science Engineering

Deadlocks In Distributed Systems

SR-IT Academy

SR - IT Academy is one of the leading tutorial point providing services like tutoring and computer training...

Deadlocks in Distributed Systems: Deadlock can occur whenever two or more processes are competing for limited resources and the processes are allowed to acquire and hold a resource (obtain a lock) thus preventing others from using the resource while the process waits for other resources. Two common... read more

Deadlocks in Distributed Systems:

Deadlock can occur whenever two or more processes are competing for limited resources and the processes are allowed to acquire and hold a resource (obtain a lock) thus preventing others from using the resource while the process waits for other resources.

Two common places where deadlocks may occur are with processes in an operating system (distributed or centralized) and with transactions in a database.

  • Deadlocks in distributed systems are similar to deadlocks in single processor systems, only worse:

    • They are harder to avoid, prevent or even detect.

    • They are hard to cure when tracked down because all relevant information is scattered over many machines.

  • People sometimes might classify deadlock into the following types:

    • Communication deadlocks : Competing with buffers for send/receive

    • Resources deadlocks : Exclusive access on I/O devices, files, locks, and other resources

  • Four best-known strategies to handle deadlocks:

    • The ostrich algorithm (ignore the problem).

    • Detection (let deadlocks occur, detect them, and try to recover).

    • Prevention (statically make deadlocks structurally impossible).

    • Avoidance (avoid deadlocks by allocating resources carefully).

Deadlock Detection:

Deadlock detection attempts to find and resolve actual deadlocks.  These strategies rely on a Wait-For-Graph (WFG) that in some schemes is explicitly built and analyzed for cycles.   In the WFG, the nodes represent processes and the edges represent the blockages or dependencies.  Thus, if process A is waiting for a resource held by process B, there is an edge in the WFG from the node for process A to the node for process B. 

 In the AND model (resource model), a cycle in the graph indicates a deadlock.  In the OR model, a cycle may not mean a deadlock since any of a set of requested resources may unblock the process.  A knot in the WFG is needed to declare a deadlock.  A knot exists when all nodes that can be reached from some node in a directed graph can also reach that node.

In a centralized system, a WFG can be constructed fairly easily.  The WFG can be checked for cycles periodically or every time a process is blocked, thus potentially adding a new edge to the WFG.   When a cycle is found, a victim is selected and aborted.

 Centralized Deadlock Detection:

 We use a centralized deadlock detection algorithm and try to imitate the nondistributed algorithm.

  • Each machine maintains the resource graph for its own processes and resources.

  • A centralized coordinator maintain the resource graph for the entire system.

  • When the coordinator detect a cycle, it kills off one process to break the deadlock.

  • In updating the coordinator’s graph, messages have to be passed.

  • Method 1) Whenever an arc is added or deleted from the resource graph, a message have to be sent to the coordinator.

  • Method 2) Periodically, every process can send a list of arcs added and deleted since previous update.

  • Method 3) Coordinator ask for information when it needs it.

 

False Deadlocks :

  • When the coordinator gets a message that leads to a suspect deadlock:

  • It send everybody a message saying “I just received a message with a timestamp T which leads to deadlock. If anyone has a message for me with an earlier timestamp, please send it immediately”

  • One possible way to prevent false deadlock is to use the Lamport’s algorithm to provide global timing for the distributed systems.

  • When every machine has replied, positively or negatively, the coordinator will see that the deadlock has really occurred or not.

The Chandy-Misra-Haas algorithm:

  • Processes are allowed to request multiple resources at once the growing phase of a transaction can be speeded up.

  • The consequence of this change is a process may now wait on two or more resources at the same time.

  • When a process has to wait for some resources, a probe message is generated and sent to the process holding the resources. The message consists of three numbers: the process being blocked, the process sending the message, and the process receiving the message.

  • When message arrived, the recipient checks to see it it itself is waiting for any processes. If so, the message is updated, keeping the first number unchanged, and replaced the second and third field by the corresponding process number.

  • The message is then send to the process holding the needed resources.

  • If a message goes all the way around and comes back to the original sender -- the process that initiate the probe, a cycle exists and the system is deadlocked.

  • There are several ways to break the deadlock:

  • The process that initiates commit suicide : this is overkilling because several process might initiates a probe and they will all commit suicide in fact only one of them is needed to be killed.

  • Each process append its id onto the probe, when the probe come back, the originator can kill the process which has the highest number by sending him a message. (Hence, even for several probes, they will all choose the same guy)

The Probe Scheme:

The scheme proposed by Chandy, Misra and Haas [1] uses local WFGs to detect local deadlocks and probes to determine the existence of global deadlocks.  If the controller at a site suspects that process A, for which it is responsible, is involved in a deadlock it will send a probe message to each process that A is dependent on.  When A requested the remote resource, a process or agent was created at the remote site to act on behalf of process A to obtain the resource.  This agent receives the probe message and determines the dependencies at the current site.

The probe message identifies process A and the path it has been sent along.  Thus, the message probe(i,j,k) says that it was initiated on behalf of process i and was sent from the controller for agent j (which i is dependent on) to the controller for agent k.

When an agent whose process is not blocked receives a probe, it discards the probe.  It is not blocked so there is no deadlock.   An agent that is blocked sends a probe to each agent that it is blocked by.  If process i ever receives probe(i,j,i), it knows it is deadlocked.

This popular approach, often called "edge-chasing", has been shown correct in that it detects all deadlocks and does not find deadlocks when none exist.

In the OR model, where only one out of several requested resources must be acquired, all of the blocking processes are sent a query message and all of the messages must return to the originator in order for a deadlock to be declared.  The query message is similar to a probe, but has an additional identifier so the originator can determine whether or not all the paths were covered.

There are several variations to these algorithms that seek to optimize different properties such as: number of messages, length of messages, and frequency of detection. One method proposed by Mitchell and Merritt for the single-resource model, sends information in the opposite direction from the WFG edges .

Deadlock Prevention:

Prevention is the name given to schemes that guarantee that deadlocks can never happen because of the way the system is structured.   One of the four conditions for deadlock is prevented, thus preventing deadlocks. 

Collective Requests:

One way to do this is to make processes declare all of the resources they might eventually need, when the process is first started.  Only if all the resources are available is the process allowed to continue.  All of the resources are acquired together, and the process proceeds, releasing all the resources when it is finished.  Thus, hold and wait cannot occur.

The major disadvantage of this scheme is that resources must be acquired because they might be used, not because they will be used.  Also, the pre-allocation requirement reduces potential concurrency.

Ordered Requests

Another prevention scheme is to impose an order on the resources and require processes to request resources in increasing order.  This prevents cyclic wait and thus makes deadlocks impossible.

One advantage of prevention is that process aborts are never required due to deadlocks.  While most systems can deal with rollbacks, some systems may not be designed to handle them and thus must use deadlock prevention.

  • A method that might work is to order the resources and require processes to acquire them in strictly increasing order. This approach means that a process can never hold a high resource and ask for a low one, thus making cycles impossible.

  • With global timing and transactions in distributed systems, two other methods are possible -- both based on the idea of assigning each transaction a global timestamp at the moment it starts.

  • When one process is about to block waiting for a resource that another process is using, a check is made to see which has a larger timestamp.

  • We can then allow the wait only if the waiting process has a lower timestamp.

  • The timestamp is always increasing if we follow any chain of waiting processes, so cycles are impossible --- we can used decreasing order if we like.

  • It is wiser to give priority to old processes because

    • they have run longer so the system have larger investment on these processes.

    • they are likely to hold more resources.

    • A young process that is killed off will eventually age until it is the oldest one in the system, and that eliminates starvation.

Preemption:

Wait-die Vs. Wound-wait:

  • Definition it can be restarted safely later.

i. Wait-die:

  • If an old process wants a resource held by a young process, the old one will wait.

  • If a young process wants a resource held by an old process, the young process will be killed.

  • Observation: The young process, after being killed, will then start up again, and be killed again. This cycle may go on many times before the old one release the resource.

  • Once we are assuming the existence of transactions, we can do something that had previously been forbidden: take resources away from running processes.

  • When a conflict arises, instead of killing the process making the request, we can kill the resource owner. Without transactions, killing a process might have severe consequences. With transactions, these effects will vanish magically when the transaction dies.

ii. Wound-wait:

  • If an old process wants a resource held by a young process, the old one will preempt the young process, wounded and killed, restarts and wait.

  • If a young process wants a resource held by an old process, the young process will wait.

read less
Comments
Dislike Bookmark

Lesson Posted on 05/07/2017 Learn BSc Computer Science +3 BTech Computer Science Engineering BTech Information Science Engineering BCA Tuition

What Are The Two Forms Of #Include?

Shiladitya Munshi

Well, I love spending time with students and to transfer whatever computing knowledge I have acquired...

There are two variants of #include. The one is #include and the other one is #include”file”. In general the first form that is #include is used to include system defined header files which are searched in the standard list of system directories. The second form i,e #include”file”... read more

There are two variants of #include. The one is #include and the other one is #include”file”. In general the first form that is #include is used to include system defined header files which are searched in the standard list of system directories. The second form i,e #include”file” is generally used to include user defined header files. These files are searched first in the current directory, if failed, then secondly in the standard list of system directories.

Both the forms of #include share a common property that is no escape sequence or commenting  styles are recognized within or “file”. What I mean to say is that if I write #include <x/*y>, that means I want to include a file named x/*y.  Similarly writing #include<x\y\\z> is also perfect if the file x\y\\z is needed to be included.

read less
Comments
Dislike Bookmark

Take BSc Tuition from the Best Tutors

  • Affordable fees
  • Flexible Timings
  • Choose between 1-1 and Group class
  • Verified Tutors

Lesson Posted on 05/07/2017 Learn BSc Computer Science +3 BTech Computer Science Engineering BTech Information Science Engineering BCA Tuition

What Is the Difference Between Function And Subroutine?

Shiladitya Munshi

Well, I love spending time with students and to transfer whatever computing knowledge I have acquired...

The difference is indeed very thin and it depends on the context. In general Function is a piece of code with a name, inputs (arguments) and an output (return types). That is function should always return. Some programming languages are there which uniquely maps inputs to the corresponding outputs. But... read more

The difference is indeed very thin and it depends on the context. In general Function is a piece of code with a name, inputs (arguments) and an output (return types). That is function should always return. Some programming languages are there which uniquely maps inputs to the corresponding outputs. But surely C is not such.  There is also a convention (but not a very strict one) that a function should not change any global variable. In C language, a function is always a realistic part of the code.

Subroutine is just a piece of any sorts of code with a definite entry and exit point. It should not return anything and a subroutine can always change global variables. These are generally used for conceptual purposes like in psudocodes.

read less
Comments
Dislike Bookmark

Lesson Posted on 05/07/2017 Learn BSc Computer Science +3 BTech Computer Science Engineering BTech Information Science Engineering BCA Tuition

Pointers And References

Shiladitya Munshi

Well, I love spending time with students and to transfer whatever computing knowledge I have acquired...

Are reference and pointers same? No. I have seen this confusion crumbling up among the student from the first day. So better clear out this confusion at thevery beginning. Pointers and reference both hold the address of other variables. Up to this they look similar, but their syntax and further consequences... read more

Are reference and pointers same?

No.

I have seen this confusion crumbling up among the student from the first day. So better clear out this confusion at thevery beginning.

Pointers and reference both hold the address of other variables. Up to this they look similar, but their syntax and further consequences are totally different. Just consider the following pieces of code

Code 1                                         Code 2

int i;                                             int i;

int *p = &i;                                 int &r = i ;

Here in code 1 we have declared and defined one integer pointer p which points to variable i, that is now , p holds the address of i.

In code 2, we have declared and defined one integer reference r which points to variable i, that is now, r holds the address of i. This is completely same as that of p.

So where is the difference?

The first difference can be found just by looking at the code. Their syntaxes!

 Secondly the difference will come up when they would be used differently to assign a value (suppose 10) to i.

 If you are using a pointer, you can do it like *p = 10; but if you are using a reference, you can do it like r = 10.  Just be careful to understand that when you are using pointers, the address must be dereferenced using the *, whereas, when you are using references, the address is dereferenced without using any operators at all.

 This notion leaves a huge effect as consequences. As the address of the variable is dereferenced by * operator, while using a pointer, you are free to do any arithmetic operations on it. That is you can increment the pointer p to point to the next address just by doing p++. But, this is not possible using references.  So a pointer can point to many different elements during its lifetime; where as a reference can refer to only one element during its life time.

Does C language support references?

No. The concept of reference has been added to C++, not in C. So if you run the following code, C compiler will object then and there.

 #include

#include

int main(void)

{

    int i;

    int &r = i;

    r = 10;

    printf("\n Value of i assigned with reference r = %d",i);

    getch();

    return 0;

}

 But if you are using any C++ compiler, this code will work fine as expected.

 If there is no concept of reference in C language, then how come there exists C function call by reference?

Strictly speaking, there is no concept of function call by reference in C language. C only supports function call by value. Though in some books ( I will not name any one) it is written that C supports function call by reference or the simulation of  function call by reference can be achieved through pointers, I will strongly say that C language neither directly supports function call by reference, nor provides any other mechanism to simulate the same effect.

I know you are at your toes to argue that what about calling a C function with address of a variable and receiving it with a pointer? The change made to that variable within the function has a global effect. How this cannot be treated as an example of function call by reference?

You probably argue with a code like following

 #include

#include

void foo(int* p)

{

     *p = 5;

      printf("\n Inside foo() the value of the variable: %d",*p);

}

int main(void)

{

    int i = 10;

    printf("\n before calling  foo() the value of the variable: %d",i);

    foo(&i);

    printf("\n after calling  foo() the value of the variable: %d",i);

    getch();

    return 0;

}      

  Your code will show the result as 

Your points are well taken. But the thing is what you are showing is not at all calling a function by reference. It just the function call by value! Here you are essentially copying the value of address of your variable i and calling the function foo with that copy. Now eventually in this case, the value that is being passed contains the address of another variable. Within the function, you are accepting this value with a pointer and changing the value of the content addressed by that pointer. So it is nothing but a function call by value only.

 Please note that to change the value of the content addressed by a pointer, you are to use *, no way could it be thought of as a reference.

 Now let me give you one example of true function call by reference

 #include

#include

void foo (int& r1)

{

     r1 = 5;

     printf("\n Inside foo() the value of the variable: %d", r1);

}

int main(void)

{

    int i = 10;

    int &r = i;

    printf("\n before calling  foo() the value of the variable: %d",i);

    foo(r);

    printf("\n after calling  foo() the value of the variable: %d",i);

    getch();

    return 0;

}

 Will this run with your C compiler? No.

 Note:: I have used DevC++ as the coding platform

 

read less
Comments
Dislike Bookmark

Lesson Posted on 05/07/2017 Learn BSc Computer Science +3 BTech Computer Science Engineering BTech Information Science Engineering BCA Tuition

Some Interview Questions And Answers For Fresher Level On Pointers

Shiladitya Munshi

Well, I love spending time with students and to transfer whatever computing knowledge I have acquired...

What is a void pointer? Void pointer is a special type of pointer which can reference or point to any data type. This is why it is also called as Generic Pointer. As a void pointer can point to any data type, neither it is possible to dereference a void pointer, nor is it possible to do any arithmetic... read more

What is a void pointer?

Void pointer is a special type of pointer which can reference or point to any data type.  This is why it is also called as Generic Pointer.

As a void pointer can point to any data type, neither it is possible to dereference a void pointer, nor is it possible to do any arithmetic operations on void pointer. Hence an explicit type casting is absolutely must for using a void pointer.

What is a null pointer?

A null pointer is also a special type of pointer which points definitively to nothing.

As per official C language description, every valid type of pointer has a special value called “Null Pointer” and this value is always distinguishable from all other pointer values and it is guaranteed to compare unequal to a pointer of any object or function.  

Hold it for a second! So what you are saying is there are different versions of null pointers for every pointer type. Is it so?

No. We cannot call it as a “different versions” rather their internal values are different. But as a programmer, we need not to worry about it because compilers take care of and track the internal values (may be differently depending on the compiler types)

So why don’t you say that a null pointer is a pointer which is not initialized yet?

I cannot go with this idea because an uninitialized pointer may point to any thing. There might be an uninitialized char pointer, an uninitialized int pointer and so on. But a null pointer is definitely something which is there not to point anything.

Ok. Now tell me what is a dangling pointer?

Dangling pointers are the pointers which point to already de allocated or de referenced memory locations. Suppose I write a piece of code like this

int*p;

p = (int*)malloc(n*sizeof(int);

----

----

free(p);

In this piece of code, after allocating some memory to p, we are de allocating the memory which was initially pointed by p. Hence now onwards, p acts as dangling pointer.

How to rectify this?

In this case, after de allocation that is after using free() we should set the pointer p to null as p = NULL   

Could you please exhibit any other source of dangling pointer problems?

Well, there might be other sources also, like

Suppose I have declared int*p, and within a specific scope, I have defined another integer x as 10; and then within that specific scope I am making the pointer p to hold the base address of x. Now outside this specific scope the p will act as a dangling pointer. I can write the code as

void foo (void)

{

   int *p;

   {

          {

int x = 10;

                 p = &x;

          }

          // here p will act as dangling pointer

   }

}

Another case might occur when from a function, we return an address of any variable and a corresponding pointer in the receiver section receives it, then the receiving pointer might get into dangling pointer problem, as just after the returning, as the variable has passed it scope, the memory stack (where the variable is stored currently) might be refreshed. But this phenomenon is not obvious, this might happen.

Fine, so how to deal with this second case?

To deal with this, to be in safer side, we must have the scope of the variable intact out of the function call; declaring it as static might be an option.

read less
Comments
Dislike Bookmark

Take BSc Tuition from the Best Tutors

  • Affordable fees
  • Flexible Timings
  • Choose between 1-1 and Group class
  • Verified Tutors

Answered on 22/03/2017 Learn BSc Computer Science +3 Computer Architecture BTech Tuition BTech Computer Science Engineering

Kousalya Pappu

Tutor

Just register yourself on UrbanPro.
Answers 38 Comments 1
Dislike Bookmark

About UrbanPro

UrbanPro.com helps you to connect with the best BSc Tuition in India. Post Your Requirement today and get connected.

Overview

Lessons 29

Total Shares  

+ Follow 78,326 Followers

You can also Learn

Top Contributors

Connect with Expert Tutors & Institutes for BSc Computer Science

x

Ask a Question

Please enter your Question

Please select a Tag

X

Looking for BSc Tuition Classes?

The best tutors for BSc Tuition Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Take BSc Tuition with the Best Tutors

The best Tutors for BSc Tuition Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more