LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-27-2016, 06:13 PM   #1
argamanza
LQ Newbie
 
Registered: Mar 2016
Posts: 1

Rep: Reputation: Disabled
Can't get 3 processes to work together in synchronization (C + Linux)


I have a process with 2 child processes which are paused, and i'm trying to get the main process to "wake" one of them using a signal, wait for it to finish, make it pause again and wake the other one, and so on, all of this inside a while loop.

Here is the important parts of the code:

Creating and saving the IDs of the main and two child processes:

Code:
ref = getpid();

p1 = fork();

if(p1 != 0){
    p2 = fork();
}
Pausing the child processes after determining the signal handler

Code:
if(!p1 || !p2){ // player processes
    signal(SIGINT, signalHandler);
    pause();
}
The game loop

Code:
printf("%d entering game!\n",getpid());

while(!(*end_game)){
    printf("Start of while - player is %d\n",*player_turn);
    printf("Start of while - pid is %d\n",getpid());

    if(ref == getpid()){
        if(*player_turn == 1){
            kill(p1, SIGINT);
            usleep(0.5 * SECOND);
        }
        else{
            kill(p2, SIGINT);
            usleep(0.5 * SECOND);
        }
    }

    printf("%d entering IF!\n",getpid());
    usleep(0.2 * SECOND);

    if(!p1 || !p2){
        playTurn(player_turn);

        if(*player_turn == 1){
            printf("First if, player turn is %d\n",*player_turn);
            if(!p1){
                *player_turn = 2;

                kill(ref, SIGINT);

                pause();
            }
        }
        else{
            printf("Second if, player turn is %d\n",*player_turn);
            if(!p2){
                *player_turn = 1;

                kill(ref, SIGINT);

                pause();
            }
        }
    }

    if(ref == getpid()){
        if(*player_turn == 1){
            waitpid(p1,&p1_status,0);
        }
        else{
            waitpid(p2,&p2_status,0);
        }
    }

    usleep(0.3 * SECOND);
}
Some notes: the player_turn is saved inside a shared memory segment, and all the text about entering while and if is for me to debug it.

Here is the output i get:

ref: 3725

p1: 3726

p2: 3727

3725 entering game!

Start of while - player is 1

Start of while - pid is 3725

3726 Received signal 2

3726 entering game!

3725 entering IF!

Start of while - player is 1

Start of while - pid is 3725

3725 entering IF!

Start of while - player is 1

Start of while - pid is 3725

3725 entering IF!

Start of while - player is 1

Start of while - pid is 3725

3725 entering IF!

Start of while - player is 1

Start of while - pid is 3725

3725 entering IF!

Start of while - player is 1

Start of while - pid is 3725

3725 entering IF!


And so on indefinitely... Notice that the player to play can be 1 or 2, it depends on some random function inside my program.

What am i doing wrong? Why the right "player" process is waking up but not doing its job and only the main process is iterating inside the while loop?
 
Old 03-28-2016, 01:02 PM   #2
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940
Quite honestly, "if you intend to do that, you should just be using a subroutine."

The entire purpose of processes/threads is to define units of work that, once launched, can run to completion without interference from anybody. If, instead, you need to "do something, wait for it to finish, then do something else," then your design will be considerably simplified by instead using: a subroutine call.

- - -
P.S.: Please, kindly understand (and assume) that I am not making fun of you, nor making light of you, in saying this.
 
Old 03-29-2016, 07:39 AM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Firstly sundialsvcs is correct. This is a game where the context is global and the only thing which becomes unique is the ownership of the player. Thus you can branch one way versus another and stay within your main process. At least as far as you have described here.

In this architecture, you are not employing the outcome of fork() properly. When the return from fork() is zero, you are the child process, and so from there you follow logic which will execute the intended child actions. You're not checking for that.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
synchronization of two processes with share memory icoming Programming 2 02-27-2011 11:16 PM
Laptop fans work, with processes other than WINE hollywoodb Linux - Hardware 1 09-11-2007 06:46 AM
IPC - suggestions for synchronization of processes emge1 Programming 12 03-27-2007 01:51 PM
email synchronization (not file synchronization) Moebius Linux - Software 6 10-05-2004 05:31 AM
nice does not seem to work in prioritizing large processes?? carlos123 Linux - Software 1 11-22-2003 04:33 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration