Help understanding some C code

J

jake

Hi!

I hope there is someone who can help me! I'm not so familiar with C and I
desperatly nead someone to comment this code. With as many details as
possible please.

Thanks!!


#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#define SHMSIZE 128
#define SHM_R 0400
#define SHM_W 0200

void
main(argc, argv)
char *argv[];
{
struct shm_struct {
int tecken;
int empty;
} *shmp;
char *addr = NULL;
int pid, i, var1, var2, shmid;
struct shmid_ds *shm_buf;

shmid = shmget(IPC_PRIVATE, SHMSIZE,
IPC_CREAT | SHM_R | SHM_W );
shmp = (struct shm_struct *) shmat(shmid, addr, 0);
shmp->empty = 0; var1 = 0; var2 = 0;
pid = fork();
if (pid != 0)
{
/* here's daddy */
while (var1 < 100)
{
/* write to shmem */
var1++;
while (shmp->empty == 1);
shmp->tecken = var1;
shmp->empty = 1;
}
shmdt(addr);
shmctl(shmid, IPC_RMID, shm_buf);
}
else
{
/* here's the child */
while (var2 < 100)
{
/* read from shmem */
while (shmp->empty == 0);
var2 = shmp->tecken;
shmp->empty = 0;
printf("%d\n", var2);
}
shmdt(addr);
shmctl(shmid, IPC_RMID, shm_buf);
}
}
 
I

Ian Collins

jake said:
Hi!

I hope there is someone who can help me! I'm not so familiar with C and I
desperatly nead someone to comment this code. With as many details as
possible please.

What do you expect it to do?
#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>

It's Unix specific.
#define SHMSIZE 128
#define SHM_R 0400
#define SHM_W 0200

