522_UNIX-Process(w3-1).

Download Report

Transcript 522_UNIX-Process(w3-1).

UNIX Process
1.
2.
3.
4.
5.
6.
Processes
Commands that deal with processes
UNIX System Call … using system(“…shell
command…”);
fork’n exec System Calls to manipulate your processes
UNIX System Calls … accessing the kernel
Process ID, Parent Process ID, Process Group ID
PID,PPID,GID
EEE522:
Ian McCrum: www.eej.ulst.ac.uk/~ian
1
The Linux Kernel
•
•
Originally 10,000 lines of C, now a bit
bigger, the kernel controls access to the
hardware. It allows the creation of user
programs and allocates resources and
keeps track of who is using what.
The first program run is called init it starts
other programs, e.g that monitor consoles.
A console is a screen and has a
keyboard… these may be through serial
ports or ethernet
EEE522:
Ian McCrum: www.eej.ulst.ac.uk/~ian
2
The Kernel boot
•
•
•
•
The processes monitoring “serial ports” will
then run a login program once input is
detected.
Each user who gives a known username and
password is then given a shell usually bash.
(look at the file /etc/passwd… the last lines)
Once in your shell, you can launch
programs
EEE522:
Ian McCrum: www.eej.ulst.ac.uk/~ian
3
Process Management
To keep track of all the processes, tables are
used. Also the CPU should provide some
kind of hardware protection. E.g supervisor
mode and user mode. Or use “protected
memory” – the aim is that a user program
can not access memory or devices “owned”
by the kernel, or supervisor program,
sometimes called an executive in other
OSes
EEE522:
Ian McCrum: www.eej.ulst.ac.uk/~ian
4
Kernel services
A Unix box can appear to be running a lot of
programs at once. If requests come over the
network then various net orientated
programs can run in the background, the
logged in user is not even aware of this. E.g.
a webserver (httpd) or ftp server (ftpd) or a
remote login facility (sshd or telnetd). Note
the ‘d’ stands for daemon… a background
program
EEE522:
Ian McCrum: www.eej.ulst.ac.uk/~ian
5
Processes and programs
A process is a program in a state of execution.
Programs exist on disk or in ram but only
when a program is executed is a process is
created. Usually there will be more than one
process per program, any input or output
will cause other processes to be launched.
(Unix is a multiprogramming system)
EEE522:
Ian McCrum: www.eej.ulst.ac.uk/~ian
6
Process management
•
•
The Process table is managed by the Kernel.
It holds one “line” per process; c.f ps –l shows some of
these … status,uid,pid,ppid whether in ram or swopped
out and pointers to a “per process data area”. …this
holds saved registers (if swopped out) and users ids for
file access. These tables are not accessible by a user they are held in kernel memory space. The user data
region is where the user’s program is stored (data and
instructions or just data).
EEE522:
Ian McCrum: www.eej.ulst.ac.uk/~ian
7
User data region
•
•
•
Each user program requires data space… a user
stack and data area (heap). And a code space. A
process image is comprised of the PPDA, the user
stack, user data and code. It gets swopped to disk.
Code can be read only… allows more than one user
to run it at the same time. Each user has his own
user data but code may be shared read only called
“pure text” in Unix
Read only code need not be swopped(written) to
disk, since it hasn’t changed. Unix has a third table
to keep track of read-only shared code .
EEE522:
Ian McCrum: www.eej.ulst.ac.uk/~ian
8
User mode and Kernel Mode
•
•
A process will execute instructions in the user
program until some outside facility is required or
an outside event, such as a clock interrupt , occurs.
The CPU switches from user mode to kernel
mode.
When the execution of kernel instructions is
complete another process will resume. The kernel
has a process schedule to look after this. A process
may be suspended or blocked (waiting on an
external event such as disk I/O to complete)
EEE522:
Ian McCrum: www.eej.ulst.ac.uk/~ian
9
Processes
u
Process.
• Running program / application.
– System – e.g. csh.
– User initiated – e.g. pico.
u
Each uniquely numbered.
• Process ID pid
• Command “ps” lists
EEE522:
(see man pages)
Ian McCrum: www.eej.ulst.ac.uk/~ian
10
Example of $ps -el
Note init is the first
process started on
powerup (PID is 1)
u
u
u
u
u
u
u
$ps
Also try out the
PID TTY TIME CMD command $top for a
dynamic display.
4086 tty1 00:00:00 bash
NB $ps –el | more will
4138 tty1 00:00:00 ps
“page” the display
$ps –el|more;echo shows 14 columns !
F S UID PID PPID C PRI …TTY …CMD
4S 0 1
0 0 75
? init
EEE522:
Ian McCrum: www.eej.ulst.ac.uk/~ian
11
Commands for process control
u
u
u
u
u
u
u
Try the following… look up the man pages!
$find / -name * -print &
$jobs
$fg
$bg
$ps –el |grep find
$kill –9 nnn; echo use pid of find as nnn
EEE522:
Ian McCrum: www.eej.ulst.ac.uk/~ian
12
System calls
u
stdlib.h includes the function system()
• Permits system commands to be run from
inside a ‘C’ program.
• Prototype…..
int system(char *string)
• Where “string”
– Unix command
– shell script
– user program
EEE522:
Ian McCrum: www.eej.ulst.ac.uk/~ian
13
System call - example
u
Calling ls with a program
• system(“ls”);
or
• char *command = “ls”;
system(command);
or
• int main(void
{
printf(“Files in Directory\n”);
system(“ls –l”);
return 0;
}
EEE522:
Ian McCrum: www.eej.ulst.ac.uk/~ian
14
system() - return
u
system()
•
•
•
•
u
returns an integer
exit status of command
exit(); value
final return value in main
That is why main returns a value.
EEE522:
Ian McCrum: www.eej.ulst.ac.uk/~ian
15
Restrictions
u
u
Restrictions on using system()
Availability of memory
• Calling program remains in memory
• New copy of shell and calling program loaded
into RAM.
• If insufficient memory an error message is
displayed.
EEE522:
Ian McCrum: www.eej.ulst.ac.uk/~ian
16
Other system calls
u
u
execl(), wait() and fork()
execl – execute an leave
• process executed and then terminate
• The “ls” program could have been written
int main(void)
{
printf(“Files in Directory\n”);
execl(“/bin/ls, “ls”, “-l” ,0);
return 0;
}
u
See man pages for details.
EEE522:
Ian McCrum: www.eej.ulst.ac.uk/~ian
17
UNIX System Calls
* System Calls
- A system call is a direct entry point through which an active
process can obtain services from the kernel.
- The system calls are specific routines in the operating
system kernel that are directly accessible to application
programs.
EEE522:
Ian McCrum: www.eej.ulst.ac.uk/~ian
18
Features of UNIX System Calls
* Features of System Calls
- The system calls allow you to perform low-level I/O,
manipulate files and directories, create and control
multiple concurrent processes, manage interrupts, and
control I/O to terminals.
- Because UNIX is implemented in C, its system calls are
specified in C syntax and directly called from C
programs.
- A system call may need one or more associated header files.
These header files are clearly indicated with each call
described.
EEE522:
Ian McCrum: www.eej.ulst.ac.uk/~ian
19
UNIX Process
* UNIX Process
- An UNIX process is an instance of a program that is being
executed by the operating system. (fork system call)
- From a programming point of view, a UNIX process is the
entity created by the fork system call.
EEE522:
Ian McCrum: www.eej.ulst.ac.uk/~ian
20
States of the Process (1)
* Running
The process is executing.
* Asleep
A process in this state is waiting for an event to occur. Such
an event could be an I/O completion by a peripheral device,
the termination of another process, the availability of date or
space in a buffer, the freeing of a system resource, and so on.
When a runnng process has to wait for such an event it is
blocked and goes to sleep. This creates an opportunity for a
context switch, shifting the CPU to another process. Later,
when the event that a sleeping process is waiting for occurs,
it awakens and becomes ready to run.
EEE522:
Ian McCrum: www.eej.ulst.ac.uk/~ian
21
States of the Process (2)
* Ready
A process in this state is then scheduled for CPU service.
* Zombie
After termination of execution, a process goes into the
zombie state. The process no longer exits. The data structure
left behind contains its exit status and any timing statistics
collected. This is always the last state of a process.
EEE522:
Ian McCrum: www.eej.ulst.ac.uk/~ian
22
Creating a Process
1. Process
In Unix, when an executable program is read into system
memory by the kernel and executed, it become a process.
2. fork System Call
With the exception of some initial process generated by the
kernel during bootstrapping (e.g. swapper, init and
pagedaemon), all processes in a Unix programming
environment are created by a fork system call.
3. Parent process and Child process
The initiating process in termed the parent process.
The newly generated process the child process.
EEE522:
Ian McCrum: www.eej.ulst.ac.uk/~ian
23
fork System Call
1. Syntax of fork System Call
Include File(s): <sys/types.h>
<unistd.h>
Summary: pid_t fork (void);
Return
Success : 0 in child, child PID in parent
Failure: -1
Set errno: yes
2. Function of fork System Call
When a fork system call is made, the operating system
generates a copy of the parent process which becomes the
child process.
EEE522: Ian McCrum: www.eej.ulst.ac.uk/~ian
24
Parent /Child Process Relationship
Kernel
swapper PID 0
Kernel Process
init PID 1
pagedaemon PID 2
Parent
Kernel Process
Parent/child
Parent/Child
Child
Child
EEE522:
Child
Ian McCrum: www.eej.ulst.ac.uk/~ian
25
Child Process Unique Information
1.The operating system will pass to the child process most of
the parent’s system information (e.g. open file descriptors,
environment information, etc.)
2. Unique Information to the Child Process
# The child has its own process ID (PID).
# The child will have a different parent process ID (PPID) than
its parent.
# System imposed process limits (e.g., amount of CPU time the
process is allotted) are reset to zero.
# All record locks on files are reset.
# The action to be taken when receiving signals is different.
EEE522:
Ian McCrum: www.eej.ulst.ac.uk/~ian
26
Generating Child Process(ian2.cc)
Parent:
{fork();
printf(“Hello\n”);
fork();
Printf(“Bye\n”);}
Child/Parent
{printf(“Hello\n”);
fork();
Printf(“Bye\n”);}
Child
Printf(“Bye\n”);
EEE522:
Child
Printf(“Bye\n”);
Ian McCrum: www.eej.ulst.ac.uk/~ian
27
Process ID
* Process ID
Associated with each process is a unique positive integer
identification number called a process ID (PID).
* Initial Processes generated by the Kernel
- Process 0 (swapper process in BSD-based UNIX, sched
process in Solaris): responsible for process scheduling.
- Process 1 (init process ): basic responsibility is the
managing of terminal line and who is the parent process
of all other UNIX processes.
- Process 2 (pagedaemon process in BSD-based UNIX,
pageout process in Solaris): responsible for paging.
(BSD: Berkeley Software Distribution)
EEE522:
Ian McCrum: www.eej.ulst.ac.uk/~ian
28
getpid system Call
* getpid System Call
Function: to obtain the process ID
Include File(s):
<sys/types.h>
<unistd.h>
Summary:
pid_t getpid(void);
Return:
Successthe process ID
Failure-1
Sets errnoyes
* Example of getpid
printf (“My PID is %d \n”, getpid( ) );
EEE522:
Ian McCrum: www.eej.ulst.ac.uk/~ian
29
Parent Process ID
* Parent Process ID
The parent process is the process that forked the child
process. Associated with each parent process is a unique
positive integer identification number called a parent process
ID (PPID).
* Save the Return Child Process ID
There is no system call that allows a parent process to
determine the process IDs of all its child processes. if such
information is needed, the parent process should save the
returned child process ID value from the fork system call as
each child process is created.
EEE522:
Ian McCrum: www.eej.ulst.ac.uk/~ian
30
getppid System Call
* getppid System Call
Function: to obtain parent process ID
Include File(s): <sys/types.h>
Summary:
pid_t getppid( void );
Return:
Successthe parent process ID
Failure-1
Sets errnoyes
* Example of getppid System Call
printf(“My Parent Process ID is %d \n”, getppid( ) );
EEE522:
Ian McCrum: www.eej.ulst.ac.uk/~ian
31
Process Group ID
* Process Group
When a process generates child processes, the operating
system will automatically create a process group. The initial
parent process is known as the process leader.
* Process Group ID
Every process belong to a process group that is identified by
an integer number called a process group ID. The process
leader’s ID will be the same as its process group ID.
EEE522:
Ian McCrum: www.eej.ulst.ac.uk/~ian
32
getpgid System Call
* getpgid System Call
Function: to obtain process group ID
Include Files:
<sys/types.h>
<unistd.h>
Summary:
pid_t getpgid(pid_t pid);
Return:
Success the process group ID
Failure -1
Sets errno:
yes
errno message:
#1 : invalid access permissions for the calling process
#3:
no such process ID as pid
EEE522:
Ian McCrum: www.eej.ulst.ac.uk/~ian
33
Example of getpgid (ian3.cc) (1)
printf(“...PID PPID GID...,...);
(PID 2728
PPID 2437
i=0;
if(fork()==0)
printf(...);2729
i=1;
i=1;
if(fork()==0)
if(fork()==0)
printf(...);2733
printf(...);2730
i=2;
i=2;
i=2;
if(fork()==0) if(fork()==0) if(fork()==0)
printf(...);2735 printf(...);2734 printf(...);2732
EEE522:
GID 2437)
i=2;
if(fork()==0);
printf(...);2731
Ian McCrum: www.eej.ulst.ac.uk/~ian
34
Example of getpgit (Ian3.cc) (2)
2437
2728
2729
2730
2733
2732
2735
2734
2731
EEE522:
Ian McCrum: www.eej.ulst.ac.uk/~ian
35
To Use Floppies:
cd /mnt
mkdir flop
mount –t MSDOS /dev/fd0 /mnt/flopp
ls –l /home
mv /home/student99/*
/mnt/flopp
umount /dev/fd0
NEVER REMOVE WITHOUT
UNMOUNTING
EEE522:
Ian McCrum: www.eej.ulst.ac.uk/~ian
36
Exercises 2
1.
2.
3.
4.
Use the find command to list alphabetically all 3
letter commands on your system. (hint search the
path, find files with their ‘x’ bit set, use the wildcard
???. Send the output of your command into a text file
called 3.txt
Put your command into a script, check it works
Write a C program that calls it using the system call
Write a C program that invokes fork three times each
child can call a script. Search three different
directories; /bin, /usr/bin and /usr/X11R6/bin and
writethe results to three textfiles “pid.txt”
EEE522:
Ian McCrum: www.eej.ulst.ac.uk/~ian
37