質問するログイン新規登録

質問編集履歴

11

重複削除

2025/07/22 15:40

投稿

nryk
nryk

スコア2

title CHANGED
File without changes
body CHANGED
@@ -142,49 +142,7 @@
142
142
  }
143
143
  ```
144
144
 
145
- ### 追記(ppidが指すプログラムを取得)
146
-
147
- ```C
148
- #include <stdio.h>
149
- #include <unistd.h>
150
- #include <stdlib.h>
151
- #include <libproc2/pids.h>
152
- #include <string.h>
153
-
154
- int main(void)
155
- {
156
- struct pids_info *info1 = NULL;
157
- struct pids_info *info2 = NULL;
158
- enum pids_item item1[] = {PIDS_ID_PPID};
159
- enum pids_item item2[] = {PIDS_CMD};
160
- procps_pids_new(&info1, item1, 1);
161
- procps_pids_new(&info2, item2, 1);
162
-
163
- pid_t ppid = getppid();
164
- struct pids_fetch *pidread1 = NULL;
165
- struct pids_fetch *pidread2 = NULL;
166
- pidread1 = procps_pids_select(info1, (unsigned int *)&ppid, 1, PIDS_SELECT_PID);
167
- pidread2 = procps_pids_select(info2, (unsigned int *)&ppid, 1, PIDS_SELECT_PID);
168
- printf("PID: <%d>\n", getpid());
169
- printf("PPID: <%d>\n", getppid());
170
- printf("PPPID: <%d>\n", pidread1->stacks[0]->head->result.u_int);
171
- printf("PPPCMD: <%s>\n", pidread2->stacks[0]->head->result.str);
172
- fflush(stdout);
173
-
174
- return 0;
175
- }
176
- ```
177
-
178
- ```shell
179
- $ sudo taskset -c 0 nice -n 19 ./a.out
180
-
181
- PID: <11972>
182
- PPID: <11971>
183
- PPPID: <11970>
184
- PPPCMD: <sudo>
185
- ```
186
-
187
- ### 追記 double cloneの詳細
145
+ ### 追記 double cloneの詳細
188
146
  ご参考までに、手元でdouble cloneが発生したソースコードとコンソール出力を貼り付けておきます。環境については「補足①バージョン情報」に掲載しています。
189
147
 
190
148
  ```C

10

追記② double cloneの詳細

2025/07/22 02:35

投稿

nryk
nryk

スコア2

title CHANGED
File without changes
body CHANGED
@@ -182,4 +182,58 @@
182
182
  PPID: <11971>
183
183
  PPPID: <11970>
184
184
  PPPCMD: <sudo>
185
- ```
185
+ ```
186
+
187
+ ### 追記② double cloneの詳細
188
+ ご参考までに、手元でdouble cloneが発生したソースコードとコンソール出力を貼り付けておきます。環境については「補足①バージョン情報」に掲載しています。
189
+
190
+ ```C
191
+ //check_pid.c
192
+ #include <stdio.h>
193
+ #include <unistd.h>
194
+ #include <stdlib.h>
195
+ #include <libproc2/pids.h>
196
+ #include <string.h>
197
+
198
+ int main(void)
199
+ {
200
+ struct pids_info *info = NULL;
201
+ enum pids_item item[] = {PIDS_ID_PPID, PIDS_CMD};
202
+ procps_pids_new(&info, item, 2);
203
+
204
+ pid_t ppid = getppid();
205
+ struct pids_fetch *pidread = NULL;
206
+ pidread = procps_pids_select(info, (unsigned int *)&ppid, 1, PIDS_SELECT_PID);
207
+ printf("PID: <%d>\n", getpid());
208
+ printf("PPID: <%d>\n", getppid());
209
+ printf("PPPID: <%d>\n", pidread->stacks[0]->head[0].result.u_int);
210
+ printf("PPPCMD: <%s>\n", pidread->stacks[0]->head[1].result.str);
211
+ fflush(stdout);
212
+ system("ps afT");
213
+ fflush(stdout);
214
+
215
+ return 0;
216
+ }
217
+ ```
218
+
219
+ ```shell
220
+ $ gcc -O0 -Wall -Wextra check_pid.c -lproc2
221
+ $ sudo ./a.out &
222
+ [1] 3952
223
+ $
224
+ PID: <3955>
225
+ PPID: <3954>
226
+ PPPID: <3952>
227
+ PPPCMD: <sudo>
228
+ PID TTY STAT TIME COMMAND
229
+ 1715 pts/3 Ss+ 0:00 /bin/bash --init-file /home/user/.vscode-server/cli/servers/Stable-xxxxxxxxxxxxxxxxxxxxxxxx
230
+ 3952 pts/3 S 0:00 \_ sudo ./a.out
231
+ 3954 pts/4 Ss+ 0:00 \_ sudo ./a.out
232
+ 3955 pts/4 S 0:00 \_ ./a.out
233
+ 3956 pts/4 S 0:00 \_ sh -c -- ps afT
234
+ 3957 pts/4 R 0:00 \_ ps afT
235
+ 974 tty1 Ss+ 0:00 /sbin/agetty -o -p -- \u --noclear - linux
236
+
237
+ [1]+ Done sudo ./a.out
238
+ $
239
+ ```

9

コード修正

2025/07/21 14:01

投稿

nryk
nryk

スコア2

title CHANGED
File without changes
body CHANGED
@@ -135,14 +135,8 @@
135
135
 
136
136
  double real_time = (real_end.tv_sec - real_start.tv_sec) + (real_end.tv_usec - real_start.tv_usec) / 1000000.0;
137
137
 
138
- if (strcmp(pppcmd, "sudo") == 0)
138
+ pid_t disp = (strcmp(pppcmd, "sudo") == 0) ? pppid : getpid();
139
- {
140
- printf("\n<%d>Done. CPU time: %.2f seconds Real time: %.2f sec", pppid, elapsed, real_time);
139
+ printf("\n<%d>Done. CPU time: %.2f seconds Real time: %.2f sec", disp, elapsed, real_time);
141
- }
142
- else
143
- {
144
- printf("\n<%d>Done. CPU time: %.2f seconds Real time: %.2f sec", getpid(), elapsed, real_time);
145
- }
146
140
  fflush(stdout);
147
141
  return 0;
148
142
  }

8

コードを更新

2025/07/21 13:28

投稿

nryk
nryk

スコア2

title CHANGED
File without changes
body CHANGED
@@ -91,6 +91,7 @@
91
91
  ```
92
92
 
93
93
  ### 補足②
94
+ **教えていただいた内容を盛り込んで更新しました**
94
95
  元々はプロセスの優先順位と実行時間の関係を観察しようとしていました。質問内容には直接関係ありませんが、一応元のコードも貼り付けておきます。
95
96
 
96
97
  ```C
@@ -98,9 +99,21 @@
98
99
  #include <unistd.h>
99
100
  #include <time.h>
100
101
  #include <sys/time.h>
102
+ #include <libproc2/pids.h>
103
+ #include <string.h>
101
104
 
102
105
  int main(void)
103
106
  {
107
+ struct pids_info *info = NULL;
108
+ enum pids_item item[] = {PIDS_ID_PPID, PIDS_CMD};
109
+ procps_pids_new(&info, item, 2); // 第3引数はitem[]の要素数
110
+
111
+ pid_t ppid = getppid();
112
+ struct pids_fetch *pidread = procps_pids_select(info, (unsigned int *)&ppid, 1, PIDS_SELECT_PID);
113
+
114
+ pid_t pppid = pidread->stacks[0]->head[0].result.u_int;
115
+ char *pppcmd = pidread->stacks[0]->head[1].result.str;
116
+
104
117
  clock_t start = clock();
105
118
 
106
119
  // 実時間の開始(リアルタイム時計)
@@ -122,7 +135,14 @@
122
135
 
123
136
  double real_time = (real_end.tv_sec - real_start.tv_sec) + (real_end.tv_usec - real_start.tv_usec) / 1000000.0;
124
137
 
138
+ if (strcmp(pppcmd, "sudo") == 0)
139
+ {
140
+ printf("\n<%d>Done. CPU time: %.2f seconds Real time: %.2f sec", pppid, elapsed, real_time);
141
+ }
142
+ else
143
+ {
125
- printf("\n<%d>Done. CPU time: %.2f seconds Real time: %.2f sec", getpid(), elapsed, real_time);
144
+ printf("\n<%d>Done. CPU time: %.2f seconds Real time: %.2f sec", getpid(), elapsed, real_time);
145
+ }
126
146
  fflush(stdout);
127
147
  return 0;
128
148
  }

7

自分で試した結果を追記

2025/07/21 10:54

投稿

nryk
nryk

スコア2

title CHANGED
File without changes
body CHANGED
@@ -126,4 +126,46 @@
126
126
  fflush(stdout);
127
127
  return 0;
128
128
  }
