Alle 21:34, mercoledì 28 gennaio 2004, Steven Augart ha scritto:
First, pthread_setconcurrency() does nothing on Linux.
I know, but it is an exercise and it was required (not targetted specifically to linux).
Second of all, the lack of a Makefile made it difficult for me to run this program.
gcc -lpthread file.c -o yourchoice
Third of all, a documentation issue: the use of M and N is appropriate for a mathematical formula, but not for software; better would have been the names NBERTHS and NSHIPS.
Right, but I did not choose those names. The other names were roughly translated into something meaningful in english, maybe not always the best choice everytime. Uhm; I am sure I did not take a good choice almost everything, but the italian names (chosen by the teacher)
The condition "full" needs to be described. What do you actually intend it to do?
When the port if full, you can not place your ship there.
The constant 3000 also needs to be described and probably made into an explicit named constant.
That means nothing, just a way to make the "ships" do something many times.
If you did those things it would be easier for me to look at your code and understand what you're trying to do.
The variables "ships" and "waiting" might be better named "ndocked_ships" and "nwaiting_ships", or something of the sort. Incidentally, document that you use waiting purely for printing debugging messages.
it's used also in an "if" statement. Btw this is surely not a "real world" program. Thank you for your advices anyway:) Right.
It would also be very nice if your code would compile cleanly under GCC with various warnings enabled. I personally have the environment variable CFLAGS set to the following by default: export CFLAGS="-pipe -ggdb3 -W -Wall -Wbad-function-cast -Wcast-align -Wpointer-arith -Wcast-qual -Wshadow -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -fkeep-static-consts -fkeep-inline-functions -Wundef -Wwrite-strings -Wno-aggregate-return -Wmissing-noreturn -Wnested-externs -Wtrigraphs -Wconversion -Wsign-compare -Wno-float-equal -Wmissing-format-attribute -Wno-unreachable-code -Wdisabled-optimization -Wendif-labels"
Now to the meat of the matter, the *pthread* calls:
I have personally always used pthread_cond_wait() from inside of a while() loop, just in case the condition status has changed between the time someone sent the signal and the time the waiting thread received it. This code would be more robust and simpler if you assumed that the condition being signalled did not necessarily mean that there was actually a berth available, just that there might be one. Then, at least while you're debugging, use pthread_cond_broadcast() instead of pthread_cond_signal(). And get rid of the if() statement guarding the pthread_cond_signal (now to be pthread_cond_broadcast()). Just always broadcast; if there are no listeners, the runtime system will throw away the broadcast. You can add the if() back after you have your implementation working in the simple mode.
Yes I figured it out while I was out to eat. A really silly problem after all. Strangely the examples from my teacher do not use always a while statement.
E, finalmente, chi é Tazio?
That's me. Why? I am not praising myself "praise" is a nickname given to me by some guys who did not know the english meaning of that word.
Ma tu sai l'italiano?