Does program C get linux PID?

I have a simple C program, and when I compile and run it with ./output , does it get the PID on Linux? (I think every running program is a process, and it should have a PID.)

I used the ps aux command, but I could not find the process name there.

I remember when my console application (program C) was running in Windows 7, I was able to get its PID using the Volatility tool.

 #include<stdio.h> void main() { printf("Hello World!"); } 
+4
source share
5 answers

Yes, every running program on Linux receives a PID.

Your program simply prints "Hello, World!" and will be completed so quickly that by the time ps aux starts, it will be completed.

In addition, void main() should be int main(void) , and you should add \n to the end of the output line.

+11
source

Must. just printf getpid () in your program to see it.

+1
source

Or just add a dream to find the process id. Probably ran while you blinked.

Use this code in a separate ps terminal

 #include <stdio.h> #include <unistd.h> int main(void) { printf("Hello World!\n"); sleep(3600); printf("Are you bored yet?\n"); } 
+1
source

You can get it using getpid

 int main() { pid_t pid; printf("Hello World!"); printf("pid of program is %d" , getpid()); } 

or just use your code and put it in the background.

 void main() { printf("Hello World!"); } 

Output:

 [ xxxxx@localhost ~]$ ./c1 & [1] 3007 3007 
+1
source

Yes, every process receives a PID at the time it starts. Since this C program seems like a very short process, it may exit before you can even run the ps aux command to get your PID. Try calling getchar() or scanf() , which will wait for user input, and then run the ps command from another terminal to see the PID.

0
source

Source: https://habr.com/ru/post/1438130/


All Articles