实施信号灯 [英] semaphore implementation

查看:151
本文介绍了实施信号灯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即时得到错误下面的程序..我想证明两个进程如何共享使用旗语可变.....任何人都可以指导我?我不是能够调试错误...

 #包括LT&;&stdlib.h中GT;
#包括LT&;&stdio.h中GT;
#包括LT&;&unistd.h中GT;
#包括LT&; SYS / ipc.h>
#包括LT&; SYS /&sem.h中GT;
#包括LT&;&semaphore.h GT;
诠释的main()
{
  INT PID,互斥= 1;
  INT semid的; / *信号灯设置的semid的* /
  关键的key_t = 1234; / *键传递给了semget()* /
  INT nsems = 1; / * nsems传递给了semget()* /
  semid的= semget子(键,nsems,IPC_CREAT | 0666);
  如果(semid的℃,)
  {
  PERROR(信号量创建失败);
  }
如果((PID =叉())℃,)
{
        PERROR(叉);
        返回1;
}
否则,如果(PID == 0)
{
sem_wait(安培; semid的);
的printf(儿童PROCESS:\\ n);
互斥++;
的printf(共享变量值=%d个,互斥体);
sem_post(安培; semid的);
返回0;
}
sem_wait(安培; semid的);
的printf(在父进程中:\\ n);
mutex--;
的printf(共享变量值=%d个,互斥体);
sem_post(安培; semid的);
返回0;
}


解决方案

您基本面是错误的,该程序将无法正常工作,所以要经过基础知识,并重写程序。

有些则必须改正的是:

1)你必须信号灯类型的变量

  sem_t semvar;

2)的功能 sem_wait() sem_post()要求信号变量,但你传递信号ID,这是没有意义的。

  sem_wait(安培; semvar);
   //你的关键部分code
sem_post(安培; semvar);

3)你是传递信号量 sem_wait() sem_post()没有初始化它。您必须使用它之前把它初始化为1(你的情况),或者你将有一个死锁。

  = RET和semctl(semid的,1,SETVAL,SEM);
如果(保留== 1)
     PERROR(信号量初始化失败);

从该名男子页的研究报告信号灯API和经历这个的例如

im getting error in the following program.. i want to demonstrate how two processes can share a variable using semaphore..... can anyone guide me?? im not able to debug the errors...

#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#include<semaphore.h>
int main()
{
  int pid,mutex=1;
  int semid;               /* semid of semaphore set */
  key_t key = 1234; /* key to pass to semget() */
  int nsems = 1; /* nsems to pass to semget() */
  semid=semget(key,nsems,IPC_CREAT|0666);
  if (semid<0) 
  { 
  perror("Semaphore creation failed ");
  }
if ((pid = fork()) < 0) 
{
        perror("fork");
        return 1;
}
else if(pid==0)
{
sem_wait(&semid);
printf("IN CHILD PROCESS :\n");
mutex++; 
printf("value of shared variable =%d",mutex);
sem_post(&semid);
return 0;
}
sem_wait(&semid);
printf("IN PARENT PROCESS :\n");
mutex--;
printf("value of shared variable =%d",mutex);
sem_post(&semid);
return 0;
} 

解决方案

Your Fundamentals are wrong, the program won't work, so go through the basics and rewrite the program.

Some of the corrections you must make are:

1) You must make a variable of semaphore type

sem_t semvar;

2) The functions sem_wait(), sem_post() require the semaphore variable but you are passing the semaphore id, which makes no sense.

sem_wait(&semvar);
   //your critical section code
sem_post(&semvar);

3) You are passing the semaphore to sem_wait() and sem_post() without initializing it. You must initialize it to 1 (in your case) before using it, or you will have a deadlock.

ret = semctl( semid, 1, SETVAL, sem);
if (ret == 1)
     perror("Semaphore failed to initialize");

Study the semaphore API's from the man page and go through this example.

这篇关于实施信号灯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