A weird problem with POSIX function


 
Thread Tools Search this Thread
Top Forums Programming A weird problem with POSIX function
# 1  
Old 09-16-2011
A weird problem with POSIX function

Hi all,

Sorry for the title because I didn't find a proper name for it. My question is about POSIX functions, such as timer_create(), mq_open() and pthread_create().

Code:
void test_queue()
{
struct mq_attr attr;

attr.mq_maxmsg = 10;
attr.mq_msgsize = 64;

mq_unlink("/my_test_queue");
if(mq_open("/my_test_queue", (O_RDWR + O_CREATE + O_EXCL), 0777, &attr) == -1)
{
  printf("\n__%s__errno(%d: %s)", __FUNCTION__, errno, strerror(errno));
  return FAIL;
}
}

If I put the above function in main.c, there will have no problem. The queue is successfully created. But if I put in a file, let say foo.c. This file is compiled into libFOO.a and linked with main.c. Then I got the following error: errno(2: No such file or directory)

The other POSIX functions are also same if put in libFOO.a, for example, timer_create() failed with "Invalid argument" errro, pthread_create() succeeds but it is never run.

Is there any difference between running in main.c and foo.c? If you ever experienced such problem then please give me some hints

Thank you very much
# 2  
Old 09-16-2011
Your code looks okay.

How are you making (compiling) the final complete file? What OS are you on?
# 3  
Old 09-16-2011
I am working on embedded linux.
It turns out that the problem is not come from the source code itself but from the compile option. If I add -D_GNU_SOURCE when compiling libFOO then problem will be solved.
# 4  
Old 09-16-2011
Code:
(O_RDWR + O_CREATE + O_EXCL)

This is wrong. Sometimes you can get away with adding bitfields, but not always. Imagine these binary numbers:

Code:
  00000001
+ 00000010
=========
  00000011

No overlap, so they act like you expect. If they overlap:

Code:
  00000011
+ 00000010
=========
  00000101

1+1=10, so it carries, enabling a brand-new bit which had nothing to do with anything you meant to enable.

If you want to set bits and nothing but set bits, | is what you want.

Code:
O_RDWR | O_CREATE | O_EXCL

# 5  
Old 09-17-2011
Hi!

Quote:
Originally Posted by bus147
Hi all,

Sorry for the title because I didn't find a proper name for it. My question is about POSIX functions, such as timer_create(), mq_open() and pthread_create().

[...]

If I put the above function in main.c, there will have no problem. The queue is successfully created. But if I put in a file, let say foo.c. This file is compiled into libFOO.a and linked with main.c. Then I got the following error: errno(2: No such file or directory)

The other POSIX functions are also same if put in libFOO.a, for example, timer_create() failed with "Invalid argument" errro, pthread_create() succeeds but it is never run.

Is there any difference between running in main.c and foo.c? If you ever experienced such problem then please give me some hints

Thank you very much
Strange. Can you check if the following works on your platform:

Code:
/* foo.c */
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "foo.h" 

static pthread_t tid;

void* my_thread(void* ignore)
{
   int i;
   printf("Hello from thread\n");
   for (i=0; i<10; i++) {
      printf ("%d ", i); 
      fflush(stdout);
      sleep(1);
   }
}

void init(void)
{
   int rc;
   
   rc = pthread_create(&tid, NULL, my_thread, NULL);
   if (rc!=0) {
      fprintf(stderr,"pthread_create failed: %s (%d)\n", strerror(rc), rc);
      exit(1);
   }
}

Code:
/* foo.h */
#ifndef __FOO_H_
#define __FOO_H_
void init(void);
#endif

Code:
//myprog.c
#include <unistd.h>
#include "foo.h"

int
main()
{
   int val; 
   init();
   sleep(6);
}

Compile as follows:
Code:
cc -c foo.c
ar rcs libfoo.a foo.o
cc -pthread -static myprog.c -o myprog -L. -lfoo

If everything works as expected, you should see the following output:
Code:
Hello from thread
0 1 2 3 4 5

/Lew
# 6  
Old 10-04-2011
Hi NH2,

My problem comes from invalid compile option that I mentioned from my previous post. By the way, I tested with your code and get error code 11 returned from pthread_create() function. The meaning is "Resource temporarily unavailable". Do you have any idea?

