Tuesday, December 5, 2023

Optimize you working enviorenment : Single command to create & move to a directory in linux (C Shell, Bash)

Usually move to a directory just after creating is bit of a anxious task specially if the directory name is too long.

mkdir long-name-of-directory 
cd long-name-of-directory

I checked what are the shourcuts for this. specially is there an one line command to both create and move in to the directory. Actually there is no one line command. But we can create one in our shell. 

If you are using bash, following function can be added

mkcd () { mkdir "$1" cd "$1" }

If you are using csh, you have to add an alias because C shell doesn't support function.
 
alias mkcd 'mkdir $PWD/\!:1;cd $PWD/\!:1'

If you are can't configure your enviorenment we can use following commands.
 
mkdir long-name-of-directory 
cd !^ 

Let me know If you have any other solutions.

How to invoke operating system command from a C/C++ program.

There can be situations in where we need to excute a linux commands from a C++ program.

Then system() is the function you need.

It is simple, only the command has to be passed and it will return 0 if command successfuly executed, 1 otherwise.

syntax: int system(const char *command);

As system function is declared in stdlib.h, it can be considered a C function. But in C++, stdlib.h is merged into the std namespace and is located in the cstdlib .

Advantages of using an initialization list : OOP in C++

There are two different methods we can use when wtriting a constructor for a C++ class. Most of  newbies to C++ have a problem of what is the difference of these two methords or is there no difference between these two. Some people ask what does that wierd colon (" : ") syntax in the C++ construstor.

These two ways are known as constructor with an initialization list and constructor without an initialization list.



Example 1: constructor with an initialization list 

Example 2: constructor without an initialization list

Values given after the colon is known as initialization list. If there are multiple values, we can provide a comma seperated list of values as initialization list.

Example 3: initialization list of multiple values.

In first example C++ initialize property "age" with the value x where the second example initialize property "age" with it's default constructor. that means if the property is an integer value it will be initialized with an undefined value.

In first example what C++ does under the hood is,
int price = x;

In the second example it is as follows,

int price;
price = x;

Lets see what are the advantages of using an initialiation list with constructor over the none initialization list way.
  • It helps to keep the code clean and less code in constructor. So we can implement important functionalities in constructor body.
  • In above example there is no significant amout of runtime improvement. But if the age is a complex object, with a heavy construction cost there can be an advantage with runtime. 
  • If we have constant attributes in class, using initialization list is the only way of initializing those values since other ways would give errors. 


Sunday, December 3, 2023

How to identify process is listening to FIFO (Handle brocken pipe issues)

 In my previous post about named pipes, I mentioned that, "there has to be a reading process "attached" for the other side of the pipe. If you try to open pipe for writing and there is no reading process, open will hang waiting for it"

But there can be issues if reading process is killed/ternminated after attching to FIFO. In that case there will  not be any listner on the other end and writing process would give "Broken pipe" error.

In that case you could use command lsof


e.g. lsof /tmp/myfifo 
Output: 
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME 
wr 64769 isurusa 3w FIFO 202,3 0t0 67668074 /tmp/myfifo 
rd 64781 isurusa 3r FIFO 202,3 0t0 67668074 /tmp/myfifo 

Obove output can be processed and make sure there is a propper listner attched to FIFO.

Below is some C++ code snipet for that.


Using named pipes (FIFO) to inter process communication (IPC) in c++

 Named pipes can be used, if you need to setup a bidirectional channel between two processes.

Traditional UNIX pipe is unnamed and it lasts only during the process lifetime, where as named pipes last as long as the system is up, beyond the life of the process.

A named pipe is basically a file and converted into a FIFO using mkfifo(). 

Then processes attach to it for reading/writing purpose. After that reading and writing can be done as on a regular file in C++.

There have to be reading process "attached" for the other side of the pipe. If you try to open pipe for writing and there is no reading process, open will hang waiting for it or return -1 with errno set to ENXIO (when O_NONBLOCK flag is used)




NOTE:

Pipes are byte-oriented, not message oriented. If you want messages, there's other IPC for that (e.g., SysV message queues).

Since it is a stream of bytes we may need to establish a minimal transport protocol (for example use '\0' byte as a delimiter, Number of bytes write/read form sender/reciever)




Thursday, November 30, 2023

Difference between 'struct' and 'typedef struct' in C++

 

In C++ there is a subtle difference between.
struct Foo { ... };
and
typedef struct { ... } Foo;

In C++ there are few types of identifiers. 
  • Constants 
  • Variables 
  • Functions 
  • Labels 
  • Defined data types 

 These identifiers are stored in different namesapces. If someone write following, he would get an compiler error.
 
struct Foo { ... }; Foo x;

That is because Foo is stored only in namespace for defined data types. 
So, everytime you need to declare an object of Foo, you need to write struct Foo x;

But with following code Foo will be defined in namesapce for variables .
 
struct Foo { ... };
typedef struct Foo Foo;

In short, It can be written as follows.
 
typedef struct Foo { ... } Foo;

So with typedef we can create objects with Foo x;
So we dont have to use struct Foo x; pattern every time we create an object of Foo.

Difference of strcpy and stpcpy in c++

The main difference is the return value of stpcpy.

stpcpy returns the pointer to the terminating \0 character of the target string. This immediately makes clear the purpose of stpcpy and the rationale behind its existence: this function is intended to be used as an intelligent replacement to strcat function in situations when you need to concatenate multiple sub-strings into one string. 
strcat works pretty poorly in such application. The problem with strcat is that it rescans the destination string every time you add something to it, thus doing lots of unnecessary work and basically generating more heat than light. For example, the following code suffers from that problem


const char *part1, *part2, *part3, *part4;
... 
char buffer[size]; /* assume that `size` is calculated properly */ 

strcpy(buffer, part1); 
strcat(buffer, part2); 
strcat(buffer, part3); 
strcat(buffer, part4);

This code can be reimplemented in a much more reasonable way by using stpcpy stpcpy(stpcpy(stpcpy(stpcpy(buffer, part1), part2), part3), part4); 

And if you don't like chained calls, you can use an intermediate pointer to store the return value of intermediate stpcpy calls
 
char *end = buffer; 
end = stpcpy(end, part1); 
end = stpcpy(end, part2); 
end = stpcpy(end, part3); 
end = stpcpy(end, part4);


Of course it is worth mentioning that strcpy and strcat are standard functions, while stpcpy is not. Refrerence : SOF

Optimize you working enviorenment : Single command to create & move to a directory in linux (C Shell, Bash)

Usually move to a directory just after creating is bit of a anxious task specially if the directory name is too long. mkdir long-name-of...