Target: A process on the board needs to read the value of the Gpio, when the Gpio value is 1 o'clock, the specified process is turned on, and when the Gpio is 0 o'clock, the specified process is killed.
#include <stdio.h>intMain () {FILE*pfile; while(1){ Const Char* FILEPATH ="/sys/class/gpio/gpio26/value"; Pfile= fopen (FILEPATH,"R"); if(Pfile = =NULL) {printf ("Open error\n"); Exit (0); }Charbuf[ -]; Fgets (BUF, -, pfile);
Cvalue Save the value of the Gpio file read to, 0 or 1CharCvalue = buf[0]; Charcommand[ Max]; Charbufresult[Ten]; intCount =0; FILE*FP;
Here the command grep Mjpg_streamer is filtering out the mjpg_streamer process, grep-v \ "Grep\" is the process of removing grep itself sprintf (command,"PS | grep mjpg_streamer | grep-v \ "Grep\""); if(fp = popen (Command,"R")) ==NULL) {printf ("Popen error\n"); Exit (0); }
Popen operation takes time, sleep sleep (1);
When there is no mjpg_streamer process, the bufresult read result is null. At this point, Count is set to 0, indicating that no mjpg_streamer process is runningif(Fgets (Bufresult,Ten, fp))! =NULL) {Count=atoi (Bufresult); }ElseCount=0; printf ("count =%d\n", Count); if(Count = =0&& Cvalue = ='1') {
When the Gpio is 1 o'clock, count is 0 o'clock, indicating that no mjpg_streamer process is running, and that the process needs to be opened to invoke the process startup script
Write in startup script, CD/directory,./program--parameters
System ("/modbustest/startmjpgstreamer.sh");
Program start takes time, sleep sleep (2); printf ("Start ok!\n"); } Else if(Count! =0&& Cvalue = ='0') {
Count does not have a mjpg_streamer process for 0, cvalue = = ' 0 ' indicates that the instruction requires the kill process, calling the Killall command
System"Killall Mjpg_streamer"); printf ("Kill ok!\n"); }Elseprintf"count =%d, Cvalue =%c\n", Count, Cvalue); Pclose (FP); Fclose (pfile); Sleep (1); } return 0;}
Controlling a process to turn on or off via Gpio