How to create a semaphore for a file

Discussion in 'C++' started by azi, Aug 9, 2003.

  1. azi

    azi Guest

    I have created a file and I'm trying to create a semaphore for it using
    POSIX system calls. My program creates children processes who will open
    the file and write their pid in the file at the same time.

    This is my file:

    void Parent process()

    File *p, *p2;
    p=fopen("file1", "w+");
    if(p==NULL) //error
    else
    fprintf(p,"%u", getpid())
    pclose(p);

    p2=fopen("file2", "w+");
    if(p2==NULL) //error
    else
    fprintf(p2,"%u", getpid())
    pclose(p2);


    void child1(){
    pid=fork();
    if (pid==0)
    //open file1 and write its pid there
    //open file2
    //close file2
    //close file1
    }

    void child1(){
    pid=fork();
    if (pid==0)
    //open file2 and write its pid there
    //open file1
    //close file1
    //close file2
    }

    --
    Posted via http://dbforums.com
    azi, Aug 9, 2003
    #1
    1. Advertising

  2. azi

    Jack Klein Guest

    On Sat, 09 Aug 2003 06:24:42 +0000, azi <>
    wrote in comp.lang.c++:

    >
    > I have created a file and I'm trying to create a semaphore for it using
    > POSIX system calls. My program creates children processes who will open
    > the file and write their pid in the file at the same time.


    [snip]

    Neither POXIS nor semaphores are supported by the C++ language, and
    are off-topic here. Ask this in news:comp.unix.programmer, or perhaps
    a group for your specific *nix flavor, such as
    news:comp.os.linux.development.*

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c /faq
    Jack Klein, Aug 9, 2003
    #2
    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. dede
    Replies:
    0
    Views:
    572
  2. Dima
    Replies:
    0
    Views:
    2,227
  3. Frank Gerlach
    Replies:
    34
    Views:
    15,212
  4. techi_C
    Replies:
    2
    Views:
    1,394
    Richard Bos
    Aug 10, 2006
  5. GHUM
    Replies:
    5
    Views:
    735
    Tim Golden
    Aug 29, 2008
Loading...

Share This Page