129
+ ```
130
+
131
+ ### 追記(ppidが指すプログラムを取得)
132
+
133
+ ```C
134
+ #include <stdio.h>
135
+ #include <unistd.h>
136
+ #include <stdlib.h>
137
+ #include <libproc2/pids.h>
138
+ #include <string.h>
139
+
140
+ int main(void)
141
+ {
142
+ struct pids_info *info1 = NULL;
143
+ struct pids_info *info2 = NULL;
144
+ enum pids_item item1[] = {PIDS_ID_PPID};
145
+ enum pids_item item2[] = {PIDS_CMD};
146
+ procps_pids_new(&info1, item1, 1);
147
+ procps_pids_new(&info2, item2, 1);
148
+
149
+ pid_t ppid = getppid();
150
+ struct pids_fetch *pidread1 = NULL;
151
+ struct pids_fetch *pidread2 = NULL;
152
+ pidread1 = procps_pids_select(info1, (unsigned int *)&ppid, 1, PIDS_SELECT_PID);
153
+ pidread2 = procps_pids_select(info2, (unsigned int *)&ppid, 1, PIDS_SELECT_PID);
154
+ printf("PID: <%d>\n", getpid());
155
+ printf("PPID: <%d>\n", getppid());
156
+ printf("PPPID: <%d>\n", pidread1->stacks[0]->head->result.u_int);
157
+ printf("PPPCMD: <%s>\n", pidread2->stacks[0]->head->result.str);
158
+ fflush(stdout);
159
+
160
+ return 0;
161
+ }
162
+ ```
163
+
164
+ ```shell
165
+ $ sudo taskset -c 0 nice -n 19 ./a.out
166
+
167
+ PID: <11972>
168
+ PPID: <11971>
169
+ PPPID: <11970>
170
+ PPPCMD: <sudo>
129
171
  ```

6

表現の修正

2025/07/21 01:35

投稿

nryk
nryk

スコア2

title CHANGED
File without changes
body CHANGED
@@ -39,8 +39,8 @@
39
39
  ### ~~質問①そもそも上記の事象が起こる原因~~
40
40
  **melian様からのコメントで解決済み**
41
41
  基本的なことは勉強した結果、下記のように考えました。あっているでしょうか?
42
- * `taskset`および`nice`は内部で`exec`しているためPIDが同じになる
42
+ * `taskset`および`nice`は内部でclone,forkせずに`exec`しているためPIDが同じになる
43
- * `sudo`は内部で~~fork~~ `clone`しているのでPIDが異なる
43
+ * `sudo`は内部で~~fork~~ cloneしてから`exec`しているのでPIDが異なる
44
44
 
45
45
  ### 質問② PIDを一致させる方法
46
46
  上記のshell出力において、`[8] 66597`は`sudo`のpid、`PID: <66600>`は`a.out`のpidという認識です。これを一致させるにはどうしたらいいでしょうか?(質問タイトルには両方sudoのPIDにしたいと書いていますが、一致すればどちらでもいいです)

5

タイトル修正

2025/07/20 17:22

投稿

nryk
nryk

スコア2

title CHANGED
@@ -1,1 +1,1 @@
1
- Linux sudoでプログラムを実行した時はsudoのpidを取得したい
1
+ Linux sudoで実行したプログラムからsudoのpidを取得したい
body CHANGED
File without changes

4

解決済みの質問をクローズ

2025/07/20 17:11

投稿

nryk
nryk

スコア2

title CHANGED
File without changes
body CHANGED
@@ -36,10 +36,11 @@
36
36
  `sudo`で実行しない場合、`[7] 66596`, `PID: <66596>`のように2つのPIDが一致しています。
37
37
  しかし`sudo`で実行した方のプロセスは`[8] 66597`, `PID: <66600>`のように異なるPIDが表示されています。これについて何点か質問したいです。
38
38
 
39
- ### 質問①そもそも上記の事象が起こる原因
39
+ ### ~~質問①そもそも上記の事象が起こる原因~~
40
+ **melian様からのコメントで解決済み**
40
41
  基本的なことは勉強した結果、下記のように考えました。あっているでしょうか?
41
42
  * `taskset`および`nice`は内部で`exec`しているためPIDが同じになる
42
- * `sudo`は内部で`fork`しているのでPIDが異なる
43
+ * `sudo`は内部で~~fork~~ `clone`しているのでPIDが異なる
43
44
 
44
45
  ### 質問② PIDを一致させる方法
45
46
  上記のshell出力において、`[8] 66597`は`sudo`のpid、`PID: <66600>`は`a.out`のpidという認識です。これを一致させるにはどうしたらいいでしょうか?(質問タイトルには両方sudoのPIDにしたいと書いていますが、一致すればどちらでもいいです)

3

バージョン情報を追加

2025/07/20 16:43

投稿

nryk
nryk

スコア2

title CHANGED
File without changes
body CHANGED
@@ -67,7 +67,29 @@
67
67
  `[10]`(sudo無し)の方については、bashのPID`1799`が出力されており、想定通りの動きです。
68
68
  しかし`[11]`(sudoあり)の方は余計にわからなくなってしまいました。`[11] 75811`と`PID: <75813>`という異なるPIDが表示されていますが、これはそれぞれ何のPIDなのでしょうか。
69
69
 
70
+ ### 補足①バージョン情報
71
+
72
+ ```shell
73
+ $ lsb_release -a
74
+ No LSB modules are available.
75
+ Distributor ID: Ubuntu
76
+ Description: Ubuntu 24.04.2 LTS
77
+ Release: 24.04
78
+ Codename: noble
79
+
80
+ $ gcc --version
81
+ gcc (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0
82
+ Copyright (C) 2023 Free Software Foundation, Inc.
83
+ This is free software; see the source for copying conditions. There is NO
84
+ warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
85
+
86
+ $ dpkg -l | grep libc6
87
+ ii libc6:amd64 2.39-0ubuntu8.4 amd64 GNU C Library: Shared libraries
88
+ ii libc6-dbg:amd64 2.39-0ubuntu8.4 amd64 GNU C Library: detached debugging symbols
89
+ ii libc6-dev:amd64 2.39-0ubuntu8.4 amd64 GNU C Library: Development Libraries and Header Files
90
+ ```
91
+
70
- ### 補足
92
+ ### 補足
71
93
  元々はプロセスの優先順位と実行時間の関係を観察しようとしていました。質問内容には直接関係ありませんが、一応元のコードも貼り付けておきます。
72
94
 
73
95
  ```C

2

タイトル修正

2025/07/19 15:04

投稿

nryk
nryk

スコア2

title CHANGED
@@ -1,1 +1,1 @@
1
- Linux sudoで実行したプログラムのpidを取得したい
1
+ Linux sudoでプログラムを実行した時はsudoのpidを取得したい
body CHANGED
@@ -1,5 +1,5 @@
1
1
  ### 実現したいこと
2
- Linux初学者です。プログラムをsudo権限でバックグラウンド実行した時に表示されるPID、プログラム内で`getpid()`するPIDと一致させたいです。
2
+ Linux初学者です。プログラムをsudo権限でバックグラウンド実行した時に表示されるPID、プログラム内で`getpid()`するPIDと一致させたいです。
3
3
 
4
4
  上記「プログラム」は下記をコンパイルしたものです。
5
5
 
@@ -42,10 +42,8 @@
42
42
  * `sudo`は内部で`fork`しているのでPIDが異なる
43
43
 
44
44
  ### 質問② PIDを一致させる方法
45
- 上記のshell出力において、`[8] 66597`は`sudo`のpid、`PID: <66600>`は`a.out`のpidという認識です。どちらでもいいので、これを一致させるにはどうしたらいいでしょうか?
45
+ 上記のshell出力において、`[8] 66597`は`sudo`のpid、`PID: <66600>`は`a.out`のpidという認識です。これを一致させるにはどうしたらいいでしょうか?(質問タイトルには両方sudoのPIDにしたいと書いていますが、一致すればどちらでもいいです)
46
46
 
47
-
48
-
49
47
  ### 試したこと・調べたこと
50
48
  - [x] teratailやGoogle等で検索した
51
49
  - [x] ソースコードを自分なりに変更した

1

誤字の修正

2025/07/19 14:34

投稿

nryk
nryk

スコア2

title CHANGED
File without changes
body CHANGED
@@ -46,20 +46,14 @@
46
46
 
47
47
 
48
48
 
49
- ### 該当のソースコード
50
-
51
- ```
52
- 特になし
53
- ```
54
-
55
49
  ### 試したこと・調べたこと
56
50
  - [x] teratailやGoogle等で検索した
57
51
  - [x] ソースコードを自分なりに変更した
58
52
  - [ ] 知人に聞いた
59
- - [x] その他
53
+ - [ ] その他
60
54
 
61
55
  ##### 上記の詳細・結果
62
- 一旦sudo無しの方は無視して、sudoありの方だけPIDを一致させようと考えました
56
+ 一旦sudo無しの方は無視して、sudoありの方だけPIDを一致させようと考えました
63
57
  `a.out`の親プロセスのpidを見れば`sudo`のpidがわかるかと思い、`cpu_hog.c`の`getpid`を`getppid`に変更してみました。その結果がこれです。
64
58
 
65
59
  ```
@@ -72,8 +66,8 @@
72
66
  [10] Done taskset -c 0 nice -n 19 ./a.out
73
67
  [11] Done sudo taskset -c 0 nice -n -10 ./a.out
74
68
  ```
75
- [10](sudo無し)の方については、bashのPID`1799`が出力されており、想定通りの動きです。
69
+ `[10]`(sudo無し)の方については、bashのPID`1799`が出力されており、想定通りの動きです。
76
- しかし[11](sudoあり)の方は余計にわからなくなってしまいました。`[11] 75811`と`PID: <75813>`という異なるPIDが表示されていますが、これはそれぞれ何のPIDなのでしょうか。
70
+ しかし`[11]`(sudoあり)の方は余計にわからなくなってしまいました。`[11] 75811`と`PID: <75813>`という異なるPIDが表示されていますが、これはそれぞれ何のPIDなのでしょうか。
77
71
 
78
72
  ### 補足
79
73
  元々はプロセスの優先順位と実行時間の関係を観察しようとしていました。質問内容には直接関係ありませんが、一応元のコードも貼り付けておきます。