1.c
来源:互联网 发布:数据报送管理办法 编辑:程序博客网 时间:2024/06/06 04:54
#include <sys/shm.h>
#define TEXT_SIZE 4096
struct share_use_st
{
int iFlag; // 0: write, 1: read
int iSize;
char cText[TEXT_SIZE];
};
void* shm = NULL;
struct share_use_st *pShared;
int shmid;
FILE* pFp;
FILE* pWriteFp;
char cBuf[TEXT_SIZE];
int iSize;
pid_t fatherId;
pid_t sonId;
void readDate();
void catchFatherSigUsr1(int sig)
{
readDate();
}
void readDate()
{
if (iSize=fread(cBuf, 1, TEXT_SIZE, pFp)) > 0)
{
memset(pShared->cText, 0, TEXT_SIZE);
memcpy(pShared->cText, cBuf, iSize);
pShared->iSize = iSize;
pShared->iFlag = 1; // ready to read
kill(sonId, SIGUSR1);
}
// read End
else if (0 == iSize)
{
kill(sonId, SIGUSR2);
}
}
int main(int argc, char** argv)
{
pFp = fopen("input.txt", "r");
if (NULL == pFp)
{
printf("open file failed\n");
return -1;
}
shmid = shmget((key_t)1234, sizeof(struct shared_use_st), 0666|IPC_CREAT);
if (-1 == shmid)
{
printf("create shmget failed\n");
fclose(pFp);
return -1;
}
shm = shmat(shmid, 0, 0);
if (shm == (void*)-1)
{
cout << "shmat failed" << endl;
fclose(fp);
return -1;
}
cout << "shm =" << shm << endl;
pShared = (struct share_use_st*)shm;
pShared->iFlag = 0; // write
fatherId = getpid(); // get the fatherId
if ((sonId=fork()) < 0)
{
printf("fork failed\n");
return -1;
}
else if (0 == sonId)
{
}
else
{
// start to read file and write to share memory
readDate();
}
fclose(pFp);
}
#define TEXT_SIZE 4096
struct share_use_st
{
int iFlag; // 0: write, 1: read
int iSize;
char cText[TEXT_SIZE];
};
void* shm = NULL;
struct share_use_st *pShared;
int shmid;
FILE* pFp;
FILE* pWriteFp;
char cBuf[TEXT_SIZE];
int iSize;
pid_t fatherId;
pid_t sonId;
void readDate();
void catchFatherSigUsr1(int sig)
{
readDate();
}
void readDate()
{
if (iSize=fread(cBuf, 1, TEXT_SIZE, pFp)) > 0)
{
memset(pShared->cText, 0, TEXT_SIZE);
memcpy(pShared->cText, cBuf, iSize);
pShared->iSize = iSize;
pShared->iFlag = 1; // ready to read
kill(sonId, SIGUSR1);
}
// read End
else if (0 == iSize)
{
kill(sonId, SIGUSR2);
}
}
int main(int argc, char** argv)
{
pFp = fopen("input.txt", "r");
if (NULL == pFp)
{
printf("open file failed\n");
return -1;
}
shmid = shmget((key_t)1234, sizeof(struct shared_use_st), 0666|IPC_CREAT);
if (-1 == shmid)
{
printf("create shmget failed\n");
fclose(pFp);
return -1;
}
shm = shmat(shmid, 0, 0);
if (shm == (void*)-1)
{
cout << "shmat failed" << endl;
fclose(fp);
return -1;
}
cout << "shm =" << shm << endl;
pShared = (struct share_use_st*)shm;
pShared->iFlag = 0; // write
fatherId = getpid(); // get the fatherId
if ((sonId=fork()) < 0)
{
printf("fork failed\n");
return -1;
}
else if (0 == sonId)
{
}
else
{
// start to read file and write to share memory
readDate();
}
fclose(pFp);
}
0 0
- 1.c
- 1. C,C++,VC++有什么区别
- 1.C语言-----潜入C的海洋
- 1.vc从零开始 -- c/c++/vc 简介
- 1.C语言概述
- 1.C学习笔记
- C语言-1.简介
- e10-1.c
- 1. A+B+C
- 1.初识c语言
- C-1.基础知识
- 1.Objective-C简介
- 1.Linux C
- 1.C/C++基础
- 1.C子空间
- 1.初识C语言
- c
- c
- 渗透测试之Nmap命令(三) idle 扫描
- 超级采样 Supersampling 方式汇总
- CSDN
- Linux元素集合—原子变量
- 错误代码调试成功后。。。
- 1.c
- 判断进程是否以管理员权限运行(***)
- 黑马程序员 JAVA基础学习笔记
- Android开发之Handler消息传递机制
- 深入理解Java:注解(Annotation)自定义注解入门
- 2864:分跑道
- Office 2010 中打开多个Excel文件只能在同一窗口中显示的问题
- jQuery ajax 传数据到后台,后台返回时success函数不执行问题
- char | string | NSString 之间的转换