linux实验二---fork()的用法,产生子进程

#include
#define MASKVALH 0x0000ff00
#define MASKVALL 0x000000ff
#define HBYTE(x)  ((x & MASKVALH)>>8)
#define LBYTE(x)   (x & MASKVALL)

main()
{
	int getpid(),getppid(),pid,status;
	puts("Before fork system call");
	printf("The parent pid of process %d is [%d]\n",getpid(),getppid());
	if((pid=fork())==-1)
	{
		perror("fork");
		exit(1);
	}
	else if(pid==0)
    {
		puts("Child process after fork system call");
		printf("The parent pid of process %d is [%d]",
			getpid(),getppid());
		exit(1);
    }
	else
    {
		wait(&status);
		puts("Parent process after fork system call");
		printf("The parent pid of process %d is [%d]\n",
			getpid(),getppid());
		printf("The child pid of process %d is [%d]\n",
			getpid(),pid);
		if(LBYTE(status)==0)
			printf("The child's exit code is %d\n",
			HBYTE(status));
    }
	exit(0);
}                                  


你可能感兴趣的:(Unix高级编程,linux)