1、程序的编译、运行
2、进程
3、文件
4、时间
5、信号
6、消息
7、线程
8、网络
简单的编译:gcc test.c
运行:./a.out
#include <unistd.h> #include <pwd.h> #include <sys/types.h> #include <stdio.h> int main(int argc,char **argv) { pid_t my_pid,parent_pid; //进程id,进程的父进程id uid_t my_uid,my_euid; //用户id,有效用户id gid_t my_gid,my_egid; //组id,有效组id struct passwd *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("Process ID:%ld ",my_pid); printf("Parent ID :%ld ",parent_pid); printf("User ID:%ld ",my_uid); printf("Effective User ID :%ld ",my_euid); printf("Group ID :%ld ",my_gid); printf("Effective Group ID :%ld ",my_egid): if(my_info) { printf("My Login Name:%s " ,my_info->pw_name); //登录名称 printf("My Password:%s " ,my_info->pw_passwd); //登录口令 printf("My User ID:%ld ",my_info->pw_uid); //用户ID printf("My Group ID:%ld ",my_info->pw_gid); //用户组ID printf("My Real Name:%s " ,my_info->pw_gecos); //用户的真名 printf("My Home Dir:%s ", my_info->pw_dir); //用户的目录 printf("My Work Shell:%s ", my_info->pw_shell); //用户的SHELL } }
运行结果:
进程的创建
创建进程
#include <unistd.h>
pid_t fork();
阻塞进程
#include <sys/types.h>;
#include <sys/wait.h>;
pid_t wait(int *stat_loc);
pid_t waitpid(pid_t pid,int *stat_loc,int options);
新进程执行一个程序
#include <unistd.h>;
int execl(const char *path,const char *arg,...);
int execlp(const char *file,const char *arg,...);
int execle(const char *path,const char *arg,...);
int execv(const char *path,char *const argv[]);
int execvp(const char *file,char *const argv[]);
#include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #include <stdio.h> #include <errno.h> #include <math.h> void main(void) { pid_t child; int status; printf("This will demostrate how to get child status "); if((child=fork()) == -1) { printf("Fork Error:%s ",strerror(errno)); exit(1); } else if(child == 0) { int i; printf("I am the child:%ld ",getpid()); for(i=0;i<1000000;i++) sin(i); i=5; printf("I exit with %d ",i); exit(i); } while(((child=wait(&status)) == -1)&(errno == EINTR)); if(child == -1) printf("Wait Error:%s ",strerror(errno)); else if(!status) printf("Child %ld terminated normally return status is zero ", child); else if(WIFEXITED(status)) printf("Child %ld terminated normally return status is %d ", child,WEXITSTATUS(status)); else if(WIFSIGNALED(status)) printf("Child %ld terminated due to signal %d znot caught ", 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> #define MAIL "/var/spool/mail/hoyt" /* Linux 的默任个人的邮箱地址是 /var/spool/mail/用户的登录名 */ #define SLEEP_TIME 10 /* 睡眠 10 秒钟 */ main(void) { pid_t child; if((child=fork())==-1) { printf("Fork Error:%s ",strerror(errno)); exit(1); } else if(child>0) while(1); if(kill(getppid(),SIGTERM)==-1) { printf("Kill Parent Error:%s ",strerror(errno)); exit(1); } { int mailfd; while(1) { if((mailfd=open(MAIL,O_RDONLY))!=-1) { fprintf(stderr,"%s","