Click to See Complete Forum and Search --> : Mutual Exlusion


Nkilian
10-20-2003, 07:18 PM
I need the program to demonstrate that mutual exclusion fails. In other words, the two vaules can get into sum and lose the CPU to make the total not 50
I even tried running a big loop to force one of the processes to lose the cpu. What's wrong here. All i want to have happen is the proccess lose the cpu

when i compile i keep getting 50, 50 as the printout becasue process 1 always finishes and is not preempted by the child or vice versa.

int total, temp = 0, n = 50;

void sum(){

int count;

for (count = 0; count < n; count++){
temp = total;
temp = temp + 1;
total = temp;

}

}

main(void){

total = 0;

fork();
sum();
printf("%d\n",total);

}

bwkaz
10-20-2003, 09:27 PM
(1): Use [/i]] tags please, and indent your code. It's much easier to read. :) Like this:

[code]int total, temp = 0, n = 50;

void sum()
{
int count;

for (count = 0; count < n; count++){
temp = total;
temp = temp + 1;
total = temp;
}
}

main(void)
{
total = 0;

fork();
sum();
printf("%d\n",total);
} (stylistic differences notwithstanding.)

(2): The reason you never see mutual exclusion fail, is because you're using fork. Nothing is shared between the two processes when you fork, and in order for mutual exclusion to even be a problem, two processes (or threads) have to be concurrently accessing some shared resource.

After you call fork(), there are two copies of your entire program (two copies of main, two copies of sum, two copies of total, two copies of temp, two copies of n, and two copies of count). The parent process acts on one copy, and the child process acts on the other.

In short, you can't use fork() to do this, unless you manually set up some POSIX (or SysV) shared memory using shm_open or shm_get first. But that's fairly complicated to do...

You could also use pthread_create() instead of fork(). Threads do share memory.

Nkilian
10-21-2003, 03:22 PM
Thanks, i actually had to use shmget and shared memory libs like that. took a long time! ;]

bsh152s
10-21-2003, 04:51 PM
Originally posted by bwkaz


After you call fork(), there are two copies of your entire program (two copies of main, two copies of sum, two copies of total, two copies of temp, two copies of n, and two copies of count). The parent process acts on one copy, and the child process acts on the other.



For some reason, I thought that the child process uses the same memory the parent does until something is modified/written. I believe this is called "copy on write". Am I confusing this with something else?

bwkaz
10-21-2003, 06:27 PM
Yeah, OK, so I glossed over that. ;)

Copy-on-write is only a performance hack. It makes fork() itself much faster, because you don't have to copy all the memory pages right when you fork(), only when you write to them. But it doesn't change the semantics of fork() at all -- there are still two conceptually different versions of each variable (and each piece of code, though the code is probably shared because I don't know of a single binary that modifies itself at runtime...).

You're right, they do use the same memory until something is written. But just before that write happens, then the process that did the write gets its own private copy. So the two processes still effectively have two copies of everything.

Nkilian
10-21-2003, 11:51 PM
Thanks, its good to know linux peeps like to help out ;p