The Process - Part II
Process Attributes Each UNIX process is associated with a number of attributes which help the system control the running and scheduling of processes, maintain the security of the file system and so on –process id –environment –effective user-id –privileges
The process-id A process can obtain its own process-id by using the getpid() function call.
Process group and process group-ids Unix allows processes to be placed into groups. For example processes are connected by pipes from the command line, they are typically placed into a process group. whoawk ‘(print $1)’Sort -u
Process groups Process groups are useful when you want to handle a set of processes as a whole using IPC mechanism called signals. Each process group is denoted by a process group-id of type pid_t. Usage: #include pid_t getpgrp (void);
Changing process group Usage #include int setpgid (pid_t pid, pid_t pgid);
Session and session-ids Each process group belongs to a session. A session is about a process’s connection to a connecting terminal. All processes explicitly or implicitly created after a user logs in belong to a session related to their current terminal. A session is a collection of a single foreground process group using the terminal and one or more background processes.
Session-id A session is identified by a session-id of type pid_t. A process can obtain its current session-id with a call to getsid as follows: Usage #include pid_t getsid(pid_t pid);
Session-d continued If getsid is supplied a value of 0 then it returns the session-id of the calling process otherwise the session-id of the process identified by pid is returned. The idea of a session is useful with background or daemon process. A daemon process is simply a process which does not have a controlling terminal.
Session continued An example of a daemon process is a cron, which executes commands at specific times and dates. A daemon can set itself to be in a session without a controlling terminal by calling the setsid system call, and moving itself into a new session.
Session id Usage #include pid_t setsid(void);
The environment variable The environment of a process is simply a collection of null-terminated strings. A programmer can make direct use of the environment of a process by adding an extra parameter envp to the parameter list of the main function within a program. –main(int argc, char **argv, char **envp) { // do something }
To modify the environment from within a process /* setmyenv.c set environment for program */ main() { char *argv[2], *envp[3]; argv[0] = “showmyenv”; argv[1] = (char *)0; envp[0] = “foo=bar”; envp[1] = “bar=foo”; envp[2] = (char *)0; execve(“./showmyenv”, argv, envp); perror(“execve failed\n”); }
Getting specific variable The getenv system call can be used to get specific variables in the environment. Usage #include char *getenv(const char *name); main() { printf(“PATH = %s\n”, getenv(“PATH”); } A similar putenv(“NEWVARIABLE = value”); exists for modifying the env.
Obtaining the user- and group-ids
Setting the e-user- and group-ids