Need help with a unix/c program

P

preethamkumark

- 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...
 
I

Ian Collins

- 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).
 
T

Tom St Denis

Ian said:
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
 
K

Kenny McCormack

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.
 
F

Flash Gordon

Kenny said:
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.
 
P

preethamkumark

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.
 
I

Ian Collins

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.
 
W

Walter Roberson

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.
 
A

Al Balmer

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.
 
R

Rod Pemberton

Walter Roberson said:
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
 
R

Rod Pemberton

Flash Gordon said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top