C 进程

/****

进程

***/

#include<sys/types.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
pid_t pid;
if( ( pid = fork() ) < 0 )
{
printf("fork error\n");
exit(1);
}
else if( pid == 0 )
{
printf("in the child process:%d\n", pid);
}
else
{
printf("in the parent process:%d\n", pid);
}
exit(0);

}

/****

wait()函数

****/

#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
void exit_s(int status)
{
if( WIFEXITED( status ) )
{
printf("normal exit,status=%d\n",WEXITSTATUS(status));
}
else if( WIFSIGNALED( status ) )
{
printf("signal exit!status=%d\n",WTERMSIG(status));
}
}
int main()
{
pid_t pid,pid1;
int status;
if( ( pid = fork() ) < 0)
{
printf("child process error!\n");
exit(0);
}
else if( pid == 0 )
{
printf("this is child process!\n");
exit(0);
}
if(wait(&status) != pid )
{
printf("this is a parent process!\nwait error!\n");
exit(0);
}
exit_s(status);
if( ( pid = fork() ) < 0)
{
printf("child process error!\n");
exit(0);
}
else if( pid == 0 )
{
printf("this is child process!\n");
pid1 = getpid();
kill( pid1,19);
}
if(wait(&status) != pid )
{
printf("this is a parent process!\nwait error!\n");
exit(0);
}
exit_s(status);
exit(0);
}

posted on 2016-10-26 10:51  魄灠  阅读(214)  评论(0)    收藏  举报