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
Tag: Programming
How to get the running process’ pid in Go?
Posted onHow to get the running process’ pid in Go lang? In Go, you may call the os.Getpid() func from the os package to get the parent process’ pid. func Getpid() int Getppid returns the process id of the caller. One example is as follows. $ gore gore version 0.2.6 :help for help gore> :import “fmt”
Read more
Getting Process Own Pid in C and C++
Posted onHow 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
How to get the running process’ parent process’ ID in Bash?
Posted onHow to get the running process’ parent process’ ID in Bash? In Bash, you can get the parent process’s pid from the variable PPID. Note that in a (…) subshell, the $$ stores the subshell’s parent shell pid actually.
How to Debug a Bash script?
Posted onHow to debug a Bash script if it has some bugs? Common techniques like printing varibles out for checking apply for bash too. For bash, I also use 2 bash-specific techniques. Use errexit option Run the script with -e option like bash -e your-script.sh or add set -o errexit to the beginning of the script.
Read more
How to get the running process’ pid in Bash?
Posted onHow to get the running process’ pid in Bash? In Bash, you can get the process ID from the variable $$. Note that in a subshell invoked by ‘(…)‘, $$ is actually the parent process’ pid. In Bash 4, you can also use $BASHPID to get the process pid.
Getting the running process’ own pid in Python
Posted onHow 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 all the keys in an associative array in Bash?
Posted onHow to get all the keys of an associative array in Bash? There are at least 2 ways to get the keys from an associative array of Bash. Let’s start with an example associative array: $ declare -A aa $ aa[“foo”]=bar $ aa[“a b”]=c We can use the @ special index to get all the
Read more
How to get the one character’s next character in ASCII table in Bash?
Posted onHow 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 onHow 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 onHow 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
Most important aspects or features of the C++ programming language?
Posted onCould you list the most important features or aspects for the C++ programming languages that describe its most important features/domains that are different from the others? For C++, as in Going Native 2012 Keynote by Bjarne Stroustrup: light-weight abstraction Key strengths: infrastructure software resource-constrained applications
What’s the standard or common data structure for a list of objects in C++?
Posted onIn 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
What’s the standard or common data structure for a list of objects in C?
Posted onIn C, what’s the standard or common data structure for a list of objects? In C, one common way to store a list/collection of objects of the same type is to use one-dimensional arrays. In C, an array is a collection of data objects of the same type. An array is in a contiguous memory
Read more
Getting Hostname in C Programs in Linux
Posted onIn 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 onIn 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 Go?
Posted onIn Go lang, how to get the hostname of the node? In Go, you can use the os.Hostname() function to get the hostname of the node. func Hostname() (name string, err error) Hostname returns the host name reported by the kernel. One example is as follows. The main.go source code: package main import ( “fmt”
Read more
How to get the hostname of the node in Python?
Posted onIn 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 onIn 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
Reading and Processing a File Line by Line in C++
Posted onIn C++, how to process a file line by line? The file is a plain text line like input.txt. As an example, the process can be to just print it out. In C++, you may open a input stream on the file and use the std::getline() function from the <string> to read content line by
Read more