1833752 Members
2540 Online
110063 Solutions
New Discussion

Process existence.

 
Sandip Samanta_1
Occasional Contributor

Process existence.


Hi Admin,

Is there an API support to know if a process is already running ?
I had tried
system("export aVar=ps -e | grep -v grep | grep -n Procname);

but it did not work.
Would someone advise me how I can get this status.

Sandip
US-SUPPORT HP-UX
7 REPLIES 7
Dan Hetzel
Honored Contributor

Re: Process existence.

Hi Sandip,

This works for a single process:

#include

main()
{
FILE *command;
long process_id;
char pnum[12];
command=popen("ps -ef|grep lpsched | grep -v grep|awk '{print $2}'","r");
fgets(pnum,12,command);
process_id=atol(pnum);
printf("process number = %ld\n",process_id);
}

Best regards,

Dan

Everybody knows at least one thing worth sharing -- mailto:dan.hetzel@wildcroft.com
Dan Hetzel
Honored Contributor

Re: Process existence.

Hi again,

I didn't mention that it was a "quick and dirty" solution but you realised that by yourself, didn't you?

;-)

Dan
Everybody knows at least one thing worth sharing -- mailto:dan.hetzel@wildcroft.com
Frederic Soriano
Honored Contributor

Re: Process existence.

Have a look at the pstat() system call, which can be found in /usr/include/sys/pstat.h.

In the pstat(2) manpage, there are lots of useful examples, such as this one:

/*
* Example 4: Get a particular process' information
*/
{
struct pst_status pst;
int target = (int)getppid();

if (pstat_getproc(&pst, sizeof(pst), (size_t)0, target) != -1)
(void)printf("Parent started at %s", ctime(&pst.pst_start));
else
perror("pstat_getproc");
}

Hope that helps !

Regards.
Dan Hetzel
Honored Contributor

Re: Process existence.

Hi Frederic,

Your solution will only be useful for the current process (PID), or the parent (PPID)

I understood that Sandip is checking for a totally unrelated process.

Best regards,

Dan
Everybody knows at least one thing worth sharing -- mailto:dan.hetzel@wildcroft.com
Frederic Soriano
Honored Contributor

Re: Process existence.

Yes Dan, you're right.
Your example is better than mine :o)

I simply wanted Sandip to look at pstat(2) manpage for further help.

Regards.
Dan Hetzel
Honored Contributor

Re: Process existence.

Hi Frederic

Don't take offence, please! I was just trying to clarify the initial question.

My example wasn't any better than yours, I even mentioned above that it was a "quick and dirty" solution, didn't I ?
;-)

Cheers,

Dan
Everybody knows at least one thing worth sharing -- mailto:dan.hetzel@wildcroft.com
Frederic Soriano
Honored Contributor

Re: Process existence.

Do not worry Dan !

I did not feel offended at all !
I really thought what I said, and I agreed that your example, even quick and dirty, was really what Sandip was looking for !

Again, sorry for misunderstanding !!

Best regards.