Monday, December 31, 2018

Scheduling of Linux threads based on priorities.

Thread priorities do work on most OS's but they often have minimal effect. Priorities help to order the threads that are in the run queue only and will not change how often the threads are run in any major may unless you are doing a ton of CPU in each of the threads.
Unless you have fewer cores than there are threads, you may not see any change in output order by setting your thread priorities. If there is a free CPU then even a lower priority thread will be scheduled to run.
Also, threads are never starved. Even a lower priority thread will be given time to run quite often in such a situation as this. You should see higher priority threads be given time sliced to run more often but it does not mean lower priority threads will wait for them to finish before running themselves.
Even if priorities do help to give one thread more CPU than the others, threaded programs are subject to race conditions which help inject a large amount of randomness to their execution. What you should see, is the max priority thread is more likely to spit out its message more often than the rest.

In modern computers, many processes run at once. Active processes are placed in an array called a run queue. The run queue may contain priority values for each process, which will be used by the scheduler to determine which process to run next
To ensure each program has a fair share of resources, each one is run for some time period (quantum) before it is paused and placed back into the run queue. When a program is stopped to let another run, the program with the highest priority in the run queue is then allowed to execute.

Processes are also removed from the run queue when they ask to sleep, are waiting on a resource to become available, or have been terminated.

In the Linux operating system (prior to kernel 2.6.23), each CPU in the system is given a run queue, which maintains both an active and expired array of processes. Each array contains 140 (one for each priority level) pointers to doubly linked lists, which in turn reference all processes with the given priority. The scheduler selects the next process from the active array with the highest priority. When a process' quantum expires, it is placed into the expired array with some priority. When the active array contains no more processes, the scheduler swaps the active and expired arrays, hence the name O(1) scheduler.



In UNIX or Linux, the sar command is used to check the run queue.

The vmstat UNIX or Linux command can also be used to determine the number of processes that are queued to run or waiting to run. These appear in the 'r' column.


There are two models for Run queues: one that assigns a Run Queue to each physical processor, and the other has only one Run Queue in the system


Why do we get random results (every time I run it changes):
The output from a threaded program is rarely if ever "perfect" because by definition the threads are running asynchronously. We want the output to be random because we want the threads to be running in parallel independently from each other. That is their power. If you expecting some precise output then you should not be using threads. Still, the following program will give you the desired result every time you run it.
#define _GNU_SOURCE // must be placed prior to sched.h
#include <stdio.h>
#include <linux/errno.h>
#include <pthread.h>
#include <sched.h>
#include <unistd.h>
#include <stdio.h>

void *thread1(void *null)
{
  int policy;
  struct sched_param param;

  pthread_getschedparam(pthread_self(), &policy, &param);
  // 0 - SCHED_OTHERS     1 - SCHED_FIFO          2 - SCHED_RR
  printf("\n thread 1 Current Thread Policy : %d Priority : %d\n", policy,
         param.sched_priority);

  sleep(5);
  pthread_getschedparam(pthread_self(), &policy, &param);
  // 0 - SCHED_OTHERS     1 - SCHED_FIFO          2 - SCHED_RR
  printf("\n thread 1 Current Thread Policy : %d Priority : %d\n", policy,
         param.sched_priority);
  __fpurge();
  pthread_exit(NULL);
}
void * thread2 (void *null)
{
  //printf("i am thread 2\n");
  int policy;
  struct sched_param param;

  pthread_getschedparam(pthread_self(), &policy, &param);
  // 0 - SCHED_OTHERS     1 - SCHED_FIFO          2 - SCHED_RR
  printf("\n thread 2 Current Thread Policy : %d Priority : %d\n", policy,
         param.sched_priority);
  sleep(5);
  pthread_getschedparam(pthread_self(), &policy, &param);
  // 0 - SCHED_OTHERS     1 - SCHED_FIFO          2 - SCHED_RR
  printf("\n thread 2 Current Thread Policy : %d Priority : %d\n", policy,
         param.sched_priority);
  __fpurge();
  pthread_exit(NULL);

}
int main()
{
  int inherit, policy, rc , status;
  pthread_t t1,t2;
  pthread_attr_t attr;
  struct sched_param param;

  pthread_attr_init(&attr);
  /* switch off sched inheritence from parent */
  pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);

  /* Set processor affinity */
  cpu_set_t mask;
  CPU_ZERO(&mask); // Clears set, so that it contains no CPUs.
  CPU_SET (1, &mask);  /* use only 1 CPU core */
  unsigned int len = sizeof(mask);
  status = sched_setaffinity(0, len, &mask);
  if (status < 0) perror("sched_setaffinity");
  status = sched_getaffinity(0, len, &mask);
  if (status < 0) perror("sched_getaffinity");


  /* Assign Sched policy and priority */
  policy = SCHED_FIFO;
  pthread_attr_setschedpolicy(&attr, policy);

  param.sched_priority = 10;
  pthread_attr_setschedparam(&attr, &param);

  /* create thread with choosen attribs */
  pthread_create(&t1, &attr, thread1, NULL);// t1 ie thread object gets initialised here
  pthread_create(&t2, &attr, thread2, NULL);
  /* destroy attribute object */
  pthread_attr_destroy(&attr);
  sleep(2);

