erondem
Posts: 23
Joined: Wed Jul 11, 2018 12:19 pm

How to detect if a program is running via c++ code

Wed Dec 12, 2018 8:02 am

Hi I want to blink 3 Led according to these criterias

1. Blink if there is internet connection
2. Blink if raspberry is powered up
3. Blink if a program is running


I have a program which is written in C++. I can do it first and second , but I need advice/help on third one. Does anyone know how to do that?

User avatar
topguy
Posts: 5667
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: How to detect if a program is running via c++ code

Wed Dec 12, 2018 9:08 am

Code: Select all

ps -ef | grep programname | wc -l
This bash command lists all running processes, find all lines with "programname" and count the lines.. if this return 1 or more, your program is running.
So one way of solving this is to run this command with system() or popen() and get the result.

Another way is to learn how the /proc filesystem works and go trough that.

erondem
Posts: 23
Joined: Wed Jul 11, 2018 12:19 pm

Re: How to detect if a program is running via c++ code

Wed Dec 12, 2018 11:12 am

topguy wrote:
Wed Dec 12, 2018 9:08 am

Code: Select all

ps -ef | grep programname | wc -l
This bash command lists all running processes, find all lines with "programname" and count the lines.. if this return 1 or more, your program is running.
So one way of solving this is to run this command with system() or popen() and get the result.

Another way is to learn how the /proc filesystem works and go trough that.
Rather than using bash file I would like to use more like c language because according to connection status my already running program should blink a link or not.

n67
Posts: 938
Joined: Mon Oct 30, 2017 4:55 pm

Re: How to detect if a program is running via c++ code

Wed Dec 12, 2018 11:37 am

It is unclear whether OP's original title of:
How to detect if a program is running via c++ code
means that he wants to use c++ code to detect if a program is running

OR

if he means that he wants to be able to determine if a given running program was written in C++ (as opposed to, say, Python, or assembler, or Pascal).

I think we need, first, a clarification on that point.

If the former, then he might consider using system(3) to invoke shell commands from within the C/C++ program.

If the later, well, that's a bit more complicated.
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

n67
Posts: 938
Joined: Mon Oct 30, 2017 4:55 pm

Re: How to detect if a program is running via c++ code

Wed Dec 12, 2018 11:43 am

Note also that if you know the pid and do:

$ ps -Op <pid>

The third column of the output will tell you whether or not the program is running (i.e., that which is the goal of this exercise). If this field contains the letter R (in upper case), then you know that it is running. Most programs, most of the time, will be sleeping and this field will contain an S. It may also be stopped, in which case, it will contain a T.
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

User avatar
topguy
Posts: 5667
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: How to detect if a program is running via c++ code

Wed Dec 12, 2018 12:21 pm

Rather than using bash file I would like to use more like c language because according to connection status my already running program should blink a link or not.
I dont understand your argumentation. I'm just saying that you can run a bash command from C/C++ to get the information you need. And you can do it in like 5 lines of code.

or... you can look at the code behind a "ps" command : https://gitlab.com/procps-ng/procps
or.. you can make a routine that goes trough all the directories in "/proc" which is a number ( PID) and look and the "cmdline" file in each of them and see if that matches your program..

... but I'm rather confident that in the end you will be back looking at my original suggestion.

dsyleixa123
Posts: 328
Joined: Mon Jun 11, 2018 11:22 am

Re: How to detect if a program is running via c++ code

Thu Dec 13, 2018 1:29 pm

topguy wrote:
Wed Dec 12, 2018 9:08 am

Code: Select all

ps -ef | grep programname | wc -l
This bash command lists all running processes, find all lines with "programname" and count the lines.. if this return 1 or more, your program is running.
So one way of solving this is to run this command with system() or popen() and get the result.

Another way is to learn how the /proc filesystem works and go trough that.
just out of curiousity:
which would be the system() command not only to execute a shell command but also to retieve a value from it?

incognitum
Posts: 306
Joined: Tue Oct 30, 2018 3:34 pm

Re: How to detect if a program is running via c++ code

Thu Dec 13, 2018 2:14 pm

