1

I am developing a chat program in iOS. But I could not solve a problem related to threads. Firstly, I created a server socket handling multiple connections in a thread. My code is like that:

    - (void)viewDidLoad
    {
        [super viewDidLoad];

        [NSThread detachNewThreadSelector:@selector(aMethod:) toTarget:[self class] withObject:nil];
    }

    void doprocessing(int sock)
    {
       int n;

       char *buffer = malloc(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);
      }
   }

   +(void)aMethod:(id)param{

    int x;
    for(x=0;x<50;++x)
    {
    int sockfd, newsockfd, portno, clilen;
    char buffer[256];
    struct sockaddr_in serv_addr, cli_addr;
    int  n;

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

    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);

    if (bind(sockfd, (struct sockaddr *) &serv_addr,
             sizeof(serv_addr)) < 0)
    {
        perror("ERROR on binding");
        exit(1);
    }

    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);
        }

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

            close(sockfd);
            doprocessing(newsockfd);
            exit(0);
        }
        else
        {
            close(newsockfd);
        }
        } 
    }
}

As you know, I can not reach the gui elements in that thread. For this reason, I called mainThread function, but still I can not reach gui elements. I got this result:

The process has forked and you cannot use this CoreFoundation functionality safely. You MUST exec().
Break on __THE_PROCESS_HAS_FORKED_AND_YOU_CANNOT_USE_THIS_COREFOUNDATION_FUNCTIONALITY___YOU_MUST_EXEC__() to debug.
The process has forked and you cannot use this CoreFoundation functionality safely. You MUST exec().
Break on __THE_PROCESS_HAS_FORKED_AND_YOU_CANNOT_USE_THIS_COREFOUNDATION_FUNCTIONALITY___YOU_MUST_EXEC__() to debug.
The process has forked and you cannot use this CoreFoundation functionality safely. You MUST exec().
Break on __THE_PROCESS_HAS_FORKED_AND_YOU_CANNOT_USE_THIS_COREFOUNDATION_FUNCTIONALITY___YOU_MUST_EXEC__() to debug.
The process has forked and you cannot use this CoreFoundation functionality safely. You MUST exec().
Break on __THE_PROCESS_HAS_FORKED_AND_YOU_CANNOT_USE_THIS_COREFOUNDATION_FUNCTIONALITY___YOU_MUST_EXEC__() to debug.
The process has forked and you cannot use this CoreFoundation functionality safely. You MUST exec().
Break on __THE_PROCESS_HAS_FORKED_AND_YOU_CANNOT_USE_THIS_COREFOUNDATION_FUNCTIONALITY___YOU_MUST_EXEC__() to debug.
The process has forked and you cannot use this CoreFoundation functionality safely. You MUST exec().
Break on __THE_PROCESS_HAS_FORKED_AND_YOU_CANNOT_USE_THIS_COREFOUNDATION_FUNCTIONALITY___YOU_MUST_EXEC__() to debug.
The process has forked and you cannot use this CoreFoundation functionality safely. You MUST exec().
Break on __THE_PROCESS_HAS_FORKED_AND_YOU_CANNOT_USE_THIS_COREFOUNDATION_FUNCTIONALITY___YOU_MUST_EXEC__() to debug.
The process has forked and you cannot use this CoreFoundation functionality safely. You MUST exec().
Break on __THE_PROCESS_HAS_FORKED_AND_YOU_CANNOT_USE_THIS_COREFOUNDATION_FUNCTIONALITY___YOU_MUST_EXEC__() to debug.
The process has forked and you cannot use this CoreFoundation functionality safely. You MUST exec().
Break on __THE_PROCESS_HAS_FORKED_AND_YOU_CANNOT_USE_THIS_COREFOUNDATION_FUNCTIONALITY___YOU_MUST_EXEC__() to debug.
The process has forked and you cannot use this CoreFoundation functionality safely. You MUST exec().
Break on __THE_PROCESS_HAS_FORKED_AND_YOU_CANNOT_USE_THIS_COREFOUNDATION_FUNCTIONALITY___YOU_MUST_EXEC__() to debug.

If I do not use fork, it works. But my question is that how can i reach the gui elements by using fork function?

HCD
  • 21
  • 1
  • 4
  • Is there any reason why you are not using GCD? – sosborn Apr 08 '13 at 05:11
  • You cannot fork() or exec() on iOS devices (compare http://stackoverflow.com/questions/3619252/fork-on-iphone). Or is this for a jailbroken device? – Martin R Apr 08 '13 at 05:23
  • I used GCD but in this case, the server connection sometimes fails. I tried many things but I could not find a better solution. Yes this is for a jailbroken device. – HCD Apr 08 '13 at 05:44

1 Answers1

2

(As you said in a comment, this is for jailbroken devices, otherwise you could not use fork() or exec() at all.)

You are running into a limitation of the Apple frameworks which is documented in Obsolete Programming Techniques in Technical Note TN2083 "Daemons and Agents":

Daemonizing Frameworks

Many Mac OS X frameworks do not work reliably if you call fork but do not call exec. The only exception is the System framework and, even there, the POSIX standard places severe constraints on what you can do between a fork and an exec.

Martin R
  • 510,973
  • 84
  • 1,183
  • 1,314