Controlling a process to turn on or off via Gpio

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.