/* FOR thread 2 */
  param.sched_priority = 17;
  policy = SCHED_RR;
  pthread_setschedparam(t2, policy, &param);

  /* FOR thread 1 */
  param.sched_priority = 16;
  //policy = SCHED_RR;
  pthread_setschedparam(t1, policy, &param);

  pthread_exit(NULL); // wait for all threads to terminate.

}


References:
1. http://www.informit.com/articles/article.aspx?p=101760&seqNum=2
2. https://en.wikipedia.org/wiki/Run_queue

Tuesday, January 2, 2018

Taxation on Debt and Liquid Funds

Example:
I am coming under tax bracket of 20per cent. If I invest R10 lakh in a liquid or ultra short term debt fund for 4 or 5 years how much tax do I have to pay? Please give your answer with numerical example.
- Ramesh M

If you sell them within 3 years, capital gains on debt funds are treated as short term. It will be added to your income and taxed as per your applicable tax slab. Long term capital gains (if exit is after 3 years) are taxed at 20 per cent with an indexation benefit on your cost.

Investment holding period Taxation
Short Term Capital Gain 36 months or lesser added to income and taxed as per applicable slab rate
Long Term Capital Gain more than 36 months                 20% with indexation

Let us take an example, Since we do not have the Cost Inflation Index for future years, we will take an example of past. Say you invested R10 lakhs in 2010-2011. Assuming the fund returned 9 percent a year, and you redeem it for R15,38,624 in 2016-17, your long term capital gain would be R5,38,624.

However, if you index the cost with the Cost Inflation Index (provided by the IT department) for 2010-11 and 2015-16, then the cost would be R1,52,0393 (R10 Lakh * 1081/711). Then the long term capital gain would be R18231 (1538624-1520393). A 20 percent tax on this would be R3646.

So, as per this example, your capital gains tax for this financial year will be R3646.


I want to know the taxation on liquid fund. If I sell liquid fund and generate gain, is it taxable?
- Vinod Mehra

Yes, capital gains on liquid funds are taxable.

If you sell your funds before three years (36 months), you will have to pay short-term capital gains tax. Short-term capital gains are added to your income and taxed as per the income tax slab applicable to you. If funds are held for more than three years, your gains will qualify for long-term capital gains tax of 20 per cent with indexation benefit on your original investment.

Links: http://cafemutual.com/news/guestcolumn/51-how-to-calculate-tax-on-mutual-funds

Saturday, March 25, 2017

How can I install Red Hat Enterprise Linux (RHEL) 7 in Windows 7 with a dual boot option?



