How to start a process in its own process group?

What do you mean "start a process in its own process group"? The shell launches processes in their own process groups, that's how it does job control (by having a process group for processes in the foreground, and several process groups for every pipeline launched on the background).

To see that the shell launches a new process group for every pipeline, you can do this:

ps fax -o pid,pgid,cmd | less

which will show something like:

11816 11816  |   \_ /bin/bash
4759   4759  |       \_ ps fax -o pid,pgid,cmd
4760   4759  |       \_ less

Note that the shell has created a new process group for the pipeline, and every process in the pipeline shares the process group.

Edit:

I think I know what you are getting at. You are calling system from Perl. Apparently, sh -c doesn't create new process groups, since it's a shell without job control.

What I would do would be to fork, then on the child:

setpgrp;
system("ps fax -o pid,pgid,cmd");

and wait on the parent.


EDIT: If what you wanted to do was use setsid but find the session id and/or pid of the resulting process:

If you launch a process through the setsid command it won't be attached to your terminal, so of course it won't respond to ctrl-c.

You could find it by grepping through the output of

ps x -O sid 

or something more limited like

ps x -o %c,%p,sid

Or simple trolling through proc/[pid]/stat for all entries and looking at the session id and whatever else is of interest (see man proc for details)

The man page for setsid is not giving any flags to directly generate output, but you could trivially make your own version that prints out the desired information, by modifying the standard.

For example, grab a copy of setsid.c from one of the results for

http://www.google.com/codesearch?as_q=setsid&as_package=util-linux

Comment out the nls include, the locale stuff and the _("") error macro which will cause problems and then add this right before the execvp line:

    printf("process will be pid %d sid %d\n", getpid(), getsid(0));