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 likegetpid()
.int main() { ... }
: Defines the main function, the entry point of the program.pid_t pid;
: Declares a variablepid
of typepid_t
to store the process ID.pid = getpid();
: Calls thegetpid()
function from theunistd.h
library to retrieve the process ID of the current process and assigns it to the variablepid
.printf("My process ID is: %d\n", pid);
: Prints the process ID retrieved usinggetpid()
using theprintf()
function. The%d
format specifier is used to print an integer value.return 0;
: Indicates successful termination of themain()
function and the program.