把他的程式重新改動一下,改為一次性為訊號集合申請兩個訊號量:
**變成:
#include #include#include
#include
#include
#define lock -1
#define unlock 1
/*semophore operation (lock/unlock)
*/void semop01(int semid,int
op);
/*semophore operation (lock/unlock)
*/void semop02(int semid,int
op);
intmain()
ctl_arg;
char *file01 = "
./sem01.txt
";
intchild_cnt01;
intline_cnt01;
if ((fp01 = fopen(file01,"
w")) ==null)
setvbuf(fp01,null,_ionbf,
0);
char *file02 = "
./sem02.txt
";
intchild_cnt02;
intline_cnt02;
if ((fp02 = fopen(file02,"
w")) ==null)
setvbuf(fp02,null,_ionbf,
0);
/*to create one semophore array which hold two semophore
*/if ((semid = semget(ipc_private,2,0600)) == -1)/*
initialize the first semophore with value 1
*/ctl_arg.val = 1
;
if (semctl(semid,0,setval,ctl_arg) == -1
)
/*initialize the second semophore with value 1
*/ctl_arg.val = 1
;
if (semctl(semid,1,setval,ctl_arg) == -1
)
/*fork three child process
*/for (child_cnt01 = 0; child_cnt01 < 3; ++child_cnt01)
semop01(semid,unlock);
/*unlock
*/printf(
"child process %d ends\n
",child_cnt01 + 1
); exit(exit_success);
}
}
/*fork another two child process
*/for (child_cnt02 = 0; child_cnt02< 2; ++child_cnt02)
semop02(semid,unlock);
/*unlock
*/printf(
"another child process %d ends\n
",child_cnt02 + 1
); exit(exit_success);
}
}
/*parent process, waitint for child process ending
*/for (child_cnt01 = 0; child_cnt01 < 5; ++child_cnt01)
/*delete semophore array
*/if (semctl(semid,0,ipc_rmid,ctl_arg) == -1
)
fclose(fp01);
fclose(fp02);
printf(
"parenet process ends\n
");
return
exit_success;
}
/*semophore operation (lock/unlock)
*/void semop01(int p_semid,int
p_op)
return
;
}
/*semophore operation (lock/unlock)
*/void semop02(int p_semid,int
p_op)
return
;
}
其中,三個子程序用第乙個訊號量,另外兩個子程序用第二個訊號量。互不干擾。
執行結果:
child process 1 begin
child process 2 begin
child process 3 begin
another child process 1 begin
another child process 2 begin
child process 1 ends
another child process 1 ends
child process 2 ends
another child process 2 ends
child process 3 ends
parenet process ends
檔案內容:
[root@gaorhel01 gao]# cat sem01.txt
child process 1 msg : 1
child process 1 msg : 2
child process 1 msg : 3
child process 1 msg : 4
child process 1 msg : 5
child process 2 msg : 1
child process 2 msg : 2
child process 2 msg : 3
child process 2 msg : 4
child process 2 msg : 5
child process 3 msg : 1
child process 3 msg : 2
child process 3 msg : 3
child process 3 msg : 4
child process 3 msg : 5
[root@gaorhel01 gao]# cat sem02.txxt
cat: sem02.txxt: no such file or directory
[root@gaorhel01 gao]# cat sem02.txt
another child process 1 msg : 1
another child process 1 msg : 2
another child process 1 msg : 3
another child process 1 msg : 4
another child process 1 msg : 5
another child process 2 msg : 1
another child process 2 msg : 2
another child process 2 msg : 3
another child process 2 msg : 4
another child process 2 msg : 5
訊號量集(主要是AND訊號量)
訊號量集 當利用訊號量機制解決了單個資源的互斥訪問後,我們討論如何控制同時需要多個資源的互斥訪問。訊號量集是指同時需要多個資源時的訊號量操作。1 and型訊號量集 and型訊號量集是指同時需要多個資源且每種占用乙個資源時的訊號量操作。當一段處理 需要同時獲取兩個或多個臨界資源時,就可能出現由於各程序...
訊號量集(主要是AND訊號量)
訊號量集 當利用訊號量機制解決了單個資源的互斥訪問後,我們討論如何控制同時需要多個資源的互斥訪問。訊號量集是指同時需要多個資源時的訊號量操作。1 and型訊號量集 and型訊號量集是指同時需要多個資源且每種占用乙個資源時的訊號量操作。當一段處理 需要同時獲取兩個或多個臨界資源時,就可能出現由於各程序...
IPC之 訊號量集 多個訊號量
如果兩個程序不僅需要同步,還要保證先後執行順序,就要用兩個訊號量 互斥鎖 來解決 柵欄模型 實現以下框架中的四個子程序 所有程序做完任務後 在一起執行下一次 include include include include include include include include define ...