简体   繁体   English

C分叉不起作用

[英]C forking doesn't work

I'm trying to make a server where it listens to 2 ports and does something with the incoming data, while printing out a dot every second to let people know it's still running. 我正在尝试制作一个服务器,在其中侦听2个端口并处理传入的数据,同时每秒打印一个点以使人们知道它仍在运行。 I want to do this by using fork() . 我想通过使用fork()做到这一点。 I'm trying: 我正在努力:

  p = fork();
  if(p == 0){
      if( getsockname( sock1, (struct sockaddr *) &name1, &length1) == -1 ) {
        perror( "getting socket name" );
        exit(3);
      }
      printf( "Socket port #%d\n", ntohs( name1.sin_port ) );

      if( getsockname( sock2, (struct sockaddr *) &name2, &length2) == -1 ) {
        perror( "getting socket name" );
        exit(3);
      }
      printf( "Socket port #%d\n", ntohs( name2.sin_port ) );

  }
  else if(p > 0){
      printf("inhere"); //
      p1 = fork();
          if(p1 > 0) {
            while(TRUE) {
               write(".");
               sleep(1);
            }
          }
...

This doesn't even print the inhere string in the child process. 这甚至不打印子进程中的inhere字符串。 What am I doing wrong? 我究竟做错了什么?

The child process gets a 0 as the return value. 子进程的返回值为0。 You might call an fflush(0) right after the printf call and see if it helps. 您可以在printf调用之后立即调用fflush(0),看看是否有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM