栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Linux信号量编程

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Linux信号量编程

Linux信号量编程 信号量编程的一个小应用 代码如下:
#include
#include
#include
#include
#include

union semun{
        int val;
        struct semid_ds *buf;
        unsigned short *array;
        struct seminfo *__buf;
};
void pGetKey(int id)
{
        struct sembuf set;
        set.sem_num = 0;//
        set.sem_op = -1;
        set.sem_flg = SEM_UNDO;
        semop(id,&set,1);
        printf("getKEYn");
}

void vPutBack(int id)
{
        struct sembuf set;
        set.sem_num = 0;
        set.sem_op = 1;
        set.sem_flg = SEM_UNDO;
        semop(id,&set,1);
        printf("Put back Keyn");


}
int main(int argc,char **argv)
{
        key_t key;
        int semid;
        key = ftok(".",2);
        semid = semget(key,1,IPC_CREAT|0666);//获取/创建信号量
        union semun initsem;
        initsem.val = 0;//锁
        semctl(semid,0,SETVAL,initsem);//初始化信号量
        int pid = fork();
        if(pid > 0){
                //去拿锁
                pGetKey(semid);//拿钥匙
                printf("This is fathern");
                vPutBack(semid);//放钥匙
                semctl(semid,0,IPC_RMID);
        }else if(pid == 0){

                printf("This is childn");
                vPutBack(semid);
        }else{
                printf("create errorn");

        }


        return 0;
}

通过上边的代码运行后,就能确保子进程比父进程先运行,通过信号量来控制。

函数原型:

NAME
semop, semtimedop - System V semaphore operations

SYNOPSIS
#include
#include
#include

   int semop(int semid, struct sembuf *sops, size_t nsops);

   int semtimedop(int semid, struct sembuf *sops, size_t nsops,
                  const struct timespec *timeout);

Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

   semtimedop(): _GNU_SOURCE

DEscriptION
Each semaphore in a System V semaphore set has the following associated values:

       unsigned short  semval;   
       unsigned short  semzcnt;  
       unsigned short  semncnt;  
       pid_t           sempid;   
       short          sem_op;   
       short          sem_flg;  

   Flags recognized in sem_flg are IPC_NOWAIT  and  SEM_UNDO.   If  an  operation  specifies  SEM_UNDO, it will be automatically undone when the process terminates.

NAME
semctl - System V semaphore control operations

SYNOPSIS
#include
#include
#include

   int semctl(int semid, int semnum, int cmd, ...);

DEscriptION
semctl() performs the control operation specified by cmd on the System V semaphore set identified by semid, or on the semnum-th semaphore of that set. (The semaphores in a set are numbered starting at 0.)

   This  function  has  three or four arguments, depending on cmd.  When there are four, the
   fourth has the type union semun.  The calling program must define this union as follows:

       union semun {
           int              val;    
           struct semid_ds *buf;    
           unsigned short  *array;  
           struct seminfo  *__buf;  
       };

   The semid_ds data structure is defined in  as follows:

       struct semid_ds {
           struct ipc_perm sem_perm;  
           time_t          sem_otime; 
           time_t          sem_ctime; 
           unsigned long   sem_nsems; 
       };

运行结果:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/307975.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号