四川建設網站怎么做網站教程視頻
討論光標共享情況
1.dup和dup2定義變量賦值都共享光標
2.使用兩個描述符調用兩次open函數(shù)打開同一個文件,不共享光標
#include <myhead.h>int main(int argc, const char *argv[])
{//1、描述符賦值給新的變量char buff[1024] = "abcdefg";int newfd;int fd1 = open("./1.txt",O_RDWR);if(fd1==-1){perror("open");return -1;}write(fd1,buff,sizeof(buff));newfd = fd1;//新的變量當作描述符被賦值lseek(newfd,3,SEEK_SET);//從文件開頭移動3個字節(jié)read(fd1,buff,sizeof(buff));printf("%s\n",buff);memset(buff,0,sizeof(buff));//清空buff//2、兩個描述符指向同一個文件char str[100] = "";int fd3,fd4;fd3 = open("./2.txt",O_RDWR);if(fd3==-1){perror("open");return -1;}fd4 = open("./2.txt",O_RDWR);if(fd4==-1){perror("open");return -1;}lseek(fd3,3,SEEK_SET);//fd3從文件開頭移動3個字節(jié)read(fd4,str,sizeof(str));//讀取fd4的光標printf("%s",str);//輸出return 0;
}
stat
#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>int stat(const char *pathname, struct stat *statbuf);功能:獲取文件的詳細信息參數(shù)1:文件路徑和文件名參數(shù)2:獲取到的文件信息存儲地址。struct stat {dev_t st_dev; /* 設備號 */ino_t st_ino; /* Inode號 */mode_t st_mode; /*文件類型和權限 */nlink_t st_nlink; /* 硬鏈接數(shù) */uid_t st_uid; /* 所有者ID */gid_t st_gid; /* 所有者組ID */dev_t st_rdev; /* 特殊文件的設備號 */off_t st_size; /* 最大容量按照字節(jié)存儲 */blksize_t st_blksize; /* 每一塊大小,按照字節(jié) */blkcnt_t st_blocks; /* 塊的數(shù)量 */返回值:成功返回0,失敗返回-1,并置位錯誤碼。
#include <myhead.h>int main(int argc, const char *argv[])
{struct stat sb;int res = stat("./09.c",&sb);//文件的信息存入sb結構體變量if(res==-1){perror("stat");return -1;}switch (sb.st_mode & S_IFMT) {case S_IFBLK: printf("block device\n"); break;case S_IFCHR: printf("character device\n"); break;case S_IFDIR: printf("directory\n"); break;case S_IFIFO: printf("FIFO/pipe\n"); break;case S_IFLNK: printf("symlink\n"); break;case S_IFREG: printf("regular file\n"); break;case S_IFSOCK: printf("socket\n"); break;default: printf("unknown?\n"); break;} return 0;
}
opendir和closedir
#include <sys/types.h>#include <dirent.h>DIR *opendir(const char *name);功能:打開文件目錄,返回指向目錄的指針。參數(shù):目錄路徑和名字返回值:成功返回打開的目錄指針,失敗返回NULL,并置位錯誤碼。int closedir(DIR *dirp);功能:關閉文件目錄指針指向的文件。參數(shù):opendir返回的指針。返回值:成功返回0,失敗返回-1,并置位錯誤碼。
readdir
#include <dirent.h>struct dirent *readdir(DIR *dirp);功能:讀取目錄里面的所有文件或者文件夾信息參數(shù):目錄指針返回值:成功返回目錄指針指向的文件信息結構體,失敗返回NULL,并置位錯誤碼。struct dirent {ino_t d_ino; /* Inode number */off_t d_off; /* Not an offset; see below */unsigned short d_reclen; /* Length of this record */unsigned char d_type; /* Type of file; not supportedby all filesystem types */char d_name[256]; /* Null-terminated filename */};
#include <myhead.h>int main(int argc, const char *argv[])
{DIR *dir = opendir("/home/ubuntu/24101/IO/IO-1/");//打開一個文件夾if(dir==NULL){perror("opendir");return -1;}while(1){struct dirent *hangzhou = readdir(dir);//讀取文件夾的內容if(hangzhou==NULL){perror("readdir");return -1;}printf("文件名:%s\n",hangzhou->d_name);switch(hangzhou->d_type)//輸出文件的類型{case DT_BLK:printf("塊設備文件\n");break;case DT_CHR:printf("字符設備文件\n");break;case DT_DIR:printf("目錄文件\n");break;case DT_FIFO:printf("管道文件\n");break;case DT_REG:printf("普通文件\n");break;}}closedir(dir);//關閉文件夾 return 0;
}
進程(process)
程序的一次執(zhí)行過程就是進程
1、進程概念:
時間片輪詢 上下文切換。
1、進程1在3ms時間內運行結束,那么cpu會將進程1出隊,繼續(xù)詢問進程2.
2、進程1需要8ms才能結束,那么cpu會在第5ms時將進程1,放入就緒隊列尾部
3、進程1正好5ms運行結束cpu將進程1出隊。
4、如果有外部優(yōu)先級更高的事件打斷進程1,進程1也會進入就緒隊列等待。例如IO
2、進程內存管理
1、進程在創(chuàng)建時系統(tǒng)會從1G的物理內存中,通過mmu映射出4G的虛擬內存,其中0--3G的內存是所有進程的空間,進程間空間是獨立的,但是3--4G的空間是內核空間,由所有進程共享。
2、注意在32位的操作系統(tǒng)中,1G物理內存映射出來4G虛擬內存,在64位的操作系統(tǒng)中,1G物理內存映射出來256T虛擬內存。
3、進程之間并不能直接進程數(shù)據(jù)交換,但是可以通過內核空間實現(xiàn)數(shù)據(jù)交換也就是進程間通信。
3、進程和程序區(qū)別
1、進程是動態(tài)的,隨著程序的執(zhí)行而創(chuàng)建,隨著程序的結束而消亡。
2、程序是存儲在內存的二進制文件,進程消亡程序并不會消失。
3、進程是動態(tài),程序是靜態(tài)。
4、進程種類
Linux系統(tǒng)中進程種類可以為三種:
1、交互進程,如vi編輯器,實現(xiàn)用戶和主機之間的交流。
2、批處理進程,如gcc編譯器的一步到位的編譯,批處理進程將所有進程放入就緒隊列,一次性全部運行結束。
3、守護進程,不受用戶控制,不依賴于終端,隨著系統(tǒng)的開啟而開啟,隨著系統(tǒng)的關閉而消亡。
5、進程號
pid(process ID):當前進程的進程號。
ppid(parent process ID):父進程號。
每一個子進程都不會憑空產生,都由父進程創(chuàng)建而生(除了守護進程等伴隨操作系統(tǒng)的特殊進程),
每一個子進程和其父進程都有自己的ID號,也就是進程號。
進入到該文件下:cd /proc/
可以查看操作系統(tǒng)中所有的進程號。
6、Linux下5個特殊進程
1、0號進程,又叫idel進程沒有父進程,系統(tǒng)啟動時創(chuàng)建的,維護系統(tǒng)運行,當系統(tǒng)內沒有任何進程需要執(zhí)行時運行0號進程,系統(tǒng)處于待機狀態(tài)。
2、1號進程,又叫 init進程父進程就是0號進程,也被稱為守護進程,當有子進程處于僵尸狀態(tài)時,1號進程會為子進程收尸。
3、2號進程,又稱為kthreadd進程,是調度進程,完成任務器各項任務的調度工作。
4、僵尸進程:父進程還在運行,但是子進程已經消亡,而父進程并沒有回收子進程的資源,這時子進程就被稱為僵尸進程。
5、孤兒進程:父進程已經消亡,但是子進程還在運行,此時子進程就被稱為孤兒進程。
7、進程的shell指令(ps top kill pidof)
1、查看進程狀態(tài):ps -ajx
PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND0 1 1 1 ? -1 Ss 0 0:03 /sbin/init splash0 2 0 0 ? -1 S 0 0:00 [kthreadd]2 3 0 0 ? -1 I< 0 0:00 [rcu_gp]2 4 0 0 ? -1 I< 0 0:00 [rcu_par_gp]2 5 0 0 ? -1 I 0 0:00 [kworker/0:0-cgr]2 6 0 0 ? -1 I< 0 0:00 [kworker/0:0H-kb]2 8 0 0 ? -1 I< 0 0:00 [mm_percpu_wq]2 9 0 0 ? -1 S 0 0:00 [ksoftirqd/0]2 10 0 0 ? -1 I 0 0:02 [rcu_sched]2 11 0 0 ? -1 S 0 0:00 [migration/0]2 12 0 0 ? -1 S 0 0:00 [idle_inject/0]2 14 0 0 ? -1 S 0 0:00 [cpuhp/0]2 15 0 0 ? -1 S 0 0:00 [kdevtmpfs]2 16 0 0 ? -1 I< 0 0:00 [netns]2 17 0 0 ? -1 S 0 0:00 [rcu_tasks_kthre]
PPID:父進程進程號
PID:當前進程的進程號
PGID:當前進程組的進程號
TTY:如果是?表示不依賴于任何終端而運行。
STAT:進程的狀態(tài)(S:可中斷的休眠態(tài),+:前臺運行狀態(tài))
TIME:進程執(zhí)行的時間
COMMAND:進程的名稱。
2、查看進程的CPU占用率 ps -aux
ubuntu@ubuntu:proc$ ps -aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.3 159804 7232 ? Ss 10:23 0:03 /sbin/init splash root 2 0.0 0.0 0 0 ? S 10:23 0:00 [kthreadd] root 3 0.0 0.0 0 0 ? I< 10:23 0:00 [rcu_gp] root 4 0.0 0.0 0 0 ? I< 10:23 0:00 [rcu_par_gp] root 5 0.0 0.0 0 0 ? I 10:23 0:00 [kworker/0:0-cgr] root 6 0.0 0.0 0 0 ? I< 10:23 0:00 [kworker/0:0H-kb] root 8 0.0 0.0 0 0 ? I< 10:23 0:00 [mm_percpu_wq] root 9 0.0 0.0 0 0 ? S 10:23 0:00 [ksoftirqd/0] root 10 0.0 0.0 0 0 ? I 10:23 0:02 [rcu_sched] root 11 0.0 0.0 0 0 ? S 10:23 0:00 [migration/0] root 12 0.0 0.0 0 0 ? S 10:23 0:00 [idle_inject/0] root 14 0.0 0.0 0 0 ? S 10:23 0:00 [cpuhp/0] root 15 0.0 0.0 0 0 ? S 10:23 0:00 [kdevtmpfs] root 16 0.0 0.0 0 0 ? I< 10:23 0:00 [netns] USER :當前用戶 %CPU:CPU占用率 %MEM:內存占用率 START:創(chuàng)建時間
3、進程之間的樹狀關系 pstree
ubuntu@ubuntu:proc$ pstree
systemd─┬─ModemManager───2*[{ModemManager}]├─NetworkManager─┬─2*[dhclient]│ └─2*[{NetworkManager}]├─VGAuthService├─accounts-daemon───2*[{accounts-daemon}]├─acpid├─avahi-daemon───avahi-daemon├─bluetoothd
4、查看進程間的關系 ps -ef
ubuntu@ubuntu:proc$ ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 10:23 ? 00:00:03 /sbin/init splash
root 2 0 0 10:23 ? 00:00:00 [kthreadd]
root 3 2 0 10:23 ? 00:00:00 [rcu_gp]
root 4 2 0 10:23 ? 00:00:00 [rcu_par_gp]
root 5 2 0 10:23 ? 00:00:00 [kworker/0:0-cgr]
root 6 2 0 10:23 ? 00:00:00 [kworker/0:0H-kb]
root 8 2 0 10:23 ? 00:00:00 [mm_percpu_wq]
root 9 2 0 10:23 ? 00:00:00 [ksoftirqd/0]
root 10 2 0 10:23 ? 00:00:02 [rcu_sched]
root 11 2 0 10:23 ? 00:00:00 [migration/0]
5、動態(tài)查看進程的狀態(tài) top
top - 14:13:35 up 3:50, 1 user, load average: 0.00, 0.01, 0.00
任務: 315 total, 1 running, 248 sleeping, 0 stopped, 0 zombie
%Cpu(s): 2.1 us, 2.1 sy, 0.0 ni, 95.8 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 1987060 total, 130112 free, 1469492 used, 387456 buff/cache
KiB Swap: 969960 total, 874984 free, 94976 used. 331056 avail Mem 進程 USER PR NI VIRT RES SHR � %CPU %MEM TIME+ COMMAND 1649 ubuntu 20 0 600048 102164 28996 S 3.0 5.1 0:47.74 Xorg 1846 ubuntu 20 0 3041844 156168 41260 S 2.0 7.9 0:59.59 gnome-shell 2255 ubuntu 20 0 922236 75776 33960 S 1.7 3.8 0:38.26 x-terminal-emul 10 root 20 0 0 0 0 I 0.3 0.0 0:02.19 rcu_sched 647 avahi 20 0 47408 3244 2836 S 0.3 0.2 0:06.59 avahi-daemon 3455 root 20 0 0 0 0 I 0.3 0.0 0:08.05 kworker/0:2-eve 1 root 20 0 159804 7232 5244 S 0.0 0.4 0:03.49 systemd 2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd 3 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 rcu_gp 4 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 rcu_par_gp 5 root 20 0 0 0 0 I 0.0 0.0 0:00.02 kworker/0:0-cgr 6 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 kworker/0:0H-kb 8 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 mm_percpu_wq 9 root 20 0 0 0 0 S 0.0 0.0 0:00.27 ksoftirqd/0 11 root rt 0 0 0 0 S 0.0 0.0 0:00.09 migration/0 12 root -51 0 0 0 0 S 0.0 0.0 0:00.00 idle_inject/0
8、向進程發(fā)送信號指令 kill
發(fā)送信號:kill -信號號 進程ID
8、進程狀態(tài)和切換
1、進程創(chuàng)建后進入就緒隊列
2、通過CPU的時間片輪詢,處于運行態(tài)
3、如果被外部事件如IO,中斷,會處于阻塞狀態(tài)。
4、解除阻塞狀態(tài)后,會重新進入到就緒隊列。
5、直至運行結束處于消亡態(tài)。
9.進程的創(chuàng)建
fork函數(shù)
#include <sys/types.h>#include <unistd.h>pid_t fork(void);功能:創(chuàng)建子進程,子父進程空間獨立,在父進程中返回值時子進程的ID,在子進程中返回值是0,創(chuàng)建子進程失敗返回-1.參數(shù):無返回值:在父進程中返回值時子進程的ID,在子進程中返回值是0,創(chuàng)建子進程失敗返回-1。
創(chuàng)建子進程
#include <myhead.h>int main(int argc, const char *argv[])
{pid_t pid = fork();//創(chuàng)建子進程if(pid==0)//進入到子進程執(zhí)行流{//子進程執(zhí)行流printf("我是子進程\n");}else if(pid>0)//進入到父進程執(zhí)行流{//進入到父進程執(zhí)行流sleep(1);printf("我是父進程\n");printf("父進程中子進程的ID:%d\n",pid);}else{perror("fork");return -1;}printf("*************\n");//子父進程都會執(zhí)行這行代碼while(1);return 0;
}
寫時拷貝技術
1、寫時拷貝技術,當創(chuàng)建子進程后,子父進程空間在發(fā)生數(shù)據(jù)寫操作時就獨立。
2、子進程中對局部變量的操作不會影響到父進程,父進程中對局部變量的操作不會影響到子進程。
3、進程之間數(shù)據(jù)不共享。
1、討論子父進程是否共享全局變量
fork之后子父進程獨立運行,子進程拷貝了父進程的資源,所以子進程修改變量,不會影響到父進程內的變量。
#include <myhead.h>
int n = 100;
int main(int argc, const char *argv[])
{pid_t pid = fork();//創(chuàng)建子進程if(pid==0)//進入到子進程執(zhí)行流{//子進程執(zhí)行流n = n+1;printf("我是子進程 n= %d\n",n);}else if(pid>0)//進入到父進程執(zhí)行流{//進入到父進程執(zhí)行流sleep(1);printf("我是父進程n = %d\n",n);printf("父進程中子進程的ID:%d\n",pid);}else{perror("fork");return -1;}printf("*************\n");while(1);return 0;
}
局部變量不會共享
#include <myhead.h>
int main(int argc, const char *argv[])
{int n = 100;pid_t pid = fork();//創(chuàng)建子進程if(pid==0)//進入到子進程執(zhí)行流{//子進程執(zhí)行流n = n+1;printf("我是子進程 n= %d\n",n);}else if(pid>0)//進入到父進程執(zhí)行流{//進入到父進程執(zhí)行流sleep(1);printf("我是父進程n = %d\n",n);printf("父進程中子進程的ID:%d\n",pid);}else{perror("fork");return -1;}printf("*************\n");while(1);return 0;
}
作業(yè)
#include <luochen.h>
int main(int argc, const char *argv[])
{int fd1= open("./1.txt",O_RDONLY);int fd2= open("./2.txt",O_CREAT|O_TRUNC|O_WRONLY,0664);int fd3= open("./3.txt",O_RDONLY);int fd4= open("./4.txt",O_CREAT|O_TRUNC|O_WRONLY,0664);pid_t pid = fork();if(pid==0){if(-1==fd1){perror("open");return -1;}char buff[1024];while(1){int res = read(fd1,buff,sizeof(buff));if(res == 0){break;}write(fd2,buff,res);}}if(pid>0){if(-1==fd3){perror("open");return -1;}char buff[1024];while(1){int res = read(fd3,buff,sizeof(buff));if(res == 0){break;}write(fd4,buff,res);}}return 0;
}
#include <luochen.h>
int main(int argc, const char *argv[])
{ int fd =open("./1.txt",O_RDONLY);if(-1==fd){perror("open");return -1;}pid_t pid = fork();int len = lseek(fd,0,SEEK_END);int half = len/2;if(pid>0){//父進程lseek(fd,0,SEEK_SET);int fd1 = open("./5.txt",O_CREAT|O_TRUNC|O_WRONLY);char *buff = malloc(sizeof(char)*half);int res = read(fd,buff,half);write(fd1,buff,res);close(fd1);}if(pid == 0){//子進程lseek (fd,half,SEEK_SET);int fd2 = open("./6.txt",O_CREAT|O_TRUNC|O_WRONLY);char *buff = malloc(sizeof(char)*(len-half));int res = read(fd2,buff,len-half);close(fd2);}close(fd);return 0;
}