Installation will be simple, like others has already mentioned it.
Few Important Points before you install Red Hat Enterprise Linux 7 on a system already installed with Windows 7 and want to have it dual boot.
1. First and foremost thing, take a backup of your Windows MBR :D , I know few non-Linux people wants to install Linux/RHEL with Windows dual boot but after few days, they want to remove Linux and just want to keep Windows. So after dual boot, one has to repair Windows in order to correct the MBR which gets replaced by RHEL MBR.
To take backup of MBR, boot your machine with RHEL DVD and go into rescue mode, once in rescue mode, choose shell, and in the shell, run the commands as follows.
# fdisk -l
This should show what is your disk name, I suppose it’s /dev/sda.
Take backup of first 512 bytes as follows. The following command is very critical, one shouldn’t mess it up.
# dd if=/dev/sda of=/tmp/WindowsMBR.backup bs=512 count=1
Then scp this /tmp/WindowsMBR.backup to some safe location from where you can download/take it at any point of time.
So, once you want to remove Linux, just come back again in rescue mode and remove Linux Partitions and just restore the MBR as follows.
# dd if=/path/to/WindowsMBR.backup of=/dev/sda
And simply reboot. You won’t need to repair Windows at all.
2. Don’t ever touch the Windows partitions while doing the RHEL7 installation.
3. If you are using UEFI mode in your System, then don’t even worry about MBR backup. UEFI maintains separate bootloader entries on first sector of the bootable PARTITION instead of the Bootable disk. The MBR is stored with the EFI files so win win situation in UEFI mode.
4. If you have already three primary partitions created in your Windows machine, and are in use, then even if your remaining disk space is 1000GB, you won’t be able to create two partitions (1 primary and another primary/extended) RHEL requires at least two partitions, 1 primary for /boot and another primary/extended for further filesystem. So if three consumed by Windows at this moment, only /boot creation is possible, extended won’t be created.
So, keep in mind, Windows must have maximum of two primary partitions. Again this applies only for Legacy Boot mode, not for UEFI mode. In UEFI mode, to store the partition table, we have a lot of disk space available, so doesn’t even matter how many primary partitions you are creating.
I think rest is fine :)

Tuesday, October 25, 2016

Affiliated Links

Excellent product. Due to High Ampere output, mobile gets charged quickly without heating it. Good Choice for Sony ,HTC,Samsung and other mobile&nbsp  

Link to sony earphones
WD My Passport 2 TB Wired External Hard Disk Drive
Lenovo PA13000 PowerBank 13000 mAh Power Bank
Healthy Buddha Organic Amla Honey (500GR)

One Plus 5 Updated Price
 




if you have any queries regarding the product i would be happy to answer your question if like the product you can purchase it from amazon from the link
Link to Windows Activators


OnePlus 5 (Slate Gray 6GB RAM + 64GB memory) Moto G5s Plus (Lunar Grey, 64GB) Shop now

Wednesday, April 27, 2016

CDAC Hyderabad DESD Study Material

Click on the following link as per your requirement 

1.For ARM material-------------------------------->  click here
2.For Device Drivers material ---------------->       click here
3.For OS material ---------------------------------->  click here
4.For Aptitude material ----------------------------> click here
5.For GSM material --------------------------------> click here
6.For JAVA material ------------------------------->  click here
7.For Rtos material -------------------------------->  click here
8.For DCN material --------------------------------> click here
9.For DSP material --------------------------------> click here

Other Important links which will add-on to your knowledge 
1.For OS Tutorials in Hindi -----------------------> click here
2.For Pre-Dac Queries  ----------------------------------> click here
3.For installing arm tool chain --------------------------> click here

if you like this post you can join my blog , by clicking on join this site also you can like my page on facebook by clicking here My Journey.
For further queries you can mail me at - kapil0123@gmail.com.

Tuesday, April 5, 2016

Installing ARM Tool Chain

Steps:-
1.  Copy ARM folder to desktop.
2. Open Terminal do cd Desktop/ARM/arm_tool_chain then press enter.
3. Type chmod 777 arm_install.sh  (Changing the permissions of arm_install.sh .). press enter.
4. Type sudo apt-get install arm_install.sh (press enter and type your password this will install your arm tool chain).
5. If above step doesn’t work use ./arm_install.sh or sudo ./arm_install.sh.  (press enter and type your password).Restart your PC.
6.To check whether arm tool chain is installed or not in command prompt type arm and press two time tab arm tool chain related file if shown means tool chain is installed otherwise it is not there.
7. If you face an error like libtercap.so.2 permission denied or something similar to that  while compiling then you just need to trace this file and change its permssions. 
8.You will find this file in filesystem/user/lib go there and search and change the permission using chmod libtercap.so.2

to download setup and respective material click here Arm tool chain and Material.
if you like this post you can join my blog , by clicking on join this site also you can like my page on facebook by clicking here My Journey.

Sunday, March 6, 2016

Acess Windows Drive on Ubuntu

Suppose i want to access the software drive as shown above in ubuntu. For that 
 


1- Open Terminal using ctrl+alt+t
2- Type cd  /media then press enter.
3- Then type cd  username/drivename. for example in my case it is cd  innovative/Softwares.
4-You can now type ls  to view files in that drive. see the screen shots for better understanding.

Please note that if you have dual boot Windows 8 or higher and Ubuntu then remember that before using above method .Your windows must be in non hibernation mode. When you restart your PC windows it is in non - Hibernation mode. if you simply shutdown windows then it always remain in hibernation.