创建文件 |
|
Euser.c |
#include<fcntl.h> #include<stdio.h> #include<stdlib.h> //bzero() #include<string.h> #include<unistd.h> main() { int fd; char buf[1025]; int r; fd = open("test.txt", O_RDWR); if(fd == -1) { printf("open error:%m\n"),exit(-1); } bzero(buf, 1024); while((r = read(fd, buf, 1024)) > 0) { buf[r] = 0; printf("%s\n", buf); } close(fd); } |
切换其他用户 |
没有执行权限 |
当前用户执行设置位 |
切换到其他用户 |
有执行权限 |
Euser.c |
#include<fcntl.h> #include<stdio.h> #include<stdlib.h> //bzero() #include<string.h> #include<unistd.h> main() { int fd; char buf[1025]; int r; fd = open("test.txt", O_RDWR); printf("real user: %d\n", getuid()); printf("effectiv user: %d\n", geteuid()); if(fd == -1) { printf("open error:%m\n"),exit(-1); } bzero(buf, 1024); while((r = read(fd, buf, 1024)) > 0) { buf[r] = 0; printf("%s\n", buf); } close(fd); } |
|
Dir.c |
#include<stdio.h> #include<unistd.h> #include<dirent.h> #include<stdlib.h> main() { DIR *d; struct dirent *de; //打开目录 d = opendir("../day05"); if(d === NULL) { printf("opendir:%m\n"); exit(-1); } //循环读取目录 while(de = readdir(d)) { printf("%s, \t%d"\n", de->d_name, de->d_type); } //关闭目录 closedir(d); } |
执行结果 |
|
三指针例子
遍历指定目录scandir3.c |
#include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<dirent.h>
main() { struct dirent **d; int r; r = scandir("/home", &d, 0, 0); printf("son dir count: %d\n",r); } |
遍历所有目录scandir4.c |
#include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<dirent.h>
main() { struct dirent **d; int r; int I; r = scandir("/home", &d, 0, 0); printf("son dir count: %d\n",r); for(i = 0; i < r; i ++) { printf("%s\n", d[i]->d_name); } } |
#include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<dirent.h>
main() { struct dirent **d; int r; r = scandir("/home", &d, 0, 0); printf("son dir count: %d\n",r); while(*d) { printf("%s\n", (*d)->d_name); d ++; } } |
#include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<dirent.h>
int myfilter(const struct dirent *d) { if(memcmp(d->d_name, ".", 1) == 0) { return 0; } else { return -1; } }
main() { struct dirent **d; int r; int I; r = scandir("/home", &d, myfilter, 0); printf("son dir count: %d\n",r); for(i = 0; i < r; i ++) { printf("%s\n", d[i]->d_name); } } |
#include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<dirent.h>
int myfilter(const struct dirent *d) { if(memcmp(d->d_name, ".", 1) == 0) { return 0; } else { return -1; } }
main() { struct dirent **d; int r; int I; r = scandir("/home", &d, myfilter, alphasort); printf("son dir count: %d\n",r); for(i = 0; i < r; i ++) { printf("%s\n", d[i]->d_name); } } |
Ps -a |
PID进程ID TTY进程所占用的终端设备 STAT进程状态 TIME持续时间 COMMAND所对应的物理程序 |
Top |
Pstree(在unix用ptree) |
|
Kill –l |
Kill –s 9 224//结束进程 |
|
Ulimit -a |
Test.c |
#include<stdio.h> #include<unistd.h>
main() { printf("%d\n", getpid(); sleep(20); } |
System1.c |
#include<stdio.h> #include<unistd.h> #include<stdlib.h>
main() { int r; printf("%d\n",getpid()); r = system("./test"); printf("%d\n", r); } |
Test.c |
#include<stdio.h> #include<unistd.h> #include<stdlib.h>
int main() { int r; printf("%d\n",getpid()); r = system("./test"); printf("%d\n", r);
return 99; } |
#include<stdio.h> #include<unistd.h> #include<stdlib.h>
int main() { int r; printf("%d\n",getpid()); r = system("./test"); printf("%d\n", r>>8&255);
return 99; } |
|
|
Popen1.c |
#include<stdio.h> #include<unistd.h> #include<stdlib.h> #include<sys/wait.h>
int main() { char buf[1024]; FILE *p = popen("ls -l", "r"); int fd = fileno(f); int r;
printf("===============\n"); while((r = read(fd, buf, 1024)) > 0) { buf[r] = 0; printf("%s", buf); } printf("===============\n"); close(fd); pclose(f); } |
|
Test2.c |
#include<stdio.h> #include<unistd.h>
int main() { printf("%d\n", getpid(); sleep(5); return 99 } |
Exec1.c |
#include<stdio.h> #include<unistd.h>
int main() { int r = execl("test2", "mytest2", NULL); printf("end %d\n",r); return 0; } |
问题:为什么没有打印printf("end %d\n",r); ? |
答案:int r = execl("test2", "mytest2", NULL);代码替换 |
问题:有没有创建新的进程呢? |
#include<stdio.h> #include<unistd.h>
int main() { printf("main:%d\n, getpid()); int r = execl("test2", "mytest2", NULL); printf("end %d\n",r); return 0; } |
答案:进程替换,不创建进程。 |
#include<stdio.h> #include<unistd.h>
int main() { int r = execlp("ls", "ls", NULL); printf("end %d\n",r); return 0; } |
#include<stdio.h> #include<unistd.h>
int main() { int r = execlp("ls", "ls", NULL); printf("end %d\n",r); return 0; } |
#include<stdio.h> #include<unistd.h>
int main() { int r = execlp("bin/ls", "-l", NULL); printf("end %d\n",r); return 0; } |
execl只认识当前路径,execlp使用系统的搜索路径-which能找到的路径 |
#include<stdio.h> #include<unistd.h>
int main() { int r = execl("bin/ls", "ls", "-l", NULL); printf("end %d\n",r); return 0; } |
#include<stdio.h> #include<unistd.h>
int main() { int r = execlp("ls", "ls", "-l", NULL); printf("end %d\n",r); return 0; } |
int r = execl("bin/ls", "-l", NULL); int r = execlp("ls", "ls", "-l", NULL); 第一参数:程序名称 第二参数:命令名称 第三参数:选项 |
Fork1.c |
#include<stdio.h> #include<unistd.h>
int main() { printf("creat befor!\n"); int pid = fork(); printf("create after: %d\n", pid);
return 0; } |
问题:为什么会输出3行? |
#include<stdio.h> #include<unistd.h>
int main() { printf("creat befor!\n"); int pid = fork(); while(1) { printf("create after: %d\n", pid); } return 0; } |
父子进程同时执行 |
逻辑上区分父子进程。 |
#include<stdio.h> #include<unistd.h>
int main() { printf("creat befor!\n"); int pid = fork(); if(pid == 0) { while(1) { printf("son \n"); sleep(1); } } else { while(1) { printf("father\n"); sleep(1); } } return 0; } |
单个CPU多任务
Demo1.c |
#include<curses.h> #include<unistd.h> #include<time.h> #include<stdlib.h> #include<stdio.h> #include<string.h> #include<math.h>
WINDOW *wtime, *wnumb; main() { initscr(); wtime = derwin(stdscr, 3, 10, 0, (COLS-10)); wnumb = derwin(stdscr, 3, 11, (LINES-3)/2, (COLS - 11)/2); box(wtime, 0, 0); box(wnumb, 0, 0); refresh(); wrefresh(wtime); wrefresh(wnumb);
if(fork()) { time_t tt; struct tm *t; //parent --showtime while(1) { tt = time(0); //time(0) === time(&tt) t = localtime(&tt); mvprintw(wtime, 1, 1, "%02d:%02d:%02d", t->tm_hour, t->tm_min, t->tm_sec); //格式输出 refresh(); wrefresh(wtime); wrefresh(wnumb); sleep(1); } } else { //child --show number int num = 0; int i; while(1) {
//num = rand()%1000000; num = 0; for(i = 0; i < 7; i ++) { num += num*10 + rand()%10; } mvwprintw(wnumb, 1, 2, "%07d", num); refresh(); wrefresh(wtime); wrefresh(wnumb); usleep(10000); //10^(-6)s } } endwin(); //释放窗体 } |
|
Parentchild1.c |
#include<stdio.h> #include<unistd.h>
main() { if(fork()) { //parent while(1) { printf("parent\n"); } } else { //child while(1) { printf("child\n"); } }
} |
Ps -a |
可见这两个进程共享终端。 |
用pstree查看父子进程时 |
Parent1.c |
#include<stdio.h> #include<unistd.h>
main() { if(fork()) { //parent printf("parent\n"); sleep(20); } else { //child printf("child\n"); sleep(200); }
} |
Child1.c |
#include<stdio.h> #include<unistd.h>
main() { if(fork() == 0) { printf("child\n"); sleep(20); } else { printf("parent\n"); sleep(200); }
} |
|
Child2.c |
#include<stdio.h> #include<unistd.h> #include<stdlib.h> #include<sys/wait.h> main() { if(fork() == 0) { printf("child\n"); sleep(20); exit(88); } else { int status; printf("parent\n"); wait(&status); printf("wait over: %d\n", WEXITSTATUS(status)); sleep(200); }
} |
Child3.c |
#include<stdio.h> #include<unistd.h> #include<stdlib.h> #include<sys/wait.h> #include<signal.h>
void deal(int s) { int status; wait(&status); printf("wait over: %d\n", WEXITSTATUS(status)); }
main() { if(fork() == 0) { printf("child\n"); sleep(10); exit(88); } else { signal(17, deal); while(1) { printf("parent\n"); sleep(200); } }
} |
从内存结构上看
父进程 |
子进程 |
VM |
|
全局栈 |
|
局部 |
|
堆 |
|
Fd文件描述符 |
|
|
|
|
|
Promem1.c |
#include<stdio.h> #include<unistd.h> #include<stdlib.h> #include<sys/wait.h> #include<signal.h>
main() { int a = 20;; if(fork()) { printf("parent: %d\n", a); } else { printf("child: %d\n", a); }
} |
#include<stdio.h> #include<unistd.h> #include<stdlib.h> #include<sys/wait.h> #include<signal.h>
main() { int a = 20;; if(fork()) { printf("parent: %d\n", a); a = 90; } else { printf("child: %d\n", a); sleep(3); printf("child: %d\n", a); }
} |
#include<stdio.h> #include<unistd.h> #include<stdlib.h> #include<sys/wait.h> #include<signal.h>
main() { int *a = malloc(4); //堆 *a = 40; if(fork()) { printf("parent: %d\n", *a); *a = 90; } else { printf("child: %d\n", *a); sleep(3); printf("child: %d\n", *a); }
} |
#include<stdio.h> #include<unistd.h> #include<stdlib.h> #include<sys/wait.h> #include<signal.h> #include<sys/mman.h> //mmap() main() { int *a = mmap(0, 4, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_SHARED, 0,0); *a = 40; if(fork()) { printf("parent: %d\n", *a); *a = 90; } else { printf("child: %d\n", *a); sleep(3); printf("child: %d\n", *a); }
} |
#include<stdio.h> #include<unistd.h> #include<stdlib.h> #include<sys/wait.h> #include<signal.h> #include<sys/mman.h> //mmap() main() { int *a = mmap(0, 4, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, 0,0); *a = 40; if(fork()) { printf("parent: %d\n", *a); *a = 90; } else { printf("child: %d\n", *a); sleep(3); printf("child: %d\n", *a); }
} |
#include<stdio.h> #include<unistd.h> #include<stdlib.h> #include<sys/wait.h> #include<signal.h> #include<sys/mman.h> //mmap() main() { int *a = sbrk(4); *a = 40; if(fork()) { printf("parent: %d\n", *a); *a = 90; } else { printf("child: %d\n", *a); sleep(3); printf("child: %d\n", *a); }
} |
Profile1.c |
#include<stdio.h> #include<unistd.h> #include<stdlib.h> #include<sys/wait.h> #include<signal.h> #include<sys/mman.h> //mmap() #include<fcntl.h> //open() main() { int fd = open("test.txt", O_RDWR); if(fork()) { char buf[1024] = {0}; read(fd, buf, 1024); printf("parent: %s\n", buf); close(fd); } else { char buf[1024] = {0}; read(fd, buf, 1024); printf("child: %s\n", buf); close(fd);
} //close(fd); } |
#include<stdio.h> #include<unistd.h> #include<stdlib.h> #include<sys/wait.h> #include<signal.h> #include<sys/mman.h> //mmap() #include<fcntl.h> //open() main() { int fd = open("test.txt", O_RDWR); if(fork()) { char buf[1024] = {0}; lseek(fd, 0, SEEK_SET); read(fd, buf, 1024); printf("parent: %s\n", buf); while(1); close(fd); } else { char buf[1024] = {0}; read(fd, buf, 1024); printf("child: %s\n", buf); while(1); close(fd);
} //close(fd); } |
可见这两个进程所用文件相同。 |
#include<stdio.h> #include<unistd.h> #include<stdlib.h> #include<sys/wait.h> #include<signal.h> #include<sys/mman.h> //mmap() #include<fcntl.h> //open() main() { int fd = open("test.txt", O_RDWR); if(fork()) { write(fd, "hello" 5); close(fd); } else { write(fd, "word", 5); close(fd); } } |
#include<stdio.h> #include<unistd.h> #include<stdlib.h> #include<sys/wait.h> #include<signal.h> #include<sys/mman.h> //mmap() #include<fcntl.h> //open() main() {
if(fork()) { int fd = open("test.txt", O_RDWR); printf("%d\n", fd); sleep(5); write(fd, "Killer" 6); close(fd); } else { int fd = open("test.txt", O_RDWR); printf("%d\n", fd); write(fd, "ClintonWWW", 10); sleep(8); close(fd); } } |