当前位置: 技术问答>linux和unix
请问关于信号量的问题
来源: 互联网 发布时间:2015-11-06
本文导语: //createsem.c #include #include #include "common.h" int main() { int semid; /* Create the semaphore with the id MY_SEM_ID */ semid = semget( MY_SEM_ID, 1, 0666 | IPC_CREAT ); if (semid >= 0) { printf( "semcreate: Created a...
//createsem.c
#include
#include
#include "common.h"
int main()
{
int semid;
/* Create the semaphore with the id MY_SEM_ID */
semid = semget( MY_SEM_ID, 1, 0666 | IPC_CREAT );
if (semid >= 0) {
printf( "semcreate: Created a semaphore %dn", semid );
}
return 0;
}
//semacq.c
#include
#include
#include
#include "common.h"
int main()
{
int s e m i d;
struct s e m b u f s b;
/* Get the semaphore with the id MY_SEM_ID */
s e m i d = semget( MY_SEM_ID, 1, 0 );
if (sem_id >= 0)
{
s b.s e m n u m = 0;
s b.s e m o p = -1;
s b.s e m f l g = 0;
printf( "semacq: Attempting to acquire semaphore %dn", semid );
/* Acquire the semaphore */
if ( s e m o p( s e m i d, &s b, 1 ) == -1 ) {
printf( "semacq: semop failed.n" );
exit(-1);
}
printf( "semacq: Semaphore acquired %dn", sem_id );
}
return 0;
}
//semrel.c
#include
#include
#include
#include "common.h"
int main()
{
int s e m i d;
struct s e m b u f sb;
/* Get the semaphore with the id MY_SEM_ID */
semid = semget( MY_SEM_ID, 1, 0 );
if (semid >= 0)
{
printf( "semrel: Releasing semaphore %dn", semid );
s b.s e m n u m = 0;
s b.s e m o p = 1;
s b.s e m f l g = 0;
/* Release the semaphore */
if (s e m o p( s e m i d, &s b, 1 ) == -1)
{
printf("semrel: semop failed.n");
exit(-1);
}
printf( "semrel: Semaphore released %dn", semid );
}
return 0;
}
In shell command prompt,I excuted them like this:
Listing 16.4: Execution of the Sample Semaphore Applications
1: # ./semcreate
2: semcreate: Created a semaphore 1376259
3: # ./semacq &
4: [1] 12189
5: semacq: Attempting to acquire semaphore 1376259
6: # ./semrel
7: semrel: Releasing semaphore 1376259
8: semrel: Semaphore released 1376259
9: # semacq: Semaphore acquired 1376259
10:
11: [1]+ Done ./semacq
12: #
#include
#include
#include "common.h"
int main()
{
int semid;
/* Create the semaphore with the id MY_SEM_ID */
semid = semget( MY_SEM_ID, 1, 0666 | IPC_CREAT );
if (semid >= 0) {
printf( "semcreate: Created a semaphore %dn", semid );
}
return 0;
}
//semacq.c
#include
#include
#include
#include "common.h"
int main()
{
int s e m i d;
struct s e m b u f s b;
/* Get the semaphore with the id MY_SEM_ID */
s e m i d = semget( MY_SEM_ID, 1, 0 );
if (sem_id >= 0)
{
s b.s e m n u m = 0;
s b.s e m o p = -1;
s b.s e m f l g = 0;
printf( "semacq: Attempting to acquire semaphore %dn", semid );
/* Acquire the semaphore */
if ( s e m o p( s e m i d, &s b, 1 ) == -1 ) {
printf( "semacq: semop failed.n" );
exit(-1);
}
printf( "semacq: Semaphore acquired %dn", sem_id );
}
return 0;
}
//semrel.c
#include
#include
#include
#include "common.h"
int main()
{
int s e m i d;
struct s e m b u f sb;
/* Get the semaphore with the id MY_SEM_ID */
semid = semget( MY_SEM_ID, 1, 0 );
if (semid >= 0)
{
printf( "semrel: Releasing semaphore %dn", semid );
s b.s e m n u m = 0;
s b.s e m o p = 1;
s b.s e m f l g = 0;
/* Release the semaphore */
if (s e m o p( s e m i d, &s b, 1 ) == -1)
{
printf("semrel: semop failed.n");
exit(-1);
}
printf( "semrel: Semaphore released %dn", semid );
}
return 0;
}
In shell command prompt,I excuted them like this:
Listing 16.4: Execution of the Sample Semaphore Applications
1: # ./semcreate
2: semcreate: Created a semaphore 1376259
3: # ./semacq &
4: [1] 12189
5: semacq: Attempting to acquire semaphore 1376259
6: # ./semrel
7: semrel: Releasing semaphore 1376259
8: semrel: Semaphore released 1376259
9: # semacq: Semaphore acquired 1376259
10:
11: [1]+ Done ./semacq
12: #
|
进程1只是创建了信号量,还没初始化呢。
进程3的Release过程在第一次执行就相当于是初始化。
进程3的Release过程在第一次执行就相当于是初始化。
|
先问楼主,明白了什么叫信号量了吗?
两个进程都拥有了,怎么来区分谁获得了啊?怎么做进程的互斥啊?
两个进程都拥有了,怎么来区分谁获得了啊?怎么做进程的互斥啊?