Showing posts with label technical. Show all posts
Showing posts with label technical. Show all posts

Monday, July 9, 2012

Tip: traversing arrays in C-Shell

Below I have described two methods to traverse arrays in C-Shell - one uses the foreach loop and other while loop.
In both cases, the important thing to note is that array index in C-shell starts with '1' instead of '0' as in most programming languages, like C, C++, Java, Perl.


set a = (1 2 3 4)
set b = (5 6 7 8)
 

Method 1 - using the foreach loop, iterating on one array as the foreach index, and the accessing the other array inside the loop body using the index operator "[]"
set i = 1
foreach x ( `echo $a` )
  echo "x = $x b = $b[$i]"
  @ i = $i + 1
end
 

Output: 
x = 1 b = 5
x = 2 b = 6
x = 3 b = 7
x = 4 b = 8

Method 2 - using the while loop. Iterating on the size of array and accessing both the arrays inside the loop body using the index operator "[]"


set i = 1
while ($i <= 4)
  echo "a = $a[$i] b = $b[$i]"
  @ i = $i + 1
end
 

Output:
a = 1 b = 5
a = 2 b = 6
a = 3 b = 7
a = 4 b = 8




Wednesday, February 15, 2012

gdb stops at SIGPIPE



By default, gdb captures SIGPIPE of a process and pauses it. However, some program ignores SIGPIPE. So, the default behavour of gdb is not desired when debugging those program. To avoid gdb stopping in SIGPIPE, use the folloing command in gdb:

handle SIGPIPE nostop noprint pass

Monday, February 14, 2011

gdb: script

Recently I discovered the easy way of debugging in gdb - GDB scripting. Often while debugging I got stuck wondering what the value of an item in a complex data structure was. Till a few days ago, I used to modify the code to add debug messages and then recompile and then rerun. This wasted a lot of time.
Then one day after having wasted a lot of time, in recompile and rerun, I decided to use GDB scripts (which I had known since long, but had never used). And it is really fun now. I save a lot of time in debugging.
A sample script that I used to count the number of elements in a linked list (which we use a lot in our code) is below:

define cntList
set $l = $arg0
set $i = 0
if ($l == 0)
printf "NULL list found\n"
else
set $n = $l
while ($n != 0)
set $i = $i+1
set $n = $n->next
end
end
printf "list size %d\n", $i
end
document cntList
count the number of elements in the list
Usage: cntList ptr
end

Tuesday, December 14, 2010

Nube Technologies in news

Sonal's company Nube Technologies (earlier known as Meghsoft) found mention in this post about companies and start ups working in Cloud Computing from Jeff Hammerbacher.
Way to go Sonal!!

Monday, July 26, 2010

Unable to move windows in KDE

Many times I have faced a problem where I am not able to move or resize windows in a KDE session. So must have you and wondered how to fix it.

Now there is help available to fix this issue:
kwin -replace

will replace the window manager and voila you can now resize and/or move the windows in KDE

Wednesday, July 14, 2010

using sed in alias in C shell

Using sed commands in your .alias file in C shell is always tricky. One example of the same is:

alias grf 'set fileNline = `echo "\!*" | sed -e "s/\([^ ]*\):\([0-9]*\):.*/+\2 \1/"`;gvim $fileNline'

This alias is called as:

grf file:line_num:

and then it open the file "file" in gvim with cursor at line "line_num".

Saturday, April 25, 2009

Ways to extract data from a space delimited string

Ways to tokenize a string if you cannot use the space character as a field delimiter in an input where the fields can have spaces:

1. create the input with the delimiter as some other character as the delimiter. The delimiter character should be non-printable. This would reduce its chances of occuring in the input and thus reduce handling of special cases.

2. If the input was generated with space as the delimiter then we have a problem at hand. For such cases there are two approaches and both require the knowledge of the format of the input.

If the format of the input is known then one can use regular expressions to search for the tokens in the string. (Typical scripting languages, like Perl, TCL support regular expressions. C++ user can use the Boost library for regular expression support).

However using regular expressions can be expensive if the number of searches during the program execution are large. So these can be used only when the number of searches are small.

For programs that do such search more often, let us understand the other approach using an example:

// input format:

