linux系統程式設計筆記 程序

2021-10-19 08:56:29 字數 3125 閱讀 2069

使用者許可權以及組許可權

system()

程序會計

程序時間

守護程序

系統日誌

型別 pid_t

#include

#include

#include

#include

intmain()

else

getchar()

;printf

("pid %d end\n"

,getpid()

);return0;

}

#include

#include

#include

#include

#define left 2

#define right 200

intmain()

}if(mark)

exit(0

);}}

getchar()

;exit(0

);}

異常終止

#include

#include

#include

#include

#include

#define n 3

#define left 100000002

#define right 100000200

//交叉演算法計算 池類演算法涉及到競爭

intmain()

exit(1

);}if

(pid ==0)

}if(mark)

}printf

("[%d] exit\n"

,n);

exit(0

);}}

int st,n;

for(n =

0;n < n;n++

)exit(0

);}

exec 替換 當前程序映像

乙個小shell

//lhq yyds

#include

#include

#include

#include

#include

#include

#include

#define bufsize 1024

#define delims " \t\n"

extern

char

**environ;

static

intcd

(char

*path)

return ret;

}static

void

prompt()

static

intparse

(char

*linebuf,glob_t *globres)

elseif(

strcmp

(tok,

"cd")==

0)else

if(tok[0]

=='\0'

)glob

null

,globres)

; flag =1;

}return1;

}//之後記得 將 ctrl+c 轉為 stdout:\n 將ctrl+d 轉為 退出+再見標語

intmain()

else

if(ret ==0)

else

if(ret ==1)

else

if(pid ==0)

}waitpid

(pid,

null,0

);}exit(0

);}

函式

// mysudo

#include

#include

#include

#include

#include

intmain

(int argc,

char

**ar**)

pid_t pid;

pid =

fork()

;if(pid ==0)

else

exit(0

);}

$ su

# chown root ./mysudo

# chmod u+s ./mysudo

$ ./mysudo 0 /etc/shadow

可以理解成 fork+exec+wait 封裝

#include

#include

#include

#include

#include

#include

#include

#include

#define fname "/tmp/out"

static

intdeamonize()

if(pid >0)

fd =

open

("/dev/null"

,o_rdwr)

;//輸出都忽略

if(fd <0)

if(pid ==0)

setsid()

;//脫離終端

//umask();

chdir

("/");

}return0;

}int

main()

else

fp =

fopen

(fname,

"w+");

if(fp ==

null

)syslog

(log_info,

"%s opened"

,fname)

;for

(int i =0;

;i++

)closelog()

;fclose

(fp)

;exit(0

);}

journalctl _pid=***

Linux系統程式設計之程序

程序狀態 4 5種 就緒 初始化 執行,掛起 等待 停止 pbc成員 1 程序id 2 程序狀態 3 程序切換需要儲存和恢復的cpu暫存器 4 描述虛擬位址空間資訊 5 描述控制終端的資訊 6 當前工作目錄 7 umask掩碼 8 檔案描述符 9 訊號相關的資訊 10 使用者和使用者組id 11 會...

linux系統程式設計 多程序

最近發現每天都在寫業務 好多基礎的東西都忘了,今天開始每天覆習一點基礎知識。今天先複習多程序 程序是具有一定功能的程式的一次執行活動,是系統資源分配和排程的基本單位 孤兒程序 父程序退出,子程序沒有退出,被init程序收養 init程序 是核心啟動後執行的第乙個程序,是所有孤兒程序的子程序。init...

《Linux系統程式設計 程序管理》

在linux的核心的五大組成模組中,程序管理模組時非常重要的一部分,它雖然不像記憶體管理 虛擬檔案系統等模組那樣複雜,也不像程序間通訊模組那樣條理化,但作為五大核心模組之一,程序管理對我們理解核心的運作 對於我們以後的程式設計非常重要。同時,作為五大組成模組中的核心模組,它與其他四個模組都有聯絡。下...