the process - part ii. process attributes each unix process is associated with a number of...

20
The Process - Part II

Post on 21-Dec-2015

217 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: The Process - Part II. Process Attributes  Each UNIX process is associated with a number of attributes which help the system control the running and

The Process - Part II

Page 2: The Process - Part II. Process Attributes  Each UNIX process is associated with a number of attributes which help the system control the running and

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

Page 3: The Process - Part II. Process Attributes  Each UNIX process is associated with a number of attributes which help the system control the running and

The process-id

A process can obtain its own process-id by using the getpid() function call.

Page 4: The Process - Part II. Process Attributes  Each UNIX process is associated with a number of attributes which help the system control the running and
Page 5: The Process - Part II. Process Attributes  Each UNIX process is associated with a number of attributes which help the system control the running and
Page 6: The Process - Part II. Process Attributes  Each UNIX process is associated with a number of attributes which help the system control the running and
Page 7: The Process - Part II. Process Attributes  Each UNIX process is associated with a number of attributes which help the system control the running and

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.

who awk ‘(print $1)’ Sort -u

Page 8: The Process - Part II. Process Attributes  Each UNIX process is associated with a number of attributes which help the system control the running and

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 <sys/types.h>

#include <unistd.h>

pid_t getpgrp (void);

Page 9: The Process - Part II. Process Attributes  Each UNIX process is associated with a number of attributes which help the system control the running and

Changing process group

Usage#include <sys/types.h>

#include <unistd.h>

int setpgid (pid_t pid, pid_t pgid);

Page 10: The Process - Part II. Process Attributes  Each UNIX process is associated with a number of attributes which help the system control the running and

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.

Page 11: The Process - Part II. Process Attributes  Each UNIX process is associated with a number of attributes which help the system control the running and

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 <sys/types.h>

#include <unistd.h>

pid_t getsid(pid_t pid);

Page 12: The Process - Part II. Process Attributes  Each UNIX process is associated with a number of attributes which help the system control the running and

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.

Page 13: The Process - Part II. Process Attributes  Each UNIX process is associated with a number of attributes which help the system control the running and

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.

Page 14: The Process - Part II. Process Attributes  Each UNIX process is associated with a number of attributes which help the system control the running and

Session id

Usage#include <sys/types.h>

#include <unistd.h>

pid_t setsid(void);

Page 15: The Process - Part II. Process Attributes  Each UNIX process is associated with a number of attributes which help the system control the running and

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

}

Page 16: The Process - Part II. Process Attributes  Each UNIX process is associated with a number of attributes which help the system control the running and
Page 17: The Process - Part II. Process Attributes  Each UNIX process is associated with a number of attributes which help the system control the running and

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”);

}

Page 18: The Process - Part II. Process Attributes  Each UNIX process is associated with a number of attributes which help the system control the running and

Getting specific variable

The getenv system call can be used to get specific variables in the environment.

Usage#include <stdlib.h>

char *getenv(const char *name);

main()

{

printf(“PATH = %s\n”, getenv(“PATH”);

}

A similar putenv(“NEWVARIABLE = value”); exists for modifying the env.

Page 19: The Process - Part II. Process Attributes  Each UNIX process is associated with a number of attributes which help the system control the running and

Obtaining the user- and group-ids

Page 20: The Process - Part II. Process Attributes  Each UNIX process is associated with a number of attributes which help the system control the running and

Setting the e-user- and group-ids