snipplets.dev/code/C/System/get-pid-by-process-name.c

13 lines
212 B
C
Raw Normal View History

2024-04-27 23:54:46 +03:00
pid_t getPIDbyName(char *ps_name) {
FILE *fp;
char *cmd = (char *)calloc(1, 200);
sprintf(cmd, "pidof %s", ps_name);
fp = popen(cmd, "r");
fread(cmd, 1, 200, fp);
fclose(fp);
return atoi(cmd);
}