简体   繁体   中英

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. I want to do this by using 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. What am I doing wrong?

The child process gets a 0 as the return value. You might call an fflush(0) right after the printf call and see if it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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