c get pid

#include <stdio.h>
#include <unistd.h>

int main() {
    pid_t pid;

    pid = getpid();
    printf("My process ID is: %d\n", pid);

    return 0;
}

Explanation:

  • #include <stdio.h>: Includes the standard input-output header file for input and output operations.
  • #include <unistd.h>: Includes the header file that provides access to the POSIX operating system API, including functions like getpid().
  • int main() { ... }: Defines the main function, the entry point of the program.
  • pid_t pid;: Declares a variable pid of type pid_t to store the process ID.
  • pid = getpid();: Calls the getpid() function from the unistd.h library to retrieve the process ID of the current process and assigns it to the variable pid.
  • printf("My process ID is: %d\n", pid);: Prints the process ID retrieved using getpid() using the printf() function. The %d format specifier is used to print an integer value.
  • return 0;: Indicates successful termination of the main() function and the program.