ºìÁªLinuxÃÅ»§
Linux°ïÖú

linux½ø³ÌͨÐÅ-ÐźÅÁ¿Ê¹ÓÃ

·¢²¼Ê±¼ä:2007-01-06 01:04:00À´Ô´:ºìÁª×÷Õß:wuhu911
Ò»¡¢Ïà¹Ø֪ʶ
ÐźÅÁ¿£ºÒ»¸öÕûÊý£»
´óÓÚ»òµÈÓÚ0ʱ´ú±í¿É¹©²¢·¢½ø³ÌʹÓõÄ×ÊԴʵÌåÊý£»
СÓÚ0ʱ´ú±íÕýÔڵȴýʹÓÃÁÙ½çÇøµÄ½ø³ÌÊý£»
ÓÃÓÚ»¥³âµÄÐźÅÁ¿³õʼֵӦ´óÓÚ0£»
Ö»ÄÜͨ¹ýP¡¢VÔ­Óï²Ù×÷¶ø¸Ä±ä£»
ÐźÅÁ¿ÔªËØ×é³É£º
1¡¢±íʾÐźÅÁ¿ÔªËصÄÖµ£»
2¡¢×îºó²Ù×÷ÐźÅÁ¿ÔªËصĽø³ÌID
3¡¢µÈ´ýÐźÅÁ¿ÔªËØÖµ+1µÄ½ø³ÌÊý£»
4¡¢µÈ´ýÐźÅÁ¿ÔªËØֵΪ0µÄ½ø³ÌÊý£»

¶þ¡¢Ö÷Òªº¯Êý

1.1 ´´½¨ÐźÅÁ¿
int semget(
key_t key, //±êʶÐźÅÁ¿µÄ¹Ø¼ü×Ö£¬ÓÐÈýÖÖ·½·¨£º1¡¢Ê¹ÓÃIPC----PRIVATEÈÃϵͳ²úÉú£¬
// 2¡¢ÌôÑ¡Ò»¸öËæ»úÊý£¬3¡¢Ê¹ÓÃftok´ÓÎļþ·¾¶ÃûÖвúÉú
int nSemes, //ÐźÅÁ¿¼¯ÖÐÔªËظöÊý
int flag //IPC_CREAT£»IPC_EXCL Ö»ÓÐÔÚÐźÅÁ¿¼¯²»´æÔÚʱ´´½¨
)
³É¹¦£º·µ»ØÐźÅÁ¿¾ä±ú
ʧ°Ü£º·µ»Ø-1

1.2 ʹÓÃftokº¯Êý¸ù¾ÝÎļþ·¾¶Ãû²úÉúÒ»¸ö¹Ø¼ü×Ö
key_t ftok(const char *pathname,int proj_id);
·¾¶Ãû³Æ±ØÐëÓÐÏàӦȨÏÞ

1.3 ¿ØÖÆÐźÅÁ¿
int semctl(
int semid, //ÐźÅÁ¿¼¯µÄ¾ä±ú
int semnum, //ÐźÅÁ¿¼¯µÄÔªËØÊý
int cmd, //ÃüÁî
/*union senum arg */... //
)
³É¹¦£º·µ»ØÏàÓ¦µÄÖµ
ʧ°Ü£º·µ»Ø-1

ÃüÁîÏêϸ˵Ã÷:
cmd: IPC_RMID ɾ³ýÒ»¸öÐźÅÁ¿
IPC_EXCL Ö»ÓÐÔÚÐźÅÁ¿¼¯²»´æÔÚʱ´´½¨
IPC_SET ÉèÖÃÐźÅÁ¿µÄÐí¿ÉȨ
SETVAL ÉèÖÃÖ¸¶¨ÐźÅÁ¿µÄÔªËصÄֵΪ agc.val
GETVAL »ñµÃÒ»¸öÖ¸¶¨ÐźÅÁ¿µÄÖµ
GETPID »ñµÃ×îºó²Ù×Ý´ËÔªËصÄ×îºó½ø³ÌID
GETNCNT »ñµÃµÈ´ýÔªËرäΪ1µÄ½ø³ÌÊý
GETZCNT »ñµÃµÈ´ýÔªËرäΪ0µÄ½ø³ÌÊý

