#include <stdio.h> #include <stdlib.h> #include <unistd.h> using namespace std; pid_t getProcessPidByName(const char *proc_name) { FILE *fp; char buf[100]; char cmd[200] = {'\0'}; pid_t pid = -1; sprintf(cmd, "pidof %s", proc_name); if((fp = popen(cmd, "r")) != NULL) { if(fgets(buf, 255, fp) != NULL) { pid = atoi(buf); } } printf("pid = %d \n", pid); pclose(fp); return pid; } int main(int argc, char** argv) { if(argc != 2) { printf("Invalid input! \n"); return -1; } char* process_name = argv[1]; pid_t process_pid = getProcessIDByName((const char*)process_name); return 0; }
原文链接: https://www.cnblogs.com/amwuau/p/9809108.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/283370
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!