The Best resources for …

If you are like me , who loves Computer Science for all its history ,logic and depth it packs , chances are you are not satisfied while studying a topic , until you have reached its core. And therefore I present to you , the resources that I loved the most while studying C , data structures and algorithms :

C : http://cslibrary.stanford.edu/101/ (All pdfs ,each one on this page is a classic in itself , esp the pointers and memory – I can read it like a romantic novel anyday!)

Heaps and Heapsort : MIT 6.006 https://www.youtube.com/watch?v=B7hVxCmfPtM

Dynamic Programming : https://www.youtube.com/watch?v=oBt53YbR9Kk&t=181s (freeCodeCamp.org)

To be continued…

Interesting bits

Did you know , Mergesort was invented by John von Neumann in 1945.

Embedded programmers know John von Neumann as the computer scientist who proposed von Neumann architecture. However ,John von Neumann was not only a computer scientist but also a mathematician, physicist engineer and polymath.He published 150 papers in his life- 60 in pure mathematics, 60 in applied mathematics, 20 in physics ,and the remainder on special mathematical subjects or non mathematics one ( source : Wikipedia articles)

How to install tensorflow in Anaconda

Link for Installing tensorflow on Windows for Anaconda ( and pip also)

For more details on creating virtual environment in Anaconda ( first step in installing tensorflow on Anaconda) , this link here is quite helpful.

 

 

Machine Learning

I am going to add another menu item on blog and that is machine learning, proud to have navigated through “mathematical “and “equational” maze of basics of Machine Learning.

While I hope to present the basics of ML in a concise form soon  , I really liked the following articles to understand few concepts – in the most simple of language .

It’s good if you read these and then go for some specific course , eg the one by Andrew Ng on Coursera .

Linear Regression using gradient descent 

Logistic Regression detailed overview 

 

~Happy M-Learning !

 

 

Re-entrant functions

When is a function re-entrant ?

“Robert Love” himself answers the question on Quora 🙂

To quote  him:

When is a function reentrant?

A function is reentrant if it can be invoked while already in the process of executing. That is, a function is reentrant if it can be interrupted in the middle of execution (for example, by a signal or interrupt) and invoked again before the interrupted execution completes.

For example, the following function is not reentrant, because the observed value of the summation depends on when and where the function is interrupted (or, in the case of multithreading, how two or more threads race into the function):

static int sum = 0;
int increment(int i) {
  sum += i;
  return sum;
}

We can make this function reentrant by making the sum not a global variable and instead requiring the caller to maintain it:

int increment(int sum, int i) {
    return sum + i;
}

ctim(),gmtime(),and strtok()are examples of functions in Standard C that are (or at least often are) not reentrant. Later versions of the standard have created reentrant versions, for example strtok_r() with the primary difference that the caller passes in storage instead of the function maintaining it globally, as above.

How does that relate to it being thread-safe?

The term reentrancy predates threads and is often used only in a single threaded context, for example when discussing if a function is signal or interrupt-safe. Reentrancy and thread-safety are not the same and should not be confused.

Although a reentrant function is more likely to be thread-safe all else equal, a reentrant function need not be thread-safe, and a thread-safe function need not be reentrant. For example, reentrant functions that operate on the same data (same inputs) may not be thread-safe. Similarly, a thread-safe function that achieves safety through a mutex is potentially not reentrant, as the interruption could deadlock on the already-acquired mutex.

To summarize, we might define reentrancy and thread safety in similar terms, but with slightly different semantics:

A reentrant function can be invoked, interrupted, and re-invoked. Such a function can be invoked simultaneously by multiple threads if and only if each invocation references or provides unique data and inputs.

A thread-safe function can be invoked simultaneously by multiple threads, even if each invocation references or provides the same data or input, as all access is serialized.

Writing reentrant code.

Writing code that is reentrant is not hard. The styles and patterns adapted by programmers since the mid-1990s are largely reentrant. Indeed, most programmers today balk at solutions to problems such as the approach adopted by

strtok()

, even if not concerned with reentrancy or thread safety.

Basic guidelines:

  • Do not access mutable global or function-static variables.

  • Do not self-modify code.

  • Do not invoke another function that is itself non-reentrant.

Reference : https://www.quora.com/When-is-a-function-reentrant-How-does-that-relate-to-it-being-thread-safe

Mutex and Semaphores

So , what are mutex and semaphores ?What is the difference between mutex and semaphores ? And,what is the difference between a mutex and a binary semaphore.

These are the questions we are going to explore in this article.

To start with , let me quote the famous Bathroom example which is a simple way to remember the difference between mutex and semaphore :

Mutex vs. Semaphore, what is the difference?

