【问题标题】:multi-process in C : global variable's valueC中的多进程:全局变量的值
【发布时间】:2015-10-14 00:53:05
【问题描述】:

这是来自网站的代码。它使用多处理来创建服务器。我的问题是:父进程 close(newsockfd) 会在子进程 doprocessing(newsockfd) 之前执行吗?

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

#include <netdb.h>
#include <netinet/in.h>

#include <string.h>

void doprocessing (int sock);

int main( int argc, char *argv[] ) {
   int sockfd, newsockfd, portno, clilen;
   char buffer[256];
   struct sockaddr_in serv_addr, cli_addr;
   int n, pid;

   /* First call to socket() function */
   sockfd = socket(AF_INET, SOCK_STREAM, 0);

   if (sockfd < 0) {
      perror("ERROR opening socket");
      exit(1);
   }

   /* Initialize socket structure */
   bzero((char *) &serv_addr, sizeof(serv_addr));
   portno = 5001;

   serv_addr.sin_family = AF_INET;
   serv_addr.sin_addr.s_addr = INADDR_ANY;
   serv_addr.sin_port = htons(portno);

   /* Now bind the host address using bind() call.*/
   if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
      perror("ERROR on binding");
      exit(1);
   }

   /* Now start listening for the clients, here
      * process will go in sleep mode and will wait
      * for the incoming connection
   */

   listen(sockfd,5);
   clilen = sizeof(cli_addr);

   while (1) {
      newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);

      if (newsockfd < 0) {
         perror("ERROR on accept");
         exit(1);
      }

      /* Create child process */
      pid = fork();

      if (pid < 0) {
         perror("ERROR on fork");
         exit(1);
      }

      if (pid == 0) {
         /* This is the client process */
         close(sockfd);
         doprocessing(newsockfd);
         exit(0);
      }
      else {
         close(newsockfd);
      }

   } /* end of while */
}

===-=================
void doprocessing (int sock) {
   int n;
   char buffer[256];
   bzero(buffer,256);
   n = read(sock,buffer,255);

   if (n < 0) {
      perror("ERROR reading from socket");
      exit(1);
   }

   printf("Here is the message: %s\n",buffer);
   n = write(sock,"I got your message",18);

   if (n < 0) {
      perror("ERROR writing to socket");
      exit(1);
   }

}

==========-===========================

代码来自这个网站: http://www.tutorialspoint.com/unix_sockets/socket_server_example.htm

【问题讨论】:

    标签: c sockets multiprocessing


    【解决方案1】:

    当进程被分叉时,它会获取打开的文件描述符集的副本,并且打开文件的引用计数会相应增加。 close 只发生在父进程中,所以子进程仍然保持对打开文件的引用。执行顺序并不重要。在多 CPU 系统上,它可能真正同时发生。

    全局变量被分叉的子进程共享。分叉的进程在自己的虚拟内存空间中运行。

    【讨论】:

    • 应该是这样的: if (pid == 0) { /* 这是客户端进程 / close(sockfd);处理(newsockfd); / 我添加了这个关闭语句-> */ close(newsockfd);退出(0); } 其他 { 关闭(newsockfd); }
    • 文件描述符是指针吗?为什么定义为整数?
    • @user2373043 - 文件描述符是一个整数。您正在考虑 FILE 指针。
    • @alvits,正如 Jonathon Reinhart 所说:“当进程被分叉时,获取打开的文件描述符集的副本,并且打开文件的引用计数相应地增加。”那么怎么来一个整数变量newsockfd可以有参考吗? newsockfd 是一个整数,而不是一个指针。
    • 当一个子进程被分叉时,子进程将拥有父进程所有内容的完整副本。这是一个完整的副本,而不是指向父级的指针。对父级变量(包括文件描述符)的任何更改都是本地的,不会影响子级。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-30
    • 1970-01-01
    • 2010-12-20
    • 2017-06-30
    • 1970-01-01
    • 1970-01-01
    • 2015-12-16
    相关资源
    最近更新 更多