Wednesday, February 19, 2014

Get my Process ID and Parent Process ID in C, by calling getpid() and getppid()

Example to Get my Process ID (PID) and Parent Process ID (PPID) by calling getpid() and getppid().

#include <stdio.h>
#include <sys/types.h>

main(){
 pid_t mypid;
 pid_t myppid;
 
 mypid = getpid();
 myppid = getppid();
 printf("My PID: %ld\n", (long)mypid);
 printf("My PPID: %ld\n", (long)myppid);
}

getpid() and getppid()

Get GNU libc version at run-time using C

To get GNU libc version in C language at run-time, you can call gnu_get_libc_version() and gnu_get_libc_release().

- gnu_get_libc_version() returns string that identifies the glibc version available on the system.
- gnu_get_libc_release() returns string indicates the release status of the glibc version available on the system.

And, you can retrieve _CS_GNU_LIBC_VERSION with confstr().

Here is example, test_version.c:
#include <stdio.h>
#include <gnu/libc-version.h>
#include <stdlib.h>
#include <unistd.h>

main(){
    printf("GNU libc version: %s\n", gnu_get_libc_version());
    printf("GNU libc release: %s\n", gnu_get_libc_release());
    
    char *buf; size_t n;
    n = confstr(_CS_GNU_LIBC_VERSION, NULL, (size_t)0);
    if ((buf = malloc(n)) == NULL) 
        abort();
    confstr(_CS_GNU_LIBC_VERSION, buf, n);

    printf("_CS_GNU_LIBC_VERSION: %s\n", buf );
    free( buf );
}

Get GNU libc version at run-time using C
Get GNU libc version at run-time using C