Adv Unix Concepts
-
Upload
keshav-prakash -
Category
Documents
-
view
217 -
download
0
Transcript of Adv Unix Concepts
-
8/2/2019 Adv Unix Concepts
1/21
Advanced Unix Concepts
Advanced Unix Concepts
Satyajit Rai
March 17, 2003
March 22, 2003 KReSIT, IIT Bombay 1
-
8/2/2019 Adv Unix Concepts
2/21
Contents
Contents
Advanced Unix Concepts . . . . . . . . . . 1 Contents . . . . . . . . . . . . . . . . . . 2 Process Creation . . . . . . . . . . . . . . 4
fork() . . . . . . . . . . . . . . . . . 5 What you get from your parent? . . . . . . 6
What is your own? . . . . . . . . . . . . . 7 exit() . . . . . . . . . . . . . . . . . 8
Login Procedure . . . . . . . . . . . . . . 9March 22, 2003 KReSIT, IIT Bombay 2
-
8/2/2019 Adv Unix Concepts
3/21
Contents
Process Group . . . . . . . . . . . . . . . 10 Session . . . . . . . . . . . . . . . . . . 11
Controlling Terminal . . . . . . . . . . . . 12 Contents . . . . . . . . . . . . . . . . . . 14
Job Control . . . . . . . . . . . . . . . . 16 Job Control Primitives . . . . . . . . . . . 17
Shell Support for Job Control . . . . . . . . 19 Orphaned Process group . . . . . . . . . . 20
References . . . . . . . . . . . . . . . . 21March 22, 2003 KReSIT, IIT Bombay 3
-
8/2/2019 Adv Unix Concepts
4/21
Process Creation
Process Creation
Every process is created using fork() system call.
Exceptions are:
init: system specific initialization parent of all orphans.
swapper: scheduling.
pagedeamon: Virtual memory management.
These processes are specifically created during bootstrapping.
March 22, 2003 KReSIT, IIT Bombay 4
-
8/2/2019 Adv Unix Concepts
5/21
fork()
fork()
Called once, returns twice.
Returns 0 in child, pid in parent.
A processes can have only one parent, but many children.
data, heap, and stacksegments are copied for child.
All file descriptors are duplicated.
Copy on write (COW) Linux.
vfork() when child called exec() or exit(), parent
resumes.
March 22, 2003 KReSIT, IIT Bombay 5
-
8/2/2019 Adv Unix Concepts
6/21
What you get from your parent?
What you get from your parent?
process credentials (real/effective/saved UIDs and GIDs)
environment, stack, memory
open file descriptors (note that the underlying file positions are shared
between the parent and child, which can be confusing)
close-on-exec flags, signal handling settings
nice value, scheduler class
process group ID, session ID
current working directory, root directory, file mode creation mask
(umask)
resource limits, controlling terminal
March 22, 2003 KReSIT, IIT Bombay 6
-
8/2/2019 Adv Unix Concepts
7/21
What is your own?
What is your own?
process ID, parent process ID
copy of file descriptors and directory streams.
process, text, data and other memory locks are NOT inherited.
process times, in the tms struct
resource utilizations are set to 0
pending signals initialized to the empty set
timers created by timer create not inherited
asynchronous input or output operations not inherited
March 22, 2003 KReSIT, IIT Bombay 7
-
8/2/2019 Adv Unix Concepts
8/21
exit()
exit()
return from main().
calling exit() form a function.
calling exit() does not clos and flush I/O buffers.
Abnormal termination abort(), or receipt of some signal.
March 22, 2003 KReSIT, IIT Bombay 8
-
8/2/2019 Adv Unix Concepts
9/21
Login Procedure
Login Procedure
init spawn one getty process per terminal.
Normal Terminal
init getty login shell
terminal driver user
Network Logins
init ... inted telnetd login
shell psuedo terminal driver ...
user
March 22, 2003 KReSIT, IIT Bombay 9
-
8/2/2019 Adv Unix Concepts
10/21
Process Group
Process Group
pid t getgrp(void) - get the group ID.
int setpgid(pid t pid, pid t pgid) set the
Process group id. (also see setsid())
if pid == pigid, the process becomes the process group
leader.
A process can set the process group id of only itself, and its
children.
If a system does not support job control, returns error with
errno set to ENOSYS.
March 22, 2003 KReSIT, IIT Bombay 10
-
8/2/2019 Adv Unix Concepts
11/21
Session
Session
Session is a collection of one or more process groups.
pid t setsid(void) return process group ID if OK, -1
on error.
The process becomes the session leader of a newly created
session.
The process becomes the process group leader of a new process
group. The new process group id is the process id of the callingprocess.
The association with the controlling terminal is broken (if any).
March 22, 2003 KReSIT, IIT Bombay 11
-
8/2/2019 Adv Unix Concepts
12/21
Controlling Terminal
Controlling Terminal
A session can have a single controlling terminal.
Session leader that establishes connection to the controlling
terminal is the controlling process.
One of the process groups is foreground, while others are
background.
Terminal keys (Ctrl-C, Ctrl-Z, Ctrl-\) are sent to foreground
process group.
March 22, 2003 KReSIT, IIT Bombay 12
-
8/2/2019 Adv Unix Concepts
13/21
Controlling Terminal
Modem disconnect SIGHUP is sent to session leader
default action of SIGHUP is to killthe processes.
Setting and getting foreground process group
pid t tcgetpgrp(int filedes)
int tcsetpgrp(int filedes, pid t pgrid)
March 22, 2003 KReSIT, IIT Bombay 13
-
8/2/2019 Adv Unix Concepts
14/21
Contents
Contents Advanced Unix Concepts . . . . . . . . . . 1
Contents . . . . . . . . . . . . . . . . . . 2 Process Creation . . . . . . . . . . . . . . 4
fork() . . . . . . . . . . . . . . . . . 5 What you get from your parent? . . . . . . 6
What is your own? . . . . . . . . . . . . . 7 exit() . . . . . . . . . . . . . . . . . 8
Login Procedure . . . . . . . . . . . . . . 9
March 22, 2003 KReSIT, IIT Bombay 14
-
8/2/2019 Adv Unix Concepts
15/21
Contents
Process Group . . . . . . . . . . . . . . . 10 Session . . . . . . . . . . . . . . . . . . 11 Controlling Terminal . . . . . . . . . . . . 12 Contents . . . . . . . . . . . . . . . . . . 14
Job Control . . . . . . . . . . . . . . . . 16 Job Control Primitives . . . . . . . . . . . 17
Shell Support for Job Control . . . . . . . . 19 Orphaned Process group . . . . . . . . . . 20
References . . . . . . . . . . . . . . . . 21March 22, 2003 KReSIT, IIT Bombay 15
-
8/2/2019 Adv Unix Concepts
16/21
Job Control
Job Control
Job control is a feature that allows us to
control multiple jobs from single terminal.
control which jobs can access terminal, and which to run in
background.
Job control requires support form
Shell
Terminal driver in Kernel
Job control signals (SIGCHLD, SIGCONT, SIGSTOP,
SIGTSTP, SIGTTIN, and SIGTTOU)
March 22, 2003 KReSIT, IIT Bombay 16
-
8/2/2019 Adv Unix Concepts
17/21
Job Control Primitives
Job Control Primitives
Ctrl-C (SIGINT) Terminates the foreground process group.
Ctrl-\ (SIGQUIT) Terminates foreground process group with
core.
Ctrl-Z (SIGTSTP) Suspend the foreground process group.
To start a background process, use & at the end of command line.
e.g.
$ make all > make.out & # make is executed in background
March 22, 2003 KReSIT, IIT Bombay 17
-
8/2/2019 Adv Unix Concepts
18/21
Job Control Primitives
bg send process group to background. fg n bring process group (job id n assigned by shell) to
foreground.
Background jobs can not access the stdin instead stopped.
We can also disable access to stdout using stty tostop
(see man stty).
March 22, 2003 KReSIT, IIT Bombay 18
-
8/2/2019 Adv Unix Concepts
19/21
Shell Support for Job Control
Shell Support for Job Control
C-Shell offers job control, whereas Bourne shell doesnt offer it. Job
control with Korn Shell is dependent on system.For a shell without job control
A process group will have same PGID, SID, and TPGID.
For a shell with job control
PGID of the background process group is different than that of
TPGID.
TPGID of the foreground process group is same as that of
TPGID.
March 22, 2003 KReSIT, IIT Bombay 19
-
8/2/2019 Adv Unix Concepts
20/21
Orphaned Process group
Orphaned Process group
If a child lives longer than its parent,
Child receives SIGHUP default action is kill.
Child becomes orphaned and gets shelter under init.
PPID of the child process becomes 1.
Child becomes member of orphaned process group.
March 22, 2003 KReSIT, IIT Bombay 20
-
8/2/2019 Adv Unix Concepts
21/21
References
References
Advance Programming in Unix Environment Richard Stevens,
Addision-Wesley.
Unix FAQs
(http://www.faqs.org/faqs/unix-faq/programmer/faq/)
Coming Up...
Signals in Unix
The End
March 22, 2003 KReSIT, IIT Bombay 21