论坛首页 编程语言技术论坛

C: Linux C 编程 - fork

浏览 475 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2020-08-26  

fork

fork是和进程相关的一个概念。在程序中可以通过fork可以创建一个进程。

 

这样进程就存在父子关系。创建子进程的进程为父进程,被创建的进程为子进程。

 

先从一个最简单的fork子进程例子开始:

#include <stdio.h>
#include <unistd.h>
void main() 
{
  pid_t pid = fork();
  if (pid == 0)
  {
	printf("child process.\n");
  }
  else 
  {
    printf("parent process.\n");
  }
}

在上面这个最简单的fork子进程例子中,fork是一个系统调用,fork函数调用后将创建一个子进程。从上面的例子可以看出,想要创建一个子进程很简单,基本只需要几行代码。

 

核心代码:

 

pid_t pid = fork();
if (pid == 0) {
  printf("child process.\n");
} else {
  printf("parent process.\n");
}

 

 

#include <stdio.h>
#include <stdlib.h>

#include <sys/types.h>
#include <unistd.h>
#include <errno.h>

void fn() 
{
  int i;
  for (i = 0; i < 100; i++)
  {
	fprintf(stdout, "[ INFO][%04d][%04d] i=%d\n", getppid(), getpid(), i);
  }
}

void TRACE_FORK(pid) 
{
  #ifdef __TRACE_FORK   
    fprintf(stdout, "[ INFO][%04d][%04d] child process forked. pid: %d\n", getppid(), getpid(), pid);                     
  #else                 
    if (pid == 0)       
    {                   
      fprintf(stdout, "[ INFO][%04d][%04d] child process forked. in child process, pid: %d\n", getppid(), getpid(), pid); 
    }                   
    else                
    {                   
      fprintf(stdout, "[ INFO][%04d][%04d] child process forked. pid: %d\n", getppid(), getpid(), pid);                  
    }                  
  #endif        
}

void main() 
{
  pid_t pid;
  fprintf(stdout, "[ INFO][%04d][%04d] main\n", getppid(), getpid());

  pid = fork();
  if (pid == -1)
  {
	fprintf(stderr, "[ERROR][%04d][%04d] fork error, errno: %d\n", getppid(), getpid(), errno);
	return;
  }

  TRACE_FORK(pid);

  if (pid == 0)
  {
	// in child process
	fn();
    //exit(0);
  }
  else 
  {
    fn();
  }
}

 

fork

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <assert.h>

#include <sys/types.h>
#include <sys/socket.h>

#include <netinet/in.h>
#include <arpa/inet.h>

#include <unistd.h>
#include <fcntl.h>

typedef struct p_worker 
{
    pid_t pid;
    int index;	
} p_worker_t;


int stop = 0;

int max_worker_process_num = 32;


int _socket_setnonblock(int fd) 
{
    int flags = fcntl(fd, F_GETFL);
    if (flags == -1) 
    {
        printf("fcntl error %d\n", errno);
				return -1;
    }

    flags |= O_NONBLOCK;
    if (fcntl(fd, F_SETFL, flags) == -1)
    {
        printf("fcntl error %d\n", errno);
				return -1;
    }
    return 0;
}

int socket_setnonblock(int fd) 
{
    int flags = fcntl(fd, F_GETFL);
    if (flags == -1) 
    {
        printf("fcntl error %d\n", errno);
				return -1;
    }

    flags |= O_NONBLOCK;
    if (fcntl(fd, F_SETFL, flags) == -1)
    {
        printf("fcntl error %d\n", errno);
				return -1;
    }
    return 0;
}

int _socket(int domain, int type, int protocol)
{
    return socket(domain, type, protocol);
}

int socket_bind(int sock, unsigned short int port) 
{
    printf("bind to port %d\n", port);
    struct sockaddr_in sock_addr;
    bzero(&sock_addr, sizeof(struct sockaddr_in));
    sock_addr.sin_family = AF_INET;
    sock_addr.sin_addr.s_addr = htonl(INADDR_ANY);

    sock_addr.sin_port = htons(port);

    if(bind(sock, (struct sockaddr *) &sock_addr, sizeof(struct sockaddr)) == -1) 
    {
        printf("bind error %d\n", errno);
        return -1;
    }
    return 0;
}

