1,¹ØÓÚÕæʵºÍÓÐЧÓû§,Óû§×éµÄ˵Ã÷£º
½öÄÃÓû§À´ËµÃ÷,×éÔÀíÀàËÆ.
ÕæʵÓû§ÊÇÄãµÇ½µÄʱºòËùʹÓõÄÓû§ÃûµÄÊý×Ö»¯±íʾ.ËüÔÚÄãµÄµÇ½ÆÚég²»»á
±»¸Ä±ä,ÊÇÄã×î±¾ÖʵÄÆ·ÖÊ.
ÓÐЧÓû§ÊÇÖ¸ÔÚÖ´ÐÐÒ»¸ö³ÌÐòʱ±ä³ÉÁíÍâµÄȨÏÞµÄÓû§.¾Ù¸öÀý×Ó,¹Å´úµÄÒ»¸ö
´ÌÊ·,ËüƽʱÕæʵÉí·Ö¾ÍÊÇÒ»¸ö´ÌÊ·,¿Éµ±ËûÒªÐÐʹ³¬Ô½×Ô¼ºÈ¨Ï޵ķ¨°ìʱ,±ÈÈç
³Í·£Ò»¸ö±ÈËû¸ü¸ßµÄ¹ÙÔ±,Õâʱ,Èç¹ûËûÓÐÒ»¸öÉз½±¦½££¨¾ßÓлʵ۵ÄȨÁ¦£©,Õâ
ʱËûµÄ ÓÐЧÉí·Ý¾ÍÊǻʵÛ.µ«Æ½Ê±,Õâ¸öÓÐЧÉí·ÝºÍËûµÄÕæʵÉí·ÝÊÇÒ»ÑùµÄ.
ÕæʵÓû§ID: getuid(void);
ÓÐЧÓû§ID: geteuid(void);
ÕæʵÓû§×éID: getgid(void);
ÓÐЧÓû§×éID: getegid(void);
2,µÃµ½Óû§ºÍÓû§×éµÄÐÅÏ¢
getloginº¯Êý¿ÉÒÔ·µ»ØÖ´ÐгÌÐòµÄÓû§µÄµÇ½Ãû.È»ºóÕâ¸öµÇ½Ãû¿ÉÒÔ×÷Ϊ²ÎÊý
´«µÝ¸øgetpwnameº¯Êý,´Ó¶ø·µ»Ø/etc/passwdÎļþÖÐÓë¸ÃµÇ¼ÃûÏàÓ¦µÄÒ»ÐÐÍêÕûÐÅ
Ï¢.ÁíÖÖ·½·¨ÊÇ,½«½ø³ÌµÄUID´«µÝ¸øgetpwuidº¯Êý,ͬÑù·µ»Ø/etc/passwdÎļþÖÐ
ºÏÊʵÄÌõÄ¿.
#include <unistd.h>
char *getlogin(void);
#include <pwd.h>
struct passwd *getpwnam(const char *name);
3,linux ½ø³ÌµÄÈý¸ö¶ÀÁ¢µÄʱ¼äÖµ
Wall clock time(ǽÉÏʱÖÓʱ¼ä£©ÊÇÁ÷ÊŵÄʱ¼ä
User CPU time(Óû§CPUʱ¼ä£©Êǽø³Ì»¨ÔÚÖ´ÐÐÓû§Ä£Ê½£¨·ÇÄÚºËģʽ£©´úÂëÉϵÄ
ʱ¼ä×ÜÁ¿
System CPU time(ϵͳCPUʱ¼ä£©ÊÇ»¨ÔÚÖ´ÐÐÄں˴úÂëÉϵÄʱ¼ä×ÜÊÇ.
ͨ¹ýtimes»ògetrusage¿ÉÒÔ»ñµÃÕâЩÐÅÏ¢.
#include <sys/times.h>
clock_t times(struct tms *buf);
·µ»ØǽÉÏʱÖÓʱ¼ä,buf±£´æÁ˵±Ç°½ø³ÌµÄʱ¼ä.
ÔÚ<sys/times.h>¶¨ÒåµÄtms½á¹¹
struct tms {
clock_t tms_utime; // User CPU time
clock_t tms_stime; //System CPU time
clock_t tms_cutime; //User CPU time of chilren
clock_t tms_cstime; //System CPU time of chilren.
ÕâÖµÖµclock_t¶¼ÊÇʱÖӵδðÊý,Ҫת»»³ÉÃ뻹Ҫͨ¹ýsysconfº¯Êýת»».
_SC_CLK_TCKÊǶ¨ÒåÿÃëÖÓÓжàÉٵδðµÄºê.
4,×ÊÔ´ÀûÓÃÐÅÏ¢
ÀûÓú¯Êýgetrusage
#include <sys/times.h>
#include <sys/resource.h>
#include <unistd.h>
int getrusage(int who, struct rusage *usage);
ÕâÀïÖصãÔÚÓÚusageÉÏ,¶øwho²ÎÊýÖ»ÄÜÈ¡ÖµRUSAGE_SELF»òRUSAGE_CHILDREN,±íʾ
ÊÇ·µ»Øµ÷Óýø³ÌµÄ×ÊÔ´ÀûÓÃÐÅÏ¢»¹ÊÇ×Ó½ø³ÌµÄ×ÊÔ´ÀûÓÃÐÅÏ¢.ÕâÀï¶Ô°üº¬
ÔÚ<sys/resource.h>µÄrusage½á¹¹½øÐÐ˵Ã÷£º
struct rusage {
struct timeval ru_utime; // user time used
struct timeval ru_sutime; // system time used
long ru_maxrss; // maximum resident set size
long ru_maxixrss; // shared memory size
long ru_maxidrss; // unshared data dize
long ru_maxisrss; // unshared stack size
long ru_minflt; // page reclaims
long ru_majflt; // page faults
long ru_nswap; // swaps
long ru_inblock; // block input operations
long ru_outblock; // block output operations
long ru_msgsnd; // message sent
long ru_msgrcv; //message received
long ru_nsignals; // siganls received
long ru_nvcsw; // voluntary contex switches
long ru_nivcsw; //involuntary contex switches
}
˵Ã÷
µ«linuxÖ»Ö§³Ö×ÊÔ´£º
ru_utime--Óû§Ä£Ê½£¨·ÇÄںˣ©´úÂëʱ¼ä
ru_stime--Äں˴úÂëµÄʱ¼ä
ru_minflt--´ÎҪʧЧÊý(·ÃÎÊÄÚ´æ¶øûÓÐÒýÆð´ÅÅÌ·ÃÎÊ£©
ru_majflt--Ö÷ҪʧЧÊý(ÄÚ´æ·ÃÎÊÒýÆð´ÅÅÌ·ÃÎÊ£©
5,»á»°ºÍ½ø³ÌµÄ¸ÅÄî
ÓеÄÇé¿öÖнø³Ì¼ä²»Äܼòµ¥µÄÓø¸×ÓÀ´¸ÅÀ¨,Èç¹û¶ÔÓڹܵÀ,Éæ¼°µ½¼¸¸ö½ø³Ì,
Õâʱ,ÕâЩÏà¹ØµÄ½ø³Ì¾Í³ÉΪ½ø³Ì×é,¿ÉÒÔ±»Í³Ò»¿ØÖÆ£¨ÈçɱËÀ£©.Ò»¸ö½ø³Ì
×é(process group)ÊÇÏà¹Ø½ø³ÌµÄÒ»¸ö¼¯ºÏ,ÕâЩÏà¹Ø½ø³ÌÄܺÍÊÇÔÚÒ»¸ö¹ÜµÀµÄÃü
ÁîÐòÁÐ.¶¼½ø³Ì×éÖеÄËùÓеĽø³Ì¶¼ÓÐÏàͬµÄ½ø³Ì×éºÅ,¼´PGID.ʹÓýø³Ì×éµÄÄ¿
µÄÊÇΪÁË·½±ã×÷˜I¿ØÖÆ.ÀýÈç,¼Ù¶¨ÄãÔËÐÐÁËÃüÁîÐйܵÀ ls -l /usr/include |
sort | wc -l.Èç¹ûÒªÔÚÆ仹ÔÚÔËÐÐʱɱËÀËü£¨°´Ctrl C),ÔòshellÐèÄÜÖÕÖ¹ËùÓÐ
µÄ½ø³Ì.Ëüͨ¹ýɱËÀ½ø³Ì×é¶ø²»ÊÇÿ¸ö½ø³ÌÀ´×öµ½ÕâÒ»µã.
»á»°(session)ÊÇÓÉÒ»¸ö»ò¶à¸ö½ø³Ì×é¹¹³É,»á»°Áìµ¼(session leader)½ø³ÌÊÇ´´
½¨»á»°µÄ½ø³Ì,ÿ¸ö»á»°¶¼ÓÐΩһ±êʶºÅ,³ÆΪ»á»°ID(session ID),ËüÖ»ÊǻỰ
Áìµ¼µÄ½ø³ÌPID.»á»°¶Ô½ø³Ì×éÆðµÄ×÷Óúͽø³Ì×é¶Ôµ¥¸ö½ø³ÌÆðµÄ×÷ÓÃÒ»Ñù.·½±ã
¿ØÖÆ.
6,fork
££include <unistd.h>
pid_t fork(void);
³É¹¦Ïò¸¸½ø³Ì·µ»Ø×Ó½ø³ÌµÄPID,Ïò×Ó½ø³Ì·µ»Ø0.
×Ó½ø³ÌºÍ¸¸½ø³ÌÓÐÒ»ÑùµÄÕæʵºÍÓÐЧµÄUID,GID,½ø³Ì×éºÍ»á»°ID,»·¾³,×ÊÔ´ÏÞ
ÖÆ,´ò¿ªµÄÎļþÒÔ¼°¹²ÏíÄÚ´æ¶Î.µ«×Ó½ø³Ì²»¼Ì³Ð¸¸½ø³ÌµÄ³¬Ê±ÉèÖÃ(alarmµ÷
Óã©,¸¸½ø³Ì´´½¨µÄÎļþËø,δ¾öÐźÅ.
7,execº¯Êý×å
ÒÔÇ°×÷¹ý±Ê¼Ç,ÔÙ¸´Ï°Ò»´Î.
#include <unistd.h>
int execl(const char *path, const char *arg, ...);
int execlp(const char *file, const char *arg, ...);
int execle(const *path, const char *arg,char *cont envp[]);
int execv(const char *path, char *const argv[]);
int execve(const char *path, const argv[], char *const envp[]);
int execvp(const char *file, char *const argv[]);
Ïà¹ØµÄ,²Ù¿Ø½ø³ÌÔËÐеĻ·¾³µÄº¯Êý£º
#include <stdlib.h>
int utenv(const char *string);
char *getenv(const char *name);
˵Ã÷£ºgetenv²éÕÒÃûΪnameµÄ»·¾³±äÁ¿²¢·µ»ØÖ¸ÏòÆäÖµµÄÖ¸Õë,Èç¹ûûÓÐÕÒµ½Ôò
·µ»ØNULL.putenvÌí¼Ó»ò¸Ä±ästringÖÐÖ¸¶¨µÄ"name = value" ¶Ô.Èç¹ûËüÖ´ÐгÉ
¹¦,Ôò·µ»Ø0,Èç¹ûÖ´ÐÐʧ°Ü,Ôò·µ»Ø-1.
8,popenº¯Êý
ËüºÍsystemº¯ÊýÐÐΪÀàËÆ,µ«ËüÊÇÒ»ÖÖÎÞÐèʹÓÃforkºÍexec¾ÍÄÜÔÚÖ´ÐÐÍⲿµÄ³Ì
ÐòµÄº†Ò×·½·¨.µ«ºÍsystem²»Í¬,popenʹÓùܵÀÀ´¹¤×÷.ÔÐÍ
#include <stdio.h>
FILE *popen(const char *command, const char *type);
int pclose(FILE *stream);
˵Ã÷£ºpopenµ÷ÓùܵÀ,²¢´´½¨Í¨¹ý±ê×¼ÊäÈë,»òÕß´ÓcommandÖ¸¶¨µÄ³ÌÐò»ò½Å±¾
µÄ±ê×¼Êä³öÀ´µÄ¹ÜµÀ,µ«²»ÊÇÁ½Õ߶¼ÓÐ.µÚ¶þ¸ö²ÎÊýtypeÔÚ¶ÁÈ¡¹ÜµÀµÄstdoutʱΪ
r,ÔÚдÈëstdinʱΪw.ËüºÍI/OµÄÓ÷¨ÓеãÎ¥·´Ö±¾õ.Çëд¶¼ÊÇÏà¶ÔÓÚcommand¶øÑÔ
µÄ,commandµÄÊä³öÊÇ´Óstdout¶ÁÈëµÄ.ÒªÏòcommandÊäÈë,ÔòÐèÏòËüµÄstdin
дÈë.
9,µÈ´ý½ø³Ì££wait º¯Êý×å
Ö»ÓÐÁ½¸öº¯Êý
#include <sys/wait.h>
#include <sys/types.h>
pid_t wait(int *status);
pid_t waitpid(pid_t pid, int *status, int options);
˵Ã÷,status±£´æÁ˽ø³ÌµÄÍ˳ö״̬,¶øwaitpidÖÐpid ¿ÉÄÜÈ¡Öµ£º
-1 µÈ´ýÈκÎPGID==PID¾ø¶ÔÖµµÄ×Ó½ø³Ì
1 µÈ´ýÈκÎ×Ó½ø³Ì
0 µÈ´ýÈκÎPGID==µ÷Óýø³ÌµÄ×Ó½ø³Ì
>0 µÈ´ýPID==pidµÄ×Ó½ø³Ì
¶ÔÓÚoption¾ö¶¨µ÷ÓõÄÐÐΪ,¿ÉÒÔÊÇWNOHANGµ¼ÖÂÔÚûÓÐ×Ó½ø³ÌÍ˳öʱÁ¢¼´·µ»Ø.
Ò²¿ÉÒÔÊÇWUNTRACED,ÔÚûÓб¨¸æ״̬µÄ½ø³Ì¶ø·µ»Ø.
10, killº¯ÊýɱËÀ½ø³Ì.
#include <signal.h>
#include <sys/types.h>
int kill(pid, int sig);
11,ÐźÅ
¶ÔÓÚÊÕµ½Ò»¸öÐźźóÓÐÈýÖÖ´¦Àí°ì·¨£º
¡·ºöÂÔÕâ¸öÐźÅ
¡·²¶»ñÕâ¸öÐźÅ,ʹִÐÐÒ»¶Î³ÆΪÐźŴ¦ÀíÆ÷µÄÌØÊâ´úÂë.
¡·ÔÊÐíÖ´ÐÐÆäĬÈϲÙ×÷
¿ÉÀûÓÃkillº¯Êý·¢ËÍÐźÅ.
³ýÁËSIGSTOP ºÍ SIGKILL,ÆäËüÐźŶ¼¿ÉÒÔ±»²¶»ñ»òºöÂÔ.
pauseº¯Êý¹ÒÆðµ÷ÓÃËüµÄ½ø³ÌÖ±µ½ÓÐÈκÎÐźŵ½´ï.
#include <unistd.h>
int pause(void);
´´½¨ºÍÉèÖÃÐźż¯ºÏ,È»ºóͨ¹ýsigprocmaskÉèÖûòÐ޸ĵ±Ç°ÐźÅÑÚÂë,ÉèÖÃÒª²¶
»ñµÄÐźÅ,ÔÙÏòÄں˵ǼÇÒ»¸öÐźŴ¦ÀíÆ÷(sigactionº¯Êý£©,µÚ¶þ¸ö²¶×½ÐÅ
ºÅ.
´´½¨,ÉèÖÃ,²éѯһ¸öÐźż¯ºÏ
#include <signal.h>
int sigemptyset(sigset_t *set);
int sigfillset(sigset_t *set);
int sigaddset(sigset_t *set, int signum);
int sigdelset(sigset_t *set, int signum);
int sigismember(const sigset_t *set, int signum);
µÇ¼ÇÐźŴ¦ÀíÆ÷
#include <signale.h>
int sigprocmask(int how, const sigset_t *set, sigset_t *oldset);
˵Ã÷£º how ¿ÉÈ¡Öµ£º
SIG_BLOCK --set°üº¬ÆäËüÒª×èÈûµÄÐźÅ
SIG_UNBLOCK -- set °üº¬Òª½â³ý×èÈûµÄÐźÅ
SIG_SETMASK --set°üº¬ÐµÄÐźÅÑÚÂë
Èç¹ûhowΪNULL,Ëü±»ºöÂÔ.Èç¹ûsetΪNULL,µ±Ç°ÑÚÂë±£´æÔÚoldsetÖÐ,Èç¹û
oldsetΪNULL,Ò²±»ºöÂÔ.
#include <signal.h>
int sigaction(int signum, const struct sigaction *act, struct
sigaction *oldact);
// sigactionΪsignumËùÖ¸¶¨µÄÐźÅÉèÖÃÐźŴ¦ÀíÆ÷,sigaction½á
¹¹(act,oldactÃèÊöÁËÐźŵIJ¿Êð.ÔÚsignal.hÖÐÓÐsigaction½á¹¹£º
struct sigaction{
void (*sa_hundler)(int);
sigset_t sa_mask;
int sa_flag;
void (*sa_restorer)(void);
}
Õâ¸ö½á¹¹ÃèÊöÁ˵±signumÖеÄÐźŲúÉúºóÒªµ÷ÓõĴ¦ÀíÆ÷»òÕߺ¯Êýsa_hundler,
ËüÒ²¿É±»¸³ÖµSIG_DEF,ÒýÆðsignumĬÈ϶¯×÷·¢Éú,¸³ÖµSIG_IGN,ʹºöÂÔÕâ¸öÐÅ
ºÅ.
sa_mask¶¨ÒåÁËÔÚÖ´Ðд¦ÀíÆ÷ÆÚégÓ¦¸Ã×èÈûµÄÆäËüÐźż¯ºÏµÄÐźÅÑÚÂë.
sa_flagsÊÇÐÞÕýsa_handlerÐÐΪµÄÑÚÂë.¿ÉÒÔΪÈçϵÄÖµ£º
SA_NOCLDSTOP-- ½ø³ÌºöÂÔ×Ó½ø³Ì²úÉúµÄÈκÎSIGSTOP,SIGSTP,SIGTTINºÍSIGTTOU
ÐźÅ
SA_ONESHOT »ò SA_RESETHAND--µÇ¼ÇµÄ×Ô¶¨ÒåÐźŴ¦ÀíÆ÷Ö»Ö´ÐÐÒ»´Î.ÔÚÖ´ÐÐÍê
±Ïºó,»Ö¸´ÐźŵÄĬÈ϶¯×÷.
SA_RESTART--ÈÿÉÖØÆôµÄϵͳµ÷ÓÃÆð×÷ÓÃ
SA_NOMASK»ò SA_NODEFER--²»±ÜÃâÔÚÐźÅ×Ô¼ºµÄ´¦ÀíÆ÷ÖнÓÊÕÐźű¾Éí.
¶ÔÓÚº¯ÊýÖеÄsa_restorerÔªËØ,ÒѾ±»·ÏÆú.
12,¼ì²âÐźÅ
sigpendingʹ½ø³Ì¼ì²âδ¾öÐźţ¨µ±×èÈûʱ±»¹ÒÆðµÄÐźţ©,È»ºó¿É¾ö¶¨ÊǺöÂÔ
ËüÃÇ»¹ÊǵÝËÍËüÃÇ.
££include <signal.h>
int sigpending(sigset_t *set);
δ¾öÐźż¯ºÏÔÚ setÖзµ»Ø,È»ºó¿ÉÒÔʹÓÃsigismemberÅжϸÐÐËȤµÄÐźÅÊÇ·ñÊÇ
δ¾öÐźÅ.
13,½ø³Ìµ÷¶È
#include <sched.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/resource.h>
int sched_setscheduler(pid_t pid, int policy ,const struct sched_param
*p);
int sched_getscheduler(pid_t pid);
int sched_get_priority_max(int policy);
int sched_get_priority_min(int policy);
int getpriority(int which, int who);
int setpriority(int which, int who, int prio);
int nice(int inc); ±¾ |