Linux 進程管理與開發0

來源:互聯網
上載者:User

1.進程的基本屬性

        和進程基本相關的屬性有:進程號(PID)、父進程號(PPID)、進程組號(PGID)系統的第一個進程是init進程,他的pid=1

2.擷取進程屬性的函數

        extern _pid_t getpid(void);擷取本身進程的PID函數 extern _pid_t getppid(void);擷取父進程的PID函數 extern _pid_t getpgid(_pid_t _pid);擷取本進程的進程組的PID函數,此參數為0則為本進程的進程組的PID如果為其他的值,則傳回值就是其他進程組的PID extern _pid_t getpgrp(void);擷取當前進程的進程組號

3、簡單程式測試:

code:

#include<stdio.h>
#include<unistd.h>
int main(int argc,char *argv[])
{
    int i;
    printf("\tpid\t ppid \t pgid\n");
    printf("parent\t%d\t%d\t%d\n",getpid(),getppid(),getpgid(0));
    for(i=0;i<2;i++)
        if(fork()==0)
            printf("child\t%d\t%d\t%d\n",getpid(),getppid(),getpgid(0));
    return 0;
}

gcc –o getpid main.c

./getpid

 

在for迴圈裡面開闢了3個進程迴圈了2次其中2999為整個進程的進程組的pid 2921為父進程就是上一級進程的pid

查詢ps –aux 為bash也就是shell本身的進程

4.加入一個現有的組或者是一個新的進程組成的系統調用函數setpgid()

   int setpgid(pid_t pid ,pid__t pgid);

    其中第一個參數是為你想修改進程PGID的進程PID,第二個為新的進程組號,如果這兩個參數是相等的話。則由Pid指定的進程為進程組長;如果是pid為0,則修改當前進程的pgid,如果是pgid=0,則有指定的進程pid將用於進程組pgid

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.