ps// 仅显示当前终端会话中运行的进程的快照
ps aux// 仅显示当前终端会话中运行的进程的快照
ps aux|grep xxx//显示包含特定进程名称xxx的进程。
#include
#include
#include
int main(){
pid_t pid;
pid = getpid();//获取自身的进程标识符
printf("my pid is %d\n",pid);
while(1);
return 0;
}
#include
#include
#include
int main(){
pid_t pid;
pid_t pid2;
pid = getpid();
printf("before fork: pid = %d\n\n",pid );
fork();
pid2 = getpid();
printf("after fork: pid = %d\n",pid2 );
if(pid == pid2){
printf("this is father print,pid = %d\n\n",pid);fork创建之前的父进程id
}
else{
printf("this is child print,child pid = %d\n",getpid());//fork创建之后的子进程id
}
return 0;
}
#include
#include
#include
int main(){
pid_t pid;
printf("father: id =%d\n",getpid());
pid = fork();
if(pid > 0){
printf("this is father print,pid = %d\n",getpid());
}
else if(pid == 0){
printf("this is child print,pid = %d\n",getpid());
}
return 0;
}
父子进程在初始阶段共享所有的数据(全局、 栈区、 堆区、 代码), 内核会将所有的区域设置为只读。 当父子进程中任意一个进程试图修改其中的数据时, 内核才会将要修改的数据所在的区域(页) 拷贝一份。(而陈立晨讲的是,仅数据段共享,其余全拷贝,仅当数据段发生修改时才拷贝且独立分配空间)
FILE/demo7.c
#include
#include
#include
int main(){
pid_t pid;
int data = 10;
printf("father: id =%d\n",getpid());
pid = fork();
if(pid > 0){
printf("this is father print,pid = %d\n",getpid());
}
else if(pid == 0){
printf("this is child print,pid = %d\n",getpid());
data = data+100;
}
printf("data=%d\n",data);
return 0;
}
#include
#include
#include
int main(){
pid_t pid;
int data = 10;
while(1){
printf("please input a data\n");
scanf("%d",&data);
if(data == 1){
pid = fork();
if(pid > 0){
}
else if(pid == 0){
while(1){
printf("do net request,pid=%d\n",getpid());
sleep(3);//使程序休眠3秒
}
}
}
else{
printf("wait ,do nothing\n");
}
}
return 0;
}
#include
#include
#include
int main(){
pid_t pid;
pid = fork();
//pid = vfork();
if(pid > 0){
while(1){
printf("this is father print, pid = %d\n",getpid());
sleep(3);
}
}
else if(pid == 0){
while(1){
printf("this is chilid print, pid = %d\n",getpid());
sleep(3);
}
}
return 0;
}
#include
#include
#include
#include
#include //exit函数的
int main(){
pid_t pid;
int cnt = 0;
pid = vfork();
if(pid > 0){
while(1){
printf("cnt=%d\n",cnt);
printf("this is father print, pid = %d\n",getpid());
sleep(1);
}
}
else if(pid == 0){
while(1){
printf("this is chilid print, pid = %d\n",getpid());
sleep(1);
cnt++;
if(cnt == 3){
exit(0);
//_exit(0);
//_Exit(0);
}
}
}
return 0;
}
正常退出
异常退出
#include
int main(int argc,char *argv[]){
int i = 0;
for(i = 0; i < argc; i++){
printf("argv[%d]: %s\n",i,argv[i]);
}
return 0;
}
#include
#include
#include
//函数原型:int execl(const char *path, const char *arg, ...);
int main(void){
printf("before execl\n");
if(execl("./echoarg","echoarg","abc",NULL) == -1){
printf("execl failed!\n");
perror("why");
}
printf("after execl\n");//execl调用失败才会走到这里
return 0;
}
#include
#include
#include
//函数原型:int execl(const char *path, const char *arg, ...);
int main(void){
printf("before execl\n");
if(execl("/bin/ls","ls",NULL,NULL) == -1){
printf("execl failed!\n");
perror("why");
}
printf("after execl\n");
return 0;
}
#include
#include
#include
//函数原型:int execl(const char *path, const char *arg, ...);
int main(void){
printf("before execl\n");
if(execl("/bin/ls","ls","-l",NULL) == -1){
printf("execl failed!\n");
perror("why");
}
printf("after execl\n");
return 0;
}
#include
#include
#include
//函数原型:int execl(const char *path, const char *arg, ...);
int main(void){
printf("this pro get system date:\n");
if(execl("/bin/date","date",NULL,NULL) == -1){
printf("execl failed!\n");
perror("why");
}
printf("after execl\n");
return 0;
}
#include
#include
#include
//函数原型:int execl(const char *path, const char *arg, ...);
int main(void){
printf("this pro get system date:\n");
if(execlp("ps","ps",NULL,NULL) == -1){
printf("execl failed!\n");
perror("why");
}
printf("after execl\n");
return 0;
}
pwd//return:/home/cjq/Jessie/PRO
export PATH=$PATH:/home/cjq/Jessie/PRO
#include
#include
#include
//函数原型:int execl(const char *path, const char *arg, ...);
int main(void){
printf("this pro get system date:\n");
char *argv[] = {"ps",NULL,NULL};
if(execvp("ps",argv) == -1){
printf("execl failed!\n");
perror("why");
}
printf("after execl\n");
return 0;
}
char *argv[] = {"ps",NULL,NULL};
if(execv("/bin/ps",argv) == -1){
#include
#include
#include
#include //wait的
#include //wait的
#include
#include
#include
int main(){
pid_t pid;
int data = 10;
while(1){
printf("please input a data\n");
scanf("%d",&data);
if(data == 1){
int fdSrc;
pid = fork();
if(pid > 0){
wait(NULL);
}
if(pid == 0){
char *readBuf=NULL;
fdSrc = open("config.txt",O_RDWR);
int size = lseek(fdSrc,0,SEEK_END);
lseek(fdSrc,0,SEEK_SET);
readBuf=(char *)malloc(sizeof(char)*size + 8);
int n_read = read(fdSrc, readBuf, size);
char *p = strstr(readBuf,"LENG=");
if(p==NULL){
printf("not found\n");
exit(-1);
}
p = p+strlen("LENG=");
*p = '5';
lseek(fdSrc,0,SEEK_SET);
int n_write = write(fdSrc,readBuf,strlen(readBuf));
close(fdSrc);
exit(0);
}
}
else{
printf("wait ,do nothing\n");
}
}
return 0;
}
#include
#include
#include
#include //wait的
#include //wait的
#include
#include
#include
int main(){
pid_t pid;
int data = 10;
while(1){
printf("please input a data\n");
scanf("%d",&data);
if(data == 1){
int fdSrc;
pid = fork();
if(pid > 0){
wait(NULL);
}
if(pid == 0){
execl("./changeData","changeData","config.txt",NULL);
}
}
else{
printf("wait ,do nothing\n");
}
}
return 0;
}
#include
#include
#include
#include //wait的
#include //wait的
#include
#include
#include
int main(){
pid_t pid;
int data = 10;
while(1){
printf("please input a data\n");
scanf("%d",&data);
if(data == 1){
int fdSrc;
pid = fork();
if(pid > 0){
wait(NULL);
}
if(pid == 0){
//execl("./changeData","changeData","config.txt",NULL);
system("./changeData config.txt");//相当于在终端执行shell命令“sh -c ./changeData config.txt”
}
}
else{
printf("wait ,do nothing\n");
}
}
return 0;
}
#include
#include
#include
//函数原型:int execl(const char *path, const char *arg, ...);
int main(void){
char ret[1024] = {0};
system("ps");
printf("ret=%s\n",ret);
//popen("ps","w");
return 0;
}
#include
#include
#include
//函数原型:int execl(const char *path, const char *arg, ...);
//FILE *popen(const char *command, const char *type);
//size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
//int pclose(FILE *stream);
int main(void){
char ret[1024] = {0};
FILE *fp;
fp = popen("ps","r");
int nread = fread(ret,1,1024,fp);
printf("read ret %d byte, ret=%s\n",nread,ret);
return 0;
}