pid of a process in c
To get the process ID (pid) of a process in C, you can use the getpid()
function from the unistd.h
header file. This function returns the process ID of the current process.
Here is an example code snippet:
#include <stdio.h>
#include <unistd.h>
int main() {
pid_t pid = getpid();
printf("Process ID: %d\n", pid);
return 0;
}
This code includes the necessary header files and defines a main()
function. Inside the main()
function, the getpid()
function is called and its return value is stored in a variable pid
of type pid_t
. The process ID is then printed using printf()
.
When you run this code, it will display the process ID of the current process.