首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

UNIX编程(八)-进程控制

2012-06-30 
UNIX编程(8)-进程控制1.进程标识符每个进程都有一个非负整数表示的唯一进程ID??#include apue.h#include

UNIX编程(8)-进程控制

1.进程标识符

每个进程都有一个非负整数表示的唯一进程ID

?

?

#include "apue.h"#include <sys/wait.h>intmain(void){    pid_t   pid;    int     status;    if ((pid = fork()) < 0)        err_sys("fork error");    else if (pid == 0)              /* child */        exit(7);    if (wait(&status) != pid)       /* wait for child */        err_sys("wait error");    pr_exit(status);                /* and print its status */    if ((pid = fork()) < 0)        err_sys("fork error");    else if (pid == 0)              /* child */        abort();                    /* generates SIGABRT */    if (wait(&status) != pid)       /* wait for child */        err_sys("wait error");    pr_exit(status);                /* and print its status */    if ((pid = fork()) < 0)        err_sys("fork error");    else if (pid == 0)              /* child */        status /= 0;                /* divide by 0 generates SIGFPE */    if (wait(&status) != pid)       /* wait for child */        err_sys("wait error");    pr_exit(status);                /* and print its status */    exit(0);}


6.waitid函数

#include <sys/wait.h>int waitid(idtype_t  * rusage * struct rusage *fork, we can create new processes; and with the exec functions, we can initiate new programs. The exit function and the wait functions handle termination and waiting for termination. These are the only process control primitives we need. We'll use these primitives in later sections to build additional functions, such as popen and system.

... /* (char *)0 */ );int execv(const char * ... /* (char *)0 */ );int execvp(const char *? comp_t ac_rw;?????? /* blocks read or written */
????????????????????? /* (not present on BSD systems) */
? char?? ac_comm[8];? /* command name: [8] for Solaris, */
????????????????????? /* [10] for Mac OS X, [16] for FreeBSD, and */
????????????????????? /* [17] for Linux */
};

?

例:

产生进程会计数据

#include "apue.h"intmain(void){    pid_t   pid;    if ((pid = fork()) < 0)        err_sys("fork error");    else if (pid != 0) {       /* parent */        sleep(2);        exit(2);               /* terminate with exit status 2 */    }                               /* first child */    if ((pid = fork()) < 0)        err_sys("fork error");    else if (pid != 0) {        sleep(4);        abort();               /* terminate with core dump */    }                               /* second child */   if ((pid = fork()) < 0)       err_sys("fork error");   else if (pid != 0) {       execl("/bin/dd", "dd", "if=/etc/termcap", "of=/dev/null", NULL);       exit(7);                /* shouldn't get here */   }                               /* third child */   if ((pid = fork()) < 0)       err_sys("fork error");   else if (pid != 0) {       sleep(8);       exit(0);                /* normal exit */   }                               /* fourth child */   sleep(6);   kill(getpid(), SIGKILL);    /* terminate w/signal, no core dump */   exit(6);                    /* shouldn't get here */}


查看进程会计数据

#include "apue.h"#include <sys/acct.h>#ifdef HAS_SA_STAT#define FMT "%-*.*s  e = %6ld, chars = %7ld, stat = %3u: %c %c %c %c\n"#else#define FMT "%-*.*s  e = %6ld, chars = %7ld, %c %c %c %c\n"#endif#ifndef HAS_ACORE#define ACORE 0#endif#ifndef HAS_AXSIG#define AXSIG 0#endifstatic unsigned longcompt2ulong(comp_t comptime)    /* convert comp_t to unsigned long */{    unsigned long   val;    int             exp;    val = comptime & 0x1fff;    /* 13-bit fraction */    exp = (comptime >> 13) & 7; /* 3-bit exponent (0-7) */    while (exp-- > 0)        val *= 8;    return(val);}intmain(int argc, char *argv[]){    struct acct     acdata;    FILE            *fp;    if (argc != 2)        err_quit("usage: pracct filename");    if ((fp = fopen(argv[1], "r")) == NULL)        err_sys("can't open %s", argv[1]);    while (fread(&acdata, sizeof(acdata), 1, fp) == 1) {        printf(FMT, (int)sizeof(acdata.ac_comm),            (int)sizeof(acdata.ac_comm), acdata.ac_comm,            compt2ulong(acdata.ac_etime), compt2ulong(acdata.ac_io),#ifdef HAS_SA_STAT            (unsigned char) acdata.ac_stat,#endif            acdata.ac_flag & ACORE ? 'D' : ' ',            acdata.ac_flag & AXSIG ? 'X' : ' ',            acdata.ac_flag & AFORK ? 'F' : ' ',            acdata.ac_flag & ASU   ? 'S' : ' ');    }    if (ferror(fp))        err_sys("read error");    exit(0);}

14.用户标识


?

?

热点排行