union senum ¶¨ÒåÈçÏ£º
union senum{
int val;
struct semid_ds *buf;
unsigned short * array;
}agc;
ÆäÖÐ semid_ds ¶¨ÒåÈçÏ£º
struct semid_ds{
struct ipc_pem sem_pem; //operation pemission struct
time_t sem_otime; //last semop()time
time_t sem_ctime; //last time changed by semctl()
struct sem *sembase; //ptr to first semaphore in array
struct sem_queue *sem_pending; //pending operations
struct sem_queue *sem_pending_last; //last pending operations
struct sem_undo *undo; //undo requests on this arrary
unsigned short int sem_nsems; //number of semaphores in set
};

1.4 ¶ÔÐźÅÁ¿ +1 »ò -1 »ò²âÊÔÊÇ·ñΪ0
int semop(
int semid,
struct sembuf *sops, //Ö¸ÏòÔªËØÙ×÷Ê??BR> unsigned short nsops //Êý×éÖÐÔªËزÙ×÷µÄ¸öÊý
)

½á¹¹ sembuf ¶¨Òå
sembuf{
short int sem_num; //semaphore number
short int sem_op; //semaphore operaion
short int sem_flg //operation flag
};

Èý¡¢Àý×Ó:
2.1 ·þÎñÆ÷

#include
#include

#define SEGSIZE 1024
#define READTIME 1
union semun {
int val;
struct semid_ds *buf;
unsigned short *array;
} arg;
//Éú³ÉÐźÅÁ¿
int sem_creat(key_t key)
{
union semun sem;
int semid;
sem.val = 0;
semid = semget(key,1,IPC_CREAT|0666);
if (-1 == semid){
printf("create semaphore error\n");
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)
{
struct sembuf sops={0,+1,IPC_NOWAIT};
return (semop(semid,&sops,1));
}
//v
int v(int semid)
{
struct sembuf sops={0,-1,IPC_NOWAIT};
return (semop(semid,&sops,1));
}
int main()
{
key_t key;
int shmid,semid;
char *shm;
char msg[7] = "-data-";
char i;
struct semid_ds buf;

key = ftok("/",0);
shmid = shmget(key,SEGSIZE,IPC_CREAT|0604);
if (-1 == shmid){
printf(" create shared memory error\n");
return -1;
}
shm = (char *)shmat(shmid,0,0);
if (-1 == (int)shm){
printf(" attach shared memory error\n");
return -1;
}
semid = sem_creat(key);
for (i = 0;i <= 3;i++){
sleep(1);
p(semid);
sleep(READTIME);
msg[5] = '0' + i;
memcpy(shm,msg,sizeof(msg));
sleep(58);
v(semid);
}
shmdt(shm);
shmctl(shmid,IPC_RMID,&buf);
del_sem(semid);
return 0;
//gcc -o shm shm.c -g
}

2.2 ¿Í»§¶Ë

#include
#include
#include

#define SEGSIZE 1024
#define READTIME 1
union semun {
int val;
struct semid_ds *buf;
unsigned short *array;
} arg;

// ´òÓ¡³ÌÐòÖ´ÐÐʱ¼ä
void out_time(void)
{
static long start = 0;
time_t tm;
if (0 == start){
tm = time(NULL);
start = (long)tm;
printf(" now start ...\n");
}
printf(" second: %ld \n",(long)(time(NULL)) - start);
}

//´´½¨ÐźÅÁ¿
int new_sem(key_t key)
{
union semun sem;
int semid;
sem.val = 0;
semid = semget(key,0,0);
if (-1 == semid){
printf("create semaphore error\n");
exit(-1);
}
return semid;
}

//µÈ´ýÐźÅÁ¿±ä³É0
void wait_v(int semid)
{
struct sembuf sops={0,0,0};
semop(semid,&sops,1);
}

int main(void)
{
key_t key;
int shmid,semid;
char *shm;
char msg[100];
char i;

key = ftok("/",0);
shmid = shmget(key,SEGSIZE,0);

if(-1 == shmid){
printf(" create shared memory error\n");
return -1;
}
shm = (char *)shmat(shmid,0,0);
if (-1 == (int)shm){
printf(" attach shared memory error\n");
return -1;
}
semid = new_sem(key);
for (i = 0;i < 3;i ++){
sleep(2);
wait_v(semid);
printf("Message geted is: %s \n",shm + 1);
out_time();
}
shmdt(shm);
return 0;
// gcc -o shmc shmC.c -g
}
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 1 ÌõÆÀÂÛ

  1. love601 ÓÚ 2007-01-06 23:57:43·¢±í:

    ÊܽÌÁË!!¶àлÁË