diff --git a/8/fork/echoarg.c b/8/fork/echoarg.c new file mode 100644 index 0000000..f299c4d --- /dev/null +++ b/8/fork/echoarg.c @@ -0,0 +1,8 @@ +#include + +int main(int argc, char *argv[]) { + for(int i = 0; i < argc; i++) { + printf("Arg[%d] = %s\n", i, argv[i]); + } + return 0; +} diff --git a/8/fork/exec.c b/8/fork/exec.c new file mode 100644 index 0000000..2df5528 --- /dev/null +++ b/8/fork/exec.c @@ -0,0 +1,10 @@ +#include +#include + +int main(void) { + printf("exec fork.c -> b.out\n"); + if(execl("./b.out", NULL) < 0) { + printf("execl error\n"); + } + return 0; +} diff --git a/8/fork/exec2.c b/8/fork/exec2.c new file mode 100644 index 0000000..3c0c901 --- /dev/null +++ b/8/fork/exec2.c @@ -0,0 +1,9 @@ +#include +#include + +int main(void) { + if(execl("./itptfile", "tools", "yeah", ".", NULL) < 0) { + printf("exec error\n"); + } + return 0; +} diff --git a/8/fork/fork.c b/8/fork/fork.c new file mode 100644 index 0000000..55631b3 --- /dev/null +++ b/8/fork/fork.c @@ -0,0 +1,29 @@ +#include +#include +#include +#include + +int main(void) { + pid_t pid = fork(); + + if (pid != 0) { + pid_t pidp = getpid(); + pid_t ppidp = getpid(); + uid_t uidp = getuid(), euidp = geteuid(); + gid_t gidp = getgid(), egidp = getegid(); + printf("Father Process PID %d PPID %d UID %d EUID %d GID %d EGID %d\n", pidp, ppidp, uidp, euidp, gidp, egidp); + + int status; + pid_t pidc = wait(&status); + printf("Child Process PID %d Exited, status %d.\n", pidc, status); + } + else { + pid_t pidc = getpid(); + pid_t ppidc = getpid(); + uid_t uidc = getuid(), euidc = geteuid(); + gid_t gidc = getgid(), egidc = getegid(); + printf("Child Process PID %d PPID %d UID %d EUID %d GID %d EGID %d\n", pidc, ppidc, uidc, euidc, gidc, egidc); + exit(0); + } + return 0; +} diff --git a/8/fork/getlogin.c b/8/fork/getlogin.c new file mode 100644 index 0000000..f87b17d --- /dev/null +++ b/8/fork/getlogin.c @@ -0,0 +1,10 @@ +#include +#include + +int main(void) { + char *name = getlogin(); + + printf("%s\n", name); + + return 0; +} diff --git a/8/fork/itptfile b/8/fork/itptfile new file mode 100755 index 0000000..1434ad1 --- /dev/null +++ b/8/fork/itptfile @@ -0,0 +1 @@ +#! /home/eric/test/echoarg foo bar diff --git a/8/fork/nice.c b/8/fork/nice.c new file mode 100644 index 0000000..793ef70 --- /dev/null +++ b/8/fork/nice.c @@ -0,0 +1,25 @@ +#include +#include +#include + +int main(void) { + + printf("NZERO: %d\n", sysconf(_SC_NZERO)); + + if(nice(16) < 0) { + printf("nice error\n"); + return -1; + } + + int prior = getpriority(PRIO_PROCESS, 0); + + if(prior < 0) { + printf("getpriority error\n"); + return -1; + } + + printf("priority: %d\n", prior); + + + return 0; +} diff --git a/8/fork/times.c b/8/fork/times.c new file mode 100644 index 0000000..de75284 --- /dev/null +++ b/8/fork/times.c @@ -0,0 +1,19 @@ +#include +#include +#include + +int main(void) { + struct tms buf; + + for(int i = 0, x = 1; i < 1e8; i++) x*=2; + + if(times(&buf) < 0) { + printf("times error\n"); + return -1; + } + + printf("utime: %ld stime: %ld cutime: %ld cstime: %ld\n", buf.tms_utime, buf.tms_cutime, buf.tms_cutime, buf.tms_cstime); + + printf("clk_tck: %d\n", sysconf(_SC_CLK_TCK)); + return 0; +} diff --git a/8/fork/wait.c b/8/fork/wait.c new file mode 100644 index 0000000..0fa2e33 --- /dev/null +++ b/8/fork/wait.c @@ -0,0 +1,37 @@ +#include +#include +#include +#include + +int main(void) { + pid_t pid = fork(); + + if(pid != 0){ + + pid = fork(); + + if(pid != 0) { + int status; + pid_t pidr = waitpid(pid, &status, 0); + + printf("WPID %d %d RTN %d\n", pid, pidr, status); + + if(WIFEXITED(status)){ + printf("Child Process Exited Status %d.\n", WEXITSTATUS(status)); + } + + exit(0); + + } + else { + printf("CPID: %d\n", getpid()); + exit(2); + } + + } + else { + printf("CPID: %d\n", getpid()); + exit(1); + } + +}