加入收藏 | 设为首页 | 会员中心 | 我要投稿 瑞安网 (https://www.ruian888.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 搭建环境 > Linux > 正文

System V IPC 之信号量

发布时间:2021-02-18 10:03:00 所属栏目:Linux 来源:网络整理
导读:",而 System V 信号量的引用头文件是 " "。 解决互斥共享资源的同步问题 而引入的机制。信号量的实质是整数计数器,其中 记录了可供访问的共享资源的单元个数 。本文接下来提到的信号量都特指 System V IPC 信号量。 sem * sem_perm :对应于该信号量集的 i

<span style="color: #0000ff">int main(<span style="color: #0000ff">int argc,<span style="color: #0000ff">char *<span style="color: #000000">argv)
{
<span style="color: #0000ff">if(argc < <span style="color: #800080">3<span style="color: #000000">){
server();
}
<span style="color: #0000ff">else<span style="color: #000000">{
client(atoi(argv[<span style="color: #800080">1]),atoi(argv[<span style="color: #800080">2<span style="color: #000000">]));
}
<span style="color: #0000ff">return <span style="color: #800080">0<span style="color: #000000">;
}

<span style="color: #0000ff">void server(<span style="color: #0000ff">void<span style="color: #000000">)
{
union semun sunion;
<span style="color: #0000ff">int<span style="color: #000000"> semid,shmid;
<span style="color: #0000ff">char *<span style="color: #000000">buffer;
semid = safesemget(IPC_PRIVATE,<span style="color: #800080">2,SHM_R|<span style="color: #000000">SHM_W);
deleteSemid =<span style="color: #000000"> semid;
<span style="color: #008000">//<span style="color: #008000"> 在服务器端程序退出时删除掉信号量集。
atexit(&<span style="color: #000000">delete);
signal(SIGINT,&<span style="color: #000000">sigdelete);
<span style="color: #008000">//<span style="color: #008000"> 把第一个信号量设置为 1,第二个信号量设置为 0,<span style="color: #008000">//<span style="color: #008000"> 这样来控制:必须在客户端程序把数据写入共享内存后服务器端程序才能去读共享内存
sunion.val = <span style="color: #800080">1<span style="color: #000000">;
safesemctl(semid,SN_EMPTY,SETVAL,sunion);
sunion.val = <span style="color: #800080">0<span style="color: #000000">;
safesemctl(semid,SN_FULL,sunion);
shmid = safeshmget(IPC_PRIVATE,SHMDATASIZE,IPC_CREAT|SHM_R|<span style="color: #000000">SHM_W);
buffer = safeshmat(shmid,<span style="color: #800080">0,<span style="color: #800080">0<span style="color: #000000">);
safeshmctl(shmid,IPC_RMID,NULL);
<span style="color: #008000">//<span style="color: #008000"> 打印共享内存 ID 和 信号量集 ID,客户端程序需要用它们作为参数
printf(<span style="color: #800000">"<span style="color: #800000">Server is running with SHM id %dn<span style="color: #800000">"<span style="color: #000000">,shmid);
printf(<span style="color: #800000">"<span style="color: #800000">Server is running with SEM id %dn<span style="color: #800000">"<span style="color: #000000">,semid);
<span style="color: #0000ff">while(<span style="color: #800080">1<span style="color: #000000">)
{
printf(<span style="color: #800000">"<span style="color: #800000">Waiting until full...<span style="color: #800000">"<span style="color: #000000">);
fflush(stdout);
locksem(semid,SN_FULL);
printf(<span style="color: #800000">"<span style="color: #800000">done.n<span style="color: #800000">"<span style="color: #000000">);
printf(<span style="color: #800000">"<span style="color: #800000">Message received: %s.n<span style="color: #800000">"<span style="color: #000000">,buffer);
unlocksem(semid,SN_EMPTY);
}
}

<span style="color: #0000ff">void client(<span style="color: #0000ff">int shmid,<span style="color: #0000ff">int<span style="color: #000000"> semid)
{
<span style="color: #0000ff">char *<span style="color: #000000">buffer;
buffer = safeshmat(shmid,<span style="color: #800080">0<span style="color: #000000">);
printf(<span style="color: #800000">"<span style="color: #800000">Client operational: shm id is %d,sem id is %dn<span style="color: #800000">"<span style="color: #000000">,shmid,semid);
<span style="color: #0000ff">while(<span style="color: #800080">1<span style="color: #000000">)
{
<span style="color: #0000ff">char input[<span style="color: #800080">3<span style="color: #000000">];
printf(<span style="color: #800000">"<span style="color: #800000">nnMenun1.Send a messagen<span style="color: #800000">"<span style="color: #000000">);
printf(<span style="color: #800000">"<span style="color: #800000">2.Exitn<span style="color: #800000">"<span style="color: #000000">);
fgets(input,<span style="color: #0000ff">sizeof<span style="color: #000000">(input),stdin);
<span style="color: #0000ff">switch(input[<span style="color: #800080">0<span style="color: #000000">])
{
<span style="color: #0000ff">case <span style="color: #800000">'<span style="color: #800000">1<span style="color: #800000">'<span style="color: #000000">:
clientwrite(shmid,semid,buffer);
<span style="color: #0000ff">break<span style="color: #000000">;
<span style="color: #0000ff">case <span style="color: #800000">'<span style="color: #800000">2<span style="color: #800000">'<span style="color: #000000">:
exit(<span style="color: #800080">0<span style="color: #000000">);
<span style="color: #0000ff">break<span style="color: #000000">;
}
}
}

(编辑:瑞安网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读