Create a group of process

Hi all ! :slight_smile:

What I want?

  1. All child process must be in the same group of process. Parent is a leader of the group.

How to do this? I would be greatfull of some example of code, I read about setsid but I can't even start...

My code so far:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>



int main(int argc, char *argv[])
{
  int i ; 
  pid_t pid; 

  int val;
  char *endptr;  
  
   if(argc != 2)
   {
        fprintf(stderr,"Error  \n");
        exit(EXIT_FAILURE);
   }

   val = strtol(argv[1],&endptr, 0);
   if(endptr == argv[1])
   {
        fprintf(stderr,"Bad number ! \n");
        exit(EXIT_FAILURE);
   }

   // okej

   if(setsid() == -1)
   {
	perror("setsid");
   }

   for(i = 0; i < val; i++)
   {
	pid = fork(); 
	if(pid < 0)
	{
		perror("Fork");		
        }
        else if(pid==0)
	{
	    int ret; 
		
	    /* Child process */
	    printf("child process");	
 	    ret = execl("program1","program1", "145",NULL); //  I run my second program 
	    if(ret == -1)	
	    	perror("Execl");
	
	}
	else{
		wait(NULL); 
	}
   } 
}

Function call

setsid()

doesn't work if the calling process has a control terminal. This is usually done in background daemon processes. After calling setsid, the process becomes the process group leader of a new process group.

Do you know about the daemon() function in linux?
daemon(3) - Linux manual page

Or use setsid() and try the following bit of code in this example:
Creating a Daemon Process in C Language with an Example Program