Help understanding some C code

Discussion in 'C Programming' started by jake, Oct 30, 2009.

  1. jake

    jake Guest

    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);
    }
    }
    jake, Oct 30, 2009
    #1
    1. Advertising

  2. jake

    Ian Collins Guest

    jake wrote:
    > 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.

    --
    Ian Collins
    Ian Collins, Oct 30, 2009
    #2
    1. Advertising

  3. jake <> wrote:
    >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.


    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

    -Beej
    Beej Jorgensen, Oct 30, 2009
    #3
  4. jake

    Phil Carmody Guest

    jake <> writes:
    > 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
    --
    Any true emperor never needs to wear clothes. -- Devany on r.a.s.f1
    Phil Carmody, Oct 31, 2009
    #4
  5. Beej Jorgensen <> writes:
    > jake <> wrote:
    >>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.

    >
    > Better answers will be found on comp.programmer.unix.


    Correction: comp.unix.programmer.

    > A quick glance looks like it's demonstrating Peterson's Algorithm:
    >
    > http://en.wikipedia.org/wiki/Peterson's_algorithm


    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Oct 31, 2009
    #5
  6. jake

    jacob navia Guest

    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

    :)
    jacob navia, Oct 31, 2009
    #6
  7. Keith Thompson <> wrote:
    >Correction: comp.unix.programmer.


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

    -Beej
    Beej Jorgensen, Oct 31, 2009
    #7
  8. Beej Jorgensen <> writes:
    <snip>
    > 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.

    --
    Ben.
    Ben Bacarisse, Oct 31, 2009
    #8
  9. Ben Bacarisse <> wrote:
    >Beej Jorgensen <> writes:
    ><snip>
    >> 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.


    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
    Beej Jorgensen, Oct 31, 2009
    #9
  10. jake

    Moi Guest

    On Sat, 31 Oct 2009 02:20:17 +0200, Phil Carmody wrote:

    > jake <> writes:
    >> 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
    Moi, Oct 31, 2009
    #10
  11. jake

    jacob navia Guest

    Malcolm McLean a écrit :
    > "Phil Carmody" <> wrote in message news:
    >> 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.
    >>

    > 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.
    jacob navia, Oct 31, 2009
    #11
  12. "jacob navia" <> wrote in message
    news:hcg1o7$l3o$...
    > 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.
    Chris M. Thomasson, Nov 1, 2009
    #12
  13. "Beej Jorgensen" <> wrote in message
    news:hcfual$l0s$-september.org...
    > jake <> wrote:
    >>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.

    >
    > 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?
    Chris M. Thomasson, Nov 1, 2009
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Slain
    Replies:
    5
    Views:
    351
    James Kanze
    Jul 18, 2007
  2. parjit

    Understanding some code

    parjit, Aug 24, 2010, in forum: C Programming
    Replies:
    40
    Views:
    1,080
    Seebs
    Aug 29, 2010
  3. Frank Church
    Replies:
    7
    Views:
    207
    Morton Goldberg
    Jun 26, 2007
  4. N. Demos

    Need help understanding some JS code.

    N. Demos, Nov 20, 2005, in forum: Javascript
    Replies:
    3
    Views:
    132
  5. Thomas Foster

    I need help understanding some code..

    Thomas Foster, Mar 5, 2014, in forum: Ruby
    Replies:
    3
    Views:
    133
    Robert Klemme
    Mar 7, 2014
Loading...

Share This Page