By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,655 Members | 964 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

Need help with a unix/c program

P: n/a
- The program first creates a shared memory buffer containing an array
of 20 integers.
- Each slot of the buffer can have either 0 or 1, where 0 represents an

empty slot, and 1
represents an occupied one.

- Initially, the buffer is empty. Thus, all the slots are initialized
with 0.

- Then the program creates a fan of 2 child processes (producer and
consumer).

- Both processes enter a while loop, and iteratively operate on the
shared buffer in a mutually
exclusive manner.

- use 3 semaphores s, n, and e;

- s: buffer lock (provides mutual exclusion for accessing the shared
buffer)

- n: current number of buffer items (forces consumer to wait when the
buffer is empty)

- e: current number of empty slots (forces producer to wait when the
buffer is full)

- Also, use the buffer index 'in' to let the producer know where to
put
the new item, and index
'out' to let the consumer know where to take an existing item.

- Producer:

- In each iteration, the producer puts a new item (integer 1) to the
buffer. The new item must be placed in the slot next to the end of
occupied items. If the
buffer is already full, then the process is blocked until the consumer
takes at least one item.

- Right after adding a new item, print out the updated contents of the
buffer to stderr When printing, it must go through a loop, printing
one
character at a time. After printing each character, call usleep(100) to

see if it is interrupted or not (it must not - note that this printing
task must also be
included in the critical section so that it is not interrupted by other
process).

- When printing, specify who's printing (Producer or Consumer). And
the
buffer should be printed out in the following way (note that empty
slots are represented
as dots).
[Producer] 1 1 1 1 1 1 1 . . . . . . . . . 1 1 1 1

- After the printing, call usleep(pro_time) to control the speed of
buffer growing.

- Consumer:

- In each iteration, the consumer takes an existing item (integer 1)
from the buffer. The existing item must be taken from the slot at the
beginning of occupied
items. This is simulated by simply replacing 1 with 0 at the index out.
If the buffer
is already empty, then the process is blocked until the producer puts
at least one item.

- Right after removing an item, print out the updated contents of the
buffer to stderr . When printing, it must go through a
loop, printing one character at a time. After printing each character,
call usleep(100) to
see if it is interrupted or not (it must not - note that this printing
task must also be
included in the critical section so that it is not interrupted by
other process).

- When printing, follow the convention written above (in Producer
section).

- After the printing, call usleep(con_time) to control the speed of
buffer shrinking.

- Note that since we use a bounded buffer system, the buffer must be
implemented as a circular
buffer (the end of the buffer is connected to the start of the buffer).

- Initially, make con_time three times as long as pro_time, so that the

buffer can be quickly filled (for example, pro_time = 100000, con_time
= 300000). Once it is
filled, switch their speed so that now the consumer can quickly empty
the buffer (then
switch their speed again so that the buffer quickly grows). Repeat this
fill-empty cycle twice, and
then terminate both child processes (when the buffer is empty for the
third time). The parent
process must wait for the termination of both child processes, and then
terminate itself.

- An example printing result could be as follows:

[Parent] Starting...
....
[Producer] 1 1 1 1 . . . . . . . . 1 1 1 1 1 1 1 1
[Consumer] 1 1 1 1 . . . . . . . . . 1 1 1 1 1 1 1
....
[Producer] 1 1 1 1 1 1 1 1 1 1 1 1 1 . 1 1 1 1 1 1
[Producer] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[Consumer] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . 1 1 1 1 1

....
[Consumer] Two cycles completed. Exiting...
[Producer] Two cycles completed. Exiting...
[Parent] Exiting...

Oct 4 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
pr************@yahoo.com wrote:
- The program first creates a shared memory buffer containing an array
of 20 integers.
This looks very much like homework....

Even if it isn't, it's too platform specific to be topical here, try
comp.unix.programmer (assuming it isn't homework, which will get bounced
there as well).

--
Ian Collins.
Oct 4 '06 #2

P: n/a
Ian Collins wrote:
pr************@yahoo.com wrote:
- The program first creates a shared memory buffer containing an array
of 20 integers.

This looks very much like homework....
It is. If the question has arbitrary requirements like "you must use
three semaphores called s, n and e..." that's your surefire sign of a
lazy bastard.

I question the ethics of people like these. Passing school is not the
be-all of existence. If you're a useless ignorant outside of school
you eventually get found out and marginalized anyways. Might as well
learn something while you're in school.

Tom

Oct 4 '06 #3

P: n/a
In article <11**********************@m7g2000cwm.googlegroups. com>,
Tom St Denis <to********@gmail.comwrote:
>Ian Collins wrote:
>pr************@yahoo.com wrote:
- The program first creates a shared memory buffer containing an array
of 20 integers.

This looks very much like homework....

It is. If the question has arbitrary requirements like "you must use
three semaphores called s, n and e..." that's your surefire sign of a
lazy bastard.

I question the ethics of people like these. Passing school is not the
be-all of existence. If you're a useless ignorant outside of school
you eventually get found out and marginalized anyways. Might as well
learn something while you're in school.
While I don't disagree with anything you've said, I'm always prone to
wonder the following: Is there anything inherently worse in some
student schmoe trying to put one over on his teachers (by cheating,
e.g., by posting on Usenet) than some working schmoe trying to put one
over on his employeers (by cheating, etc.) ?

Seriously, if you think about it, any arguements that can be used to
justify the later, can just as easily be used to justify the former.

Oct 4 '06 #4

P: n/a
Kenny McCormack wrote:
In article <11**********************@m7g2000cwm.googlegroups. com>,
Tom St Denis <to********@gmail.comwrote:
>Ian Collins wrote:
>>pr************@yahoo.com wrote:
- The program first creates a shared memory buffer containing an array
of 20 integers.
This looks very much like homework....
It is. If the question has arbitrary requirements like "you must use
three semaphores called s, n and e..." that's your surefire sign of a
lazy bastard.

I question the ethics of people like these. Passing school is not the
be-all of existence. If you're a useless ignorant outside of school
you eventually get found out and marginalized anyways. Might as well
learn something while you're in school.

While I don't disagree with anything you've said, I'm always prone to
wonder the following: Is there anything inherently worse in some
student schmoe trying to put one over on his teachers (by cheating,
e.g., by posting on Usenet) than some working schmoe trying to put one
over on his employeers (by cheating, etc.) ?

Seriously, if you think about it, any arguements that can be used to
justify the later, can just as easily be used to justify the former.
The employer does not (in general) care about where you got the solution
(except in an interview or test) as long as it works and any licensing
restrictions are ones they consider acceptable (in some cases they don't
care about licensing restrictions and will break them, but that is
another story). In my case, my employer *knows* I have taken code
published on the internet and embedded it in one of our applications
(always following the license terms strictly or, in one case, emailing
the author and asking permission). A teacher, on the other hand, wants
to know whether the student is capable of solving the problem as opposed
to actually wanting a solution for use.
--
Flash Gordon
Oct 4 '06 #5

P: n/a
Tom, and others, when you call the guy who posted the questions as
cheaters , why dont you think atleast once (just a common sense) this
is the excersise imposed on me by a stupid team lead(in other words i
would call trainer in a corporate training session for c language), I
am a java programmer , I dont have the concept of ponter and all , and
i dont need to know because i will never do a job in c programming, but
to get through this guy i have to write a solution for this, i don
know where did he get this frrom .
So people let me know if some one can help me with out all these
arguments.

Flash Gordon wrote:
Kenny McCormack wrote:
In article <11**********************@m7g2000cwm.googlegroups. com>,
Tom St Denis <to********@gmail.comwrote:
Ian Collins wrote:
pr************@yahoo.com wrote:
- The program first creates a shared memory buffer containing an array
of 20 integers.
This looks very much like homework....
It is. If the question has arbitrary requirements like "you must use
three semaphores called s, n and e..." that's your surefire sign of a
lazy bastard.

I question the ethics of people like these. Passing school is not the
be-all of existence. If you're a useless ignorant outside of school
you eventually get found out and marginalized anyways. Might as well
learn something while you're in school.
While I don't disagree with anything you've said, I'm always prone to
wonder the following: Is there anything inherently worse in some
student schmoe trying to put one over on his teachers (by cheating,
e.g., by posting on Usenet) than some working schmoe trying to put one
over on his employeers (by cheating, etc.) ?

Seriously, if you think about it, any arguements that can be used to
justify the later, can just as easily be used to justify the former.

The employer does not (in general) care about where you got the solution
(except in an interview or test) as long as it works and any licensing
restrictions are ones they consider acceptable (in some cases they don't
care about licensing restrictions and will break them, but that is
another story). In my case, my employer *knows* I have taken code
published on the internet and embedded it in one of our applications
(always following the license terms strictly or, in one case, emailing
the author and asking permission). A teacher, on the other hand, wants
to know whether the student is capable of solving the problem as opposed
to actually wanting a solution for use.
--
Flash Gordon
Oct 4 '06 #6

P: n/a
pr************@yahoo.com wrote:
Tom, and others, when you call the guy who posted the questions as
cheaters , why dont you think atleast once (just a common sense) this
is the excersise imposed on me by a stupid team lead(in other words i
would call trainer in a corporate training session for c language), I
am a java programmer , I dont have the concept of ponter and all , and
i dont need to know because i will never do a job in c programming, but
to get through this guy i have to write a solution for this, i don
know where did he get this frrom .
So people let me know if some one can help me with out all these
arguments.
Please don't top post on comp.lang.c.

As I said in the second part of my original response, this is OT here
and you'd be better off asking on comp.unix.programmer.

--
Ian Collins.
Oct 4 '06 #7

P: n/a
In article <11**********************@b28g2000cwb.googlegroups .com>,
<pr************@yahoo.comwrote:
>Tom, and others, when you call the guy who posted the questions as
cheaters , why dont you think atleast once (just a common sense) this
is the excersise imposed on me by a stupid team lead(in other words i
would call trainer in a corporate training session for c language), I
am a java programmer , I dont have the concept of ponter and all , and
i dont need to know because i will never do a job in c programming, but
to get through this guy i have to write a solution for this,
If the training is voluntary, then quit the training because it
isn't of any use to you.

