进程控制
#include <apue.h>
#include <error.h>
int globvar = 6;
char buf[] = "a write to stdout\n";
int main(void) {
int var;
pid_t pid;
var = 88;
if (write(STDOUT_FILENO, buf, sizeof(buf) - 1) != sizeof(buf) - 1)
err_sys("write error");
printf("before fork\n");
if ((pid = fork()) < 0) {
err_sys("fork error");
} else if (pid == 0) {
globvar++;
var++;
} else {
sleep(2);
}
printf("pid = %ld, glob = %d, var = %d\n", (long) getpid(), globvar, var);
exit(0);
}
#include <apue.h>
#include <error.h>
int globvar = 6;
char buf[] = "a write to stdout\n";
int main(void) {
int var;
pid_t pid;
var = 88;
if (write(STDOUT_FILENO, buf, sizeof(buf) - 1) != sizeof(buf) - 1)
err_sys("write error");
printf("before vfork\n");
if ((pid = vfork()) < 0) {
err_sys("fork error");
} else if (pid == 0) {
globvar++;
var++;
_exit(0);
}
printf("pid = %ld, glob = %d, var = %d\n", (long) getpid(), globvar, var);
exit(0);
}
#include <apue.h>
#include <error.h>
#include <sys/wait.h>
void pr_exit(int status) {
if (WIFEXITED(status))
printf("normal termination, exit status = %d\n", WEXITSTATUS(status));
else if (WIFSIGNALED(status))
printf("abnormal termination, signal number = %d%s\n", WTERMSIG(status),
#ifdef WCOREDUMP
WCOREDUMP(status) ? " (core file generated)" : "");
#else
"");
#endif
else if (WIFSTOPPED(status))
printf("child stopped, signal number = %d\n", WSTOPSIG(status));
}
int main(void) {
pid_t pid;
int status;
if ((pid = fork()) < 0)
err_sys("fork error");
else if (pid == 0)
exit(7);
if (wait(&status) != pid)
err_sys("wait error\n");
pr_exit(status);
if ((pid = fork()) < 0)
err_sys("fork error");
else if (pid == 0) {
status /= 0;
}
if (wait(&status) != pid)
err_sys("wait error");
pr_exit(status);
exit(0);
}
#include <apue.h>
#include <error.h>
#include <sys/wait.h>
int main(void) {
pid_t pid;
if ((pid = fork()) < 0) {
err_sys("fork error");
} else if (pid == 0) {
if ((pid = fork()) < 0)
err_sys("fork error");
else if (pid > 0) {
printf("first child pid is: %d\n", getpid());
printf("first child, parent pid = %ld\n", (long) getppid());
exit(0);
}else{
printf("second child, parent pid = %ld\n", (long) getppid());
}
sleep(2);
printf("second child pid is: %d\n", getpid());
printf("second child, parent pid = %ld\n", (long) getppid());
exit(0);
}
printf("current pid is: %d\n", getpid());
if (waitpid(pid, NULL, 0) != pid)
err_sys("waitpid error");
sleep(2);
exit(0);
}
#include <apue.h>
#include <error.h>
static void charatatime(char *);
int main(void) {
pid_t pid;
if ((pid = fork()) < 0)
err_sys("fork error");
else if (pid == 0)
charatatime("output from child\n");
else
charatatime("output from parent\n");
exit(0);
}
static void charatatime(char *str) {
char *ptr;
int c;
setbuf(stdout, NULL);
for (ptr = str; (c = *ptr++) != 0;)
putc(c, stdout);
}
#include <apue.h>
#include <error.h>
#include <tellwait.h>
static void charatatime(char *);
int main(void) {
pid_t pid;
if ((pid = fork()) < 0)
err_sys("fork error");
else if (pid == 0) {
// WAIT_PARENT();
charatatime("output from child\n");
TELL_PARENT(getppid());
} else {
WAIT_CHILD();
charatatime("output from parent\n");
// TELL_CHILD(pid);
}
exit(0);
}
static void charatatime(char *str) {
char *ptr;
int c;
setbuf(stdout, NULL);
for (ptr = str; (c = *ptr++) != 0;)
putc(c, stdout);
}
//tellwait.h
#include "apue.h"
static volatile sig_atomic_t sigflag; /* set nonzero by sig handler */
static sigset_t newmask, oldmask, zeromask;
static void sig_usr(int signo) /* one signal handler for SIGUSR1 and SIGUSR2 */
{
sigflag = 1;
}
void TELL_WAIT(void)
{
if (signal(SIGUSR1, sig_usr) == SIG_ERR)
err_sys("signal(SIGUSR1) error");
if (signal(SIGUSR2, sig_usr) == SIG_ERR)
err_sys("signal(SIGUSR2) error");
sigemptyset(&zeromask);
sigemptyset(&newmask);
sigaddset(&newmask, SIGUSR1);
sigaddset(&newmask, SIGUSR2);
/* Block SIGUSR1 and SIGUSR2, and save current signal mask */
if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)
err_sys("SIG_BLOCK error");
}
void TELL_PARENT(pid_t pid)
{
kill(pid, SIGUSR2); /* tell parent we're done */
}
void WAIT_PARENT(void)
{
while (sigflag == 0)
sigsuspend(&zeromask); /* and wait for parent */
sigflag = 0;
/* Reset signal mask to original value */
if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
err_sys("SIG_SETMASK error");
}
void TELL_CHILD(pid_t pid)
{
kill(pid, SIGUSR1); /* tell child we're done */
}
void WAIT_CHILD(void)
{
while (sigflag == 0)
sigsuspend(&zeromask); /* and wait for child */
sigflag = 0;
/* Reset signal mask to original value */
if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
err_sys("SIG_SETMASK error");
}
#include <apue.h>
#include <error.h>
#include <sys/wait.h>
char *env_init[] = {"USER=unlnown", "PATH=/tmp", NULL};
int main(void) {
pid_t pid;
if ((pid = fork()) < 0)
err_sys("fork error");
else if (pid == 0)
if (execle("/Users/yuq/code/c_plus_plus/aupe3/build/7_proc_env/pro_env", "pro_env", "myarg1", "MY ARG2", (char *) 0, env_init) < 0)
err_sys("ececle error");
if (waitpid(pid, NULL, 0) < 0)
err_sys("wait error");
if ((pid = fork()) < 0)
err_sys("fork error");
else if (pid == 0)
if (execlp("pro_env", "pro_env", "only 1 arg", (char *) 0) < 0)
err_sys("execlp error");
exit(0);
}
#include <apue.h>
#include <error.h>
#include <sys/wait.h>
char *env_init[] = {"USER=unlnown", "PATH=/tmp", NULL};
int main(void) {
pid_t pid;
if ((pid = fork()) < 0)
err_sys("fork error");
else if (pid == 0)
if (execle("/Users/yuq/code/c_plus_plus/aupe3/build/7_proc_env/pro_env", "pro_env", "myarg1", "MY ARG2",
(char *) 0, env_init) < 0)
err_sys("ececle error");
if (waitpid(pid, NULL, 0) < 0)
err_sys("wait error");
if ((pid = fork()) < 0)
err_sys("fork error");
else if (pid == 0){
if (setenv("PATH", "/Users/yuq/code/c_plus_plus/aupe3/build/7_proc_env/", 1) < 0)
err_sys("set environment value error");
if (execlp("pro_env", "pro_env", "only 1 arg", (char *) 0) < 0)
err_sys("execlp error");
}
exit(0);
}
#include <apue.h>
#include <error.h>
#include <sys/wait.h>
#include <errno.h>
#include <unistd.h>
int system(const char *cmdstring) {
pid_t pid;
int status;
if (cmdstring == NULL) {
return 1;
}
if ((pid = fork()) < 0) {
status = -1;
} else if (pid == 0) {
execl("/bin/sh", "sh", "-c", cmdstring, (char *) 0);
_exit(127);
} else {
while (waitpid(pid, &status, 0) < 0) {
if (errno != EINTR) {
status = -1;
break;
}
}
}
return status;
}
int main(void) {
int status;
if ((status = system("date")) < 0) {
err_sys("system() error");
}
pr_exit(status);
if ((status = system("nosuchcommand")) < 0) {
err_sys("system() error");
}
pr_exit(status);
if ((status = system("who; exit 44")) < 0) {
err_sys("system() error");
}
pr_exit(status);
exit(0);
}
#include <apue.h>
#include <error.h>
int main(void) {
pid_t pid;
if ((pid = fork()) < 0) {
err_sys("fork error");
} else if (pid != 0) {
sleep(2);
exit(2);
}
if ((pid = fork()) < 0) {
err_sys("fork error");
} else if (pid != 0) {
sleep(4);
abort();
}
if ((pid = fork()) < 0) {
err_sys("fork error");
} else if (pid != 0) {
execl("/bin/dd", "dd", "if=/etc/passwd", "of=/dev/null", NULL);
exit(7);
}
if ((pid = fork()) < 0) {
err_sys("fork error");
} else if (pid != 0) {
sleep(8);
exit(0);
}
sleep(6);
kill(getpid(), SIGKILL);
exit(6);
}
#include <apue.h>
#include <error.h>
#include <sys/acct.h>
#if defined(BSD)
#define acct acctv2
#define ac_flag ac_trailer.ac_flag
#define FMT "%-*.*s e = %.0f, chars = %.0f, %c %c %c %c\n"
#elif defined(HAS_AC_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
#if !defined(HAS_ACORE)
#define ACORE 0
#endif
#if !defined(HAS_AXSIG)
#define AXSIG 0
#endif
#if !defined(BSD)
static unsigned long compt2ulong(comp_t comptime) {
unsigned long val;
int exp;
val = comptime & 0x1fff;
exp = (comptime >> 13) & 7;
while (exp--)
val *= 8;
return val;
}
#endif
int main(int argc, char *argv[]) {
struct acct acdata;
FILE *fp;
if (argc != 2)
err_quit("usage: acct 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,
#if defined(BSD)
acdata.ac_etime,acdata.ac_io,
#else
compt2ulong(acdata.ac_etime), compt2ulong(acdata.ac_io),
#endif
#if defined(HAS_AC_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);
}
#include <apue.h>
#include <error.h>
#include <sys/time.h>
#include <errno.h>
#if defined(MACOS)
#include <sys/syslimits.h>
#elif defined(SOLARIS)
#include <limits.h>
#elif defined(BSDLY)
#include <sys/param.h>
#endif
unsigned long long count;
struct timeval end;
void checktime(char *str) {
struct timeval tv;
gettimeofday(&tv, NULL);
if (tv.tv_sec >= end.tv_sec && tv.tv_usec >= end.tv_usec) {
printf("%s count = %lld\n", str, count);
exit(0);
}
}
int main(int argc, char *argv[]) {
pid_t pid;
char *s;
int nzero, ret;
int adj = 0;
setbuf(stdout, NULL);
#if defined(NZERO)
nzero = NZERO;
#elif defined(_SC_NZERO)
nzero=sysconf(_SC_NZERO);
#else
#error NZERO undefined
#endif
printf("NZERO = %d\n", nzero);
if (argc == 2)
adj = strtol(argv[1], NULL, 10);
gettimeofday(&end, NULL);
end.tv_sec += 10;
if ((pid = fork()) < 0)
err_sys("fork failed");
else if (pid == 0) {
s = "child ";
printf("currrent nice value in child is %d, adjusting by %d\n", nice(0) + nzero, adj);
errno = 0;
if ((ret = nice(adj)) == -1 && errno != -1)
err_sys("child set scheduling priority");
printf("now child nice value is %d\n", ret + nzero);
} else {
s = "parent";
printf("current nice value in parent is %d\n", nice(0) + nzero);
}
for (;;) {
if (++count == 0)
err_quit("%s counter wrap", s);
checktime(s);
}
}
#include <apue.h>
#include <error.h>
#include <sys/times.h>
static void pr_times(clock_t, struct tms *, struct tms *);
static void do_cmd(char *);
int main(int argc, char *argv[]) {
int i;
setbuf(stdout, NULL);
for (i = 1; i < argc; i++) {
do_cmd(argv[i]);
}
exit(0);
}
static void do_cmd(char *cmd) {
struct tms tmsstart, tmsend;
clock_t start, end;
int status;
printf("\ncommand: %s\n", cmd);
if ((start = times(&tmsstart)) == -1)
err_sys("times error");
if ((status = system(cmd)) < 0) {
err_sys("system() error");
}
if ((end = times(&tmsend)) == -1)
err_sys("times error");
pr_times(end - start, &tmsstart, &tmsend);
pr_exit(status);
}
static void pr_times(clock_t real, struct tms *tmsstart, struct tms *tmsend) {
static long clktck = 0;
if (clktck == 0) {
if ((clktck = sysconf(_SC_CLK_TCK)) < 0)
err_sys("sysconf error");
}
printf(" real: %7.2f\n", real / (double) clktck);
printf(" user: %7.2f\n", (tmsend->tms_utime - tmsstart->tms_utime) / (double) clktck);
printf(" sys: %7.2f\n", (tmsend->tms_stime - tmsstart->tms_stime) / (double) clktck);
printf(" child user: %7.2f\n", (tmsend->tms_cutime - tmsstart->tms_cutime) / (double) clktck);
printf(" child sys: %7.2f\n", (tmsend->tms_cstime - tmsstart->tms_cstime) / (double) clktck);
}
网友评论