Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Get Process ID

Subject: Get Process ID

From: Zeph Davies

Date: 16 Dec, 2009 11:54:04

Message: 1 of 12

Is there a function to gain the process ID (PID) of a matlab session?

How about if matlab is loaded via the unix() command?

For example:
  unix('nohup matlab -nodesktop -nosplash -nojvm -nodisplay -r filename;);
  exit;
will run filename.m without any terminal or ssh connection, allowing it to run completely stand alone. Because it is stand alone, there is no GUI or Terminal to stop/close the process, and therefore a "kill PID" command is needed, hence can i retrieve the PID?

Thanks

Zeph

Subject: Get Process ID

From: Rune Allnor

Date: 16 Dec, 2009 12:04:54

Message: 2 of 12

On 16 Des, 12:54, "Zeph Davies" <mat...@zephmail.co.uk> wrote:
> Is there a function to gain the process ID (PID) of a matlab session?
>
> How about if matlab is loaded via the unix() command?
>
> For example:
>   unix('nohup matlab -nodesktop -nosplash -nojvm -nodisplay -r filename;);
>   exit;
> will run filename.m without any terminal or ssh connection, allowing it to run completely stand alone. Because it is stand alone, there is no GUI or Terminal to stop/close the process, and therefore a "kill PID" command is needed, hence can i retrieve the PID?

It's been more than ten years since I last used UNIX, but
as I remember it, there were two ways to do this:

1) Some times the PID was reported to the command line
   when the process was started. I *think* this happened
   if the process was started from the command line and
   run in the background. Or maybe it was after a bakground
   process had finished? I just can't remember.

2) Use some command (I don't remember which one) to get
   a list of processes. If there were several sessions of
   the same program up and running, one needed to make
   and educated guess about which one to kill.

Apart from that, PIDs is not a universal term, so don't
expect matlab to have any built-in tool to get it. You
will almost certainly need to use some OS functionality.

Rune

Subject: Get Process ID

From: Zeph Davies

Date: 16 Dec, 2009 12:16:21

Message: 3 of 12

Rune Allnor <allnor@tele.ntnu.no> wrote in message <c2925bf9-abb2-4796-8394-48420eb1d3d8@g25g2000vbl.googlegroups.com>...
> On 16 Des, 12:54, "Zeph Davies" <mat...@zephmail.co.uk> wrote:
> > Is there a function to gain the process ID (PID) of a matlab session?
> >
> > How about if matlab is loaded via the unix() command?
> >
> > For example:
> > ? unix('nohup matlab -nodesktop -nosplash -nojvm -nodisplay -r filename;);
> > ? exit;
> > will run filename.m without any terminal or ssh connection, allowing it to run completely stand alone. Because it is stand alone, there is no GUI or Terminal to stop/close the process, and therefore a "kill PID" command is needed, hence can i retrieve the PID?
>
> It's been more than ten years since I last used UNIX, but
> as I remember it, there were two ways to do this:
>
> 1) Some times the PID was reported to the command line
> when the process was started. I *think* this happened
> if the process was started from the command line and
> run in the background. Or maybe it was after a bakground
> process had finished? I just can't remember.
>
> 2) Use some command (I don't remember which one) to get
> a list of processes. If there were several sessions of
> the same program up and running, one needed to make
> and educated guess about which one to kill.
>
> Apart from that, PIDs is not a universal term, so don't
> expect matlab to have any built-in tool to get it. You
> will almost certainly need to use some OS functionality.
>
> Rune

As you said, it is highly likely that i may be running many matlabs, so just searching for "matlab" and my user ID may list more than one PID. I'm hoping for a way to print the exact ID to screen (or something) of the run just started.

Zeph

Subject: Get Process ID

From: Praetorian

Date: 16 Dec, 2009 14:00:04

Message: 4 of 12

On Dec 16, 5:16 am, "Zeph Davies" <mat...@zephmail.co.uk> wrote:
> Rune Allnor <all...@tele.ntnu.no> wrote in message <c2925bf9-abb2-4796-8394-48420eb1d...@g25g2000vbl.googlegroups.com>...
> > On 16 Des, 12:54, "Zeph Davies" <mat...@zephmail.co.uk> wrote:
> > > Is there a function to gain the process ID (PID) of a matlab session?
>
> > > How about if matlab is loaded via the unix() command?
>
> > > For example:
> > > ? unix('nohup matlab -nodesktop -nosplash -nojvm -nodisplay -r filename;);
> > > ? exit;
> > > will run filename.m without any terminal or ssh connection, allowing it to run completely stand alone. Because it is stand alone, there is no GUI or Terminal to stop/close the process, and therefore a "kill PID" command is needed, hence can i retrieve the PID?
>
> > It's been more than ten years since I last used UNIX, but
> > as I remember it, there were two ways to do this:
>
> > 1) Some times the PID was reported to the command line
> >    when the process was started. I *think* this happened
> >    if the process was started from the command line and
> >    run in the background. Or maybe it was after a bakground
> >    process had finished? I just can't remember.
>
> > 2) Use some command (I don't remember which one) to get
> >    a list of processes. If there were several sessions of
> >    the same program up and running, one needed to make
> >    and educated guess about which one to kill.
>
> > Apart from that, PIDs is not a universal term, so don't
> > expect matlab to have any built-in tool to get it. You
> > will almost certainly need to use some OS functionality.
>
> > Rune
>
> As you said, it is highly likely that i may be running many matlabs, so just searching for "matlab" and my user ID may list more than one PID. I'm hoping for a way to print the exact ID to screen (or something) of the run just started.
>
> Zeph

Why don't you just put a QUIT command at the end of filename.m?

Subject: Get Process ID

From: Zeph Davies

Date: 16 Dec, 2009 14:11:19

Message: 5 of 12

Praetorian <ashish.sadanandan@gmail.com> wrote in message <aed160af-a809-4f8f-a98a-ba17ed4c3188@1g2000vbm.googlegroups.com>...
> On Dec 16, 5:16?am, "Zeph Davies" <mat...@zephmail.co.uk> wrote:
> > Rune Allnor <all...@tele.ntnu.no> wrote in message <c2925bf9-abb2-4796-8394-48420eb1d...@g25g2000vbl.googlegroups.com>...
> > > On 16 Des, 12:54, "Zeph Davies" <mat...@zephmail.co.uk> wrote:
> > > > Is there a function to gain the process ID (PID) of a matlab session?
> >
> > > > How about if matlab is loaded via the unix() command?
> >
> > > > For example:
> > > > ? unix('nohup matlab -nodesktop -nosplash -nojvm -nodisplay -r filename;);
> > > > ? exit;
> > > > will run filename.m without any terminal or ssh connection, allowing it to run completely stand alone. Because it is stand alone, there is no GUI or Terminal to stop/close the process, and therefore a "kill PID" command is needed, hence can i retrieve the PID?
> >
> > > It's been more than ten years since I last used UNIX, but
> > > as I remember it, there were two ways to do this:
> >
> > > 1) Some times the PID was reported to the command line
> > > ? ?when the process was started. I *think* this happened
> > > ? ?if the process was started from the command line and
> > > ? ?run in the background. Or maybe it was after a bakground
> > > ? ?process had finished? I just can't remember.
> >
> > > 2) Use some command (I don't remember which one) to get
> > > ? ?a list of processes. If there were several sessions of
> > > ? ?the same program up and running, one needed to make
> > > ? ?and educated guess about which one to kill.
> >
> > > Apart from that, PIDs is not a universal term, so don't
> > > expect matlab to have any built-in tool to get it. You
> > > will almost certainly need to use some OS functionality.
> >
> > > Rune
> >
> > As you said, it is highly likely that i may be running many matlabs, so just searching for "matlab" and my user ID may list more than one PID. I'm hoping for a way to print the exact ID to screen (or something) of the run just started.
> >
> > Zeph
>
> Why don't you just put a QUIT command at the end of filename.m?

Ah, i didn't make that clear. It does quit matlab automatically at the end of the automated simulation, but the simulations can take days to run, and if i need to stop it mid-run i need the PID and the "kill" command.

Maybe an alternate view would be to apply a unique title to the process to identify it on a PID listing? Just a thought, if anyone knows if this is possible.

Subject: Get Process ID

From: Rune Allnor

Date: 16 Dec, 2009 14:27:37

Message: 6 of 12

On 16 Des, 15:11, "Zeph Davies" <mat...@zephmail.co.uk> wrote:

> Maybe an alternate view would be to apply a unique title to the process to identify it on a PID listing? Just a thought, if anyone knows if this is possible.

What about a symbolic link to the matlab executable?
Invoke matlab through this link. With a little bit of
luck, it is the name of the link that is listed along
with the PID.

Rune

Subject: Get Process ID

From: Doug Schwarz

Date: 18 Dec, 2009 04:45:43

Message: 7 of 12

In article <hgapq7$1la$1@fred.mathworks.com>,
 "Zeph Davies" <matlab@zephmail.co.uk> wrote:

[snip]

> Maybe an alternate view would be to apply a unique title to the process to
> identify it on a PID listing? Just a thought, if anyone knows if this is
> possible.

You can do something like that with a disp. If you run this command
line:

  nohup matlab -nodesktop -r "disp('run 1'),filename"

then you can grep whatever you put in the disp. (I trimmed some of the
command line switches for clarity.)

  ps | grep "run 1"

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

Subject: Get Process ID

From: Jan Simon

Date: 19 Dec, 2009 11:12:04

Message: 8 of 12

Dear Zeph!

> Is there a function to gain the process ID (PID) of a matlab session?

What about the C-mex:

% ------------------------------------ 8< ------------------
#include "mex.h"
#inlcude <process.h>
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[])
{
  plhs[0] = mxCreateDoubleScalar((double) getpid());
}
% ------------ >8 ------------------------------------------

