Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
$B%W%m%;%9$H(Btask_struct$B9=B$BN(B
[go: Go Back, main page]

$B%W%m%;%9$H(Btask_struct$B9=B$BN(B

					2010$BG/(B12$B7n(B14$BF|(B
$B>pJs2J3XN`(B $B%*%Z%l!<%F%#%s%0%7%9%F%`(B II

                                       $BC^GHBg3X(B $B%7%9%F%`>pJs9)3X8&5f2J(B 
                                       $B%3%s%T%e!<%?%5%$%(%s%9@l96(B, $BEE;R!&>pJs9)3X7O(B
                                       $B?7>k(B $BLw(B
                                       <yas@is.tsukuba.ac.jp>

$B$3$N%Z!<%8$O!" http://www.coins.tsukuba.ac.jp/~yas/coins/os2-2010/2010-12-14
$B$"$k$$$O!" http://www.coins.tsukuba.ac.jp/~yas/
http://www.cs.tsukuba.ac.jp/~yas/

$B"#O"Mm;v9`(B

Softlab $B8&5f<<>R2p(B

$B"#:#F|$NBg;v$JOC(B

$B"#JdB-(B

$B%7%9%F%`!&%3!<%k$N=hM}$G$O!"FC8"L?Na$rI,MW$H$9$k!#%i%$%V%i%j4X?t$G$O!"(B $BIaDL$OFC8"L?Na$r;H$($J$$(B($BI,MW$J;~$K$O%7%9%F%`!&%3!<%k$rMxMQ$9$k(B)$B!#(B

$B"#%W%m%;%9$H$O(B

$B!V%*%Z%l!<%F%#%s%0%7%9%F%`(BI$B!W$NHO0O(B

$B"!%W%m%;%9$N#3$D$N>uBV(B

$B%W%m%;%9$N4pK\E*$J>uBV(B
$B>uBV(B $B@bL@(B
Ready CPU$B$,$"$l$P
Running CPU$B$,
Blocked (waiting) I/O$B40N;BT$A!"%M%C%H%o!<%/$d%f!<%6$+$i$NF~NOBT$A!#(B

Ready$B!

$B?^(B? $B%W%m%;%9$N#3>uBV(B

$B"!%W%m%;%9$H%9%l%C%I(B

$B%9%l%C%I$H$O!"#1$D$N%W%m%;%9!J$N%"%I%l%96u4V!K$NFbIt$K$U$/$^$l$F$$$kO@(B $BM}E*$JJBNs=hM}$NC10L!#(B

$B%7%s%0%k%9%l%C%I$N%W%m%0%i%`(B
$B#1EY$K#1$D$N $B%^%k%A%9%l%C%I$N%W%m%0%i%`(B
$B#1EY$K%9%l%C%I$N?t$@$1$N $B%9%l%C%I$O%W%m%;%9$N5!G=$N$&$A!"J]8n$N5!G=$rH4$$$F9bB.2=$7$?$b$N!#Nr;K(B $BE*$K$O!"(BSMP (Symmetric multiprocessor) $B$G$N8zN(E*$JJBNs=hM}$N$?$a$KF3F~(B $B$5$l$?!#F10l%W%m%;%9$KB0$9$k%9%l%C%I$O!"%"%I%l%96u4V$d%a%b%j$r6&M-$9$k!#(B $B@8@.!">CLG!"%9%l%C%I4VDL?.$,9bB.$K

$B"#MxMQ

$B
  • $B%7%9%F%`!&%3!<%k(B
  • ps$B%3%^%s%I(B
  • /proc$B%U%!%$%k!&%7%9%F%`(B

    $B"!(BUnix$B$K$*$1$k%W%m%;%9$K4X$9$k%7%9%F%`!&%3!<%k(B($B0lIt(B)

    • fork(): $B%W%m%;%9$N%3%T!<$r:n$k!#(B
    • getpid(), getppid(): $B<+J,$N%W%m%;%9<1JL;R(B(process identifier)$B!"?F(B $B%W%m%;%9(B(parent)$B$N<1JL;R$N getuid(), geteuid(), setuid(), seteuid(), setreuid(): $B%W%m%;%9$NB0(B $B@-$N$&$A!"(BUID (user identifier)$B$N getgid(), getegid(), getgroups(), setgid(), setegid(), setregid(), setgroups(): $B%W%m%;%9$NB0@-$N$&$A!"(BGID (group identifier)$B$N(B $B

      $B"!(Bps -l$B%3%^%s%I(B

      ps -l $B$N7k2L$N$&$A!" $BI=<((B $B@bL@(B STAT State$B!#>uBV!#(B PID Process ID$B!#%W%m%;%9(B1$B$D(B1$B$D$K=EJ#$J$$$h$&$K(B(unique)$B3d$jEv$F$?HV9f!#(B PPID Parent PID$B!#?F%W%m%;%9$N(BPID$B!#(B UID User ID$B!#%W%m%;%9$r@8@.$7$?MxMQ
      $ ps l [$B
      F   UID   PID  PPID PRI  NI   VSZ  RSS WCHAN  STAT TTY        TIME COMMAND
      0  1013 20591 20590  15   0  9224 2696 rt_sig Ss   pts/1      0:00 -tcsh
      0  1013 20761 20591  16   0  3724  904 finish T    pts/1      0:00 nm /usr/lib/l
      0  1013 20762 20591  16   0  2720  624 finish T    pts/1      0:00 lv
      0  1013 20795 20591  16   0  4256  628 -      R+   pts/1      0:00 ps l
      $ []
      
      • $B%W%m%;%9$O!"%W%m%;%9<1JL;R(B(PID)$B$G6hJL$5$l$k!#(B
      • $B%W%m%;%9$K$O!"?F%W%m%;%9$,$$$k!#(B
      • $B%W%m%;%9$O!"A4BN$GLZ9=B$$r7A@.$9$k!#(B

      $B"!$=$NB>$N%3%^%s%I(B

      • kill: $B%W%m%;%9$r6/@)=*N;(B
      • pstree: $B%W%m%;%9$rLZ9=B$$r;k3PE*$KJ,$+$j$d$9$$7A$GI=<((B
      • strace(Linux): $B%7%9%F%`!&%3!<%k$NI=<((B
      • lsof: $B%W%m%;%9$,3+$$$F$$$k%U%!%$%k$NI=<((B

      $B"!(B/proc $B%U%!%$%k!&%7%9%F%`(B

      /proc $B$N2<$K!"%+!<%M%kFb$N%G!<%?$r/proc/PID $B$N2<$K$O!"%W%m%;%9<1JL;R(B $B$,(BPID $B$N%W%m%;%9$N>pJs$,8=$l$k!#>\$7$/$O!"(Bman proc$B$r8+(B $B$J$5$$!#(B
      $ echo $$ [$B
      23069
      $ ls /proc/$$ [$B
      attr             cpuset   fd        maps        numa_maps  schedstat  status
      auxv             cwd      io        mem         oom_adj    smaps      task
      cmdline          environ  limits    mounts      oom_score  stat       wchan
      coredump_filter  exe      loginuid  mountstats  root       statm
      $ head -11 /proc/$$/status  [$B
      Name:   bash
      State:  S (sleeping)
      SleepAVG:       98%
      Tgid:   23069
      Pid:    23069
      PPid:   23068
      TracerPid:      0
      Uid:    1013    1013    1013    1013
      Gid:    510     510     510     510
      FDSize: 256
      Groups: 20 510 1020 1065 1150 
      $ []
      

      $B"!(Bfork() $B$H(B pid $B$rD4$Y$k%W%m%0%i%`(B

         1:	/*
         2:	        fork-pid.c -- fork() $B$7$F2hLL$K(B pid $B$rI=<($9$k%W%m%0%i%`(B
         3:	        ~yas/syspro/proc/fork-pid.c
         4:	        Created on: 2010/12/13 21:19:17
         5:	*/
         6:	
         7:	#include <sys/types.h>  /* getpid(), getppid() */
         8:	#include <unistd.h>     /* getpid(), getppid() */
         9:	#include <stdio.h>
        10:	
        11:	main()
        12:	{
        13:	    pid_t pid;
        14:	        fork();
        15:	        pid = getpid();
        16:	        printf("pid=%d\n", pid );
        17:	}
      
      $ make fork-pid [$B
      cc     fork-pid.c   -o fork-pid
      $ ./fork-pid [$B
      pid=11041
      pid=11040
      $ ./fork-pid [$B
      pid=11042
      pid=11043
      $ []
      
      • fork() $B$O!"(BUnix $B$G%W%m%;%9$r@8@.$9$k%7%9%F%`!&%3!<%k!#(B $B%W%m%;%9$r%3%T!<$9$k$3$H$G@8@.$9$k!#(B
      • $B%3%T!<$,:n$i$l$F$$$k$N$G(B printf() $B$,$=$l$>$l$N%W%m%;%9$G(B $B $B%W%m%;%9$N%3%T!<$H$$$C$F$b!"(BPID $B$O0[$J$k!#(B

      $B"!(Bfork() $B$H(B pid $B$rD4$Y$k%W%m%0%i%`(B

      pid
         1:	/*
         2:	        proc-pid-ppid.c -- $B2hLL$K(B pid $B$H(B ppid $B$rI=<($9$k%W%m%0%i%`(B
         3:	        ~yas/syspro/proc/proc-pid-ppid.c
         4:	        Created on: 2010/12/13 21:00:48
         5:	*/
         6:	
         7:	#include <sys/types.h>  /* getpid(), getppid() */
         8:	#include <unistd.h>     /* getpid(), getppid() */
         9:	#include <stdio.h>
        10:	
        11:	main()
        12:	{
        13:	    pid_t pid, ppid;
        14:	        pid = getpid();
        15:	        ppid = getppid();
        16:	        printf("pid=%d, ppid=%d\n", pid, ppid );
        17:	}
      
      $ make proc-pid-ppid [$B
      cc     proc-pid-ppid.c   -o proc-pid-ppid
      $ echo $$ [$B
      10771
      $ ./proc-pid-ppid  [$B
      pid=10873, ppid=10771
      $ ./proc-pid-ppid  [$B
      pid=10874, ppid=10771
      $ ./proc-pid-ppid  [$B
      pid=10875, ppid=10771
      $ []
      
      • $B%7%'%k$N(B PID $B$O!"(B$$ $B$H$$$&JQ?t$G;2>H$G$-$k!#(B
      • $B%7%'%k$O!"%3%^%s%I$r $B@8@.$5$l$?%W%m%;%9$N(B PID $B$O!"%W%m%;%9$4$H$K0[$J$k!#(B
      • fork() $B$7$?>l9g!"(BPPID $B$O!"%3%T!<85$N(B PID $B$K$J$k!#(B
      • $B%7%'%k$N%W%m%;%9$K$b!"?F%W%m%;%9$,$$$k!#(B
      • PID $B$H(B PPID $B$K$h$j!"%7%9%F%`A4BN$G$O%W%m%;%9$NLZ9=B$$,:n$i$l$k!#(B

      $B"!(Bfork()$B$K$h$k%W%m%;%9$NLZ(B

         1:	/*
         2:	        fork-hello.c -- $B2hLL$KJ8;zNs$rI=<($9$k%W%m%0%i%`(B
         3:	        ~yas/syspro/proc/fork-hello.c
         4:	        Start: 2001/05/13 23:19:01
         5:	*/
         6:	
         7:	#include <stdio.h>
         8:	
         9:	main()
        10:	{
        11:	        fork();
        12:	        fork();
        13:	        fork();
        14:	        printf("hello\n");
        15:	}
      
      $ make fork-hello [$B
      cc     fork-hello.c   -o fork-hello
      $ ./fork-hello  [$B
      hello
      hello
      hello
      hello
      hello
      hello
      hello
      hello
      $ []
      
  • Unix$B$G$O!"A4$F$N%U%!%$%k$d%W%m%;%9$O!"$"$k%f!<%6$N=jM-J*$G$"$k!#(B $B%U%!%$%k$H%W%m%;%9$K$O!"(BUID $B$,IU2C$5$l$F$$$k!#(B

    $B"!%0%k!<%W(B

    $B%0%k!<%W(B(group)$B$H$O!"(BUnix$B$N30$N@$3&$G$O!"7W;;5!$r;H$&?M4V$N=89g!#(B Unix$B$NFbIt$G$O!"
    $B%0%k!<%WL>(B(group name)
    $BJ8;zNs(B
    UID(user ID, user identifier)
    16$B%S%C%H(B-32$B%S%C%H$N@0?t(B

    $B#1?M$N%f!<%6$,J#?t$N%0%k!<%W$KB0$9$k$3$H$,$G$-$k!#(B

    $B"!%W%m%;%9$NB0@-(B

    $B%W%m%;%9$O!"(BUID (1$B8D(B) $B$H(B GID $B$rJ#?t8D;}$D!#(B

    $B"!(Bid$B%3%^%s%I(B

    id$B%3%^%s%I$r;H$&$H!"$=$N%W%m%;%9(B($B$N?F%W%m%;%9$G$"$k%7%'%k$+$i7Q>5$7$?(B) UID$B$H(BGID$B$,D4$Y$i$l$k!#(B
    $ id [$B
    uid=1013(yas) gid=510(prof) groups=20(games),510(prof),1020(c-admin),1065(c-spec),1150(tebiki)
    $ []
    

    $B"!(Bid-simple.c

    id $B%3%^%s%I$O!"FbItE*$K$O(Bgetuid(), getgid(), getgroups() $B%7%9%F%`!&%3!<(B $B%k$r;H$C$F$$$k!#0J2<$O!"(Bid $B%3%^%s%I$N4J0WHG$G$"$k!#(B
       1:	
       2:	/*
       3:	        id-simple.c -- a simple id command
       4:	        Created on: 2009/12/07 22:16:23
       5:	*/
       6:	
       7:	#include <unistd.h>     /* getuid(), getgid(), getgroups() */
       8:	#include <sys/types.h>  /* getuid(), getgid(), getgroups() */
       9:	#include <stdio.h>      /* printf() */
      10:	
      11:	#define MAXNGROUPS 100
      12:	
      13:	main( int argc, char *argv[], char *envp[] )
      14:	{
      15:	    uid_t uid ;
      16:	    gid_t gid ;
      17:	    gid_t groups[MAXNGROUPS];
      18:	    int len, i;
      19:	        uid = getuid();
      20:	        gid = getgid();
      21:	        len = getgroups(MAXNGROUPS,&groups[0]);
      22:	        printf("uid=%d gid=%d groups=", uid, gid );
      23:	        for( i=0; i<len; i++ )
      24:	            printf("%d,", groups[i]);
      25:	        printf("\n");
      26:	}
    
    $ cc id-simple.c -o id-simple [$B
    $ ./id-simple [$B
    uid=1013 gid=510 groups=20,510,1020,1065,1150,
    $ []
    

    $B"!(BUID$B!"(BGID$B$H%"%/%;%9@)8f(B

    $B#1G/@8$N!V%3%s%T%e!<%?%j%F%i%7!W$N;nNA$b!";29M$K$J$k!#(B http://www.coins.tsukuba.ac.jp/~yas/coins/literacy-2010/2010-05-11/

    $B"#(BLinux task$B9=B$BN(B

    $BMxMQ

    Linux $B$NFCp(B

    $B"!(Btask_struct$B9=B$BN(B

    linux-2.6.36/include/linux/sched.h

    1163:	struct task_struct {
    1164:	        volatile long state;    /* -1 unrunnable, 0 runnable, >0 stopped */
    ...
    1224:	        int exit_state;
    ...
    1251:	        struct task_struct *real_parent; /* real parent process */
    1252:	        struct task_struct *parent; /* recipient of SIGCHLD, wait4() reports */
    1253:	        /*
    1254:	         * children/sibling forms the list of my natural children
    1255:	         */
    1256:	        struct list_head children;      /* list of my children */
    1257:	        struct list_head sibling;       /* linkage in my parent's children list */
    1258:	        struct task_struct *group_leader;       /* threadgroup leader */
    ...
    1268:	        /* PID/PID hash table linkage. */
    1269:	        struct pid_link pids[PIDTYPE_MAX];
    1270:	        struct list_head thread_group;
    ...
    1290:	/* process credentials */
    1291:	        const struct cred *real_cred;   /* objective and real subjective task
    1292:	                                         * credentials (COW) */
    1293:	        const struct cred *cred;        /* effective (overridable) subjective task
    1294:	                                         * credentials (COW) */
    ...
    1300:	        char comm[TASK_COMM_LEN]; /* executable name excluding path
    1301:	                                     - access with [gs]et_task_comm (which lock
    1302:	                                       it with task_lock())
    1303:	                                     - initialized normally by setup_new_exec */
    ...
    1497:	};
    

    $B"!(Bstate

    $B%W%m%;%9$N>uBV!#(Bps$B%3%^%s%I$G(B STAT $B$NItJ,$K8=$l$k!#(B $B0lHLE*$K!"%W%m%;%9$O!"(B $B#3$D$N>uBV$r;}$D(B$B!#(B Linux $B$N%W%m%;%9$N>uBV$O$b$&>/$7B?$$!#uBV$rI=$F$$$k!#(B($BJd=uE*$K(B task_struct $B$N(B exit_state $B$b;H$&(B)$B!#(B
    include/linux/sched.h 
     182:	#define TASK_RUNNING            0
     183:	#define TASK_INTERRUPTIBLE      1
     184:	#define TASK_UNINTERRUPTIBLE    2
     185:	#define __TASK_STOPPED          4
     186:	#define __TASK_TRACED           8
     187:	/* in tsk->exit_state */
     188:	#define EXIT_ZOMBIE             16
     189:	#define EXIT_DEAD               32
     190:	/* in tsk->state again */
     191:	#define TASK_DEAD               64
     192:	#define TASK_WAKEKILL           128
     193:	#define TASK_WAKING             256
     194:	#define TASK_STATE_MAX          512
    
    $B0lHLE*$J>uBV(B Linux$B$N>uBV(B ps$BI=<((B$B@bL@(B
    Ready TASK_RUNNING R$B
    Running TASK_RUNNING
    Blocked TASK_INTERRUPTIBLE S$B%-!<%\!<%I$dB>$N%W%m%;%9$+$i$NF~NO$rBT$C$F$$$k!#(B
    TASK_UNINTERRUPTIBLE D$B%G%#%9%/F~=PNO$J$I$N40N;$rBT$C$F$$$k!#3d$j9~$_IT2D!#(B
    __TASK_STOPPED, __TASK_TRACED T$B0l;~E*$KDd;_$7$F$$$k$+!"%G%P%C%0$NBP>]$K$J$C$F$$$k!#(B
    TASK_DEAD Z$B4{$K=*N;$7$F$$$F!"=*N;=hM}$N40N;$rBT$C$F$k!#(B

    $B"!(Bpid_t$B7?(B

    pid_t $B7?$O!"%7%9%F%`!&%3!<%k$N%l%Y%k$G(B(API$B$H$7$F(B) $B%W%m%;%9<1JL;R$rI=$9(B $B7?!#(Bint$B7?$r(B typedef $B$GL>A0$rIU$1$F$$$k!#(B1$B$+$i(B32,768$B$^$G$GL@$$$F$$$kItJ,(B $B$r:FMxMQ$9$k!#(B(32,768$B$rD6$($?$i!"(B1$B$KLa$C$F3+$$$F$$$kHV9f$rC5$7$F;H$&!#(B)

    $B"!(Bstruct pid$B$H(Bstruct upid

    API$B$H$7$F$N%W%m%;%9<1JL;R$r!"%+!<%M%k$NCf$GI=8=$9$k$?$a$N9=B$BN!#(B

    $B"!(Bpids[]

    pids[] $B$O!"%W%m%;%9<1JL;R$rJ];}$9$k$?$a$NG[Ns!#$$$/$D$+$Ngetpid(),getppid(),fork() $B$K4XO"$7$F$$$k$b$N$O!"(B0 $BHVL\(B $B$N(B pids[PIDTYPE_PID]$B!#(B

    pids[PIDTYPE_PID] $B$O!"(Bpid_link $B7?$G!"FbIt$K(Bstruct pid $B$r;}$D!#(Bstruct pid $B$NCf$K$O!"(Bstruct upid $B$,$"$j!"$=$NCf$K$O(B getpid() $BEy$GMQ$$$k(B pid $B$r(B $BJ];}$9$k%U%#!<%k%I(B nr $B$,$"$k!#(B

    include/linux/pid.h
      50:	struct upid {
    ...
      52:	        int nr;
    ...
      55:	};
      57:	struct pid
      58:	{
    ...
      64:	        struct upid numbers[1];
      65:	};
    ...
      69:	struct pid_link
      70:	{
    ...
      72:	        struct pid *pid;
      73:	};
    

    $B"!(Bchar comm[TASK_COMM_LEN]

    command$BL>!#:GBg(B16$BJ8;z!#(B

    $B"!%W%m%;%9$NLZ9=B$(B

    real_parent, children, sibling $B$H$$$&%U%#!<%k%I$G%W%m%;%9$NLZ9=B$$,:n$i$l$k!#(B

    task_struct$B!

    $B?^(B? $B%W%m%;%9$NLZ9=B$(B

    $B"!(Bstruct task_struct *real_parent

    $B?F%W%m%;%9(B(fork() $B%7%9%F%`!&%3!<%k$G%3%T!<85$N%W%m%;%9!#(B)$B$N(Btask_struct$B9=B$BN$X$N%]%$%s%?!#(B $B?F$,@h$K;`$s$@>l9g$K$O!"(Binit$B%W%m%;%9$KM\;R$K=P$5$l$k!#(B

    $B"!(Bstruct task_struct *parent

    ptrace $B$G!"%H%l!<%9$7$F$$$k%W%m%;%9(B($B%G%P%C%,Ey(B)$B!#(B

    $B"!(Bstruct list_head children

    $B;R%W%m%;%9$N%j%9%H$N@hF,!#(B

    $B"!(Bstruct list_head sibling

    $B7;Do%W%m%;%9!J$D$^$j!"F1$8?F%W%m%;%9$N;R%W%m%;%9!K$N%j%9%H!#(B

    $B"!(Bstruct cred *real_cred

    credentials$B!#;q3J!#%U%!%$%k$r%"%/%;%9$9$k;~Ey$K%W%m%;%9$,$I$N$h$&$J8"8B(B $B$r;}$C$F$$$k$+$rI=$9!#(B

    task_struct$B!

    $B?^(B? UID$B$NJ]B8J}K!(B

    $B"!(Bstruct cred

    task_struct $B$KJ]B8$9$Y$-FbMF$N$&$A!"(Buid$B!"(Bgid$B!"(Bgroups $BEy$rH4$-=P$7$?$b$N!#(B
    include/linux/cred.h
    
      25:	/*
      26:	 * COW Supplementary groups list
      27:	 */
      28:	#define NGROUPS_SMALL           32
      29:	#define NGROUPS_PER_BLOCK       ((unsigned int)(PAGE_SIZE / sizeof(gid_t)))
      30:	
      31:	struct group_info {
      32:	        atomic_t        usage;
      33:	        int             ngroups;
      34:	        int             nblocks;
      35:	        gid_t           small_block[NGROUPS_SMALL];
      36:	        gid_t           *blocks[0];
      37:	};
    ...
     116:	struct cred {
     117:	        atomic_t        usage;
     118:	#ifdef CONFIG_DEBUG_CREDENTIALS
     119:	        atomic_t        subscribers;    /* number of processes subscribed */
     120:	        void            *put_addr;
     121:	        unsigned        magic;
     122:	#define CRED_MAGIC      0x43736564
     123:	#define CRED_MAGIC_DEAD 0x44656144
     124:	#endif
     125:	        uid_t           uid;            /* real UID of the task */
     126:	        gid_t           gid;            /* real GID of the task */
     127:	        uid_t           suid;           /* saved UID of the task */
     128:	        gid_t           sgid;           /* saved GID of the task */
     129:	        uid_t           euid;           /* effective UID of the task */
     130:	        gid_t           egid;           /* effective GID of the task */
    ...
     148:	        struct user_struct *user;       /* real user ID subscription */
     149:	        struct group_info *group_info;  /* supplementary groups for euid/fsgid */
    ...
     151:	};
    

    $B"!(Buid$B$H(Bgid

    uid$B$O!"%f!<%6$r<1JL$9$kHV9f!"(Bgid$B$O!"%0%k!<%W$r<1JL$9$kHV9f$rJ];}$9$k!#(B $B%U%!%$%k$r3+$/;~Ey$N%"%/%;%98"$N%A%'%C%/$K;H$o$l$k!#(B

    $B"!(Beuid$B$H(Begid

    effective uid $B$H(B effective gid$B!#(B

    Unix $B$K$O!"(Bset-uid $B$N%W%m%0%i%`$,$"$k!#(Bls -l $B$G(B user $B$N(B x $B$N=j$,(B s $B$K$J$C(B $B$F$$$k!#(B

    $ ls -l /usr/bin | egrep '^...s' [$B
    ...
    -rwsr-xr-x 1 root root       49392 Jan 27  2010 at
    ...
    -rwsr-sr-x 1 root root      315432 Jan  6  2010 crontab
    -rwsr-xr-x 1 root root       51576 Mar 31  2010 gpasswd
    ...
    ---s--x--x 2 root root      180448 Oct 19 22:11 sudo
    ...
    $ []
    
    $B$3$N$h$&$J%W%m%0%i%`$r$B"!(Bgroup_info $BDI2C$N(Bgid (getgroups()) $B$rJ];}$9$k!#(B

    $B"#(Bcurrent

    $BJQ?t(B current $B$O!"%+!<%M%kCf$G8=:_H$9$k!#(B

    $B"!(Bgetpid()$B%7%9%F%`!&%3!<%k(B

    kernel/timer.c
    
    1351:	SYSCALL_DEFINE0(getpid)
    1352:	{
    1353:	        return task_tgid_vnr(current);
    1354:	}
    
    include/linux/sched.h
    1600:	static inline pid_t task_tgid_vnr(struct task_struct *tsk)
    1601:	{
    1602:	        return pid_vnr(task_tgid(tsk));
    1603:	}
    
    1538:	static inline struct pid *task_tgid(struct task_struct *task)
    1539:	{
    1540:	        return task->group_leader->pids[PIDTYPE_PID].pid;
    1541:	}
    
    
    • getpid() $B%7%9%F%`!&%3!<%k$N task_tgid() $B$G$O!"(B(group_leader $B$N(B) pids[PIDTYPE_PID].pid (struct pid *$B7?(B)$B$rJV$7$F$$$k!#(B
    • pid_vnr() $B$O!"(Bstruct pid *$B$r(Bpid_t (int$B7?(B) $B$XJQ49$9$k!#(B
    group_leader $B$O!"%f!<%6%l%Y%k$GF1$8%W%m%;%9$KB0$9$k!"%+!<%M%k!&%l%Y%k$N(B $B%9%l%C%I$N@hF,$N$b$N$r;X$9!#(Bgroup_leader $B$O!"$I$N%+!<%M%k!&%l%Y%k$N%9%l%C(B $B%I$+$iD4$Y$F$b!"F1$8(B struct task_struct $B$r;X$7$F$$$k!#(B
    
    kernel/pid.c
     474:	pid_t pid_vnr(struct pid *pid)
     475:	{
     476:	        return pid_nr_ns(pid, $B>JN,(B);
     477:	}
    ...
     461:	pid_t pid_nr_ns(struct pid *pid, $B>JN,(B)
     462:	{
     463:	        struct upid *upid;
     464:	        pid_t nr = 0;
    ...
     467:	                upid = &pid->numbers[$B>JN,(B];
    ...
     469:	                        nr = upid->nr;
     471:	        return nr;
     472:	}
    
    • struct pid $B9=B$BN$+$i(B struct upid $B9=B$BN$r struct upid $B9=B$BN$N(B nr $B$H$$$&%U%#!<%k%I$r

      $B"!(Bgetppid()$B%7%9%F%`!&%3!<%k(B

      kernel/timer.c
      1362:	SYSCALL_DEFINE0(getppid)
      1363:	{
      1364:	        int pid;
      ...
      1367:	        pid = task_tgid_vnr(current->real_parent);
      ...
      1370:	        return pid;
      1371:	}
      
      current$B$N(Breal_parent$B$r0z?t$K$7$F(B task_tgid_vnr() $B$H$$$&4X?t$r8F$V!#(B $B0J9_!"(Bgetpid() $B$HF1$8!#(B

      $B"!(Bgetuid()$B%7%9%F%`!&%3!<%k(B

      kernel/timer.c
      1373:	SYSCALL_DEFINE0(getuid)
      1374:	{
      1375:	        /* Only we change this so SMP safe */
      1376:	        return current_uid();
      1377:	}
      
      current_uid() $B$H$$$&4X?t(B($B%^%/%m(B)$B$r8F$S=P$9!#(B

      include/linux/cred.h
      
       342:	#define current_cred_xxx(xxx)                   \
       343:	({                                              \
       344:	        current->cred->xxx;                     \
       345:	})
       346:	
       347:	#define current_uid()           (current_cred_xxx(uid))
      
      current$B$+$i(Bcred$B$r0z$-!"$=$NCf$N(B uid $B$H$$$&%U%#!<%k%I$rJV$9!#(B

      $B"#%/%$%:(B2 $B%W%m%;%9$H(Btask_struct$B9=B$BN(B

      $B!zLdBj(B(201) Ready$B>uBV$N%W%m%;%9(B

      $B%*%Z%l!<%F%#%s%0!&%7%9%F%`$G$O!"!V0lHL$K!W%W%m%;%9$O!"(BReady$B!"(BRunning$B!"(B Blocked$B$N>uBV$r;}$D!#(BLinux $B$K$*$$$F!"%W%m%;%9$,(B Ready $B>uBV$G$"$k$3$H$r(B $B<($9$?$a$K!"(Btask struct $B$N%U%#!<%k%I(B state $B$K!"2?$H$$$&CM$r@_Dj$7$F$$$k$+!#(B

      $B!zLdBj(B(202) pid$B$H(Bppid

      $B 1: #include <stdio.h> 2: #include <unistd.h> 3: 4: main() { 5: pid_t pid, ppid; 6: fork(); 7: pid = getpid(); 8: ppid = getppid(); 9: printf("hello: (pid=%d,ppid=%d)\n",pid, ppid); 10: } $B0J2<$N6uMs!J6uMs#A!"6uMs#B!"6uMs#C!"6uMs#D!K$rKd$a$F!"5/$3$jF@$k7k2L$r(B 1$B$D:n$j$J$5$$!#(B
      $ echo $$ [$B
      1001
      $ ./fork-printf  [$B
      hello: (pid=$B6uMs#A(B,ppid=$B6uMs#B(B)
      hello: (pid=$B6uMs#C(B,ppid=$B6uMs#D(B)
      $ []
      
      $B$?$@$7!"(BPID $B$H$7$F$O!"(B1001,1002,1003,1004 $B$NCf$+$iA*$S$J$5$$!#(B $B$J$*!"Ez$($O#1DL$j$G$O$J$$!#(B

      $B!zLdBj(B(203) getgid()$B%7%9%F%`!&%3!<%k(B

      getuid() $B%7%9%F%`!&%3!<%k$K=,$C$F(B getgid() $B%7%9%F%`!&%3!<%k$r

      $B%R%s%H(B: current $B$+$i=PH/$9$k!#(Bcurrent_cred_xxx() $B%^%/%m$r;H$C$F$b;H$o$J(B $B$/$F$b$h$$!#(B


      Last updated: 2010/12/21 16:47:46
      Yasushi Shinjo / <yas@is.tsukuba.ac.jp>