The Process - Part II. Process Attributes Each UNIX process is associated with a number of...
-
date post
21-Dec-2015 -
Category
Documents
-
view
218 -
download
2
Transcript of The Process - Part II. Process Attributes Each UNIX process is associated with a number of...
![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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d585503460f94a378c5/html5/thumbnails/1.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d585503460f94a378c5/html5/thumbnails/2.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d585503460f94a378c5/html5/thumbnails/3.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d585503460f94a378c5/html5/thumbnails/4.jpg)
![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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d585503460f94a378c5/html5/thumbnails/5.jpg)
![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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d585503460f94a378c5/html5/thumbnails/6.jpg)
![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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d585503460f94a378c5/html5/thumbnails/7.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d585503460f94a378c5/html5/thumbnails/8.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d585503460f94a378c5/html5/thumbnails/9.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d585503460f94a378c5/html5/thumbnails/10.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d585503460f94a378c5/html5/thumbnails/11.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d585503460f94a378c5/html5/thumbnails/12.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d585503460f94a378c5/html5/thumbnails/13.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d585503460f94a378c5/html5/thumbnails/14.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d585503460f94a378c5/html5/thumbnails/15.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d585503460f94a378c5/html5/thumbnails/16.jpg)
![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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d585503460f94a378c5/html5/thumbnails/17.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d585503460f94a378c5/html5/thumbnails/18.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d585503460f94a378c5/html5/thumbnails/19.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062421/56649d585503460f94a378c5/html5/thumbnails/20.jpg)
Setting the e-user- and group-ids