Skip to content

Commit 96f33a1

Browse files
committed
study
1 parent 5284a2c commit 96f33a1

File tree

6 files changed

+315
-0
lines changed

6 files changed

+315
-0
lines changed

8.13.c

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <errno.h>
4+
#include <unistd.h>
5+
#include <sys/wait.h>
6+
7+
int system(const char *cmdstring)
8+
{
9+
pid_t pid;
10+
int status;
11+
12+
if (NULL == cmdstring) {
13+
return 1;
14+
}
15+
16+
if ((pid = fork()) < 0) {
17+
status = -1;
18+
} else if (0 == pid) {
19+
execl("/bin", "sh", "-C", cmdstring, (char *)0);
20+
_exit(127);
21+
} else {
22+
while (waitpid(pid, &status, 0) < 0) {
23+
if (errno != EINTR) {
24+
status = -1;
25+
break;
26+
}
27+
}
28+
}
29+
30+
return status;
31+
}
32+
33+
void pr_exit(int status)
34+
{
35+
if (WIFEXITED(status)) {
36+
printf("normal termination, exit status = %d\n", WEXITSTATUS(status));
37+
} else if (WIFSIGNALED(status)) {
38+
printf("abnormal termination, signal number = %d%s\n",-
39+
WTERMSIG(status),
40+
#ifdef WCOREDUMP
41+
WCOREDUMP(status) ? " (core file generated)" : ""
42+
#else-
43+
""
44+
#endif
45+
);
46+
} else if (WIFSTOPPED(status)) {
47+
printf("child stopped, signal number = %d\n", WSTOPSIG(status));
48+
}
49+
}
50+
51+
int main(void)
52+
{
53+
int status;
54+
55+
if ((status = system("date")) < 0) {
56+
printf("system() error\n");
57+
exit(-1);
58+
}
59+
60+
pr_exit(status);
61+
62+
if ((status = system("nosuchcommand")) < 0) {
63+
printf("system() error\n");
64+
exit(-1);
65+
}
66+
67+
pr_exit(status);
68+
69+
if ((status = system("who; exit 44")) < 0) {
70+
printf("system() error\n");
71+
exit(-1);
72+
}
73+
74+
pr_exit(status);
75+
76+
return 0;
77+
}

8.18.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <unistd.h>
4+
#include <sys/times.h>
5+
6+
static void pr_times(clock_t, struct tms *, struct tms *);
7+
static void do_cmd(char *);
8+
9+
int main(int argc, char **argv)
10+
{
11+
int i;
12+
setbuf(stdout, NULL);
13+
for (i = 1; i < argc; i++) {
14+
//printf("%s\n", *(argv + i));
15+
do_cmd(*(argv + i));
16+
}
17+
18+
return 0;
19+
}
20+
21+
static void do_cmd(char *cmd)
22+
{
23+
struct tms tmsstart, tmsend;
24+
clock_t start, end;
25+
int status;
26+
27+
printf("\ncommand: %s\n", cmd);
28+
29+
if ((start = time(&tmsstart)) == -1) {
30+
printf("times error\n");
31+
exit(-1);
32+
}
33+
34+
if ((status = system(cmd)) < 0) {
35+
printf("system() error\n");
36+
exit(-1);
37+
}
38+
39+
if ((end = times(&tmsend)) == -1) {
40+
printf("times error\n");
41+
exit(-1);
42+
}
43+
44+
pr_times(end - start, &tmsstart, &tmsend);
45+
}
46+
47+
static void pr_times(clock_t real, struct tms *tmsstart, struct tms *tmsend)
48+
{
49+
static long clktck = 0;
50+
if (0 == clktck) {
51+
if (0 > (clktck = sysconf(_SC_CLK_TCK))) {
52+
printf("sysconf error\n");
53+
exit(-1);
54+
}
55+
}
56+
57+
printf("\t real: %7.2f\n", real / (double) clktck);
58+
printf("\t user: %7.2f\n", (tmsend->tms_utime - tmsstart->tms_utime) / (double) clktck);
59+
printf("\t sys: %7.2f\n", (tmsend->tms_stime - tmsstart->tms_stime) / (double) clktck);
60+
printf("\t child user: %7.2f\n", (tmsend->tms_cutime - tmsstart->tms_cutime) / (double) clktck);
61+
printf("\t child sys: %7.2f\n", (tmsend->tms_cstime - tmsstart->tms_cstime) / (double) clktck);
62+
}