The Toilet Example  (c) Copyright 2005, Niclas Winquist 😉

Mutex:

Is a key to a toilet. One person can have the key – occupy the toilet – at the time. When finished, the person gives (frees) the key to the next person in the queue.

Officially: “Mutexes are typically used to serialise access to a section of  re-entrant code that cannot be executed concurrently by more than one thread. A mutex object only allows one thread into a controlled section, forcing other threads which attempt to gain access to that section to wait until the first thread has exited from that section.”
Ref: Symbian Developer Library

(A mutex is really a semaphore with value 1.)

Semaphore:

Is the number of free identical toilet keys. Example, say we have four toilets with identical locks and keys. The semaphore count – the count of keys – is set to 4 at beginning (all four toilets are free), then the count value is decremented as people are coming in. If all toilets are full, ie. there are no free keys left, the semaphore count is 0. Now, when eq. one person leaves the toilet, semaphore is increased to 1 (one free key), and given to the next person in the queue.

Officially: “A semaphore restricts the number of simultaneous users of a shared resource up to a maximum number. Threads can request access to the resource (decrementing the semaphore), and can signal that they have finished using the resource (incrementing the semaphore).”
Ref: Symbian Developer Library

The author thus concludes that the correct use of a semaphore is for signaling from one task to another. A mutex is meant to be taken and released, always in that order, by each task that uses the shared resource it protects.

However , there is a controversial statement here “(A mutex is really a semaphore with value 1.)” .

