Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.

Linuxカーネルを読んで改めて知るプロセスとスレッドの違い

1 023 vues

Publié le

2018年7月25日のレトリバセミナーの資料です。

Publié dans : Logiciels
  • Soyez le premier à commenter

Linuxカーネルを読んで改めて知るプロセスとスレッドの違い

  1. 1. 8 A 1. 0 1., 2 1 2 1
  2. 2. / 2/ , 5 44 201 8 /8/ /. • ed 8 • R@ I • a • cg A @
  3. 3. • c c gd t l • c efho t Rs • t c gd = I • t i a • 81 . , 8 3 81 . . ., 82 t • c xu t i a Rs A • efho n • i a c rv .8 2.: , 33 2018 . . :.
  4. 4. • 02 8 I RA L R A cd • I • a e • I 0 2 11 0. 4 4 ,
  5. 5. • .: C I c il • A 8: C 4 2 5A : C • ,/1 .: 2 G il • A 8 C 8 A 7 2 : 4 A 2 5 • aI Rnv otRghm I I fsL U • I I Rbd r U • .: C Rpue UN 0 : 2 4 :8 A A 5
  6. 6. • • 0268A L • • • • R a • I • 0 2 11 0. ,
  7. 7. ( 0 0 - 7. ) 2 0 0 0 • 18 • A I • A I • 18 • 18 ,18 R #include <stdio.h> #include <unistd.h> int main() { pid_t pid = fork(); // forkA if (pid == -1) { printf("Fork failed¥n"); // } else if (pid == 0) { printf("Child process, pid: %d¥n", getpid()); // } else { printf("Parent process, pid: %d¥n", getpid()); } } $ gcc fork_sample.c && ./a.out Parent process, pid: 72390 Child process, pid: 72391
  8. 8. 0 A: 2 . 4# A:8 A A 5# • , 8: #4 A 2 5 : (# A 7 A #4 / ) • 17 A RL15 17 A I #ifdef __ARCH_WANT_SYS_FORK SYSCALL_DEFINE0(fork) { #ifdef CONFIG_MMU return _do_fork(SIGCHLD, 0, 0, NULL, NULL, 0); #else /* can not support in nommu mode */ return -EINVAL; #endif } #endif
  9. 9. • • 02 8A L • • • • R a • I • 9 0 2 11 0. ,
  10. 10. 8 8 << 8 8 8 • .-0 8 GT • I G.-0 O S • NP • AC1 8 • , .1 • ,.1 ,2 < GLR
  11. 11. ). 1. ( , 1 0 8 .8. . • 0 . , . . I • 0 . 2 1 I R A #include <stdio.h> #include <pthread.h> void* print_doubled(void *x) { int *ix = (int *)x; printf("%d¥n", *ix * 2); } int main() { pthread_t th; int x = 100; int ret = pthread_create( &th, NULL, print_doubled, (void *)&x); if (ret != 0) { printf("thread execution failed"); return 255; } pthread_join(th, NULL); return 0; } $ gcc -lpthread pthread_sample.c $ ./a.out 200
  12. 12. ( ( # 2 : 2 / 70 66 : :2 2: 21 • 7 6 :2/1.0:2/ 2 0 , # • R • 72 :2/1 I 1 • / : :2/1. :2/1./ : • /: .:8 72 I A LR • /: /: .:8 72 R int __pthread_create_2_1 (pthread_t *newthread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg)
  13. 13. ( ( 2 , ) (33 201 8 8 . • 1 ,. 8 , 2 , 0 A • , 1 ,. 1 ,. int __pthread_create_2_1 (pthread_t *newthread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg) { const struct pthread_attr *iattr = (struct pthread_attr *) attr; struct pthread *pd = NULL; int err = ALLOCATE_STACK(iattr, &pd); /* */ pd->start_routine = start_routine; pd->arg = arg; /* */ *newthread = (pthread_t) pd; /* */ int retval = create_thread(pd, iattr, ...); return retval; }
  14. 14. 8 8 / # A 8A8 87# ( • A A78 A C E A A CE 8 8 8 7# 0() • ,.4,021 A A4 8 R HN O IL static int create_thread (struct pthread *pd, const struct pthread_attr *attr, bool *stopped_start, STACK_VARIABLES_PARMS, bool *thread_ran) { /* */ const int clone_flags = (CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SYSVSEM | CLONE_SIGHAND | CLONE_THREAD | CLONE_SETTLS | CLONE_PARENT_SETTID | CLONE_CHILD_CLEARTID | 0); /* */ ARCH_CLONE(&start_thread, STACK_VARIABLES_PARMS, clone_flags, pd, &pd->tid, tp, &pd->tid); /* */ return 0; }
  15. 15. 0 A: 2 . 4# A:8 A A 5# ) • , 8: #4 A 2 5 : (# A 7 A #4 / • 14 15 17 A L I SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp, int __user *, parent_tidptr, int __user *, child_tidptr, unsigned long, tls) { return _do_fork(clone_flags, newsp, 0, parent_tidptr, child_tidptr, tls); }
  16. 16. ) ) ( I GEA H: 0C EA GF E F EH • N LM _ O DC > : FO • TV 60. 1 • RS 1 , 82 1 , -6 1 , -01,6 1 , ,
  17. 17. M I : /D IH H • IIFH D LAC EH D AF IC 0 C:D F: H C:D ED IC • 0 2, 81 • Rx mtedV mted Sop u • 0 2, - • Rx mtedV mtedP S rcdhn v T • 0 2, -/0, • Rx mtedV mted ri_da m hNlr v T • 0 2, 7. , • mted Rx mtedV Sdsgj rNm O
  18. 18. # . / , 1 77 45 2 /* * cloning flags: */ #define CSIGNAL 0x000000ff /* signal mask to be sent at exit */ #define CLONE_VM 0x00000100 /* set if VM shared between processes */ #define CLONE_FS 0x00000200 /* set if fs info shared between processes */ #define CLONE_FILES 0x00000400 /* set if open files shared between processes */ #define CLONE_SIGHAND 0x00000800 /* set if signal handlers and blocked signals shared */ /* */ #define CLONE_NEWPID 0x20000000 /* New pid namespace */ #define CLONE_NEWNET 0x40000000 /* New network namespace */ #define CLONE_IO 0x80000000 /* Clone io context */ • 5 4 5 0 1:8 : /72 7 A 07:0 17 2 / 7 A 15 2 5 • L 0 • I 0 4 /7
  19. 19. 8 , ) ( 8 12 9 8 9 8 . • _ Ied 0 8 R A • Ied R A • I R . 0 8 • ed fcI ag
  20. 20. • • 1 R L • I R • • IA • 8 , 2 , 1 00 2 . 2, ,2 ,
  21. 21. # . A 7 / , 1 75 A 2 • AA 57A 0 1 : A / 2 7 0 0 8 4 8 1 # • R I L long _do_fork(unsigned long clone_flags, unsigned long stack_start, unsigned long stack_size, int __user *parent_tidptr, int __user *child_tidptr, unsigned long tls)
  22. 22. # /5A 85 1 ,:: 8 7A 5 5 54 • 7AA 8A7 2 A 1:4 :8 2: 2 8 : 45 :8 754 7 . • A1 90 A A aI b • _ R L struct task_struct { struct thread_info thread_info; volatile long state; void *stack; struct mm_struct *mm; struct mm_struct *active_mm; pid_t pid; pid_t tgid; /* Filesystem information */ struct fs_struct *fs; /* Open file information */ struct files_struct *files; /* c */ }
  23. 23. 2 , ) ( 201 . • I A I • ,3 , 3 I R A 8 long _do_fork(unsigned long clone_flags, unsigned long stack_start, unsigned long stack_size, int __user *parent_tidptr, int __user *child_tidptr, unsigned long tls) { struct task_struct *p; /* */ p = copy_process( clone_flags, stack_start, stack_size, child_tidptr, NULL, trace, tls, NUMA_NO_NODE); /* */ struct pid *pid = get_task_pid(p, PIDTYPE_PID); long nr = pid_vnr(pid); wake_up_new_task(p); /* */ return nr; }
  24. 24. 0 A: 2 . 4# A:8 A A 5# ( • , 8: #4 A 2 5 : (# A 7 A #4 / ) • 5 1 2 1 A 4 • m Rb hf o I di a kr • 4 1 • p Rh_ m e • nl e g c L static struct task_struct *copy_process( unsigned long clone_flags, unsigned long stack_start, unsigned long stack_size, int __user *child_tidptr, ...) { struct task_struct *p; int retval; /* s */ p = dup_task_struct(current, node); /* s */ retval = copy_fs(clone_flags, p); if (retval) goto bad_fork_cleanup_files; retval = copy_mm(clone_flags, p); if (retval) goto bad_fork_cleanup_signal; /* s */ return p; }
  25. 25. N # 4 I 8 . : I • II I :EC IE 8 L E A E A : / • :E M7CCt mnpus diP U R • : I vx, 5 I8 A7 I :I ah e lbr • e gh ,/21 7 0 oc y V_ diPV_ Ok rfe ,/21 7 0 oc y V_ diP static int copy_mm(unsigned long clone_flags, struct task_struct *tsk) { struct mm_struct *mm, *oldmm; /* */ oldmm = current->mm; /* */ if (clone_flags & CLONE_VM) { mmget(oldmm); mm = oldmm; goto good_mm; } mm = dup_mm(tsk); good_mm: tsk->mm = mm; tsk->active_mm = mm; return 0; /* */ }
  26. 26. 0 22 0. 6 6 , • A 61 6 8 a • _ 2 R I A c • d A
  27. 27. • • 02 8A R • • R • L I • 0 7 2 11 0. 7 ,
  28. 28. • l Sd O l • Sd O ah • = i c I Ie n R ad A • ISd O g 8 . 1. , 22 1 0 . . .
  29. 29. ( ( ) ( I , 28 8 4 0 # / 9: 8 8 87# • : A . 9 : 5# 4 7 5 5 )# 8 8 :87 8# 1 ,( • :3 abed g RL :3 _ c g R static __always_inline struct rq * context_switch(struct rq *rq, struct task_struct *prev, struct task_struct *next, /* h */) { struct mm_struct *mm, *oldmm; mm = next->mm; old_mm = prev->active_mm; /* h */ if (!mm) { /* h */ } else switch_mm_irqs_off(oldmm, mm, next); /* h */ switch_to(prev, next, prev); barrier(); return finish_task_switch(prev); }
  30. 30. ) ( ) # /4 74A0 :2 ,88 7 6 4 4 A43 # • 6 7 6 1 2 A083 87: 18 1 A 0 26 81 2 . • R I L void switch_mm_irqs_off(struct mm_struct * prev, struct mm_struct *next, struct task_struct *tsk) { struct mm_struct *real_prev = this_cpu_read(cpu_tlbstate.loaded_mm); if (real_prev == next) { /* */ return; } else { /* */ } /* */ }
  31. 31. 8 . 1. 3, 22 1 0 . . . • i a d R c • a ge l nA I Sc O h
  32. 32. • a • 02 8 L R • I • R I • R c • • A 30 2 11 30. 3 3 ,
  33. 33. 0 0 ) . ( 23 0 0 0 • go 18 h A I nad . 8 0 h A I R , . v I , 8,18 R • nad x . 8 0fk l_A go s R • nad i ltr s R c a go L • 18 u Ae I L i
  34. 34. /4A 74 0 - 2 ,:: 7 A 4 4 43 • da.7 g l • .7 R bmLjihc • AA 71 2 34 4: 4 9 8 :7 :71 0 : :7 24 0 0 4 4 A 7 34 A : • .7 • AA 797 17A 7 4 2 :7 94 4: 4 • L e kI • AA 3 0A4 0 4 8 0 0
  35. 35. 1. 0 1., 2 1 2 1

×