8.4.c

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <sys/wait.h>
4+
5+
void pr_exit(int status)
6+
{
7+
if (WIFEXITED(status)) {
8+
printf("normal termination, exit status = %d\n", WEXITSTATUS(status));
9+
} else if (WIFSIGNALED(status)) {
10+
printf("abnormal termination, signal number = %d%s\n",
11+
WTERMSIG(status),
12+
#ifdef WCOREDUMP
13+
WCOREDUMP(status) ? " (core file generated)" : ""
14+
#else
15+
""
16+
#endif
17+
18+
);
19+
} else if (WIFSTOPPED(status)) {
20+
printf("child stopped, signal number = %d\n", WSTOPSIG(status));
21+
}
22+
}
23+
24+
int main(void)
25+
{
26+
pid_t pid;
27+
int status;
28+
29+
if (0 > (pid = fork())) {
30+
printf("fork error\n");
31+
exit(-1);
32+
} else if (0 == pid) {
33+
exit(7);
34+
}
35+
36+
if (pid != wait(&status)) {
37+
printf("wait error\n");
38+
exit(-1);
39+
}
40+
41+
pr_exit(status);
42+
43+
if (0 > (pid = fork())) {
44+
printf("fork error\n");
45+
exit(-1);
46+
} else if (0 == pid) {
47+
abort();
48+
}
49+
50+
if (pid != wait(&status)) {
51+
printf("wait error\n");
52+
exit(-1);
53+
}
54+
55+
pr_exit(status);
56+
57+
58+
if (0 > (pid = fork())) {
59+
printf("fork error\n");
60+
exit(-1);
61+
} else if (0 == pid) {
62+
status /= 0;
63+
}
64+
65+
if (pid != wait(&status)) {
66+
printf("wait error\n");
67+
exit(-1);
68+
}
69+
70+
pr_exit(status);
71+
72+
return 0;
73+
}

8.5.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <sys/wait.h>
4+
5+
int main(void)
6+
{
7+
pid_t pid;
8+
9+
if (0 > (pid = fork())) {
10+
printf("fork error\n");
11+
exit(-1);
12+
} else if (0 == pid) {
13+
if (0 > (pid = fork())) {
14+
printf("fork error\n");
15+
exit(-1);
16+
} else if (pid > 0) {
17+
exit(0);
18+
}
19+
20+
sleep(2);
21+
printf("second child, parent pid = %d\n", getppid());
22+
exit(0);
23+
}
24+
25+
if (pid != waitpid(pid, NULL, 0)) {
26+
printf("waitpid error\n");
27+
}
28+
29+
exit(0);
30+
}

8.6.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
static void charatatime(char *str)
5+
{
6+
char *ptr;
7+
int c;
8+
9+
setbuf(stdout, NULL);
10+
11+
for (ptr = str; (c = *ptr++) != 0;) {
12+
putc(c, stdout);
13+
}
14+
15+
}
16+
17+
int main(void)
18+
{
19+
pid_t pid;
20+
21+
if ((pid = fork()) < 0) {
22+
printf("fork error\n");
23+
exit(-1);
24+
} else if (0 == pid) {
25+
charatatime("output from child\n");
26+
} else {
27+
charatatime("output from parent\n");
28+
}
29+
30+
return 0;
31+
}

apue.9.1.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <errno.h>
4+
#include <signal.h>
5+
#include <unistd.h>
6+
7+
8+
static void sig_hup(int signo)
9+
{
10+
printf("SIGHUP received, pid = %d\n", getpid());
11+
}
12+
13+
static void pr_ids(char *name)
14+
{
15+
printf("%s: pid = %d, ppid = %d, pgrd = %d, tpgrd = %d\n", name, getpid(), getppid(), getpgrp(), tcgetpgrp(STDIN_FILENO));
16+
fflush(stdout);
17+
}
18+
19+
int main(void)
20+
{
21+
char c;
22+
pid_t pid;
23+
24+
pr_ids("parent");
25+
if ((pid = fork()) < 0) {
26+
printf("fork error\n");
27+
exit(-1);
28+
} else if (pid > 0) {
29+
sleep(5);
30+
exit(0);
31+
} else {
32+
pr_ids("child");
33+
signal(SIGHUP, sig_hup);
34+
kill(getpid(), SIGTSTP);
35+
pr_ids("child");
36+
if (read(STDIN_FILENO, &c, 1) != 1 ) {
37+
printf("read error from controlling TTY, errno = %d\n", errno);
38+
}
39+
40+
exit(0);
41+
}
42+
}

0 commit comments

Comments
 (0)