test/pgrp/main.c
static int create_process_with_pgid(int pgid) {
    int ret = 0;
    posix_spawnattr_t attr;
    int child_pid = 0;

    // set child process spawn attribute
    ret = posix_spawnattr_init(&attr);
    if (ret != 0) {
        THROW_ERROR("init spawnattr error");
    }
    ret = posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETPGROUP);
    if (ret != 0) {
        THROW_ERROR("set attribute flag error");
    }
    // child process will have its own process group
    ret = posix_spawnattr_setpgroup(&attr, pgid);
    if (ret != 0) {
        THROW_ERROR("set process group attribute error");
    }

    int child_argc = 2; // /bin/pgrp again
    char **child_argv = calloc(1, sizeof(char *) * (child_argc + 1));
    child_argv[0] = strdup("pgrp");
    child_argv[1] = strdup("again");
    ret = posix_spawn(&child_pid, "/bin/pgrp", NULL, &attr, child_argv, NULL);
    if (ret < 0) {
        THROW_ERROR("ERROR: failed to spawn a child process\n");
    }
    printf("Spawn a new proces successfully pid = %d\n", child_pid);
    posix_spawnattr_destroy(&attr);
    free(child_argv);
    return child_pid;
}
// Create a child process with different args which will have the pgid specified by `pgid`
// and the child will sleep and then abort.
// This new process should be killed to prevent aborting.
Create a child process with different args which will have the pgid specified by `pgid`
and the child will sleep and then abort.
This new process should be killed to prevent aborting.
Create a child process with different args which will have the pgid specified by ` pgid ` and the child will sleep and then abort . This new process should be killed to prevent aborting .
static int create_process_with_pgid(int pgid) {
    int ret = 0;
    posix_spawnattr_t attr;
    int child_pid = 0;
    ret = posix_spawnattr_init(&attr);
    if (ret != 0) {
        THROW_ERROR("init spawnattr error");
    }
    ret = posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETPGROUP);
    if (ret != 0) {
        THROW_ERROR("set attribute flag error");
    }
    ret = posix_spawnattr_setpgroup(&attr, pgid);
    if (ret != 0) {
        THROW_ERROR("set process group attribute error");
    }
    int child_argc = 2; 
    char **child_argv = calloc(1, sizeof(char *) * (child_argc + 1));
    child_argv[0] = strdup("pgrp");
    child_argv[1] = strdup("again");
    ret = posix_spawn(&child_pid, "/bin/pgrp", NULL, &attr, child_argv, NULL);
    if (ret < 0) {
        THROW_ERROR("ERROR: failed to spawn a child process\n");
    }
    printf("Spawn a new proces successfully pid = %d\n", child_pid);
    posix_spawnattr_destroy(&attr);
    free(child_argv);
    return child_pid;
}
static int create_process_with_pgid ( int pgid ) { int ret = 0 ; posix_spawnattr_t attr ; int child_pid = 0 ; ret = posix_spawnattr_init ( & attr ) ; if ( ret != 0 ) { THROW_ERROR ( " " ) ; } ret = posix_spawnattr_setflags ( & attr , POSIX_SPAWN_SETPGROUP ) ; if ( ret != 0 ) { THROW_ERROR ( " " ) ; } ret = posix_spawnattr_setpgroup ( & attr , pgid ) ; if ( ret != 0 ) { THROW_ERROR ( " " ) ; } int child_argc = 2 ; char * * child_argv = calloc ( 1 , sizeof ( char * ) * ( child_argc + 1 ) ) ; child_argv [ 0 ] = strdup ( " " ) ; child_argv [ 1 ] = strdup ( " " ) ; ret = posix_spawn ( & child_pid , " " , NULL , & attr , child_argv , NULL ) ; if ( ret < 0 ) { THROW_ERROR ( " \n " ) ; } printf ( " \n " , child_pid ) ; posix_spawnattr_destroy ( & attr ) ; free ( child_argv ) ; return child_pid ; }
Create a child process with different args which will have the pgid specified by `pgid`
and the child will sleep and then abort.
Create a child process with different args which will have the pgid specified by ` pgid ` and the child will sleep and then abort .
// set child process spawn attribute // child process will have its own process group // /bin/pgrp again