As I see, even author has updated this with info “(* – Please note, that some web posts indicate, that this statement is not quite accurate”

And that is what , we are going to discuss here further in detail.

This blog post on semaphores on Feabhas  take this statement heads on .It also contains the history information of semaphores which I also intended to visit here , to clarify the difference.

The blog post says :

Back in 1965, Edsger Dijkstra, a Dutch computer scientist, introduced the concept of a binary semaphore into modern programming to address possible race conditions in concurrent programs. His very simple idea was to use a pair of function calls to the operating system to indicate entering and leaving a critical region. This was achieved through the acquisition and release of an operating system resource called a semaphore. In his original work, Dijkstra used the notation of P & V, from the Dutch words Prolagen (P), a neologism coming from To try and lower, and Verhogen (V) To raise, To increase.

With this model the first task arriving at the P(S) [where S is the semaphore] call gains access to the critical region. If a context switch happens while that task is in the critical region, and another task also calls on P(S), then that second task (and any subsequent tasks) will be blocked from entering the critical region by being put in a waiting state by the operating system. At a later point the first task is rescheduled and calls V(S) to indicate it has left the critical region.The second task will now be allowed access to the critical region.

A variant of Dijkstra’s semaphore was put forward by another Dutchman, Dr. Carel S. Scholten. In his proposal the semaphore can have an initial value (or count) greater than one.This enables building programs where more than one resource is being managed in a given critical region. For example, a counting semaphore could be used to manage the parking spaces in a robotic parking system. The initial count would be set to the initial free parking places. Each time a place is used the count is decremented. If the count reaches zero then the next task trying to acquire the semaphore would be blocked (i.e. it must wait until a parking space is available). Upon releasing the semaphore (A car leaving the parking system) the count is incremented by one.

Scholten’s semaphore is referred to as the General or Counting Semaphore, Dijkstra’s being known as the Binary Semaphore.

The article goes on to discuss some dangers of using semaphores which we’d discuss in a later post.

Also , if you go on to read the comments of this blog post , you’d notice that Michael Barr has pointed out that mutex is what is required in the parking space example. And that’s what we’d be discussing shortly when we reach to the part where we delve into Michael Barr’s article.

Meanwhile , this second post of Feabhas on mutex defines the mutex as :

The mutex is similar to the principles of the binary semaphore with one significant difference: the principle of ownership. Ownership is the simple concept that when a task locks (acquires) a mutex only it can unlock (release) it. If a task tries to unlock a mutex it hasn’t locked (thus doesn’t own) then an error condition is encountered and, most importantly, the mutex is not unlocked. If the mutual exclusion object doesn’t have ownership then, irrelevant of what it is called, it is not a mutex.

Further , article states how mutex overcomes the dangers of semaphores.As I mentioned,we’d discuss that in a separate article as this one deals with the difference between mutex and semaphore.

The author has also addressed the different points raised in the comments on his part 1 Semaphores , by giving examples on how different OSes have realized the mutex and semaphore. After all, it all comes down to what OS designer thinks what’d work best for him.

Part 3 of the series covers the problems which even a mutex does not address.

Going further , this article of Michael Barr on  mutexes and semaphores shuns up the whole bathroom example.

It is easiest to explain why the “multiple resource” scenario is flawed by way of an analogy. If you think of a mutex as a key owned by the operating system, it is easy to see that an individual mutex is analogous to the bathroom key owned by an urban coffee shop. At the coffee shop, there is one bathroom and one bathroom key. If you ask to use the bathroom when the key is not available, you are asked to wait in a queue for the key. By a very similar protocol, a mutex helps multiple tasks serialize their accesses to shared global resources and gives waiting tasks a place to wait for their turn.

This simple resource protection protocol does not scale to the case of two equivalent bathrooms. If a semaphore were a generalization of a mutex able to protect two or more identical shared resources, then in our analogy, it would be a basket containing two identical keys (i.e., each of the keys would work in either bathroom door).

A semaphore cannot solve a multiple identical resource problem on its own. The visitor only knows that he has a key, not yet which bathroom is free.  If you try to use a semaphore like this, you’ll find you always need other state information—itself typically a shared resource protected via a separate mutex. 2 It turns out the best way to design a two-bathroom coffee shop is to offer distinct keys to distinct bathrooms (e.g., men’s vs. women’s), which is analogous to using two distinct mutexes.

It then elaborates what is the correct usage scenario of a semaphore which is critical to understand the difference between the two

The correct use of a semaphore is for signaling from one task to another. A mutex is meant to be taken and released, always in that order, by each task that uses the shared resource it protects. By contrast, tasks that use semaphores either signal or wait—not both. For example, Task 1 may contain code to post (i.e., signal or increment) a particular semaphore when the “power” button is pressed and Task 2, which wakes the display, pends on that same semaphore. In this scenario, one task is the producer of the event signal; the other the consumer.

To summarize with an example, here’s how to use a mutex:

/* Task 1 */
   mutexWait(mutex_mens_room);
      // Safely use shared resource
   mutexRelease(mutex_mens_room);

/* Task 2 */
   mutexWait(mutex_mens_room);
      // Safely use shared resource
   mutexRelease(mutex_mens_room);

By contrast, you should always use a semaphore like this:

/* Task 1 - Producer */
    semPost(sem_power_btn);   // Send the signal

/* Task 2 - Consumer */
    semPend(sem_power_btn);  // Wait for signal

Importantly, semaphores can also be used to signal from an interrupt service routine (ISR) to a task. Signaling a semaphore is a non-blocking RTOS behavior and thus ISR safe.  Because this technique eliminates the error-prone need to disable interrupts at the task level, signaling from within an ISR is an excellent way to make embedded software more reliable by design.

And that is why you’d understand why Michael Barr did not agree with Feabhas’s blog’s example of multiple parking space problem solution through a counting semaphore ( or for that matter any example that suggests that semaphores can be used for multiple resources problem by just decrementing the count of semaphore)

What Michael Barr’s article does not cover in this article is the usage scenario and mechanism of counting semaphore .

It seems like someone else did follow up on this on stack overflow in this thread.

You need to go through both of the answers given on that link to understand how counting semaphores are used in producer-consumer problem.To cut the long story short , you’d require couple of semaphores together to make sure the producer consumer problem works error free.

Meanwhile,a paragraph from “Operating Systems” by William Stallings

To begin, the semaphore has a zero or positive value.If the value is positive,that value equals the number of processes that can issue a wait and immediately continue to execute.If the  value is zero, either by initialization or because a number of processes equal to the initial semaphore value have issued a wait.the next process to issue a wait is blocked,and the semaphore value goes negative.Each subsequent wait drives the semaphore value further into minus territory.The negative value equals the number of processes waiting to be unblocked.Each signal unblocks one of the waiting processes when the semaphore value is negative.
For both counting semaphores and binary semaphores, a queue is used to hold processes waiting on the semaphore.The question arises of the order in which processes are removed form such a queue.The fairest removal policy is  first-in-first-out (FIFO).The process that has been blocked the longest is released from the queue first;a semaphore whose definition includes this policy is called a “Strong Semaphore”.A semaphore that does not specify the order in which procceses are removed from the queue is a “weak semaphore”.
“A concept related to binary semaphore is the mutex.A key difference between the two is that the process that locks the mutex (Sets the value to zero) must be the one to unlock it.In contrast, it is possible for one process to lock a binary semaphore and for another to unlock it”
In some of the literature and in some textbooks, no distinction is made between a mutex and a binary semaphore.However, in practice, a number of operating systems such as Linux , Windows and Solaris offer a mutex facility that conforms to the definition in this book”
And the bonus for you 🙂
So , our take-aways here are difference between mutex and binary semaphore and usage scenarios of mutex, binary semaphore and counting semaphore.
Links for references :

Effective presentation skills-Part 1

This is one topic you might have read about many times . But , still there is always a scope of improvement , isn’t it ? 🙂

Presentation is something – love it or hate it, you can’t escape it . You might be the “geekiest” guy around , but when it comes to presenting what you know about – it all comes down to the delicate balance you can strike between your soft skills and your technical skills.

As you’d have noticed , I am talking about technical guys here . This is assuming those with management degree get quite a good exposure to brushing their soft skills by the time they join industry. In India ,specially , students from technical degree colleges suffer from a lack of this exposure . So , this particular post , I am writing , keeping in mind a technical presentation for someone who’d like to brush up his basics before heading to make a presentation on a technical topic to make sure it holds up everyone’s interest( i.e. you don’t find anyone napping in the back or worse,front),everyone gets a good understanding of the topic you are about to present and gets how good you are at it ;).

