3

As for threads, I have mutex and conditionals so I could manipulate them easily.
However, if I create two processes by fork(), how could I make them alternating?
Or, is there any way to create a "critical section" for processes?

I intended to make a program that prints "r" and "w" alternatively, here is the code.

#include <stdio.h>
#include <stdlib.h>

int pipe_1[2];
int flag = 0;

void r();
void w();

int main() {
    pipe(pipe_1);

    if(fork()) 
        r();
    else
        w();
}

void r() {
    int count = 0;
    while(1) {
        printf("%d \n", flag);
        if (count == 10)
            exit(0);
        if(flag == 0) {
            puts("r");
            flag = 1;
            count++;
            while(flag == 1)
                ;
        }
    }
}

void w() {
    while(1) {
        if(flag == 1) {
            puts("w");
            flag = 0;
            while(flag == 0)
                ;
        }
    }
}

The out put is only:

0
r  

Then it seems to enter a infinite loop.
What's the problem?
And what's the right way to make alternating processes?

Thanks.

4

3 回答 3

1

They are separate processes, so each has it's own flag; r changing its doesn't affect w's.

于 2013-05-05T00:57:03.563 回答
1

This may be overwhelming, but there are TONS of primitives you could use. See here for a list.

http://beej.us/guide/bgipc/output/html/singlepage/bgipc.html

Glancing at the list, just about all of those could be used. Some are more like traditional pthread synchronization primitives, others are higher-level, but can still be used for synchronization.

For example, you could just open a TCP socket between the two and send messages when it's the other side's turn. Maybe with an incrementing number.

Something perhaps more traditional would be semaphores:

http://beej.us/guide/bgipc/output/html/singlepage/bgipc.html#semaphores

Also, this assumes a modern unix-like platform. Windows is likely very different.

It looks like you have a pipe already, so you can use that to have each side send a message to the other after it's done its print. The other side would do a blocking read, then return when the message was sent, do it's print, send a message back, and go back to a blocking read.

于 2013-05-05T00:59:02.953 回答
1

In order for two processes to communicate with each other without sharing the same address space (like threads do), they must use Inter-Process Communication means (aka IPC). Some of the IPC mechanisms are: shared memory, semaphore, pipes, sockets, message queues and more. Most of the time, IPC mechanisms are operating system specific. However, many ideas are general enough so it is possible to come up with a portable implementations, which Boost project did as part of Boost.Interprocess library. What I think you should take a look at first is Synchronization Mechanisms section. Note, however, that this is a C++ library. I am not aware of any C library that is as good as Boost.

Hope it helps. Good Luck!

于 2013-05-05T01:00:31.047 回答