If the training is not voluntary, either your employer does not know
exactly what you are being trained on, or else your employer does
know and thinks that it will be useful for you.

If your employer does not know what you are being trained on, then
bring the assignment to the attention of your employer and get a waiver
on the assignment or have the employer otherwise deal with the
issue of inappropriate training.

If your employer does know what you are being trained on and thinks
you should learn it, and you do not learn the material because
you get someone else to solve it for you, then any certification
that you might receive out of the course would be a certification
that you learned the material in the course -- and that certification
would have been obtained under false pretenses if you had someone
else do the work. If you obtain something of value (the
certification) through false pretenses, then the term for that in
Canada and the USA is not "cheating": the term is "fraud".

>Tom, and others, when you call the guy who posted the questions as
cheaters , why dont you think atleast once (just a common sense) this
You didn't post any questions originally. You posted a list of
tasks required to be accompanied by a program. There wasn't
even an -implied- question: the implication was a command,
"Send me the code to do this."

If you take the matter to the appropriate forum, and post
specific questions about the parts of it you don't understand,
explaining what you have been able to figure out so far and where
your confusion is, then you are much more likely to obtain assistance.
--
If you lie to the compiler, it will get its revenge. -- Henry Spencer
Oct 4 '06 #8

P: n/a
On 4 Oct 2006 13:57:55 -0700, pr************@yahoo.com wrote:
>Tom, and others, when you call the guy who posted the questions as
cheaters , why dont you think atleast once (just a common sense) this
is the excersise imposed on me by a stupid team lead
You'd better hope that your stupid team lead doesn't read this
newsgroup.

--
Al Balmer
Sun City, AZ
Oct 4 '06 #9

P: n/a

"Walter Roberson" <ro******@ibd.nrc-cnrc.gc.cawrote in message
news:eg**********@canopus.cc.umanitoba.ca...
In article <11**********************@b28g2000cwb.googlegroups .com>,
<pr************@yahoo.comwrote:
Tom, and others, when you call the guy who posted the questions as
cheaters , why dont you think atleast once (just a common sense) this
is the excersise imposed on me by a stupid team lead(in other words i
would call trainer in a corporate training session for c language), I
am a java programmer , I dont have the concept of ponter and all , and
i dont need to know because i will never do a job in c programming, but
to get through this guy i have to write a solution for this,

If the training is voluntary, then quit the training because it
isn't of any use to you.

If the training is not voluntary, either your employer does not know
exactly what you are being trained on, or else your employer does
know and thinks that it will be useful for you.

If your employer does not know what you are being trained on, then
bring the assignment to the attention of your employer and get a waiver
on the assignment or have the employer otherwise deal with the
issue of inappropriate training.

If your employer does know what you are being trained on and thinks
you should learn it, and you do not learn the material because
you get someone else to solve it for you, then any certification
that you might receive out of the course would be a certification
that you learned the material in the course -- and that certification
would have been obtained under false pretenses if you had someone
else do the work. If you obtain something of value (the
certification) through false pretenses, then the term for that in
Canada and the USA is not "cheating": the term is "fraud".
(You failed to mention the most important option.)

If your employer does know what you are being trained on and thinks you
should learn it, learn the material because then you can ask for a _raise_.
Your company pay scale might also be distorted for C versus Java
programmers. The ones I've worked for pay/paid C programmers much more than
Java programmers. If that's the case for your company, you 'll be better
off financially being classified as a C programmer in your company even if
most of your work is in Java. Ask your "team lead" why this is of benefit
to you.
Rod Pemberton
Oct 6 '06 #10

P: n/a

"Flash Gordon" <sp**@flash-gordon.me.ukwrote in message
news:lh************@news.flash-gordon.me.uk...
The employer does not (in general) care about where you got the solution
(except in an interview or test) as long as it works and any licensing
restrictions are ones they consider acceptable (in some cases they don't
care about licensing restrictions and will break them, but that is
another story).
That reminds me of one employer I worked for. 3 million plus lines of code
of which perhaps a million was the original package. They didn't pay for a
license for the original package. They didn't own the copyright. They
didn't purchase the code. They didn't know who owned the copyrights or even
if the code was still being sold/licensed/maintained. They got the code on
a handshake deal between an employee and his reseller buddy, decades
earlier. Hundreds of millions of dollars in revenue from code whose
legality was completely unknown. Although, it was believed that no other
firm used it anymore. Since most employees didn't know, those who did
weren't about to say anything, and outsiders didn't know anything at all,
when it was finally brought to the attention of the corporate lawyers (under
new management), they basically said: "who cares, it's been decades, and we
aren't being sued." I worked on the code for 3+ years before learning the
history/truth. Moral: corporate legality is only an issue if it makes it
into court and the history of "proprietary" software is easily buried.
Rod Pemberton
Oct 6 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.