This works under WinXP, but I assume compatibility to Linux also -- can somebody confirm this?

But the problem remains: How to let the caller know the PID, especially if several different processes are started?
The caller can start the subprocess with an individual ID and the subprocess appends this ID with the PID to a table in a file. Then the caller can search for the ID and obtain the PID. After killing the subprocess, the caller should cleanup the table.

I personally do not like interprocess communication through files. Timing problems are ever a problem for that.
I think, you could create the ID/PID table in the static memory of a dynamically linked library.

Good luck, Jan

Subject: Get Process ID

From: Rune Allnor

Date: 19 Dec, 2009 12:35:11

Message: 9 of 12

On 19 Des, 12:12, "Jan Simon" <matlab.THIS_Y...@nMINUSsimon.de> wrote:
> Dear Zeph!
>
> > Is there a function to gain the process ID (PID) of a matlab session?

> #inlcude <process.h>

> This works under WinXP, but I assume compatibility to Linux also -- can somebody confirm this?

Don't have too high hopes. From http://en.wikipedia.org/wiki/Process.h
:

"process.h is a C header file which contains function declarations
and macros used in working with threads and processes. Neither the
header file nor the functions are defined by either the ANSI/ISO C
standard or by POSIX. Most C compilers that target DOS, Windows 3.1x,
Win32, OS/2, Novell NetWare or DOS extenders supply this header and
the library functions in their C library."

