Post subject: I need some help with a programming assignment
Joined: 5/17/2007
Posts: 48
Here's the assignment:
Write a code to run under Linux which has multiple threads: One thread “sleeps” for some time. Other threads pass a token between them via signals. When the sleeping task awakes, it signals the tasks that time is up. The task with the token (Hot Potato) loses.
I also have the option of using multiple processes instead, but I've heard it's easier to do with threads. I also know pretty much nothing about pthreads. Anyway, I'm having trouble with even thinking of a way to do this, so any help would be appreciated.
Post subject: Re: I need some help with a programming assignment
Editor, Active player (297)
Joined: 3/8/2004
Posts: 7469
Location: Arzareth
Schnretzl wrote:
I also know pretty much nothing about pthreads.
And they give you that kind of assignment? You cannot indulge us in your lack of motivation to participate on the lessons.
Former player
Joined: 4/16/2004
Posts: 1276
Location: Uppsala, Sweden
Sounds like the "Dining philosophers" problem. http://en.wikipedia.org/wiki/Dining_philosophers_problem Google for the appropriate language should give you a good answer I guess.
/Walker Boh
Post subject: Re: I need some help with a programming assignment
Joined: 5/17/2007
Posts: 48
Bisqwit wrote:
You cannot indulge us in your lack of motivation to participate on the lessons.
Perhaps I made an overstatement. It's not that I have a lack of motivation to learn about pthreads, but more that I don't understand them too well or have much experience with them.
Walker Boh wrote:
Sounds like the "Dining philosophers" problem.
Oddly enough, that is our next assignment, although that one must be done in MicroC/OS-II rather than Linux. Anyway, I was thinking just a bit ago of a way which I think would work: Start one thread, which starts several others that are in an endless loop that checks a global variable containing the thread id of the thread with the token and, if it is equal to the checking thread's id, changes it to the thread id of the next thread. The initial thread, after spawning these threads, then "sleeps." In main, join that thread. As I understand, this should kill the other threads and leave you with a "loser." Should this work? Does it seem like that might be bypassing the assignment since there really isn't any interthread communication?
Post subject: Re: I need some help with a programming assignment
Editor, Active player (297)
Joined: 3/8/2004
Posts: 7469
Location: Arzareth
Schnretzl wrote:
Bisqwit wrote:
You cannot indulge us in your lack of motivation to participate on the lessons.
Perhaps I made an overstatement. It's not that I have a lack of motivation to learn about pthreads, but more that I don't understand them too well or have much experience with them.
What I mean is that if they present you with that kind of assignment, they must have already taught you the concepts necessary to complete the assignments. If that is not true, one of these holds: -- They did not teach. Bad school. -- They taught but you did not listen. Not our problem. -- They taught, but you fail to understand how to apply the lessons. In that case, you should present us with your attempt and explain what it is precisely that you have trouble figuring out. The core question here is: How do they expect you to be able to solve the assignment?
Post subject: Re: I need some help with a programming assignment
Former player
Joined: 4/16/2004
Posts: 1286
Location: Finland
Bisqwit wrote:
-- They did not teach. Bad school.
Not everything is taught or even should be taught explicitly at school. In many cases the teacher's function is only to give the learners tools so that they can learn and find out about stuff by themselves. What does the student learn if he is simply given the solution that he then just copies and maybe adapts to his own assignment?
Banned User
Joined: 3/10/2004
Posts: 7698
Location: Finland
Forget about pthread and use the boost thread library instead: http://www.boost.org/doc/libs/1_36_0/doc/html/thread.html Your code will become more portable too.
Post subject: Re: I need some help with a programming assignment
Joined: 7/2/2007
Posts: 3960
Kyrsimys wrote:
Not everything is taught or even should be taught explicitly at school. In many cases the teacher's function is only to give the learners tools so that they can learn and find out about stuff by themselves. What does the student learn if he is simply given the solution that he then just copies and maybe adapts to his own assignment?
There's a difference between teaching as presenting facts to be memorized, and teaching as guiding the students to learn things on their own. The math classes I took in college were not just simply "here's a proof, now use it to complete these problems". The teacher started with something we knew, and then showed us consequences of that known thing, step by step, to achieve some other useful fact. And then we had to derive corollaries to show we understood the proof. In this particular case, the teacher should be discussing at a high level how threads work, what they do, what problems they can solve, and what problems they can create, and then leaving it to the students to demonstrate their understanding by implementing real-world programs that use threads. The problem is when the teacher says "Go implement this program using threads" and you're sitting there going "Threads? Buh?" Yes, it's possible for you to figure everything out on your own; the resources are all there in the library and on the Net. But then what value is the teacher providing?
Pyrel - an open-source rewrite of the Angband roguelike game in Python.
arflech
He/Him
Joined: 5/3/2008
Posts: 1120
I think the OP should write a Java program with Swing that interprets all that talk about threads to refer to little anthropomorphic threads of yarn.
i imgur com/QiCaaH8 png
Former player
Joined: 6/25/2004
Posts: 607
Location: Maine
I'm interpreting it as option #3, being shown the material but not quite understanding it, only because I have the same problem when it come to math. Try as I might, sometimes it just never seems to make sense to me, especially when it's brand new material. All I can say is, thank God I'm an English major.
Post subject: Re: I need some help with a programming assignment
Skilled player (1651)
Joined: 11/15/2004
Posts: 2202
Location: Killjoy
Bisqwit wrote:
What I mean is that if they present you with that kind of assignment, they must have already taught you the concepts necessary to complete the assignments. If that is not true, one of these holds: -- They did not teach. Bad school. -- They taught but you did not listen. Not our problem. -- They taught, but you fail to understand how to apply the lessons. In that case, you should present us with your attempt and explain what it is precisely that you have trouble figuring out. The core question here is: How do they expect you to be able to solve the assignment?
Welcome to college. Jeez, this happens all the time. He came asking for help, not a debate on the merits of his education. Regardless of the stupidity of the assignment, he has to do it. Sorry I can't help.
Sage advice from a friend of Jim: So put your tinfoil hat back in the closet, open your eyes to the truth, and realize that the government is in fact causing austismal cancer with it's 9/11 fluoride vaccinations of your water supply.
Joined: 8/27/2006
Posts: 883
I'm with Bisqwit on this one. I find it frustrating that people would get their diplome without doing some course because student ask for someone else to do the job, or split the work in two in a team work (as in split the work like you do the java work, I do the VB work) There won't be a whole lot of good programmer that way.
Post subject: Re: I need some help with a programming assignment
Player (210)
Joined: 7/7/2006
Posts: 798
Location: US
DarkKobold wrote:
Welcome to college. Jeez, this happens all the time. He came asking for help, not a debate on the merits of his education. Regardless of the stupidity of the assignment, he has to do it.
Derakon wrote:
Yes, it's possible for you to figure everything out on your own; the resources are all there in the library and on the Net. But then what value is the teacher providing?
Being a sophomore in Electrical Engineering myself, I can strongly identify with these statements. I go to a research university. Many teachers research 20 hours a week and teach 20 hours a week. Just because a teacher is extremely knowledgeable on a subject does not mean he can teach it effectively. I have had a couple classes where I would have been far better off ditching class and reading the book during the hours I am required to be in class. Yes, my teachers for those classes were terrible. Some teachers could be replaced by powerpoint slides because all they do is throw notes on the board. You literally can't do anything about it. It happens every once in a while. I also find myself periodically presented with homework material which I have literally not been provided sufficient background material for. I find it extremely difficult and irritating and unnecessarily time consuming to solve a homework problem without the appropriate background and reference materials. Is it so wrong for Schnretzl to ask for the overview of tackling the problem even though he would code it all himself? A push in the right direction is far from coding it for him. Although, I do agree asking specific questions would have been the better way to go about asking for help. I find myself asking people in my major questions all the time. I don't copy what they say or show me, I just require some piece of knowledge or insight that I don't have and can't find. This would be the sequence of events for attempting a homework problem. 1: Attempt to solve on own. 2: Consult References and attempt to solve. 3: Ask someone or give up. Out of the third options which one helps someone to learn more? Would you rather I get a B in a class doing it all myself and missing key information, or I get an A asking questions and "collaborating" with others, thus acquiring all the material? Should my grade reflect "the information that I, by myself, was capable of", or "the information that I learned throughout the course"? Yes, I realize that copying solutions is bad, but I feel like some of you are frowning upon collaborative learning, which I have found to be an extremely effective way of learning. Perhaps it is partially the difference between the nature of programming and the nature of my major, engineering. The programmer often has to be able to implement his program on his own, whereas the engineer works on a team of contributors, where often no one person is capable of accomplishing the entire task. Constructive criticism of my views is completely acceptable.
arflech
He/Him
Joined: 5/3/2008
Posts: 1120
Rick wrote:
All I can say is, thank God I'm an English major.
I don't think I could have survived as an English major, but math came easily.
i imgur com/QiCaaH8 png
Post subject: Re: I need some help with a programming assignment
Tub
Joined: 6/25/2005
Posts: 1377
Kirkq wrote:
This would be the sequence of events for attempting a homework problem. 1: Attempt to solve on own. 2: Consult References and attempt to solve. 3: Ask someone or give up.
If you are asking for help online (thus asking for help from people who are in no way indebted to you), it's a wise move to show that you've already done 1 and 2 before doing 3. He said he doesn't know anything about pthreads, but didn't even bother reading a pthread manual. He didn't post anything about his previous attempts or even specific questions to show that he's tried to solve it. I can't say I'd blame anyone but him for the flak he's getting. oh, useful linky.
m00
Post subject: Re: I need some help with a programming assignment
Editor, Active player (297)
Joined: 3/8/2004
Posts: 7469
Location: Arzareth
Tub wrote:
Kirkq wrote:
This would be the sequence of events for attempting a homework problem. 1: Attempt to solve on own. 2: Consult References and attempt to solve. 3: Ask someone or give up.
If you are asking for help online (thus asking for help from people who are in no way indebted to you), it's a wise move to show that you've already done 1 and 2 before doing 3. He said he doesn't know anything about pthreads, but didn't even bother reading a pthread manual. He didn't post anything about his previous attempts or even specific questions to show that he's tried to solve it. I can't say I'd blame anyone but him for the flak he's getting.
That's precisely the point I was trying to make… Only worded so much better.
Joined: 8/27/2006
Posts: 883
Stackoverflow.com might be able to help with specific questions, but I doubt they'll help you if you don't provide clear question.
arflech
He/Him
Joined: 5/3/2008
Posts: 1120
ZeXr0 wrote:
Stackoverflow.com
I prefer buffer overflows but whatever exploit floats your boat...
i imgur com/QiCaaH8 png
Joined: 7/2/2007
Posts: 3960
Kirkq: Well said. However, programmers should not be focusing on working alone. Yes, it's useful to be able to complete large projects on your own, but realistically, few projects are the work of a single person. Collaboration is key in this day and age for basically any significant project. Sadly, most college-level projects that I've seen aren't significant, and the classes where you are collaborating are designed to teach you about collaboration, not demonstrate its utility (in other words, you're handed a project you could do on your own but told to work with three other people on it instead).
Pyrel - an open-source rewrite of the Angband roguelike game in Python.
Post subject: Re: I need some help with a programming assignment
Joined: 5/17/2007
Posts: 48
Tub wrote:
He said he doesn't know anything about pthreads, but didn't even bother reading a pthread manual.
While I did say that I know little of pthreads, I didn't say I didn't read anything about them. I have looked at several sites, example programs, etc., it's just that the topic is a wee bit beyond me. I know there are a number of talented programmers on this site, and people here seem to generally be helpful, so I thought someone might be able to give me a little guidance, that's all. I'm not asking for a compiled executable, or source code, or even code snippets necessarily. Anyway, back to the problem: Does anyone think my previously presented solution sounds like it would work? I haven't had a chance to try coding it up yet, been busy the past couple days with a studying for exams I had this week.
Post subject: Re: I need some help with a programming assignment
Joined: 6/14/2007
Posts: 22
Location: Brisbane, QLD, Australia
Schnretzl wrote:
Anyway, back to the problem: Does anyone think my previously presented solution sounds like it would work? I haven't had a chance to try coding it up yet, been busy the past couple days with a studying for exams I had this week.
Why do you expect a different response by asking the same people? Look up as many sources as you can, and keep looking more up UNTIL you have the answer - getting TIRED of it is not an option. If you dont get the answer here, ask somewhere else and continue until you have the answer. IT WILL TAKE TIME, the answer wont come instantly.
Banned User
Joined: 12/23/2004
Posts: 1850
So rather than actually help the guy, we just raeg at him for (supposedly) not doing the research instead of, you know. Helping. Remind me to never ask the forums at large for assistance if I can't figure something out. Anyway, my solution would be: Have thread t1 check the current time. If mod 2 == 0, store a 1 in a global variable. Have thread t2 do the same, but check for mod 2 == 1 and write 2. At the end of x seconds, have the sleeping thread terminate the other two and consider the one that last wrote its variable the "loser". This could be changed to be based off of any other timing mechanism, e.g. a random delay of x seconds or whatever.
Perma-banned