int socket_listen(int sock)
{
    if (listen(sock, 50) == -1) 
    {
        printf("listen error %d\n", errno);
        return -1;
    }
    return 0;
}

void dump_fork(pid_t child_pid) 
{
	  pid_t parent = getppid();
    pid_t pid = getpid();
    printf("parent: %d, child: %d, pid: %d\n", parent, child_pid, pid);
}

int child(int argc, char** argv) 
{
	  printf("fork child process...\n");
    pid_t child_pid = fork();
    
    /*
    pid_t pid = getppid();
    pid_t process = getpid();
    printf("pid: %d, child: %d, process: \n", pid, child, process);
    */
    //*
    if (child_pid == 0) 
    {
    	  dump_fork(child_pid);
    } 
    else if (child_pid == -1)
    {
        dump_fork(child_pid);
    }
    else 
    {
        dump_fork(child_pid);
    }
    //*/
    return child_pid;
}

int start_acceptor_process(int argc, char** argv)
{
    return child(argc, argv);
}

void start_worker_process(int argc, char** argv, int *index, p_worker_t* w, int size)
{  
    pid_t child_pid = child(argc, argv);
    
    w[*index].pid = child_pid;
    w[*index].index = *index;
    
    //*
    if (child_pid == 0) 
    {
        dump_fork(child_pid);
    	  printf("worker process index: %d\n", *index);
    	  while (1) 
    	  {
    	      // loop to handle request...	
    	  }
    	  return;
    } 
    else if (child_pid == -1)
    {
        dump_fork(child_pid);
        printf(">worker process index: %d\n", *index);
        return;
    }
    else 
    {
        dump_fork(child_pid);
        printf(">>worker process index: %d\n", *index);
        if (*index < size) 
        {
        	  *index = *index + 1;
        	  start_worker_process(argc, argv, index, w, size);
        }
    }
    //*/
}

int main(int argc, char** argv) 
{
	  int child_pid = 0;
    int fd = -1;
    
    pid_t pid = getpid();
    pid_t parent = getppid();
    printf("pid: %d, parent: %d\n", pid, parent);
    
#ifdef USE_SOCK_NONBLOCK
    fd = _socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
#else
		fd = _socket(AF_INET, SOCK_STREAM, 0);
		if (fd == -1) 
		{
		    printf("socket error %d\n", errno);
    	  return 1;
		}
		else 
		{
		    printf("socket ok, socket: %d\n", fd);
		}

    socket_setnonblock(fd);
#endif

    if (socket_bind(fd, 2121) == -1)
    {
        return -1;
    }
    
    if (socket_listen(fd) == -1) 
    {
				return -1;
    }
    
    child_pid = start_acceptor_process(argc, argv);
    
    //*
    if (child_pid == 0) 
    {
        int i = 0;
    	  p_worker_t *w = (p_worker_t *) malloc(max_worker_process_num * sizeof(p_worker_t));
    	  
    	  
        dump_fork(child_pid);
        printf("acceptor process started...\n");
        
        start_worker_process(argc, argv, &i, w, max_worker_process_num);
    } 
    else if (child_pid == -1)
    {
        dump_fork(child_pid);
    }
    else 
    {
    		//*
    	  int new_fd = -1;
        struct sockaddr sock_addr;
    	  int addrlen = sizeof(struct sockaddr);
    	  //*/
    	  
    	  
    	  
        dump_fork(child_pid);
        
        
        //*
        printf("accepting connection...\n");
        while (1) 
        {  
        	  //printf("server socket: %d\n", fd);
            new_fd = accept(fd, &sock_addr, &addrlen);
            if (new_fd == -1) 
            {
            	  if (errno == EWOULDBLOCK) 
            	  {
            	      
            	  }
            	  else 
            	  {
                    printf("accept error %d\n", errno);
              	}
                continue;
            }
            else 
            {
                printf("accept new connection from %d\n", new_fd);
            }
      	}
      	//*/
    }
    //*/
    
    printf("started...\n");
}

 

论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics