(三十七)程序組

2021-07-25 23:13:42 字數 3497 閱讀 3472

程序組就是乙個或多個程序的集合,每個程序組都有唯一的程序組id(整數,也可以存放在pid_t型別中),程序組由程序組id來唯一標識,程序組id是乙個正整數,用來獲得當前程序程序組id的命令:

$ ps -ajx

顯示如下:(pgid列即為程序組列)

ppid

pidpgid

sidtty

tpgid

stat

uidtime

command01

11 ? -1ss0

0:01 /sbin/init

0200 ? -1s0

0:00 [kthreadd]

2300 ? -1s0

0:00 [ksoftirqd/0]

2400 ? -1s0

0:00 [kworker/0:0]

2500 ? -1

s< 00:

00 [kworker/0

:0h]

2700 ? -1s0

0:01 [rcu_sched]

2800 ? -1s0

0:00 [rcu_bh]

2900 ? -1s0

0:00 [migration/0]

2100

0 ? -1s0

0:00 [watchdog/0]

2110

0 ? -1

s< 00:

00 [khelper]

2120

0 ? -1s0

0:00 [kdevtmpfs]

2130

0 ? -1

s< 00:

00 [netns]

2140

0 ? -1

s< 00:

00 [writeback]

2150

0 ? -1

s< 00:

00 [kintegrityd]

2160

0 ? -1

s< 00:

00 [bioset]

2170

0 ? -1

s< 00:

00 [kworker/u17:

0] 2180

0 ? -1

s< 00:

00 [kblockd]

2190

0 ? -1

s< 00:

00 [ata_sff]

2200

0 ? -1s0

0:00 [khubd]

2210

0 ? -1

s< 00:

00 [md]

2220

0 ? -1

s< 00:

00 [devfreq_wq]

2250

0 ? -1s0

0:00 [khungtaskd]

用來獲得當前程序程序組id的函式:

pid_t getpgid(pid_t pid)

pid_t getpgrp(void)

獲得父子程序程序組 例子:

#include 

#include

#include

int main(void)

else

if (pid == 0)

sleep(3);

printf("parent process pid is %d\n",getpid());

printf("group id is %d\n",getpgrp());

return0;}

輸出結果:

child process pid is 3691

group id is 3690

group id is 3690

group id is 3690

parent process pid is 3690

group id is 3690

注:

1)組長程序標識:其程序組id==其程序id

2)組長程序可以建立乙個程序組,建立該程序組中的程序,然後終止,只要程序組中有乙個程序存在,程序組就存在,與組長程序是否終止無關

3)程序組生存期:程序組建立到最後乙個程序離開(終止或轉移到另乙個程序組)

4)乙個程序可以為自己或子程序設定程序組id

以下函式可以加入乙個現有的程序組或建立乙個新程序組:

int setpgid(pid_t pid, pid_t pgid)
注:

1)如要改變子程序為新的組,應在fork後,exec前使用

2)非root程序只能改變自己建立的子程序,或有許可權操作的程序

例子:(改變父子程序為新的組)

#include 

#include

#include

int main(void)

else

if (pid == 0)

sleep(1);

setpgid(pid,pid); // 父程序改變子程序的組id為子程序本身

sleep(5);

printf("parent process pid is %d\n",getpid());

printf("parent of parent process pid is %d\n",getppid());

printf("group id of parent is %d\n",getpgid(0));

setpgid(getpid(),getppid()); // 改變父程序的組id為父程序的父程序

printf("group id of parent is changed to %d\n",getpgid(0));

return0;}

輸出:child process pid is 3714

group id of child is 3713

group id of child is changed to 3714

parent process pid is 3713

parent of parent process pid is 3305

group id of parent is 3713

group id of parent is changed to 3305

題解三十七

給定乙個 沒有重複 數字的序列,返回其所有可能的全排列。示例 思路 回溯法問題用遞迴求解,可以聯絡上樹的遍歷,我們可以將決策路徑畫成一棵樹,回溯的過程就是這棵樹的遍歷過程。回溯問題關於已選集合和候選集合,例如下圖,在進行第一次選擇時,全部的 3 個數都可以選擇,候選集合的大小為 3。在第二次選擇時,...

OVS vswitchd啟動(三十七)

vswitchd是ovs中執行在使用者空間的守護程序,實現ovs主要的功能邏輯,本文將著重分析其啟動過程。在數通領域,交換機和橋很多時候可以是在說乙個東西,它工作在二層,可以新增多個埠,從乙個埠上收到的報文會根據mac表從其他某個埠 出去.在ovs中,它也還是乙個東西,不過ovs用兩個資料結構描述它...

安全駕駛 路障 三十七

路遇障礙物,寧騎不繞。2012 年4 月,我去上海,在日東高速上,有輛本地牌照的a6,時速100左右,一直騎線行駛,我在它後面,總是判斷不准他的行車意圖,後來,找準時機,我把他超了。我推測,他騎線行駛有三種可能 1 瞌睡了,他不光騎線,偶爾還在兩個車道來回切換。2 新手,日東高速上平時很少有車輛通行...