pid_t fork(void);
#include <sys/types.h> /*提供类型 pid_t 的定义*/
#include <unistd.h>
pid_t new_pid;
new_pid = fork();
switch(new_pid) {
case -1: /* Error */
break;
case 0: /* We are child */
break;
default: /* We are parent */
break;
}
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
pid_t pid;
char *message;
int n;
printf("fork program starting\n");
pid = fork();
switch(pid)
{
case -1:
perror("fork failed");
exit(1);
case 0:
message = "This is the child";
n = 5;
break;
default:
message = "This is the parent";
n = 3;
break;
}
for(; n > 0; n--) {
puts(message);
sleep(1);
}
exit(0);
}
beyes@linux-beyes:~/C/call/fork> ./fork2.exe
fork program starting
This is the parent
This is the child
This is the parent
This is the child
This is the parent
This is the child
This is the child
beyes@linux-beyes:~/C/call/fork> This is the child
beyes@linux-beyes:~/C/call/fork> ./fork2.exe
fork program starting
This is the parent
This is the child
This is the parent
This is the child
This is the child
This is the parent
This is the child
beyes@linux-beyes:~/C/call/fork> This is the child
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |