分类: LINUX
2008-08-20 18:22:21
#include<unistd> pid_tgetpid(void); pid_tgetppid(void);
#include<unistd> #include<sys/types.h> uid_tgetuid(void); uid_tgeteuid(void); gid_tgetgid(void); git_tgetegid(void);
structpasswd{ char*pw_name;/*登录名称*/ char*pw_passwd;/*登录口令*/ uid_tpw_uid;/*用户ID*/ gid_tpw_gid;/*用户组ID*/ char*pw_gecos;/*用户的真名*/ char*pw_dir;/*用户的目录*/ char*pw_shell;/*用户的SHELL*/ }; #include<pwd.h> #include<sys/types.h> structpasswd*getpwuid(uid_tuid);
#include<unistd.h> #include<pwd.h> #include<sys/types.h> #include<stdio.h> intmain(intargc,char**argv) { pid_tmy_pid,parent_pid; uid_tmy_uid,my_euid; gid_tmy_gid,my_egid; structpasswd*my_info; my_pid=getpid(); parent_pid=getppid(); my_uid=getuid(); my_euid=geteuid(); my_gid=getgid(); my_egid=getegid(); my_info=getpwuid(my_uid); printf("ProcessID:%ld",my_pid); printf("ParentID:%ld",parent_pid); printf("UserID:%ld",my_uid); printf("EffectiveUserID:%ld",my_euid); printf("GroupID:%ld",my_gid); printf("EffectiveGroupID:%ld",my_egid): if(my_info) { printf("MyLoginName:%s",my_info->pw_name); printf("MyPassword:%s",my_info->pw_passwd); printf("MyUserID:%ld",my_info->pw_uid); printf("MyGroupID:%ld",my_info->pw_gid); printf("MyRealName:%s",my_info->pw_gecos); printf("MyHomeDir:%s",my_info->pw_dir); printf("MyWorkShell:%s",my_info->pw_shell); } }
#include<unistd.h> pid_tfork();
#include<sys/types.h> #include<sys/wait.h> pid_twait(int*stat_loc); pid_twaitpid(pid_tpid,int*stat_loc,intoptions);
#include<unistd.h> intexecl(constchar*path,constchar*arg,...); intexeclp(constchar*file,constchar*arg,...); intexecle(constchar*path,constchar*arg,...); intexecv(constchar*path,char*constargv[]); intexecvp(constchar*file,char*constargv[]):
#include<unistd.h> #include<sys/types.h> #include<sys/wait.h> #include<stdio.h> #include<errno.h> #include<math.h> voidmain(void) { pid_tchild; intstatus; printf("Thiswilldemostratehowtogetchildstatus"); if((child=fork())==-1) { printf("ForkError:%s",strerror(errno)); exit(1); } elseif(child==0) { inti; printf("Iamthechild:%ld",getpid()); for(i=0;i<1000000;i )sin(i); i=5; printf("Iexitwith%d",i); exit(i); } while(((child=wait(&status))==-1)&(errno==EINTR)); if(child==-1) printf("WaitError:%s",strerror(errno)); elseif(!status) printf("Child%ldterminatednormallyreturnstatusiszero", child); elseif(WIFEXITED(status)) printf("Child%ldterminatednormallyreturnstatusis%d", child,WEXITSTATUS(status)); elseif(WIFSIGNALED(status)) printf("Child%ldterminatedduetosignal%dznotcaught", child,WTERMSIG(status)); }
#include<unistd.h> #include<sys/types.h> #include<sys/stat.h> #include<stdio.h> #include<errno.h> #include<fcntl.h> #include<signal.h> /*Linux的默任个人的邮箱地址是/var/spool/mail/用户的登录名*/ #defineMAIL"/var/spool/mail/hoyt" /*睡眠10秒钟*/ #defineSLEEP_TIME10 main(void) { pid_tchild; if((child=fork())==-1) { printf("ForkError:%s",strerror(errno)); exit(1); } elseif(child>0) while(1); if(kill(getppid(),SIGTERM)==-1) { printf("KillParentError:%s",strerror(errno)); exit(1); } { intmailfd; while(1) { if((mailfd=open(MAIL,O_RDONLY))!=-1) { fprintf(stderr,"%s","07"); close(mailfd); } sleep(SLEEP_TIME); } } }