ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi, I have a c++ code with semaphores that works well if compiled in a Fedora virtual machine over Windows. The same code compiled in the last Ubuntu doesn't work. The sem_wait launch an EINVAL error and doesn't wait for any thread. I've upgraded and downgraded libraries but I get the same result.
example of the code:
//
#include <errno.h>
#include <pthread.h>
int nResult=sem_init(&sem,0,0);
printf("nResult=%d",nResult); //gives 0 (ok)
I assume you've got an #include <semaphore.h>, and you're passing a pointer to your (successfully initialized) semaphore in to sem_wait ().
In general, "sem_wait()" and friends should work just as well on Ubuntu as any other Linux, and it shouldn't make any difference if you're running as a VM, or using a real, physical host.
I'm not sure that they are supported at all, or it may be that the declarations have been moved to mutex.h. In most places of the kernel, semaphores have been change to mutex's.
I assumed you're writing a user-space program that's using the POSIX "sem_wait()" functions. These are, by definition, completely different from kernel-space semaphores or mutexes.
I'm certain that it doesn't matter if you're using VMWare or a physical host. I'd be surprised if Fedora supported it but Ubuntu doesn't - but, frankly, I haven't checked:
Personally, I've always preferred to use the SysV APIs for stuff like this.
STRONG SUGGESTION:
Check out Beej's Guide to IPC, and see if there are alternatives to Posix semaphores you might not have previously considered ... and might actually prefer: http://beej.us/guide/bgipc/
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.