So ,let’s begin.

First ,choose the time of your presentation intelligently . I believe post lunch is not a good time – everyone has a tendency to fall asleep 😛 .Mornings are a great time, everyone is quite receptive for new ideas.Try to choose time , what you think works best to get everyone at their attentive best.

Next is , the most important part of the presentation ie “presentation” itself , ie your ppt as you’d say 🙂 .The content .

While we want to put every ounce of our knowledge out there , we have to take care of a few basic things before plunging into it :

1) Know your audience : First , your presentation content should suit your audience. So , you have to know the target audience first . What is their knowledge level on the subject .If they are novice or new to the topic, you ‘d have to break things down in terms understandable to them.If  they are subject matter experts already , you are free to take jargon to whatever height you may want.It might not be a good judgment on the part of organizer to have a mixed audience , but based on requirement or situation , that can be the case too : in that case you need to start up with the basics and then shoot off in the advanced territory.

2) Know the time limits : Whether it is a 1 hour presentation , 2- hour , full day ? The details you put in the content would have to vary accordingly .Let us just say its a 1 hour presentation ,you ‘d have to make sure you are not having number of slides running in 100s (or 90s or 80s for that matter) for such a small time.

3) Graphics : Add diagrams,tables ,visual representations wherever you can .If you have an alternative to present the information that is captured in long text sentences , into a table – go for the table . It makes information easier to understand and grasp.

4) Work on the agenda – outline the agenda – Content should be logically tied together and coherent and in flow, ie one topic should follow the previous one logically.For example , if you were talking about pollution , you ‘d start with what pollution is ? ->how is it caused ->what are the bad effects ->what are the possible solutions .The example is pretty out of context for a technical guy, but you get the gist , don’t you ?

5) Before you dive right into the technical details , a slide should outline the agenda This will give your audience a mental picture of what all is going to be covered.

Well, as my post says its Part 1 , so I am going to cover the rest in a following post soon after , this ‘s make this one short and sweet 🙂

Next one would talk about the soft skills and a tip that I learnt from a soft skill trainer and works 100% of the times in case of a large audience (~50-60).

Hope it was of some help .

Thanks for Reading.

Deeksha

Book recommendation -Designing Embedded Hardware

And I bring to you the book that was recommended by my first mentor and lead at my first job as a fresher. He used to read it . And when I read it , it was like , the answers to all my questions ( as a new fish in the world of embedded systems ) were all there .Page by page . I could not keep it down.

And then when it was my turn to mentor the new joinees , I googled that book name again to gather the complete information.

And then , it was time to own my personal copy , for there is no bigger treasure than knowledge ( and no greater smell than smell of books 🙂 )

And here it is Desigining Embedded Hardware by John Catsoulis.

I got my copy from Flipkart for 650 bucks.

Happy Reading!

Deeksha

[Edit ] My article on time management

I have published an article on ” Tips for time management” on Linkedin.Please visit my Profile on Linkedin to read the same.

I realize that as soon as you publish multiple posts on Linkedin , itys hard to track the previous ones.So , I decide to keep the copy of the article “Time Management” I published on Linkedin here .

Happy reading!

————————————————————————————————————————————————-

As one moves to mid-management or higher management role, it becomes imperative for him to manage his time more wisely .

How effectively we manage our time , has a very huge role in shaping our career. In such roles, an individual has to deal with not only his own career goals , but his organizational goals as well (which again have a direct impact on one’s own career goals as well)

So , here are a just few tips which I have started experimenting with off-late , and as I progress on this trajectory of trial and error , trying to refine my techniques of time-management , I definitely see improvement in my time-utilization – in the sense , that now I have a fair enough idea of where am I spending my time during the day, what percentage is effectively getting utilized and also giving me an insight on what are my areas of improvement .These points are based on my understanding from reading various online materials and go like below in a condensed form :

