2
Unix-
: Unix- . . . init . Unix , Unix . gcc.
ps top. Linux /proc, ps top .
:
#ps AfH | more
( 2.1):
: ps [PID] [options]
- man ps.
() tree.
A , f , H . | more | less .
2.1 Novell Suse Linux 10.0.
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 18:23? 00:00:01 init [5]
root 2 1 0 18:23? 00:00:00 [ksoftirqd/0]
root 3 1 0 18:23? 00:00:00 [events/0]
root 4 1 0 18:23? 00:00:00 [khelper]
root 5 1 0 18:23? 00:00:00 [kthread]
root 7 5 0 18:23? 00:00:00 \_ [kblockd/0]
:
UID ,
PID ,
PPID ,
TIME ,
TTY ,
CMD .
( 2.1) init, . init ( - init [5]) .
Unix
Unix- () . 1 , , , Unix.
, UNIX . , , " " , , ( trap - ). , , , " ", . , . UNIX , , , . .
|
|
. -. , , , .
, . " " " ", .. . , , , .
fork() , , . fork() . execl(execl, execlp, execle, execv, execvp). ( 2.2).
$ps - :
2.2 ps
main()
int st;
if (fork()==0)
execlp("ps", "ps", 0);
wait(&st);
. :
o , fork(), (parent process).
o (child process).
o
o . fork() , 0. , fork() -1.
|
|
:
a) main:
void main(int argc, char *argv[], char *envp[]);
$ a.out a1 a2 a3, a.out , main a.out :
argc = 4 /* */
argv[0] = "a.out" argv[1] = "a1"
argv[2] = "a2" argv[3] = "a3"
argv[4] = NULL
argv[0] .
b) "" ( 1) char *envp[], extern char **environ;
"="
NULL ( argv).
char *getenv(char *);
, NULL .
c) . () 3 :
FILE * stdin stdout stderr
fd 0 1 2
" " , .
, ( open, creat, pipe, fopen ..). 20 ( ), (, 64).
d) - .
2.3
#include <stream.h>
#include <sys/types.h>
#include <unistd.h>
main ()
{
cout << " PID: " << getpid() << \n;
cout << " - PPID: " << getppid()<< \n;
cout << " PGID: " << getpgrp()<< \n;
cout << " real UID: " << getuid() << \n;
cout << " real -GID:" <<
getgid()<< \n;
cout << " - UID: " <<
geteuid()<< \n;
cout << " GID: " <<
getegid()<< \n;
exit(0);
}
) - -"",
chdir(char *__);
, $pwd.
2.4
#include <stream.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <signal.h>
void mpinfo()
{
cout << " PID: " << getpid() << \n;
cout << " - PPID: " << getppid()<< \n;
cout << " PGID: " << getpgrp()<< \n;
cout << " real UID: " << getuid() << \n;
cout << " real -GID:" <<
getgid()<< \n;
cout << " - UID: " <<
geteuid()<< \n;
cout << " GID: " <<
|
|
getegid()<< \n;
}
void main()
{
int i,st;
signal(SIGCHLD, SIG_IGN);
for(i=1;i<=3;i++)
fork();
mpinfo();
wait(&st);
exit(0);
}
( 2.4), . wait(&st) .
, . gcc g++. make.
gcc
GNU Compiler Collection (gcc) - C, C++, GNU. http://www.gnu.org/software/gcc/gcc.html
"" , GNU. gcc Linux - .
main.c
gcc main.c -o main
g++ main.cpp -o main
o , , a.out
./main
g
gcc -g main.c -o main
gcc main.c a.c b.c -o multisource
gcc -c main.c
gcc -c a.c
gcc -c b.c
gcc main.o a.o b.o -o multisource