简体   繁体   中英

Counter not increasing inside a for using fork()

I'm working on a little project here and one of the parts requires me to do something if a flag is on and other thing is the flag is off concurrently (using fork() ). Althought I know this could bring mutex problems, I cannot seem to get it working. I don't think I ever get mutex anyway.

A piece of my code looks like this:

int i=0;
int w;
int pos=0;
pid_t pid;
char c[1];
for(i=0;i<len;i++) //len is the length of a file I'm reading. 
{
    pid=fork();
    if(pid)
    {
        wait(&w);
    }
    else
    {
        read(fd,&c,1); //fd is an int referencing a the file i'm reading. Using open(char* [], O_RDONLY);
        printf("I'm son %d, read: %s, pos value: %d\n",getpid(),c,pos);
        if(pos==0)
        {
           printf("I'm son %d, writing out in out1.txt\n",getpid());
           //some writing instructions...
           pos=1;
        }
        else
        {
           printf("I'm son %d, writing out in out2.txt\n",getpid());
           //some writing instructions...
           pos=0;
        }
        exit(0);
    }
}

The problem is that I'm always getting in the same if , thus, I always write only in one of the files and I need to alternate the writing between two files.

Actual output: 
I'm son <NUMBER>, read: 0, pos value: 0
I'm son <NUMBER>, writing out in out1.txt
I'm son <NUMBER>, read: 1, pos value: 0
I'm son <NUMBER>, writing out in out1.txt
I'm son <NUMBER>, read: 0, pos value: 0
I'm son <NUMBER>, writing out in out1.txt
I'm son <NUMBER>, read: 1, pos value: 0
I'm son <NUMBER>, writing out in out1.txt
I'm son <NUMBER>, read: 0, pos value: 0
I'm son <NUMBER>, writing out in out1.txt

Desired output:
I'm son <NUMBER>, read: 0, pos value: 0
I'm son <NUMBER>, writing out in out1.txt
I'm son <NUMBER>, read: 1, pos value: 1
I'm son <NUMBER>, writing out in out2.txt
I'm son <NUMBER>, read: 0, pos value: 0
I'm son <NUMBER>, writing out in out1.txt
I'm son <NUMBER>, read: 1, pos value: 1
I'm son <NUMBER>, writing out in out2.txt
I'm son <NUMBER>, read: 0, pos value: 0
I'm son <NUMBER>, writing out in out1.txt

Thanks!

To get the effect you want, you have to control pos in the parent process. Any changes made by a child are local to the child, and are lost when the child exits.

int i = 0;
int w;
int pos = 0;
pid_t pid;
char c[1];
for (i = 0; i < len; i++, pos = !pos)
{
    pid = fork();
    if (pid)
    {
        wait(&w);
    }
    else
    {
        read(fd, &c, 1);
        printf("I'm son %d, read: %s, pos value: %d\n", getpid(), c, pos);
        if (pos == 0)
        {
           printf("I'm son %d, writing out in out1.txt\n", getpid());
        }
        else
        {
           printf("I'm son %d, writing out in out2.txt\n", getpid());
        }
        exit(0);
    }
}

Note that you should check for an error return from fork() , from wait() and from read() . I've not coded those for you. Also note that != is not the assignment version of the ! operator (unlike += which is the assignment version of the + operator, for example); hence, I had to write pos = !pos .

Immediately after setting pos=1 , each process calls exit(0) so pos can never be set to 1 in the prints. I'm assuming it is initialized to 0 somewhere. Keep in mind, each side of the fork is a complete and largely independent copy of the memory (a process). So changes in one have no impact on the other(s). This is different for threads which may be what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM