IO redirection on shell

A simple question: When do we use ‘>’ or ‘<‘ and when do we use ‘|’ (i.e. pipe) for redirection of I/O ?
A simpler answer:

When we have to redirect to/from a file then ‘>’ or ‘<‘ is used and when we have to redirect to/from a program then ‘|’ is used.


handling interrupts stackability

An interrupt is an asynchronous event caused by devices to the kernel which must be processed urgently and in system context. Since this is not handled in process context, this cannot access u area and other process specific data structures. Only penalty that the interrupted process makes is that interrupt handling uses the time slice of the interrupted process. unfair !

Since there are several different events that may cause interrupts, so an interrupt may arrive when another one is being serviced. Again, there may be a different level of urgency associated with the interrupts. Linux gives the urgency a technical term i.e. interrupt priority level (ipl).

In the processor status word, linux stores current and previous ipl(s). When interrupt arrive, a check is made if new ipl > old ipl. If not then interrupts are saved in special registers and blocked until ipl drops enough (higher ipls get serviced). If it is, then a new context layer is created saving the process context, PSW, current ipl is set to the new ipl and handler is invoked. Old ipl is extracted from the saved PSW, again checks are made for other interrupts being available and if available, next interrupt context is unblocked and executed similarly.

I think its a poor description. I will try to improve upon that some other time.

setting effective UID/GID on linux

There are several ways to change the real or effective UID/GID of a process: –

* exec system call, when used to execute a program installed in suid (set uid) mode, changes the effective UID of the process to the effective UID of the owner of the program file. This the trick used by passwd program. Similarly, effective GID changes for the programs installed in sgid mode.

* setuid system call can be used for changing the effective UID  of the process. A process having real UID of 0 i.e. root can change the real UID as well using the same system call. Similaly, setgid can be used for setting effective and real (by root) GIDs.

UID, GID – real and effective – difference !

Reading the book by Uresh Vahalia, Unix Internals, I could understand exactly what is the concept behind real and effective UID and GID. I immediately thought of blogging it for reference.

The effective UID and effective GID affect file creation and access. During file creation, the kernel sets the owner attributes of the file to the effective UID and effective GID of the creating process. During file access, the kernel uses the effective UID and effective GID of the process to determine if it can access the file.

The real UID and real GID identify the real owner of the process and affect the permissions for sending signals. A process without superuser privilege can signal another process only if the sender’s real UID matches with the real UID of the receiver.

Child inherits the  credentials from the parent. since parent-child share the credentials, they can signal each other. CMIIW.

setting DISPLAY on linux while ssh’ing

If you are also fed up of exporting DISPLAY manually everytime you need to use gvim or anything else requiring ‘X’ then you will get a relief reading this post. you can use -X option

ssh -X [IP | Host]

If you use ssh -X, ssh will tunnel your X connection for you and automatically set up the appropriate X authentication. But its not all candies, there is a risk involved. I ran through the man page and found this

-X Enables X11 forwarding. This can also be specified on a per-host
basis in a configuration file.

X11 forwarding should be enabled with caution. Users with the
ability to bypass file permissions on the remote host (for the
user’s X authorization database) can access the local X11 display
through the forwarded connection. An attacker may then be able
to perform activities such as keystroke monitoring.

So take care.

What’s your net worth ?

The article that I just read discusses briefly about the calculation of net worth and why is it useful.

Net worth = (Total Assets) – (Total Liabilities)

Assets include –
* current account and savings
* capital in stocks and mutual funds
* debentures
* life insurance surrender cash value
* home, real estate investments
* cars/jewellery/furniture etc.

Liabilities include –
* mortgages
* car loan, home loan, college loan etc.
* credit card debts

That makes it simple to calculate the net worth and I think this is what is the accurate measure of a person’s financial success and not whether one rides a mercedes and goes for a vacation on an island.

Sequence Points in C

From the confusions in Expressions in C, as to what is legal and defined, and what is not, I read following from the C FAQs.

A sequence point is a point in time at which the dust has settled and all side effects which have been seen so far are guaranteed to be complete. The sequence points listed in the C standard are:

  • at the end of the evaluation of a full expression (a full expression is an expression statement, or any other expression which is not a subexpression within any larger expression);
  • at the ||, &&, ?:, and comma operators; and
  • at a function call (after the evaluation of all the arguments, and just before the actual call).

The Standard states that

Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored.

If there are sequence points in the expression, you can be sure that it is defined and legal. e.g. following (stupid) expression

i = i++ && i++;

and following is undefined and illegal since sequence points are missing or if you can understand the languages of common sense – it is confusing if what happens earlier –

a[i] = i++;

You can find the above information here.

This page discusses the ways to avoid confusion and be sure about these.

post/pre increment code – some digging !!

I came across the following C problem this morning. This problems are so similar in looks that i felt a deja vu. I used to think earlier that these problems are just a result of obscure thinking or for the satisfaction of an interviewer’s ego but now my view is different – they also prove as an insight into what happens inside.

What is the output of following program ?

#include <stdio.h>
int main()
int i = 2;
i = i++ * ++i;
printf(“%d\n”, i);

You can try yourself before reading further. If your answer is 10 then you are correct and need not read further, else read on.  To confess, I could not get it correct the first time. Tried my hands on modifying the problem and then reading the disassembled code to get into what’s happening. Finally, I could understand what is going on. All this effort is really worth since it saves from making false assumptions like increment is right associative (which it is not) or these are execptions (which it is not).

Disassembled code for the above C program is. [ only relevant part ]

0x08048395 <main+17>:   movl   $0x2,0xfffffff8(%ebp)
0x0804839c <main+24>:   addl   $0x1,0xfffffff8(%ebp)
0x080483a0 <main+28>:   mov    0xfffffff8(%ebp),%eax
0x080483a3 <main+31>:   imul   0xfffffff8(%ebp),%eax
0x080483a7 <main+35>:   mov    %eax,0xfffffff8(%ebp)
0x080483aa <main+38>:   addl   $0x1,0xfffffff8(%ebp)
0x080483ae <main+42>:   mov    0xfffffff8(%ebp),%eax

This shows the process clearly. It is left associative. First, i is stored on the stack. No increment is done for the expression i++ (post increment). It is incremented for the expression ++i [see addl above ]. Then this is multiplied with itself to get the value of i * ++i. This value is assigned to the i. Finally the increment pending for the first post increment is done.