当前位置: 技术问答>linux和unix
高分等,这样用信号量有问题吗?
来源: 互联网 发布时间:2016-04-18
本文导语: 用以下方式使用信号量控制多进程互斥有问题吗? 能起到互斥效果吗?谢谢 #include #include #include #include #include #include #include #include #include #include #include #include #include sem_t g_icc_reader_sem; void Proces...
用以下方式使用信号量控制多进程互斥有问题吗?
能起到互斥效果吗?谢谢
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
sem_t g_icc_reader_sem;
void Process1()
{
int rtn;
MonitorLog("1# g_icc_reader_sem = %dn", &g_icc_reader_sem);
while(1)
{
sem_wait(&g_icc_reader_sem);
MonitorLog("1111#!n");
MonitorLog("1111@n");
MonitorLog("1111%n");
sem_post(&g_icc_reader_sem);
sleep(5);
//sleep(5);
}//end while
}
void Process2()
{
int rtn;
int atrLen;
int nWriteLen;
int nCount;
int nCmdLen;
char value[200];
uchar dataBuf[200];
uchar testbuf[50];
char testbuf2[20];
memset(testbuf2, 0, 20);
memset(testbuf, 0, 50);
strcpy(testbuf, "E116.40226;N39.98754;30#");
MonitorLog("2# g_icc_reader_sem = %dn", &g_icc_reader_sem);
while(1)
{
sem_wait(&g_icc_reader_sem);
MonitorLog("222#!n");
MonitorLog("222@n");
MonitorLog("2222%n");
rtn = 0;
sem_post(&g_icc_reader_sem);
sleep(5);
//sleep(5);
}//end while
}
void Process3()
{
int rtn;
int atrLen;
int nWriteLen;
int nCount;
int nCmdLen;
char value[200];
uchar dataBuf[200];
uchar testbuf[50];
char testbuf2[20];
memset(testbuf2, 0, 20);
memset(testbuf, 0, 50);
strcpy(testbuf, "E116.40226;N39.98754;30#");
MonitorLog("3# g_icc_reader_sem = %dn", &g_icc_reader_sem);
while(1)
{
sem_wait(&g_icc_reader_sem);
MonitorLog("3333#!n");
MonitorLog("3333@n");
MonitorLog("3333%n");
sem_post(&g_icc_reader_sem);
sleep(5);
//sleep(5);
}//end while
}
pid_t StartProcess1()
{
pid_t pid;
pid=fork();
if(pid==0)
{
Process1();
exit(0); //子进程退出
}
return pid;
}
pid_t StartProcess2()
{
pid_t pid;
pid=fork();
if(pid==0)
{
Process2();
exit(0);
}
return pid;
}
pid_t StartProcess3()
{
pid_t pid;
pid=fork();
if(pid==0)
{
Process3();
exit(0);
}
return pid;
}
int main()
{
int rtn;
pid_t pid1;
pid_t pid2;
pid_t pid3;
sem_init(&g_icc_reader_sem, 0, 1);
CheckMonitorLogNum(); //检测日志文件数量
pid1 = StartProcess1();
if(pid1
能起到互斥效果吗?谢谢
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
sem_t g_icc_reader_sem;
void Process1()
{
int rtn;
MonitorLog("1# g_icc_reader_sem = %dn", &g_icc_reader_sem);
while(1)
{
sem_wait(&g_icc_reader_sem);
MonitorLog("1111#!n");
MonitorLog("1111@n");
MonitorLog("1111%n");
sem_post(&g_icc_reader_sem);
sleep(5);
//sleep(5);
}//end while
}
void Process2()
{
int rtn;
int atrLen;
int nWriteLen;
int nCount;
int nCmdLen;
char value[200];
uchar dataBuf[200];
uchar testbuf[50];
char testbuf2[20];
memset(testbuf2, 0, 20);
memset(testbuf, 0, 50);
strcpy(testbuf, "E116.40226;N39.98754;30#");
MonitorLog("2# g_icc_reader_sem = %dn", &g_icc_reader_sem);
while(1)
{
sem_wait(&g_icc_reader_sem);
MonitorLog("222#!n");
MonitorLog("222@n");
MonitorLog("2222%n");
rtn = 0;
sem_post(&g_icc_reader_sem);
sleep(5);
//sleep(5);
}//end while
}
void Process3()
{
int rtn;
int atrLen;
int nWriteLen;
int nCount;
int nCmdLen;
char value[200];
uchar dataBuf[200];
uchar testbuf[50];
char testbuf2[20];
memset(testbuf2, 0, 20);
memset(testbuf, 0, 50);
strcpy(testbuf, "E116.40226;N39.98754;30#");
MonitorLog("3# g_icc_reader_sem = %dn", &g_icc_reader_sem);
while(1)
{
sem_wait(&g_icc_reader_sem);
MonitorLog("3333#!n");
MonitorLog("3333@n");
MonitorLog("3333%n");
sem_post(&g_icc_reader_sem);
sleep(5);
//sleep(5);
}//end while
}
pid_t StartProcess1()
{
pid_t pid;
pid=fork();
if(pid==0)
{
Process1();
exit(0); //子进程退出
}
return pid;
}
pid_t StartProcess2()
{
pid_t pid;
pid=fork();
if(pid==0)
{
Process2();
exit(0);
}
return pid;
}
pid_t StartProcess3()
{
pid_t pid;
pid=fork();
if(pid==0)
{
Process3();
exit(0);
}
return pid;
}
int main()
{
int rtn;
pid_t pid1;
pid_t pid2;
pid_t pid3;
sem_init(&g_icc_reader_sem, 0, 1);
CheckMonitorLogNum(); //检测日志文件数量
pid1 = StartProcess1();
if(pid1