So process.h is essentially a patch to include features to wintel
systems that other systems provide at the OS level. Not at all
unexpected from a system that can trace its ancestry to something
that used to be named Quick'n Dirty DOS...

Rune

Subject: Get Process ID

From: Jan Simon

Date: 20 Dec, 2009 16:02:02

Message: 10 of 12

Dear Rune!

> Don't have too high hopes. From http://en.wikipedia.org/wiki/Process.h
> :
>
> "process.h is a C header file which contains function declarations
> and macros used in working with threads and processes. Neither the
> header file nor the functions are defined by either the ANSI/ISO C
> standard or by POSIX. Most C compilers that target DOS, Windows 3.1x,
> Win32, OS/2, Novell NetWare or DOS extenders supply this header and
> the library functions in their C library."
>
> So process.h is essentially a patch to include features to wintel
> systems that other systems provide at the OS level. Not at all
> unexpected from a system that can trace its ancestry to something
> that used to be named Quick'n Dirty DOS...
>
> Rune

I cite:

---------------------- 8< ----------------------------
getpid(2) - Linux man page
Name: getpid, getppid - get process identification
Synopsis
#include <sys/types.h>
#include <unistd.h>

pid_t getpid(void);
pid_t getppid(void);
Description: getpid() returns the process ID of the current process. (This is often used by routines that generate unique temporary filenames.)

getppid() returns the process ID of the parent of the current process.
Conforming to
POSIX.1-2001, 4.3BSD, SVr4
---------------------- >8 ---------------------------------

So Rune: you are right! Use "unistd.h" instead of "process.h" header on linux. But perhaps GETPID works as expected then ?!

Kind regards, Jan

Subject: Get Process ID

From: Zeph Davies

Date: 11 Apr, 2012 13:34:17

Message: 11 of 12

Hey guys. Sorry for 'necro' (digging up an old post) but some interesting updates:

For a long time i've been using:
[~,r] = unix('ps -o ppid -p `ps -o ppid -p $$ | tail -n 1` | tail -n 1');

Not great, is it? But it did work.

I've just found:
feature('GetPid')

'feature' is undocumented and unsupported.

Also of note:
feature('NumCores')
Useful for parfor.

Subject: Get Process ID

From: Yair Altman

Date: 11 Apr, 2012 17:49:20

Message: 12 of 12

"Zeph Davies" wrote in message <jm418o$opb$1@newscl01ah.mathworks.com>...
> Hey guys. Sorry for 'necro' (digging up an old post) but some interesting updates:
>
> For a long time i've been using:
> [~,r] = unix('ps -o ppid -p `ps -o ppid -p $$ | tail -n 1` | tail -n 1');
>
> Not great, is it? But it did work.
>
> I've just found:
> feature('GetPid')
>
> 'feature' is undocumented and unsupported.
>
> Also of note:
> feature('NumCores')
> Useful for parfor.


There are many more possible feature parameters:
http://UndocumentedMatlab.com/blog/undocumented-feature-function/

Yair Altman
http://UndocumentedMatlab.com
 

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us