2017 三进程同步互斥
作者:互联网
typedef struct { float a; float b; }cnum; cnum x,y,z; cnum add(cnum p,cnum q) { cnum s; s.a=p.a+q.a; s.b=p.b+q.b; return s; } sem mutex1=1;//t1和t2变量y互斥访问 sem mutex2=1;//t2与t3变量y互斥访问 sem mutex3=1;//变量z互斥访问 thread 1 { cnum w; p(mutex1); w=add(x,y); v(mutex1); } thread 2 { cnum w; p(mutex2); p(mutex3); w=add(y,z); v(mutex3); v(mutex2); } thread 3 { cnum w; w.a=1; w.b=1; p(mutex3); z=add(z,w); v(mutex3); p(mutex1); p(mutex2); y=add(y,w); v(mutex1); v(mutex2); }
标签:mutex1,mutex3,进程同步,cnum,mutex2,互斥,add,2017 来源: https://www.cnblogs.com/yangmenda/p/11708554.html