How to get the running process’ parent process’ ID in C / C++?

Posted on

How to get the running process’ parent process’ ID in C / C++? In C and C++, you can call the getppid() library function which is a function from the POSIX library. #include <sys/types.h> #include <unistd.h> pid_t getppid(void); getppid() returns the process ID of the parent of the calling process. Example usage: getppid.c #include <stdio.h>
Read more

Getting Process Own Pid in C and C++

Posted on

How to get the running process’ pid in C / C++? In C and C++, you can call the getpid() library function which is a function from the POSIX library. #include <sys/types.h> #include <unistd.h> pid_t getpid(void); getppid() returns the process ID of the calling process. An example C program to get self process ID getpid.c:
Read more

Getting the running process’ own pid in Python

Posted on

How to get the running process’ pid in Python? In Python, you can get the pid of the current process by import os os.getpid() From the official doc: os.getpid() Return the current process id. One example os using os.getpid() to get process own ID: $ python3 Python 3.8.10 (default, Jun 22 2022, 20:18:18) [GCC 9.4.0]
Read more

How to get the one character’s next character in ASCII table in Bash?

Posted on

How to get the one character’s next character in ASCII table in Bash? For example, if I have ‘a’ in variable i, how to get ‘b’? First, we need to get the integer value for the character. char=’b’ charint=$(printf “%d” “‘$char'”) Then, we increase the integer by one let charint=$charint+1 Last, we can get the
Read more

How to delete or get the number in the tail from a string in shell script?

Posted on

How to delete or get the number in the tail from a string in shell script (bash script)? Okay to call other tools. For example, from “/dev/sda8”, I want to get “/dev/sda” and “8” separately. This can be achieved by using sed as follows. To get the string after deleting the tailing numbers, we can
Read more

How to split a string by string in Bash?

Posted on

How to split a string by string in Bash? For example, “a string separated by space” => [“a”, “string”, “separated”, “by”, “space”] and “a,string,separated,by,comma” => [“a”, “string”, “separated”, “by”, “comma”] You can convert a string to an array using the grammar like inarr=(${a}) If the delimiter is not space and delimiter is a single character
Read more

What’s the standard or common data structure for a list of objects in C++?

Posted on

In C++, what’s the standard or common data structure for a list of objects? In C++, the common data structure for a sequence (“list”) of objects may be std::vector. A vector is a dynamically resizable array. It is “The most extensively used container in the C++ Standard Library …, offering a combination of dynamic memory
Read more

Getting Hostname in C Programs in Linux

Posted on

In C, how to get the hostname of the node? In C, you may use the gethostname function. #include <unistd.h> int gethostname(char *name, size_t namelen); The gethostname() function shall return the standard host name for the current machine. The namelen argument shall specify the size of the array pointed to by the name argument. The
Read more

How to get the hostname of the node in C++?

Posted on

In C++, how to get the hostname of the node? In C++, the C way works too. However, with Boost, you can use the boost::asio::ip::host_name() function to get the hostname as a std::string: namespace boost { namespace asio { namespace ip { /// Get the current host name. BOOST_ASIO_DECL std::string host_name(); … More at http://www.boost.org/doc/libs/1_63_0/boost/asio/ip/host_name.hpp
Read more

How to get the hostname of the node in Python?

Posted on

In Python, how to get the hostname of the node? In Python, you can get the hostname by the socket.gethostname() library function in the socket module: import socket hostname = socket.gethostname() Example: $ python Python 2.7.5 (default, Nov 6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2 Type “help”, “copyright”, “credits” or “license”
Read more

Getting Epoch Timestamp in C

Posted on

In C, how to get the epoch timestamp, the number of seconds passed since the epoch? In C, from man 7 time: UNIX systems represent time in seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC). A program can determine the calendar time using gettimeofday(2), which returns time (in seconds and microseconds) that have elapsed since
Read more