I used MSVC2010 for testing your code (pthreadVC2.lib)
# 7  
Old 10-04-2011
Quote:
Originally Posted by bus147
I used MSVC2010 for testing your code
That's probably your problem. Try a UNIX system.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Weird awk problem

Hi, I have a simple awk script: BEGIN{} { $a=$2-$1; print $a } END{if(NR==0){ print "0" } } to which I provide the following input 2.9 14 22.2 27 (4 Replies)
Discussion started by: jamie_123
4 Replies

2. UNIX for Advanced & Expert Users

Linkage of POSIX threads function calls

I wonder if someone knows what is the rationale behind linking function calls of the POSIX threads library at link-time vs. run-time. For example, if I create the following program: #include <pthread.h> void noop() { return; } int main() { pthread_self(); pthread_atfork(noop,... (1 Reply)
Discussion started by: jsimsa
1 Replies

3. Programming

Compilation problem with Posix Mes Q

Hi #include "training.h" #include <mqueue.h> // for posix mqs int main(int argc,char *argv) { int opt,flag; mqd_t msq; // msg q type flag=O_RDWR|O_CREAT; while((opt =getopt(argc,argv,"e")) != -1) { switch(opt) { case 'e': ... (4 Replies)
Discussion started by: kumaran_5555
4 Replies

4. Shell Programming and Scripting

In bash getting weird output from function ?

My script- result="" times() { echo "inside the times function" result=8 echo "Inside function $result" return $result } result=$(times) echo "the value is "$? echo "the value of result $result" when I run I get this, why the value still remain 0. $ ./func the value is 0 the value... (5 Replies)
Discussion started by: boy18nj
5 Replies

5. Infrastructure Monitoring

Weird dependency problem!

Hi, I want to install net-snmp-devel package but i have following dependecy problem. It's very odd, i don't get it. One of packages is depended on the other one, the other one is depended on the previous one as well. :S :S Could you help me please? Here are the steps: # ls -l total... (4 Replies)
Discussion started by: oduth
4 Replies

6. UNIX for Advanced & Expert Users

Really weird delete problem

Hi, I've Ubuntu 8.04, and it has some files that I just cannot delete. I've tried everything, inode, fsck etc. Here is what the ls -li outputs root@ubuntu:/home/luser/.local/share/Trash/files/junk# ls -l ls: cannot access TRUNK_: No such file or directory ls: cannot access 2006_output.mv:... (11 Replies)
Discussion started by: nitin
11 Replies

7. UNIX for Dummies Questions & Answers

Weird character in between echo function

Hi All, Appreciate if anyone can help. I've a script where it does echo function like this while do FILE_ARG="cu0${w}_${FILE}_${DT}.av" ORACLE_ERROR=`grep "ORA-" ${FILE_ARG}` if ]; then Func_Log_Writer "Fail! ${FILE_ARG}\n" Func_Log_Writer "Error message:... (2 Replies)
Discussion started by: agathaeleanor
2 Replies

8. Programming

Problem with POSIX pthreads and virtual memory

Hi, i have this code... in order to test my problem... #include <stdio.h> #include <iostream> #include <pthread.h> static void* cliente(void *datos); int main() { pthread_attr_t tattr; int ret; size_t size = PTHREAD_STACK_MIN + 0x0100; ret =... (8 Replies)
Discussion started by: JEscola
8 Replies

9. Solaris

Weird crontab problem

Greetings To All! I am running Solaris 10 in a sparc environment. Here is the deal: In /var/spool/cron/crontabs, there is a cron user named "sys". If I do a crontab -l sys, it returns: # 0 * * * 0-6 /usr/lib/sa/sa1 # 20,40 8-17 * * 1-5 /usr/lib/sa/sa1 # 5 18 * * 1-5 /usr/lib/sa/sa2... (8 Replies)
Discussion started by: RobSand
8 Replies

10. UNIX for Dummies Questions & Answers

Weird Problem???

I have a problem I don't understand... I am trying to declare a variable, and then output the results of that variable, couldn't be simpler #!/bin/ksh VAR='Oranges' if then echo "Found Lemons" elif then echo "Found Oranges" fi The output shouold clearly be "Found Oranges", but... (2 Replies)
Discussion started by: danhodges99
2 Replies
Login or Register to Ask a Question