int main() { if(my_fork()) { printf("I'm parent: My PID=%d\n",my_getpid()); } else { printf("I'm child: My PID=%d and my parent %d\n", my_getpid(), my_getppid()); } }
int treeTest(){ int i,j,k,total,s1,s2,count,t; char str[1024],dir[1024]; struct dirent **namelist; strcpy(dir,default_path); total = scandir(dir, &namelist, 0, alphasort); printf("path=%s,total=%d\n",dir,total); for(i=0;i<total;i++){ strcpy(str,namelist[i]->d_name); if(str[0]>='0'&&str[0]<='9') count++; } printf("进程数:%d\n",count); info file[1024]; i=0;t=0; while(i<total){ FILE *fp; char path[1024],name[1024]; int pid,ppid; strcpy(str,namelist[i]->d_name); strcpy(path,default_path); //printf("%s\n",str); if(str[0]>='0'&&str[0]<='9'){ strcat(path,str); strcat(path,"/status"); fp=fopen(path,"r"); while(!feof(fp)){ fgets(str,1024,fp); //pid if((s1=my_getpid(str))!=0) pid=s1; //ppid if((s2=my_getppid(str))!=0) ppid=s2; //name strcpy(file[t].name,my_getName(str)); file[t].pid=pid; file[t].ppid=ppid; } fclose(fp); printf("%d,%s,pid=%d,ppid=%d,name=%s#\n",t,path,file[t].pid,file[t].ppid,file[t].name); t++; } i++; } memset(&file->flag,0,count); memset(&file->rec,0,count); print_pstree(file,count,0,0); while (1){;} }