char* inputStr = "12 abc def 14";
char* firstSpaceChar = strchr(inputStr, ' ');
int firstInt = 0;
int lastInt = 0;
string midStr = "";
if (firstSpaceChar != NULL) {
*firstSpaceChar = '\0';
firstInt = atoi(inputStr);
char* lastSpaceChar = strrchr(firstSpaceChar+1, ' ');
if (lastSpaceChar != NULL) {
lastInt = atoi(lastSpaceChar+1);
*lastSpaceChar = '\0';
}
midStr = firstSpaceChar+1;
} else {
midStr = inputStr;
}



Tuesday, March 25, 2008

STL: find in a list of pointers

Most STL tutorials will not help you solve this problem:
How to search in a list of pointers?
e.g vector<'obj*'>myList;
The find alogrithm on this will match only the pointer address and if you do not share your objects between classes then your find will always fail as it matches only the pointer addresses and not the value to which it points.
To fix this problem, the solutions that can be considered are:
1. store objects instead of pointers in the list. (huge memory overhead!!)
2. use find_if instead of find and write a functor which compares value instead of pointer address
3. Overload find and implement your own comparator in it. This comparator again compares the value instead of pointer address.

Saturday, March 8, 2008

The basics of Technology Mapping in Digital Circuits

1. Generate the truth table of a given boolean expression
2. Represent the expression in terms of minterms
3. Convert each component into one of the cells of technology map.

Example 1:
a 8x1 multiplexer using a 2x1 mux:
8x1 --> a'b'c'I0 + a'b'cI1 +a'bc'I2 + a'bcI3 + ab'c'I4 + ab'cI5 + abc'I6 + abcI7
==> a'(b'c'I0 + b'cI1 + bc'I2 + bcI3) + a(b'c'I4 + b'cI5 + bc'I6 + bcI7)
==> a'(b'(c'I0 + cI1) + b(c'I2 + cI3)) + a(b'(c'I4 + cI5) + b(c'I6 + cI7))
==> a'(b'M1 + bM2) + a(b'M3 + bM4)
==> a'M5 + aM6
==> M7

Example 2:
AND gate using a 2x1 mux:
AB --> AB + A'0
So the mux will have A as the select line and B on select 1 and tie 0 on select 0

Example 3:
OR gate using a 2x1 mux:
A + B ==> 1(A + B)
==> (A + A')(A + B)
==> AA + AB + A'A + A'B
==> A + AB + A'B
==> A(1 + B) + A'B
==> A1 + A'B
So the mux will have A as the select line and tie 1 on select 1 and B on select 0

Thursday, January 24, 2008

Techy: Questions - hints

Hints to questions asked in my earlier post Techy: Questions

  1. The random function in Unix gives a number between 0 and RAND_MAX (which is fairly large). So starting with the 1st index as seed, call random function K-1 times with the seed for each call being the output of the previous call of the random function. Once these K numbers are available, sort them and iterate on the list using these numbers.
  2. Try using a modification of the quicksort alogrithm, with the pivot at the kth item. Pivots in subsequent iterations will also be the kth item of the original list. The actual location of the pivot in the sublist will depend on the size of the sublist and of any sub lists which have smaller entries than the sublist under consideration.

Wednesday, January 23, 2008

Techy: Questions - 2

1. Given two linked lists. Find whether they converge or not.
2. If the lists converge then find the point of convergence
3. How to find if a rectilinear polygon is clockwise or anticlockwise, given the coordinates of all the points in order.

Monday, December 31, 2007

Techy: Questions

Q1. Find k random elements from a very long linked list. The size of the list is not known.
[Update: if the size of the list is given then how does the answer change]

Q2. Find the kth smallest element from a list of integers.

Friday, October 26, 2007

Techy: CPU affinity on Linux

How do you assign a process to a specific CPU on a multiprocess Linux box?

Ans: Use "taskset". There are C functions for this command also, which can be see using "man taskset"

Thursday, August 23, 2007

Tuesday, August 14, 2007

Technical: CPU Affinity | Linux Journal

Interesting article on how to bind a process to a particular processor on a linux machine: CPU Affinity Linux Journal
Gives out a small C program which helps one bind a process to a processor.