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'm writing a producer-consumer program, where the producer and the consumer are different processes and they communicate using queued signals, and when I run it it comes out always 'segmentation fault'.
Please, can anyone help me? thank you a lot!!
Here is my code:
(note: I tried using both 'shm_open()' and 'mmap()', and 'shmget()' and shmat()')
Your first problem has nothing to do with shared memory. You dereference the uninitialized pointer 'market'. You should have been able to get at least past that simple problem with a debugger or a handful of printf()s to see where things go south.
Please post your source code in [ code] tags.
Your first problem has nothing to do with shared memory. You dereference the uninitialized pointer 'market'. You should have been able to get at least past that simple problem with a debugger or a handful of printf()s to see where things go south.
Please post your source code in [ code] tags.
--- rod.
Thank you for making me notice the error, but I was so concentrated looking at shared memory sintax that I didn't pay attention to it, also I found out another problem about the syncronization between the two processes and I fixed it by 'shared mutex'. I corrected the code for both the problems but it still says 'segmentation fault'. I posted in this forum without making any kind of debug just because I thought there was an error easy to find... anyway i'll try to do some debug, but if someone is able to find out the problem i'll be very thankful!
I corrected the code for both the problems but it still says 'segmentation fault'.
It appears you edited your first post to put those corrections directly into the code shown there. Others trying to help you probably wouldn't notice you did that, so they would assume they can't help because they don't know the current state of your code.
It is generally better to leave the wrong code in the initial post and post the corrected code in a later post.
Quote:
I posted in this forum without making any kind of debug just because I thought there was an error easy to find.
That is a large enough amount of code, that I would want to know where the seg fault occurs before attempting to desk check the code for the cause of the seg fault.
Quote:
anyway i'll try to do some debug, but if someone is able to find out the problem i'll be very thankful!
The most trivial debugging can find the location and backtrace of the seg fault. It may take more skill to find the reason for the seg fault.
If you wanted help, you should have done the first debugging step and posted the backtrace of the seg fault.
You should very carefully re-read the shmat() manpage, especially anything to do with its return value. That shouldn't be too hard for you; it wasn't for me.
Edit: I buried a key fact for the OP too deep in this rambling post, so copying it to the top:
Comparison of x86 pointers is unsigned. There is no pointer p that could make the comparison p<0 true.
(void*)-1 is the maximum possible pointer and thus > all other pointer values.
Quote:
Originally Posted by theNbomr
You should very carefully re-read the shmat() manpage, especially anything to do with its return value. That shouldn't be too hard for you; it wasn't for me.
I assume you're trying to make the OP feel stupid. I haven't tried as hard as the OP should have, but I don't see how the answer is in the shmat() manpage.
I pasted the code into a file, added an include of string.h (because it wouldn't compile for me without that) and ran it.
It crashes on the line
Code:
s->n=0;
because s is 0xFFFFFFFF, which is because market is 0xFFFFFFFF from executing the line:
Code:
if ((market = (shop *)shmat(shm,0,0)) < 0)
I assume the OP already understood that 0xFFFFFFFF is the documented error return from shmat(), but probably doesn't understand why the <0 test didn't work for 0xFFFFFFFF, nor why shmat() returned error.
I would never have coded a <0 test on a pointer myself. I have no idea what such a test should mean. Comparison of two pointers is unsigned so 0xFFFFFFFF is the max pointer and not < any other pointer. But 0, if it is a pointer is <= all pointers, so the test is already false at compile time.
I might have coded
Code:
if ((market = (shop *)shmat(shm,0,0)) == (shop*)(-1))
If you wanted help, you should have done the first debugging step and posted the backtrace of the seg fault.
As it happened, theNbomr made me curious enough that I copied/pasted/compiled your code and started it in gdb. So I saw that the fault was on
Code:
s->n=0;
From there, I have the experience to jump directly to the <0 bug discussed above (that I didn't have the patience to read your entire source to spot).
You should know how easy it is to identify the faulting line (with -g on your g++ command and then gdb).
You could have had that answer in my first post (or maybe even earlier from someone else) rather than waiting hours for it, if you had done that simple step yourself.
I assume you're trying to make the OP feel stupid. I haven't tried as hard as the OP should have, but I don't see how the answer is in the shmat() manpage.
Not stupid, but a bit short on ambition. He/she has shown little that there was any effort to solve the problem. As you have correctly pointed out, the problem results from an incorrect test of a return value. The details of this are explained in the manpage (at least in the one I read), and I think I pointed to a pretty specific part of that page.
EDIT: There seems to other problems with the code, as the pointer 'market' is first initialized, and then used to assign to a member of the struct to which it points. Then it is changed to be a pointer to shared memory, or at least attempted, without ever having made use of the original struct. It might be useful to give us some idea how the program is intended to work.
The details of this are explained in the manpage (at least in the one I read)
I think we're looking at similar enough manpages that we disagree despite seeing the same thing.
The manpage says the error return is -1. There is every reason to assume the OP understood that. The manpage does not tell you how a C++ program can compare a pointer to -1 (nor should it). A C++ programmer ought to know that, but most don't. A naive programmer might try (pointer==-1) and discover that is wrong (at compile time), then decide to get lazy and think (pointer<0) is good enough (because the compiler didn't complain).
Quote:
EDIT: There seems to other problems with the code
For sure, especially the important one I don't know the answer to (don't ever use shmat myself), which is what makes that shmat() fail. That is also discussed in the manpage, which lists several possibilities, none of which seem to apply to my test.
Quote:
as the pointer 'market' is first initialized, and then used to assign to a member of the struct to which it points. Then it is changed to be a pointer to shared memory, or at least attempted, without ever having made use of the original struct.
I didn't even look at that until you mentioned it. You're right of course, but I expect the OP is first focussed on getting the shared memory use to work and would look at program logic issues after that. I expect you are not implying that specific program logic error is part of the cause of the failure of the use of shared memory.
Ok, my man page explicitly states that 'on error (void *) -1 is returned'. This seems like enough of a hint that a simple test against less-than-zero is not enough.
At this stage, I don't consider the program to be failing; at least not the version I have tested (and I also found all of the problems you previously noted, along with a couple of benign things, thanks to the -Wall compiler switch). It properly tests and handles the return value. When I ran it at as root, it reported something resembling success, and left a child process running. Not too sure I will do that again, at least until I've satisfied myself that any bugs are at worst harmless.
It appears you edited your first post to put those corrections directly into the code shown there. Others trying to help you probably wouldn't notice you did that, so they would assume they can't help because they don't know the current state of your code.
It is generally better to leave the wrong code in the initial post and post the corrected code in a later post.
Thank you for the hints! As you said I'm new in this kind of forum so I have to learn lots of things.
Quote:
That is a large enough amount of code, that I would want to know where the seg fault occurs before attempting to desk check the code for the cause of the seg fault.
The most trivial debugging can find the location and backtrace of the seg fault. It may take more skill to find the reason for the seg fault.
If you wanted help, you should have done the first debugging step and posted the backtrace of the seg fault.
I found the problem which caused seg fault: using shmat() the address of mutex was 0xffffff (as you wrote in the last postes), while using mmap() the address of market->obj was 0x0. So I solved the problem using the last one and referencing market->obj=buffer after mapping.
Now the problem is that the program doesn't work well, it works only the producer and also using the debugger I saw it never enters in the consumer function, like the program doesn't fork and I don't understand why..
I hope this time someone will able to help me. Here's the corrected code:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.