#include
#include
#include
#include
int main()
{
int n;
int exit_code;
char *message;
pid_t new_pid;
new_pid = fork();
printf("fork is starting...............\n");
switch(new_pid)
{
case -1: /* Error */
perror("fork failed.\n");
exit(1);
case 0: /* This is child */
message = "This is a child thread.";
n = 3;
exit_code = 37;
break;
default: /* This is parent */
message = "This is a parent thread.";
n = 5;
exit_code = 0;
break;
}
for(;n>0;n--)
{
puts(message);
sleep(1);
}
if(new_pid !=0)
{
int stat_val;
pid_t child_pid;
child_pid = wait(&stat_val);
printf("Child has finished: PID = %d\n",child_pid);
if(WIFEXITED(stat_val))
printf("Child exited with code %d\n",WEXITSTATUS(stat_val));
else
printf("Child terminated abnormally\n");
}
exit(exit_code);
}
阅读(1082) | 评论(0) | 转发(0) |