шпора UNIX

  • View
    57

  • Download
    4

Embed Size (px)

Transcript

UNIX ; POSIX 1003.1-1998 (Application Programming Interface) (API) POSIX 1003.2-1992 UNIX POSIX 1003.1b-1993 , POSIX 1003.1-1995 (threads). UNIX

1

1. , , . UNIX . 2. UNIX , . . . 3. . UNIX , . 4. , . , , . 5. , , , . 6. , . UNIX UNIX : . , . , . . , , , UNIX. . , , . . , . . : - , . - - , . , , . , . . . . ,

, , , . , , . , . - . . . , , , . . , . . . - . . , . UNIX UNIX : . , , , . . , .. , . , . . . , . /, : /home/student/proga.cpp , . , . , , . , . . . , . UNIX : 1. 2. 3. 4. FIFO 5. (link) 6. (socket) : 1. : , . . , .

2

2. : , , , ( ), . , .. . , . 10520 . 10534 .. 8671 file1.txt 9731 file2.txt 4554 p1.c 3. : . UNIX . (, ), ( ). , . , , , . 4. FIFO : . FIFO UNIX, Release-5. 5. (link). , . . , , , , , , . : ln p1 /home/student/proga2.cpp . . , , . , . , . , . : ls -l . , , , , ... . , , . : ln -s proga3.cpp /home/student/rulezz.cpp cat rulezz.cpp proga3.cpp , , , , . 6. (socket): , . TCP/IP, . UNIX UNIX. , , . /bin . /dev , . , . /etc . , rcN, N , . /default , . /lib C .

3

/lost+found . . . /mnt () . , .. . /home . /usr . /spool UNIX. /tmp UNIX. UNIX : . . , , . - , . . ls l . . - , . , , . , , . - , . - . -. - , , . chown: chown stud1 p1.c p2.c t.txt , , () . : chgrp ppt *.c , . . - . UNIX , . : 1. - 2. , 3. , : . ls l, 10 : o rwxr--r-d , c , p FIFO , , b . :

4

I II III r w x chmod a + w t.txt chmod g+x-w r.out

, , . , , , . . :

5

. : u -, g -, o , a . , : + , , = . , , , , . , . . , cat, . . . , , , , . . , , . , ls l, , . cd. . , , , . , , , . , . , - , , . , , . : , , . . , , . . , , , , , . , , .

. , , . , . , . , . t s s 1 chmod u+s f1 t: ( ) , , . SUID SGID , . , , . SUID SGID , . , - , , . SUID - , SGID -. . 1 , . t , . , , . , , . SGID . - - . UNIX . . - . , - . : , , . . , . ls -l , . UNIX UNIX . , .

6

, . , , . ( , , ). ( ). / , . , , , , .. UNIX . , , . , .. . , . . , UNIX. , , , , , . . . . , , . : shed vhand bd f flush kmadaemon init ( /etc) . , ( ) ( , , ..) . , . , . , . (shell), , . , , . . . , , .

7

1. (PID). , . , . , .. , . , . , . 2. (PPID) , . 3. (NiceNumber). . , ( ). , , . 4. (TTY) (), . . 5. (RID) (EUID) . , , . , . , SUID. . 6. (RGID) (EGID) . , . . SUID. OS UNIX , . . , , , . : main (int argc; char *argv[]; char *envp[]) : extern char **environ; // , . main (int argc; char *argv[]) argc , , . , . , . : HOME=/home/student/851003 #include #include #include extern char **environ; void main(int argc, char *argv[]) { char *term; char buf[200], var[200]; if ((term=getenv("MYVAR"))==NULL) { printf(" MYVAR . : "); gets(buf); sprintf(var,"MYVAR=%s",buf); putenv(var); printf(" %s \n",var); }

8

else { printf("MYVAR=%s | ? ",term); gets(buf); if (buf[0]=='Y' || buf[0]=='y') { printf(" : "); gets(buf); sprintf(var,"MYVAR=%s",buf); putenv(var); printf(" MYVAR=%s",var); } } }

9

char *getenv (const char *name); int putenv(const char *string); . . , , - , , . : , , , . shell -: . getty, , . shell . , . , , . _start( ). , , , main( ). . main( ) exit( ). , , . . xit( ) : #include void exit(int status); status, exit( ), . 0, ( -1) . exit( ) : - , atexit( ) #include int atexit (void (*func)(void)); , , . _exit, .

shell , , , , , . , . fork. : #include #include pid_t fork(void); main( ) { int pid; pid = fork( ); if (pid == -1) { printf( ); exit(-1); } if (pid == 0) { printf(\n -); } else { printf(\n -); . . . } } pid_t getpid (void); - , . pid_t getppid (void); - , - . fork. (-) , , . : , . - . , , , . fork . , , , : - PID - - , fork, - -. , PID , - 0. 1. , errno. ENOMEM . EAGAIN . EXEC , . #include int execl (char *path, char arg0, , char argn, (char*)NULL); int execv (char *path, char argv[ ]); int execle (char *path, char arg0, , char argn, (char*)NULL, char *envp[ ]); int execve (char *path, char argv[ ], char *envp[ ]);

10

int execlp (char *file, char arg0, , char argn, (char*)NULL); int execvp (char *file, char argv[ ]); , . , , . , , , . . fork, , , exec . exec . 1 , . p exec , file /, , PATH. exec . #include #include #include #include #include #include #include int system (char *cmd) { pid_t pid; int status; switch (pid = fork( )) { case 1: return(-1); case 0: execl (/bin/sh, sh, -c, cmd, 0); perror (execl); exit (errno); } if (waitpid(pid, &status, 0)==pid&&WIFEXITED(status)); return(WEXITSTATUS(status)); return(-1); } int main ( ) { int rc = 0; char buf[256]; do { printf (sh>0); fflnsh (student); if (!gets (buf)) break; rc = system (buf); } while (!rc); return (rc);} include pid_t wait(int *status_p); pid_t waitpid(pid_t child_pid, unt *status_p, int option); wait waitpid . , . wait , , .

11

wait, wait , status_p, PID . , , wait, wait -1, errno . , wait . waitpid . , . child_pid : ID PID -1 0 , , . , , . pipe . UNIX- , .. fifo[0], fifo[1] , , First In First Out. . . . 1. : pipe , fork . .. -, - - fifo[0] fifo[1] 2. : pipe, 2 -. - fifo[0] fifo[1] , , . , , , . #include #include #include #include #include int main() { pid_t child_pid, pid; int status, fifo[2], char buf[80]; if (pipe(fifo)==-1) printf ( pipe!); _exit(1); switch(child_pid=fork()) { case -1 : printf ( fork!); _exit(2); case 0 : close(fifo[0]); printf ( %d , getpid())); close(fifo[1]); _exit(0);} close(fifo[1]);

13

while (read(fifo[0],buf,80)) printf(\n %s,buf); close(fifo[0]); if (waitpid(child_pid, &status, 0)==child_pid && WIFEXITED(status)); return(WEXITSTATUS(status); return(3); } , , . , . , , . , , . , , . , , , , . , . , , , SIGPIPE. . , fifo[0], fifo[1]. , , - fifo[1]. , , , . , . . , , . . #include #include int kill (pid_t pid, int sig); int raise (int sig); kill . sig, , . PID>0, , PID. PID=0, , , . PID=)) then :=- else begin :=+1; [n]:=; V();

38

P([n]); P(); end; ; V(); V(); goto n; end;

39

... ... ... N: begin ... end; 1: begin ... end; .