main.7
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
system("ls -l");
printf("end!
");
return 0;
}
main8.c
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char * env[] = {"PATH=/bin", 0};
char * argv[] = {"ls", "-l", 0};
printf("ready...
");
//printf("****************execl**************
");
//execl("ls", "ls", "-l", 0);
printf("****************execlp**************
");
execlp("ls", "ls", "-l", 0);
//printf("****************execle**************
");
//execle("/bin/ls", "ls", "-l", 0, env);
//printf("****************execv**************
");
//execv("/bin/ls", argv);
//printf("****************execvp**************
");
//execvp("ls", argv);
//printf("****************execve**************
");
//execve("/bin/ls", argv, env);
printf("end!
");
return 0;
}
main9.c
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int pid;
char *msg;
int n;
printf("father process begin...
");
pid = fork();
if (pid == -1) {
printf("fork error!
");
exit(1);
} else if (pid == 0) {
printf("In child process!
");
msg = "child process ";
n = 2;
} else {
printf("In father process!
");
msg = "father process ";
n = 1;
}
while(n--) {
printf("%s
", msg);
sleep(1);
}
printf("%s end!
", msg);
return 0;
}
main10.c
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int pid;
char *msg;
int status;
int child_pid;
int n;
printf("father process begin...
");
pid = fork();
if (pid == -1) {
printf("fork error!
");
exit(1);
} else if (pid == 0) {
printf("In child process!
");
msg = "child process ";
n = 3;
} else {
printf("In father process!
");
msg = "father process ";
n = 1;
}
while(n--) {
printf("%s
", msg);
sleep(1);
}
if (pid) {
printf("father process wait...
");
child_pid = wait(&status);
if (WIFEXITED(status)) {
printf("Wait finished and child process terminated normally, child pid = %d
", child_pid);
} else {
printf("Wait finished and child process terminated unnormally, child pid = %d
", child_pid);
}
}
printf("%s end!
", msg);
return 0;
}
main11.c
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int pid;
char *msg;
int status;
int child_pid;
int n;
printf("father process begin...
");
pid = fork();
if (pid == -1) {
printf("fork error!
");
exit(1);
} else if (pid == 0) {
printf("In child process!
");
msg = "child process ";
n = 1;
} else {
printf("In father process!
");
msg = "father process ";
n = 10;
}
while(n--) {
printf("%s
", msg);
sleep(5);
}
if (pid) {
printf("father process wait...
");
child_pid = wait(&status);
if (WIFEXITED(status)) {
printf("Wait finished and child process terminated normally, child pid = %d
", child_pid);
} else {
printf("Wait finished and child process terminated unnormally, child pid = %d
", child_pid);
}
}
printf("%s end!
", msg);
return 0;
}
main12.c
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int pid;
int i;
for (i=0; i<10; i++) {
pid = fork();
if (pid == -1) {
perror("fork error!
");
exit(1);
} else if (pid == 0) {
//printf("Start run a new child pid, pid=%d
", getpid());
} else {
//printf("Add a new child, pid = %d
", pid);
}
}
printf("My pid = %d
", getpid());
return 0;
}