1)Plan-Make a list : The first thing to do as one enters the office and settles down should be to plan the day ahead.Hardly a 5-10 minutes activities , it will give you a mental picture of how your day is going to turn out to be; as a result you are better prepared for it – you can avoid outstretching an activity , if you already have an idea of what number of important activities lie ahead , how much time they require and if they are more important than this one.Jot down all the activities in a list and keep referring to it.

2)Prioritize- Set goals : To make your plan an effective one ,it is important that your activities are sorted in order of their priorities.And how do we decide which has more priority than others? It can be based on two parameters , one is the deadline /urgency and other is the goals : one must have a clear understanding of one’s goals – what are the most important goals and which task caters to which goal .I think understanding of goals comes with time , mindfulness and practice . When we start thinking in this direction on a regular basis : evaluating our activities , analyzing constantly which direction we want to take our career to , is when clarity starts dawning upon us gradually.

Coming back to our point,the tasks should be sorted based on their priority and doing a rough time division among them would be a bonus.

3)First hour most productive : I have observed that first hour at office is generally most productive ( applies especially to those who reach office earlier than others ), because of two reasons : 1)we are at our fresh-est best ,brimming with energy 2)there are little distractions because the colleagues haven’t started their work yet , there will be lesser calls for meetings etc .Also , you’d be better prepared for status meetings etc if you have already spent some quality time on your office work.

4)Coffee break later , procrastinated work first :This goes in conjunction with previous point.Earlier as soon as I reached office , I’d enter the cafeteria to catch up with my peers , over a coffee break ,and this could last 5 mins also or 30 mins as well .I decided to try using this first hour for the work that I dreaded the most during that day , or had the most tendency to procrastinate and after spending an hour or two on the same , I was ready for a guilt free coffee break .This technique has helped me a lot.

5)Use MS office /Other professional SW tools,Calendars , monthly planners to organize your work : These help , they are there for a reason.

6)Identify an activity that helps you in technical/managerial growth and make sure to spend a stipulated amount of time on that activity everyday .I have heard complains from many friends that they don’t seem to be learning fruitful on the job because they are doing similar work everyday .I have felt similarly at some occasions and beaten this feeling by picking up a topic and doing R & D on that in my spare time which would pertain to my role role too.Depending upon your role , you can identify any such activity too.This will be good for your organization as well , if you are constantly learning and broadening your horizons.

7)Talk to your role models : Role models and mentors are very important in corporate life. One learns a lot from his observations of his surroundings.Get inputs from them on how they manage their time.

8)Read books and online materials, gather ideas on how to manage time

9)Identify and try to eliminate time-wasting activities gradually from your routine

Disclaimer : These are my personal opinions , based on various materials I have read on internet , which I could not quote here , since I have read them over the time and assimilated these tips in my daily routine. Every person is not same , so , one might need to customize these according to his own work style (eg I know people who are able to concentrate on their work better if they stay late after everyone has left the office) , but there is no harm in experimenting , right?Also, whatever you decide for you works best for your time management , make sure it enables you to have a healthy work-life balance , keeps your stress free , and healthy.

Also the list is of course not exhaustive ,I hope people share in comments section their own time-management tricks that have helped them a lot .

Meanwhile ,the take-away from this article for you should be a motivation to start working upon your time-management skills starting now.

Smiles

Deeksha

[Currently Reading]How to Learn Management from your wife

I am currently reading this book “How to learn management from your wife ” by Sharu Rangnekar  ,(Flipkart link here ) and finding it to be a great read till now. The title and the illustration at the cover of the book is quite catchy and this is for the first time , I was sold to a book by these two factors. So , I have reasons to believe that the author’s management principles are in the right place 🙂

how_to_learn_mgt

Another reason to get down to browse through this book was an anticipation of more witty images inside the book 🙂

In this book , by wife , the author implies the woman of the house and her household management tricks. He reveals the day-to-day management principles by elucidating various examples from day to day life where women manage the kids , sibling rivalry at home , the daunting mother-in-law, budget of the house in limited income of husband , and even takes a cue from her techniques on how she never runs out of household or grocery items and draws parallel with inventory management techniques prevalent in industry!

Not only the observation of author is impeccable but also , how he fits the example from the household in the same context and relevance as a situation faced by manager , and provides invaluable insights on how he could manage the sub-ordinates , maintain a team work and so on.The bonus is the humour peppered throughout the book.

Thoroughly enjoying the book and the fact that occasionally I can read out some para to my husband , and make him realize “How to Learn Management from your wife”