Find the answer to your Linux question:
Results 1 to 2 of 2
hi i want to develop a simple program: Code: int main() { int sharedvariable = 0; int pid = fork(); if(pid != 0) { cout << "i am the parent ...
  1. #1
    Just Joined!
    Join Date
    Nov 2007
    Posts
    2

    Ipc

    hi i want to develop a simple program:

    Code:
    int main()
    {
    int sharedvariable = 0;
    int pid = fork();
    
    if(pid != 0)
    {
    cout << "i am the parent process and it will increment the value of sharedvariable by 5" << endl;
    while(true)
    {
    ...
    
    sharedvariable+=5;
    cout << "parent process increments the sharedvariable by 5 and the new value is: " << sharedvariable << endl;
    ...
    }
    }
    
    else
    {
    cout << "i am the child process and it will decrement the value of sharedvariable by 3" << endl;
    
    while(true)
    {
    ...
    sharedvariable -= 3;
    cout << "child process decrements the sharedvariable by 3 and the new value is: " <<sharedvariable<< endl;
    ...
    }
    }
    }
    in the above what i want to do is simple. I want two processes (not threads). The two processes share a variable called "sharedvariable". Parent process increments the sharedvariable by 5 and the child process decrements the sharedvariable by 3. But two of them reaches sharedvariable not in the same time. So in the "..." parts of the code i think i should use semaphores. So; when parent process want to reach sharedvariable but if currently child process is modifying the sharedvariable, parent process should sleep until the child process releases the semaphore. also it is same for the child process visa versa. Can you please help me to finish the code or can you please provide some useful links about IPC and semaphores.

    thank you,
    hkullana

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    This is most likely homework, and we obviously can't do your homework for you, but here are some links to get you started:

    man semctl
    man semget
    man semop
    man shmat
    man shmctl
    man shmdt
    man shmget

    Hope this helps.
    --
    Bill

    Old age and treachery will overcome youth and skill.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...