473,396 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Need help with a unix/c program

- 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
10 1965
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
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
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
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
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
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
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
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

"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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

29
by: RAY | last post by:
Hi , my boss has asked I sit in on an interview this afternoon and that I create some interview questions on the person's experience. What is C++ used for and why would a company benefit from...
1
by: Jung Joon Park | last post by:
Hi everyone. I am JJ Park. I got some questions in C++. First of all, my English is not perfect, because I am foreigner. I have got C++ code, which have no syntex error in Visual C++ ver6.0...
162
by: techievasant | last post by:
hello everyone, Iam vasant from India.. I have a test+interview on C /C++ in the coming month so plz help me by giving some resources of FAQS, interview questions, tracky questions, multiple...
7
by: Les Coover | last post by:
I have struggled and struggled with this and still can not get it. The endless loop is supposed to be that way so that strings can be entered over and over again. When finished CTRL + C returns...
6
by: adirajuv | last post by:
I wrote a program in C long time ago and I lost my code and all the intermediate files. All I have is the "exe" file that was generated when I executed it. Can the code be retrieved from this exe...
6
by: Joseph | last post by:
Hi, I am trying to develop a C# application that will run on Windows that will do the following * Select file name to process * FTP the file to a UNIX server * Process this file on UNIX using a...
4
by: WaterWalk | last post by:
Hello, I'm currently learning string manipulation. I'm curious about what is the favored way for string manipulation in C, expecially when strings contain non-ASCII characters. For example, if...
18
by: Angus | last post by:
Hello We have a lot of C++ code. And we need to now create a library which can be used from C and C++. Given that we have a lot of C++ code using classes how can we 'hide' the fact that it is...
12
by: =?ISO-8859-1?Q?Thomas_B=F8rlum?= | last post by:
Hey all, I'm writing a c++ program that needs to read a file. I'm trying to read a file that is in the same directory as the executable. Everything works fine if I execute the program while in...
14
by: xander.grespesky | last post by:
hi all, I'm looking for recommendations for unix "shell access" services that provide a c++ compiler (gcc or intel). free would be preferable. basically i'm looking at testing out some...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.