The system functions recently developed involve queries between processes and start and stop operations of processes on processes.
In a shell environment, it is very simple to query the process ID using the process name, for example, to view the process named "proc_name" through ps -ef | grep "proc_name".
You can also get the process ID through pidof "proc_name".
For example:
mvg@ubuntu-MS-7A54:~/test$ ps -ef | grep chip_det_main
mvg 27290 23866 99 17:32 pts/16 00:05:28 ./chip_det_main
mvg 27511 26672 0 17:33 pts/27 00:00:00 grep --color=auto chip_det_main
mvg@ubuntu-MS-7A54:~/test$ pidof chip_det_main
27290
So how to implement it in C/C++?
There are more than one method. I have adopted the simplest method that seems to be, create and execute shell commands in C language, and implement the specific function as follows:
1 #include <>
2 #include <>
3 #include <>
4
5
6 using namespace std;
7
8 pid_t getProcessPidByName(const char *proc_name)
9 {
10 FILE *fp;
11 char buf[100];
12 char cmd[200] = {'\0'};
13 pid_t pid = -1;
14 sprintf(cmd, "pidof %s", proc_name);
15
16 if((fp = popen(cmd, "r")) != NULL)
17 {
18 if(fgets(buf, 255, fp) != NULL)
19 {
20 pid = atoi(buf);
21 }
22 }
23
24 printf("pid = %d \n", pid);
25
26 pclose(fp);
27 return pid;
28 }
In the above code, the cmd array stores the shell command line string: pidof "proc_name", in this example, it is pidof chip_det_main.
A brief introduction to pidof, which is used to find the ID number of the process with a specified name, and its usage is:
pidof (option) (parameters)
OptionsAs optional, there are several types:
-s: Return only one process number
-c: Show only processes with the same root directory
-x: Show the process started by the script
-o: Specify the process ID that is not displayed
parameterThat is the name of the name to be searched for.
In addition, popen() and pclose() are also used in the above code, which is also a brief introduction.
The definition of popen() and pclose() is as follows:
#inlcude<>
FILE *popen(const char *command, const char *type);
int pclose(FILE *stream);
Among them, the command parameter is a shell command string pointer, which is passed to bin/sh and executed by the shell.
popen creates a pipeline, and uses fork or invoke a child process to execute the command command. The return value of command is in a standard IO stream. Since the pipeline data stream is unidirectional, command can only produce stdout or stdin, so the type parameter can only take one of "w" or "r". The return value of popen is the pointer to the data stream that is executed to execute the command command.
Poppen must be used with pclose.
Write a small test program to test the functions of the above functions:
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;
}
Compile and execute, the result is as follows:
mvg@ubuntu-MS-7A54:~/test$ ./test_getPIDbyName chip_det_main
pid = 28258
Compare the results of direct execution of the command line:
mvg@ubuntu-MS-7A54:~/test$ ./test_getPIDbyName chip_det_main
pid = 28258
mvg@ubuntu-MS-7A54:~/test$ pidof chip_det_main
28258
mvg@ubuntu-MS-7A54:~/test$ ps -ef | grep chip_det_main
mvg 28258 10332 99 17:51 pts/18 00:15:54 ./chip_det_main
mvg 29071 26672 0 17:54 pts/27 00:00:00 grep --color=auto chip_det_main
mvg@ubuntu-MS-7A54:~/test$
Function OK!