Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

pthreads and errno



Hello All,

I'm trying to port an email server program (BlitzMail, from Dartmouth College) to OS X, mostly for my own personal interest. It was originally written for NeXT, and has been ported to Digital Unix and Linux - so I figured that it wouldn't be too hard to port to OS X. However, during its configure script, it checks to see if errno is preserved between threads, and on OS X it's failing this test. I've modified the original test code from the configure script, and will include it below. It seems to me (with admittedly limited pthread experience) that it should work. However, it's not. Here's the source code and the output.


Basically, the program calls a read() that it knows will fail, and checks errno. It then sleeps, and another thread runs that calls read() with a different errno. The original thread then resumes, and checks to see if errno is the same. It's not, and it seems to me that it should be. Can anyone tell me what I'm doing wrong? I'm running 10.2.1 with Fink (latest CVS) installed.


Thanks,

Scott

#include <pthread.h>
#include <stdio.h>
#include <fcntl.h>

#define NUM_THREADS     5

void *PrintHello(void *threadid)
{
   printf("\n%d: Hello World!\n", threadid);

   (void) open("adsfsadfdsafdsf", O_RDONLY, 0600);

   if (errno != ENOENT && errno != 0)
   {
     printf("open: errno = %d\n", errno);
     exit(1);
   }

   pthread_exit(NULL);
}

int main()
{
   pthread_t thread;
   int rc=0,t=0;
   char buf[4];
   rc = pthread_create(&thread, NULL, PrintHello, (void *)t);

   if (rc)
   {
       printf("ERROR; return code from pthread_create() is %d\n", rc);
       exit(-1);
   }

   pthread_detach(thread);

   (void) read(12345,&buf, sizeof(buf));

   if(errno != EBADF)
   {
     printf("main before sleep: errno = %d\n", errno);
     exit(1);
   }

   printf("Going to sleep...\n");

   sleep(1);

   printf("Waking up...\n");

   if(errno != EBADF)
   {
     printf("main after sleep: errno = %d\n", errno);
     exit(1);
   }

   pthread_exit(NULL);
}

########################################################333
The output:

sdwr@Scott-D-W-Rankins-Computer:~/<1>BlitzMail/blitzserv-3.9.1>./a.out
Going to sleep...

0: Hello World!
Waking up...
main after sleep: errno = 2
sdwr@Scott-D-W-Rankins-Computer:~/<1>BlitzMail/blitzserv-3.9.1>
_______________________________________________
unix-porting mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/unix-porting
Do not post admin requests to the list. They will be ignored.





Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.