手机版
你好,游客 登录 注册
背景:
阅读新闻

Linux进程间通信之信号量

[日期:2012-08-08] 来源:Linux社区  作者:bullbat [字体:大 中 小]

3,  semop系统调用

最终都调用semtimedop系统调用实现,

SYSCALL_DEFINE4(semtimedop,int, semid, structsembuf __user *, tsops,

                   unsigned,nsops, const structtimespec __user *, timeout)

{

         interror = -EINVAL;

         structsem_array *sma;

         structsembuf fast_sops[SEMOPM_FAST];

         structsembuf* sops = fast_sops, *sop;

         structsem_undo *un;

         intundos = 0, alter = 0, max;

         structsem_queue queue;

         unsignedlong jiffies_left = 0;

         structipc_namespace *ns;

 

         ns = current->nsproxy->ipc_ns;

 

         if(nsops < 1 || semid < 0)

                   return-EINVAL;

         if(nsops > ns->sc_semopm)

                   return-E2BIG;

         if(nsops> SEMOPM_FAST) {

                   sops = kmalloc(sizeof(*sops)*nsops,GFP_KERNEL);

                   if(sops==NULL)

                            return -ENOMEM;

         }

         if(copy_from_user (sops, tsops, nsops * sizeof(*tsops))){

                   error=-EFAULT;

                   gotoout_free;

         }

         if(timeout) {

                   structtimespec _timeout;

                   if(copy_from_user(&_timeout, timeout, sizeof(*timeout))){

                            error = -EFAULT;

                            goto out_free;

                   }

                   if(_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 ||

                            _timeout.tv_nsec>= 1000000000L){

                            error = -EINVAL;

                            goto out_free;

                   }

                   jiffies_left =timespec_to_jiffies(&_timeout);

         }

         max = 0;

         for(sop = sops; sop < sops + nsops; sop++) {

                   if(sop->sem_num >= max)

                            max =sop->sem_num;

                   if(sop->sem_flg & SEM_UNDO)

                            undos = 1;

                   if(sop->sem_op != 0)

                            alter = 1;

         }

 

         if(undos) {

                   un = find_alloc_undo(ns,semid);

                   if(IS_ERR(un)) {

                            error = PTR_ERR(un);

                            goto out_free;

                   }

         } else

                   un = NULL;

 

         sma = sem_lock_check(ns, semid);

         if(IS_ERR(sma)) {

                   if(un)

                            rcu_read_unlock();

                   error = PTR_ERR(sma);

                   gotoout_free;

         }

 

         /*

          * semid identifiers are not unique - find_alloc_undomay have

          * allocated an undo structure, it wasinvalidated by an RMID

          * and now a new array with received the sameid. Check and fail.

          * This case can be detected checkingun->semid. The existance of

          * "un" itself is guaranteed by rcu.

          */

         error = -EIDRM;

         if (un){

                   if(un->semid == -1) {

                            rcu_read_unlock();

                            goto out_unlock_free;

                   } else{

                            /*

                             * rcu lock can be released, "un"cannot disappear:

                             * - sem_lock is acquired, thus IPC_RMID is

                             *  impossible.

                             * - exit_sem is impossible, it always operateson

                             *  current (or a dead task).

                             */

 

                            rcu_read_unlock();

                   }

         }

 

         error = -EFBIG;

         if (max>= sma->sem_nsems)

                   gotoout_unlock_free;

 

         error = -EACCES;

         if(ipcperms(&sma->sem_perm, alter ? S_IWUGO : S_IRUGO))

                   gotoout_unlock_free;

 

         error = security_sem_semop(sma, sops,nsops, alter);

         if(error)

                   gotoout_unlock_free;

 

         error = try_atomic_semop (sma, sops,nsops, un, task_tgid_vnr(current));

         if(error <= 0) {

                   if(alter && error == 0)

                            update_queue (sma);

                   gotoout_unlock_free;

         }

 

         /* We need tosleep on this operation, so we put the current

          * task into the pending queue and go to sleep.

          */

                  

         queue.sops = sops;

         queue.nsops = nsops;

         queue.undo = un;

         queue.pid = task_tgid_vnr(current);

         queue.alter = alter;

         if(alter)

                   list_add_tail(&queue.list,&sma->sem_pending);

         else

                   list_add(&queue.list,&sma->sem_pending);

 

         queue.status = -EINTR;

         queue.sleeper = current;

         current->state = TASK_INTERRUPTIBLE;

         sem_unlock(sma);

 

         if(timeout)

                   jiffies_left =schedule_timeout(jiffies_left);

         else

                   schedule();

 

         error = queue.status;

         while(unlikely(error== IN_WAKEUP)) {

                   cpu_relax();

                   error = queue.status;

         }

 

         if(error != -EINTR) {

                   /*fast path: update_queue already obtained all requested

                    * resources */

                   gotoout_free;

         }

 

         sma = sem_lock(ns, semid);

         if(IS_ERR(sma)) {

                   error = -EIDRM;

                   gotoout_free;

         }

 

         /*

          * If queue.status != -EINTR we are woken up byanother process

          */

         error = queue.status;

         if(error != -EINTR) {

                   gotoout_unlock_free;

         }

 

         /*

          * If an interrupt occurred we have to clean upthe queue

          */

         if(timeout && jiffies_left == 0)

                   error = -EAGAIN;

         list_del(&queue.list);

 

out_unlock_free:

         sem_unlock(sma);

out_free:

         if(sops!= fast_sops)

                   kfree(sops);

         returnerror;

}

Semtimedop系统调用首先从用户空间得到参数值,然后进行权限检查,调用try_atomic_semop函数对sem的semval进行设置,如果需要休眠(semval值为负),将该sem_queue项初始化并添加到待决操作列表,然后重新进行调度,否则调用update_queue对队列其他项进行操作和进程唤醒。

linux
本文评论   查看全部评论 (2)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款
第 2 楼
* 班不女 发表于 2012/8/9 16:15:44
好几个
第 1 楼
* scott.cao 发表于 2012/8/8 18:20:14
还有socket哦