dsyleixa123 wrote:
Thu Dec 13, 2018 1:29 pm
just out of curiousity:
which would be the system() command not only to execute a shell command but also to retieve a value from it?
popen() to start the program and open a pipe to it.
Can then read the data from the pipe, using the same methods you can use to read data from a file.
There are no convenient execute-command-and-return-data-as-string methods by default.


In the case of the TS it may be easier to just use the pidof command, and look at the numeric exit code though:

Code: Select all

if ( system("pidof program-you-are-interested-in") == 0 )
{
   // program is running
}
Last edited by incognitum on Thu Dec 13, 2018 2:27 pm, edited 1 time in total.

User avatar
DavidS
Posts: 4334
Joined: Thu Dec 15, 2011 6:39 am
Location: USA
Contact: Website

Re: How to detect if a program is running via c++ code

Thu Dec 13, 2018 2:26 pm

dsyleixa123 wrote: just out of curiousity:
which would be the system() command not only to execute a shell command but also to retieve a value from it?
Just redirect stdout when running the command, make sure that what you redirect to is something you can read. Many use a file, though I would only recomend that if you have a custom RAM FS setup on your linux for the purpose.

This is basic C programming, you can look in your copy of "The C Programing Language" for more details on redirecting stdout.
RPi = The best ARM based RISC OS computer around
More than 95% of posts made from RISC OS on RPi 1B/1B+ computers. Most of the rest from RISC OS on RPi 2B/3B/3B+ computers

n67
Posts: 938
Joined: Mon Oct 30, 2017 4:55 pm

Re: How to detect if a program is running via c++ code

Thu Dec 13, 2018 3:51 pm

As indicated above, the methods are:

1) If all you want is the "return code" (errorlevel, whatever you want to call it) from the program, just use the return value of system().

2) If you want the output of the program (text on standard output and/or standard error), then use popen().

Note: To get standard error as well, you will need the command to end with "2>&1".
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

n67
Posts: 938
Joined: Mon Oct 30, 2017 4:55 pm

Re: How to detect if a program is running via c++ code

Fri Dec 14, 2018 8:23 am

I had fun putting this together...

Code: Select all

/* Compile cmd: gcc -std=gnu99 -W -Wall -Werror -s -o getpids getpids.c */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glob.h>

unsigned getpids(char *pgm, int *pids, unsigned maxget, int (*cmp)(const char *,const char *))
{
    glob_t g;
    static char *line;
    static size_t line_len;

    if (glob("/proc/[1-9]*",0,0,&g))
	perror("glob() failed!"),
	exit(1);

    char **names = g.gl_pathv;
    unsigned nf = 0;
    for (unsigned i=0; i < g.gl_pathc && nf < maxget; i++) {
	char buff[100];
	FILE *fp = fopen(strcat(strcpy(buff,*names++),"/stat"),"r");

	if (!fp)
	    perror(buff),
	    exit(1);
	getline(&line,&line_len,fp);
	fclose(fp);

	*strrchr(line,')') = 0;
	if (!cmp(strchr(line,'(') + 1,pgm))
	    pids[nf++] = atoi(line);
	}

    globfree(&g);
    return nf;
}

int main(void)
{
    int the_pids[20];
    unsigned num;
    int my_strstr(const char *s1,const char *s2) {
	return !strstr(s1,s2);
	}

    printf("Return value is: %d\n",num = getpids("bash",the_pids,20,strcmp));
    for (unsigned i=0; i < num; i++)
	printf("the_pids[%d] = %d\n",i,the_pids[i]);
}
Fixing any bugs or shortcomings in the above code is left as an exercise for the reader.

For starters, failure of the fopen() call should not be a fatal error, because of the "TOCTOU" problem.

Finally, as an exercise, try putting "my_strstr" instead of "strcmp" as the last arg to the getpids() call.
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

dsyleixa123
Posts: 328
Joined: Mon Jun 11, 2018 11:22 am

Re: How to detect if a program is running via c++ code

Fri Dec 14, 2018 9:51 am

thanks to all for your clarifications!

Return to “C/C++”