这真的让我烦恼,我真的看不出这个代码中的任何错误。它应该用一个进程做五次cout<<"Process: "<<i+1<<", K.O. num: "<<k<<" ("<<m<<"/5)"<<endl;
,而不是转向其他进程,并且不断地在这两个进程之间切换。
这段代码大多是随机的,一个流程永远不会完成它的业务。
而且,当我使用SIGINT时,共享内存段没有删除..
#include <signal.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdlib.h>
#include <unistd.h>
#include <iostream>
using namespace std;
int Id; /* Segment Id */
int *TURN;
int *FLAG_I;
int *FLAG_J;
void get_out_of_critical(int i)
{
if(i==0){
*TURN=1;i=1;
*FLAG_I=0;
}
else{
*TURN=0;i=0;
*FLAG_J=0;
}
}
void get_in_critical(int i)
{
if(i==0){
*FLAG_I=1;
while(*FLAG_J!=0){
if(*TURN==1){
*FLAG_I = 0;
while(*TURN==1){}
*FLAG_I=1;
}
}
}
else{
*FLAG_J=1;
while (*FLAG_I!=0){
if(*TURN==0){
*FLAG_J = 0;
while(*TURN==0){}
*FLAG_J=1;
}
}
}
}
void process(int i)
{
for(int k=1;k<=5;k++){
get_in_critical(i);
for(int m=1;m<=5;m++){
cout<<"Process: "<<i+1<<", K.O. num: "<<k<<" ("<<m<<"/5)"<<endl;
//sleep(1);
}
get_out_of_critical(i);
}
}
void del(int sig)
{
/* free shared memory */
(void) shmdt((char *) TURN);
(void) shmdt((char *) FLAG_I);
(void) shmdt((char *) FLAG_J);
(void) shmctl(Id, IPC_RMID, NULL);
exit(0);
}
int main()
{
cout<<endl;
/* allocating shared memory */
Id = shmget(IPC_PRIVATE, sizeof(int)*100, 0600);
if (Id == -1)
exit(1);
TURN = (int *) shmat(Id, NULL, 0);
*TURN = 0;
FLAG_I = (int*) shmat(Id, NULL, 0);
FLAG_J = (int*) shmat(Id, NULL, 0);
*FLAG_I = 0;
*FLAG_J = 0;
sigset(SIGINT, del);// in case of signal interrupt, delete shared memory
/* starting paralel processes */
if (fork() == 0) {
process(0);
exit(0);
}
if (fork() == 0) {
process(1);
exit(0);
}
wait();
wait();
del(0);
return 0;
}
答案 0 :(得分:3)
关于Dekker's Algorithm的维基百科文章解释了您的代码有什么问题:
[..]但请注意,C / C ++“volatile”属性仅保证编译器生成具有正确排序的代码;它不包括必要的内存屏障来保证该代码的有序执行。 C ++ 11原子变量可用于保证适当的排序要求[..]
因此,您必须对TURN
,FLAG_I
和FLAG_J
使用原子变量和原子操作才能使其正常工作。另见: