开发者

how can i see output in terminal for a process generated by fork()

I wrote code for concurrent server. I want to see output in terminal for child server socket that is generated by fork() call. Client is communicating with child server socket and i want to see data written by client on server. So how can i see it in terminal?

my server code:

while(1)
     {
      clilen=sizeof(cliaddr);
      connfd=accept(sockfd,(struct sockaddr*)&cliaddr,&clilen);
      printf("connection accepted\n");
       if((childpid=fork())==0)
          {
            close(sockfd);
            printf("child process\n");
            str_echo(connfd);
           // str_echo(connfd);
            exit(0);
          }
     printf("connection established\n");

str_echo()

void str_echo(int sockfd)
    {
      char buff[20];
      ssize_t n;
      while(1)
          {
           if((n=read(sockfd,buff,20))>=0)
              write(sockfd,buff,20);
           else
              write(sockfd,"blank",20);
           //else
             printf("%s",buff);
             // write(sockfd,buff,20);
              //return;
           //else
             // 开发者_JAVA技巧writen(sockfd,buff,n);
           }
     }

but i am not getting output for above printf("%s",buff); in str_echo() in terminal


Usually a forked() process shares its parents file descriptors, in particular it should have the same stdout. So I think that you should be able to see the output without doing any special setup.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