Category: My Article

Interesting Facts about Macros and Preprocessors in C – GeeksforGeeks

One of the phase in compilation is pre processor replacement. In C programming there are multiple pre processor directives. The common one are include and define. There could be other directives like pragma and others which are compiler supported ones and may not be supported in all the compilers.

#include directive can be used to include the header files. If the header files are included <> brackets, the header file will be searched in the standard include paths mentioned as part of compilation with -I option. If the header files are used within double quotes “”, the current path will be used to search first before searching the standard paths.

#define will replace all the tokens with the values defined for the token.

Source: Interesting Facts about Macros and Preprocessors in C – GeeksforGeeks

Assertions in C/C++

Assertions help programmers to test conditions and abort the programs. Mainly used when the code cannot execute further if the condition is not met.

The syntax of assert is

void assert(scalar expression);

Need to include the following header which defined the
assert macro.

#include <assert.h>

Assert prints the error message to standard error
and terminates the program by calling abort(3).

code

assert( val == 0)

output

prog: some_file.c:16: some_func: Assertion `val == 0' failed.

Assertions can be turned off by defining NDEBUG macro. Adding NDEBUG will cause assert not to generate any code to print error and abort.

Recommendation is to not use the NDEBUG macro if using assert to detect error conditions as software may behave non-deterministically.

Source: Assertions in C/C++ – GeeksforGeeks

A good article on modifying the MTU.

The maximum transmission unit (MTU) of a network interface is the size of the largest block of data that can be transmitted as a single unit. Anything larger than the MTU must be broken into smaller units prior to transmission. The following link has a short passage on modifying the MTU.

Change the MTU of a network interface

If you want to change the MTU in the linux kernel source code the macro can be found in

the location

linux/include/uapi/linux/if_ether.h

The macro name is ETH_DATA_LEN and if you are increasing it you need to increase the frame length also. The macro for from length is ETH_FRAME_LEN. I did experiment to modify MTU to 1508. So I modified the ETH_DATA_LEN to 1508 and frame length to 1522.

After the same through ifconfig I was able to set the mtu to 1508.

e.g:- ifconfig eth0 mtu 1508

Setting anything beyond ETH_DATA_LEN the ifconfig will return “SIOCSIFMTU: Invalid argument”. Also changing to any values beyond 1500 ensure your network devices support.

 

How to do ssh autologin

I got a requirement in my project to enable autologin for some build scripts to get some information from remote machine through autologin. My colleagues has given steps to create identiry and rsa files and copy them to the remote machine and rename as authorize files. But that steps didnt work. Browsing the internet I got a good reference to built-in script which Linux had. These are the 2 simple steps to enable autologin.

The machine from which you want to login just type the following commands.

ssh-keygen -q -t rsa -N ” -f ~/.ssh/id_rsa

ssh-copy-id -i ~/.ssh/id_rsa yogindar@yogindar.com

this will ask for password once. Enter the same.

Now try ssh  yogindar@yogindar.com

you will find it wont ask any password.

Makefile Tip

You might have faced Makefile not continuing with rest of the commands if any of the previous commands returns exit code as -1 or any negative values. There is a simple way for make to ignore these return codes and continue the build. Just add ‘-‘ before the command for which build is failing. Ensure that this tip is used only for the case where the tool is returning failure and the objective of the tool is working fine.

Single instance in Linux applications

Sometimes you may need to write an application in lnux for which multiple instances are not allowed. There is a simple way to achieve even though you have lot of other ways like semaphores, sockets etc to achieve the same. Here is a small snippet code to achieve the same using file locking.

#include <sys/file.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

 

int pid_file = -1; int rc = -1;

pid_file = open(“/var/run/whatever.pid”, O_CREAT | O_RDWR, 0666);

rc = flock(pid_file, LOCK_EX | LOCK_NB);

if(rc) {

if(EWOULDBLOCK == errno)     {

close(pid_file); // another instance is running

return;

}

}

else {

// this is the first instance

}

 

Page 2 of 3
1 2 3