当前位置: 技术问答>linux和unix
多个独立的进程使用信号量??
来源: 互联网 发布时间:2015-08-28
本文导语: 我现在想让多个独立的进程使用信号量同步! 程序是这样 服务器: #include #include #include #include #include union semun { int val; /* value for SETVAL */ struct semid_ds *buf; ...
我现在想让多个独立的进程使用信号量同步!
程序是这样
服务器:
#include
#include
#include
#include
#include
union semun {
int val; /* value for SETVAL */
struct semid_ds *buf; /* buffer for IPC_STAT & IPC_SET */
unsigned short *array; /* array for GETALL & SETALL */
struct seminfo *__buf; /* buffer for IPC_INFO */
void *__pad;
};
int sem_creat(key_t key);
void del_sem(int semid);
int p(int semid);
int v(int semid);
int main()
{
union semun sem;
sem.val = 0;
int i=10;
key_t ky=ftok("/var/tmp",7);
int semid=sem_creat(ky);
printf("p:the semid is %dn",semid);
p(semid);
printf("the sem value is %dn", semctl(semid,0,GETVAL,sem));
printf("p:ready to forkn");
pid_t pid=fork();
if (pid==0)
{
execl("/var/tmp/sem","sem",NULL);
}
else
{
sleep(50);
} }
int sem_creat(key_t key)
{
union semun sem;
int semid;
sem.val = 0;
semid = semget(key,1,IPC_EXCL);
if (-1 == semid){
printf("create semaphore errorn");
exit(-1);
}
semctl(semid,0,SETVAL,sem);
return semid;
}
void del_sem(int semid)
{
union semun sem;
sem.val = 0;
semctl(semid,0,IPC_RMID,sem);
}
//p
int p(int semid)
{union semun sem;
sem.val = 0;
struct sembuf sops={0,+1,0};
return (semop(semid,&sops,1));
}
//v
int v(int semid)
{union semun sem;
sem.val = 0;
struct sembuf sops={0,-1,0};
return (semop(semid,&sops,1));
}
///////
客户进程:sem
#include
#include
#include
#include
#include
union semun {
int val; /* value for SETVAL */
struct semid_ds *buf; /* buffer for IPC_STAT & IPC_SET */
unsigned short *array; /* array for GETALL & SETALL */
struct seminfo *__buf; /* buffer for IPC_INFO */
void *__pad;
};
int sem_creat(key_t key);
void del_sem(int semid);
int p(int semid);
int v(int semid);
int main()
{
union semun sem;
sem.val = 0;
int i=10;
key_t ky=ftok("/var/tmp",7);
int semid=sem_creat(ky);
printf("EXEC:the semid is %dn",semid);
printf("EXEC:the sem value is %dn", semctl(semid,0,GETVAL,sem));
v(semid);
printf("EXEC:this is a childn");
sleep(3);
p(semid);
}
int sem_creat(key_t key)
{
union semun sem;
int semid;
sem.val = 0;
semid = semget(key,1,IPC_EXCL);
if (-1 == semid){
printf("create semaphore errorn");
exit(-1);
}
semctl(semid,0,SETVAL,sem);
return semid;
}
但是sem进程会一直等待,因为信号量是0,但是我在服务器给它加1了啊!!???
还有为什么包含头文件sem.h还是会报错说 union semun不知道存储空间!!
请大家指教,或是指导一下看什么书??
程序是这样
服务器:
#include
#include
#include
#include
#include
union semun {
int val; /* value for SETVAL */
struct semid_ds *buf; /* buffer for IPC_STAT & IPC_SET */
unsigned short *array; /* array for GETALL & SETALL */
struct seminfo *__buf; /* buffer for IPC_INFO */
void *__pad;
};
int sem_creat(key_t key);
void del_sem(int semid);
int p(int semid);
int v(int semid);
int main()
{
union semun sem;
sem.val = 0;
int i=10;
key_t ky=ftok("/var/tmp",7);
int semid=sem_creat(ky);
printf("p:the semid is %dn",semid);
p(semid);
printf("the sem value is %dn", semctl(semid,0,GETVAL,sem));
printf("p:ready to forkn");
pid_t pid=fork();
if (pid==0)
{
execl("/var/tmp/sem","sem",NULL);
}
else
{
sleep(50);
} }
int sem_creat(key_t key)
{
union semun sem;
int semid;
sem.val = 0;
semid = semget(key,1,IPC_EXCL);
if (-1 == semid){
printf("create semaphore errorn");
exit(-1);
}
semctl(semid,0,SETVAL,sem);
return semid;
}
void del_sem(int semid)
{
union semun sem;
sem.val = 0;
semctl(semid,0,IPC_RMID,sem);
}
//p
int p(int semid)
{union semun sem;
sem.val = 0;
struct sembuf sops={0,+1,0};
return (semop(semid,&sops,1));
}
//v
int v(int semid)
{union semun sem;
sem.val = 0;
struct sembuf sops={0,-1,0};
return (semop(semid,&sops,1));
}
///////
客户进程:sem
#include
#include
#include
#include
#include
union semun {
int val; /* value for SETVAL */
struct semid_ds *buf; /* buffer for IPC_STAT & IPC_SET */
unsigned short *array; /* array for GETALL & SETALL */
struct seminfo *__buf; /* buffer for IPC_INFO */
void *__pad;
};
int sem_creat(key_t key);
void del_sem(int semid);
int p(int semid);
int v(int semid);
int main()
{
union semun sem;
sem.val = 0;
int i=10;
key_t ky=ftok("/var/tmp",7);
int semid=sem_creat(ky);
printf("EXEC:the semid is %dn",semid);
printf("EXEC:the sem value is %dn", semctl(semid,0,GETVAL,sem));
v(semid);
printf("EXEC:this is a childn");
sleep(3);
p(semid);
}
int sem_creat(key_t key)
{
union semun sem;
int semid;
sem.val = 0;
semid = semget(key,1,IPC_EXCL);
if (-1 == semid){
printf("create semaphore errorn");
exit(-1);
}
semctl(semid,0,SETVAL,sem);
return semid;
}
但是sem进程会一直等待,因为信号量是0,但是我在服务器给它加1了啊!!???
还有为什么包含头文件sem.h还是会报错说 union semun不知道存储空间!!
请大家指教,或是指导一下看什么书??
|
use "semctl(semid, 0, SETVAL, arg)" to initialize the semaphore in server.
|
要自己定义semun的结构,你可以参考semctl的man手册
#if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
/* union semun is defined by including */
#else
/* according to X/OPEN we have to define it ourselves */
union semun {
int val; /* value for SETVAL */
struct semid_ds *buf; /* buffer for IPC_STAT, IPC_SET */
unsigned short *array; /* array for GETALL, SETALL */
/* Linux specific part: */
struct seminfo *__buf; /* buffer for IPC_INFO */
};
#endif
欢迎访问我的个人网站 www.linuxc.net
#if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
/* union semun is defined by including */
#else
/* according to X/OPEN we have to define it ourselves */
union semun {
int val; /* value for SETVAL */
struct semid_ds *buf; /* buffer for IPC_STAT, IPC_SET */
unsigned short *array; /* array for GETALL, SETALL */
/* Linux specific part: */
struct seminfo *__buf; /* buffer for IPC_INFO */
};
#endif
欢迎访问我的个人网站 www.linuxc.net
|
semaphore 只是线程共享吧.