8 finished.
This commit is contained in:
parent
1cae79f533
commit
c9fbf46d4b
8
8/fork/echoarg.c
Normal file
8
8/fork/echoarg.c
Normal file
@ -0,0 +1,8 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
for(int i = 0; i < argc; i++) {
|
||||
printf("Arg[%d] = %s\n", i, argv[i]);
|
||||
}
|
||||
return 0;
|
||||
}
|
10
8/fork/exec.c
Normal file
10
8/fork/exec.c
Normal file
@ -0,0 +1,10 @@
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void) {
|
||||
printf("exec fork.c -> b.out\n");
|
||||
if(execl("./b.out", NULL) < 0) {
|
||||
printf("execl error\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
9
8/fork/exec2.c
Normal file
9
8/fork/exec2.c
Normal file
@ -0,0 +1,9 @@
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void) {
|
||||
if(execl("./itptfile", "tools", "yeah", ".", NULL) < 0) {
|
||||
printf("exec error\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
29
8/fork/fork.c
Normal file
29
8/fork/fork.c
Normal file
@ -0,0 +1,29 @@
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/wait.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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;
|
||||
}
|
10
8/fork/getlogin.c
Normal file
10
8/fork/getlogin.c
Normal file
@ -0,0 +1,10 @@
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void) {
|
||||
char *name = getlogin();
|
||||
|
||||
printf("%s\n", name);
|
||||
|
||||
return 0;
|
||||
}
|
1
8/fork/itptfile
Executable file
1
8/fork/itptfile
Executable file
@ -0,0 +1 @@
|
||||
#! /home/eric/test/echoarg foo bar
|
25
8/fork/nice.c
Normal file
25
8/fork/nice.c
Normal file
@ -0,0 +1,25 @@
|
||||
#include <unistd.h>
|
||||
#include <sys/resource.h>
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
19
8/fork/times.c
Normal file
19
8/fork/times.c
Normal file
@ -0,0 +1,19 @@
|
||||
#include <sys/times.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
37
8/fork/wait.c
Normal file
37
8/fork/wait.c
Normal file
@ -0,0 +1,37 @@
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user