void
main(argc, argv)
char *argv[];
{

and very old.
 
P

Phil Carmody

jake said:
Hi!

I hope there is someone who can help me! I'm not so familiar with C and I
desperatly nead someone to comment this code. With as many details as
possible please.

Thanks!!


#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>

Those are preprocessor includes. One's a standard C one, and the others
are not, so there's no way of knowing what they might contain without
looking inside them.
#define SHMSIZE 128
#define SHM_R 0400
#define SHM_W 0200

They're macros.
void
main(argc, argv)
char *argv[];

Ug, that's a very out-of-date way of specifying a function's parameters.
{
struct shm_struct {
int tecken;
int empty;
} *shmp;

That's a structure definition.
char *addr = NULL;

A pointer.
int pid, i, var1, var2, shmid;

Loads of ints - so many!
struct shmid_ds *shm_buf;

Another pointer.
shmid = shmget(IPC_PRIVATE, SHMSIZE,
IPC_CREAT | SHM_R | SHM_W );
shmp = (struct shm_struct *) shmat(shmid, addr, 0);

That's calling functions - see, it's easy!
shmp->empty = 0; var1 = 0; var2 = 0;

Assignment - always useful, learn that trick as soon as possible.
pid = fork();
if (pid != 0)
{
/* here's daddy */
while (var1 < 100)

That's what we call 'bad indenting'. It's a fun trick to play
on workmates who will be doing code review.
{
/* write to shmem */
var1++;
while (shmp->empty == 1);
shmp->tecken = var1;
shmp->empty = 1;
}
shmdt(addr);
shmctl(shmid, IPC_RMID, shm_buf);

More function calls - you can do loads if you like.
}
else
{
/* here's the child */
while (var2 < 100)
{
/* read from shmem */
while (shmp->empty == 0);
var2 = shmp->tecken;
shmp->empty = 0;
printf("%d\n", var2);
}
shmdt(addr);
shmctl(shmid, IPC_RMID, shm_buf);

Even more function calls - it really doesn't get more exciting than
this!

That's the most important bit! Not the }s. The end of file. Every
C program written, ever, has one. You ask all C programmers what's
absolutely essential to remember when programming C, and I bet you
99% of them, plus or minus 2%, will forget to mention that you _must_
have an end of file.

Well, that's about it, a whirlwind tour of C - we've had the preprocessor,
functions, ints, pointers, structures, _and_ ends of files - that's
basically everything!

Phil
 
J

jacob navia

jake a écrit :
Hi!

I hope there is someone who can help me! I'm not so familiar with C and I
desperatly nead someone to comment this code. With as many details as
possible please.

Thanks!!


#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#define SHMSIZE 128
#define SHM_R 0400
#define SHM_W 0200

void
main(argc, argv)
char *argv[];
{
struct shm_struct {
int tecken;
int empty;
} *shmp;
char *addr = NULL;
int pid, i, var1, var2, shmid;
struct shmid_ds *shm_buf;

shmid = shmget(IPC_PRIVATE, SHMSIZE,
IPC_CREAT | SHM_R | SHM_W );
shmp = (struct shm_struct *) shmat(shmid, addr, 0);
shmp->empty = 0; var1 = 0; var2 = 0;
pid = fork();
if (pid != 0)
{
/* here's daddy */
while (var1 < 100)
{
/* write to shmem */
var1++;
while (shmp->empty == 1);
shmp->tecken = var1;
shmp->empty = 1;
}
shmdt(addr);
shmctl(shmid, IPC_RMID, shm_buf);
}
else
{
/* here's the child */
while (var2 < 100)
{
/* read from shmem */
while (shmp->empty == 0);
var2 = shmp->tecken;
shmp->empty = 0;
printf("%d\n", var2);
}
shmdt(addr);
shmctl(shmid, IPC_RMID, shm_buf);
}
}

It looks like this code
(1) Starts a shared memory buffer
(2) forks, and the parent writes 100 integers into the shared memory
buffer while
(3) the child reads 100 integers from the same memory buffer.

This is a demonstration program of the usage of shared memory, i.e.
memory that is shared by two process

At least that is the intention of the code. The structure shm_struct
contains an "empty" field to synchronize the reading/writing but I
am not certain that it works. The structure starts as initially
not empty (since it is initialized to zero). The parent waits until that
value changes to 0 with this code
> while (shmp->empty == 1)
> ;

The indentation is wrong.

The opposite for the child.

Have fun with that code

:)
 
B

Beej Jorgensen

Keith Thompson said:
Correction: comp.unix.programmer.

The saddest part is that I was thinking of the acronym "CUP" while I
wrote it. Amazing!

-Beej
 
B

Ben Bacarisse

Beej Jorgensen said:
A quick glance looks like it's demonstrating Peterson's Algorithm:

Rather too quick I think! It does not look like Peterson's algorithm
to me but, as you say, not really topical here.
 
B

Beej Jorgensen

Ben Bacarisse said:
Rather too quick I think! It does not look like Peterson's algorithm
to me but, as you say, not really topical here.

Dammit--don't make me go back and look again. :) I saw some flags and
some busywaits and some shared data, Your Honor.

But I'm willing to defer to those who aren't three days into a cold on
this matter.

-Beej
 
M

Moi

jake said:
Hi!

I hope there is someone who can help me! I'm not so familiar with C and
I desperatly nead someone to comment this code. With as many details as
void
main(argc, argv)
char *argv[];

Ug, that's a very out-of-date way of specifying a function's parameters.

Which is awkward, since "void main()" could be considered a Schildtism, and so
rather "modern" ;-)

That's the most important bit! Not the }s. The end of file. Every C
program written, ever, has one. You ask all C programmers what's
absolutely essential to remember when programming C, and I bet you 99%
of them, plus or minus 2%, will forget to mention that you _must_ have
an end of file.

Well, that's about it, a whirlwind tour of C - we've had the
preprocessor, functions, ints, pointers, structures, _and_ ends of files
- that's basically everything!

LOL,
AvK
 
J

jacob navia

Malcolm McLean a écrit :
As a general rule, looking at third party include files is a poor strategy.
Usually they are written to be meaningful to the machine but not to a human
programmer, and contain strange conditional defines and the like to work
with different versions of compilers and maybe operating systems.

Normally one should go to the documentation of the functions. In this case
it can be done simply by typing "sys/shm.h" into Google.

Obviously that guy is not even trying to help the original poster but
just showing off. All his message has no information and shows just his
ignorance.
 
C

Chris M. Thomasson

jacob navia said:
jake a écrit :
Hi!

I hope there is someone who can help me! I'm not so familiar with C and I
desperatly nead someone to comment this code. With as many details as
possible please.
[...]
At least that is the intention of the code. The structure shm_struct
contains an "empty" field to synchronize the reading/writing but I
am not certain that it works. The structure starts as initially
not empty (since it is initialized to zero). The parent waits until that
value changes to 0 with this code
while (shmp->empty == 1)
;

The indentation is wrong.

The opposite for the child.

Have fun with that code

:)

AFAICT, it's as% backwards. Anyway, if it were reversed, it's still screwed.
 
C

Chris M. Thomasson

Beej Jorgensen said:
Better answers will be found on comp.programmer.unix.

A quick glance looks like it's demonstrating Peterson's Algorithm:

http://en.wikipedia.org/wiki/Peterson's_algorithm

No. Well, FWIW, clever asymmetric memory visibility schemes aside for a
moment, IMVHO Petersons Algorithm is not *really worth the time. I mean, it
still requires a `#StoreLoad' style memory barrier which "kind of" renders
performance down to that of a general purpose mutex. The difference is that
a general purpose mutex is vastly more flexible...


;^)








(*)
I need to clarify... The `#StoreLoad' barrier need not be associated with an
atomic RMW in Petersons algorithm. This may be more efficient in
architectures like the SPARC. However, it's still limited to two processes
in it's original form. Indeed, one can distribute the algorithm, but that
increases overhead because, well, perhaps this should be moved over to
`comp.programming.threads'? What about "isolated" distribution?


http://groups.google.com/group/comp.programming.threads/browse_frm/thread/baf0ab7be19262f2


brlock anyone? lol... ;^)


asymmetric memory visibility deadlock problem can be completely eliminated
by tracking global synchronization epoch's (e.g., CPU's bound within process
affinity). Something like:


http://msdn.microsoft.com/en-us/library/ms683148(VS.85).aspx


Windows... Well, does Linux have that? Sort of... `rcu_synchronize()', but
is that a user-space procedure?
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top