当前位置:网站首页>利用信号灯和共享内存实现进程间同步通信
利用信号灯和共享内存实现进程间同步通信
2022-08-09 13:05:00 【zhouyongku】
编写两个程序,利用同一个文件路径进行全局Key创建,然后创建两个信号灯,一个是读通知,一个是写通知。
发送端:
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<unistd.h>
#include<time.h>
#include<sys/time.h>
#include<sys/sem.h>
using namespace std;
#define LIGHT_READ 0
#define LIGHT_WRITE 1
long long syslocaltime()
{
struct timeval tmstart;
gettimeofday(&tmstart,NULL);
long long a=tmstart.tv_sec*1000+tmstart.tv_usec/1000;
printf("second=%lld,usec=%lld",tmstart.tv_sec,tmstart.tv_usec);
}
void Wait(int semid,int nId) //占用资源(拿走绿灯)
{
struct sembuf sops;
sops.sem_num = nId; /* 要操作的信号量的编号为0 */
sops.sem_op = -1; /* Wait for value to equal 0 */
sops.sem_flg = 0; /* 既不IPC_NOWAIT,也不IPC_UNDO */
printf("wait semop start semid=%d,channel=%d\n",semid,nId);
if (semop(semid, &sops, 1) == -1)
{
printf("wait semop failed, infor\r\n" );
}
else
{
printf("wait semop success semid=%d,channel=%d\n",semid,nId);
}
}
void Post(int semid,int nId) //释放资源(归还绿灯)
{
struct sembuf sops;
sops.sem_num = nId; /* 要操作的信号量的编号为0 */
sops.sem_op = 1; /* Wait for value to equal 0 */
sops.sem_flg = 0; /* 既不IPC_NOWAIT,也不IPC_UNDO */
printf("wait semop start semid=%d,channel=%d\n",semid,nId);
if (semop(semid, &sops, 1) == -1)
{
printf("post semop failed, infor\r\n" );
}
else
{
printf("post semop success semid=%d,channel=%d\n",semid,nId);
}
}
const int MAX_BUF_SIZE=1000*1000;
const int listlen=10;
int main(int argc,char ** argv )
{
int nkey= ftok("/mnt/hgfs/E/code/基线代码/ftokcode",33);
printf("ftok return k=%d\n",nkey);
if( nkey <0 )
{
perror("testcli");
return -1;
}
int id=shmget(nkey,MAX_BUF_SIZE,IPC_CREAT);
printf("shmget id=%d\n",id);
if( id< 0 )
{
perror("shmget");
return -2;
}
void *pAddr=shmat(id,NULL,0);
if( NULL == pAddr )
{
perror("shmat");
return -3;
}
int semid = semget(id, 2, IPC_CREAT | 0666 );//0--read enable ---1 write enable
if(-1 == semid)
{
return -1;
}
else
{
printf("semget by id=%d ok, semid = %d\n", id,semid );
}
Post(semid,LIGHT_WRITE);
int i=0,j=0;
while( true )
{
sleep(10);
Wait(semid,LIGHT_WRITE);
char *p = (char*)pAddr;
char szMsg[100]={0};
printf((char*)pAddr+10);
*p=1;
FILE * fp = fopen("./send.jpg","rb");
if( fp==NULL )
{
printf("read send.jpg error\n");
return -4;
}
else
{
printf("open ./send.jpg success\n");
}
fseek(fp,0,SEEK_END);
int nLen = ftell( fp);
fseek(fp,0,SEEK_SET);
int nDataLen = fread(p+100,1,nLen,fp );
if( nDataLen>MAX_BUF_SIZE )
{
fclose( fp );
printf("image buf=%d is mothan %d-100",nDataLen,MAX_BUF_SIZE);
return -4;
}
else
{
printf("ftell=%d,read=%d\n",nLen,nDataLen);
}
int *pImageSize= (int*)(pAddr+4);
*pImageSize=nLen;
sprintf( (char*)pAddr+10,"hello:my name is %s, i=%d,j=%d,file size=%d,byte=(%d,%d,%d,%d)\n","testcli",i++,j++,nLen,*(p+4),*(p+5),*(p+6),*(p+7));
fclose(fp);
Post(semid,LIGHT_READ);
}
}
接收端
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<unistd.h>
#include<time.h>
#include<sys/time.h>
#include<sys/sem.h>
using namespace std;
#define LIGHT_READ 0
#define LIGHT_WRITE 1
long long syslocaltime()
{
struct timeval tmstart;
gettimeofday(&tmstart,NULL);
long long a=tmstart.tv_sec*1000+tmstart.tv_usec/1000;
printf("second=%lld,usec=%lld",tmstart.tv_sec,tmstart.tv_usec);
}
void Wait(int semid,int nId) //占用资源(拿走绿灯)
{
struct sembuf sops;
sops.sem_num = nId; /* 要操作的信号量的编号为0 */
sops.sem_op = -1; /* Wait for value to equal 0 */
sops.sem_flg = 0; /* 既不IPC_NOWAIT,也不IPC_UNDO */
printf("wait semop start semid=%d,channel=%d\n",semid,nId);
if (semop(semid, &sops, 1) == -1)
{
printf("wait semop failed, infor\r\n" );
}
else
{
printf("wait semop success semid=%d,channel=%d\n",semid,nId);
}
}
void Post(int semid,int nId) //释放资源(归还绿灯)
{
struct sembuf sops;
sops.sem_num = nId; /* 要操作的信号量的编号为0 */
sops.sem_op = 1; /* Wait for value to equal 0 */
sops.sem_flg = 0; /* 既不IPC_NOWAIT,也不IPC_UNDO */
printf("wait semop start semid=%d,channel=%d\n",semid,nId);
if (semop(semid, &sops, 1) == -1)
{
printf("post semop failed, infor\r\n" );
}
else
{
printf("post semop success semid=%d,channel=%d\n",semid,nId);
}
}
const int MAX_BUF_LEN=1000*1000;
const int listlen=10;
int main(int argc,char ** argv )
{
int nkey= ftok("/mnt/hgfs/E/code/基线代码/ftokcode",33);
printf("ftok return k=%d\n",nkey);
if( nkey <0 )
{
perror("testcli");
return -1;
}
int id=shmget(nkey,MAX_BUF_LEN,IPC_CREAT);
printf("shmget id=%d\n",id);
if( id< 0 )
{
perror("shmget");
return -2;
}
void *pAddr=shmat(id,NULL,0);
if( NULL == pAddr )
{
perror("shmat");
return -3;
}
int semid = semget(id, 2, IPC_CREAT | 0666 );//0--read enable ---1 write enable
if(-1 == semid)
{
return -1;
}
else
{
printf("semget by id=%d ok, semid = %d\n", id,semid );
}
int i=0,j=0;
while( true )
{
usleep(1);
Wait(semid,LIGHT_READ);
//char szMsg[100]={0};
//sprintf( (char*)pAddr,"hello:my name is %s, i=%d,j=%d\n","testcli",i++,j++);
char *p=(char*)pAddr;
//if( *p==1 )
//{
char szFileName[100]={0};
sprintf( szFileName,"./recv%d.jpg",i++);
FILE *fp = fopen(szFileName,"wb");
if( NULL == fp )
{
printf("open file %s error\n",szFileName);
return -4;
}
else
{
printf("create file %s success\n",szFileName);
}
int nImageSize= *((int*)(pAddr+4));
printf((char*)pAddr+10);
int nTotalLen = 0;
int nLen = fwrite(p+100,1,nImageSize,fp);
syslocaltime();
printf("image %s len=%d,writelen=%d\n",szFileName,nImageSize,nLen);
fclose(fp);
//std::this_thread::sleep_for(std::chrono::seconds(1));
*p=0;
//}
//sleep(1);
Post(semid,LIGHT_WRITE);
}
}
最终实现效果:
边栏推荐
- NC192 二叉树的后序遍历
- RTSP协议的实现
- Ledong Fire Rescue Brigade was invited to carry out fire safety training for cadres
- WPF 系统托盘 图标闪烁
- Professor Chen Qiang's "Machine Learning and R Application" course Chapter 13 Assignment
- Jenkins API groovy calling practice: Jenkins Core Api & Job DSL to create a project
- ArcEngine(八) 选择要素并高亮显示
- FFMPEG multimedia file processing (deletion and renaming of ffmpeg files)
- 陈强教授《机器学习及R应用》课程 第十八章作业
- Unity3d_API_GPS_LocationService
猜你喜欢
乐东消防救援大队应邀为干部开展消防安全培训
问题系列-如何修改或更新localhost里的值
GIN a preliminary study, the environment is installed
绘制混合密度函数图以及添加分位数线
剑指offer,剪绳子2
行程和用户[阅读理解法]
The sword refers to the offer, cuts the rope 2
Uni - app - uview Swiper shuffling figure component, click on the links to jump (click to get the item after the row data, remove data operation)
kustomize entry example and basic syntax instructions
群组行动控制--自动队列化实现策略
随机推荐
jenkins api create custom pipeline
时间序列分析课程实验报告
Final assignment of R language data analysis in a university
Q_06_02 类型模型
Anta and Huawei Sports Health jointly verify the champion running shoes and lead Chinese sports with innovation
Yocto 可以下载的第三方库
Q_06_04 语句和其他构造
The sword refers to the offer, cuts the rope 2
Standing wave ratio calculation method
5G 联通网管设计思路
[FPGA Tutorial Case 48] Image Case 8 - Realization of Converting RGB Image to HSV Image Based on FPGA, Assisted Verification by MATLAB
周末看点回顾|我国IPv6网络全面建成;2022昇腾AI开发者创享日·南京站成功举办…
npm install失败
5G China unicom AP:B SMS ASCII Transcoding Requirements
ArcEngine(八) 选择要素并高亮显示
Periodic sharing of Alibaba Da Tao system model governance
LeetCode 37. Solve Sudoku
Rmarkdown Tutorial
陈强教授《机器学习及R应用》课程 第十四章作业
面试攻略系列(三)-- 高级开发工程师面试问些啥?