您的位置:首页 > 运维架构 > Linux

撸代码--linux进程通信(基于共享内存)

2017-05-06 21:26 288 查看
1.实现亲缘关系进程的通信,父写子读

思路分析:1)首先我们须要创建一个共享内存。

2)父子进程的创建要用到fork函数。fork函数创建后,两个进程分别独立的执行。

3)父进程完毕写的内容。同一时候要保证子进程退出后,在删除共享内存。

4)子进程完毕读的内容。

效果展示:












代码展示:

#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <errno.h>

int main()
{//父子进程 操作共享内存
//先创建共享内存  父进程对共享内存写 子进程对共享内存读
int flag;
flag=shmget(IPC_PRIVATE,4096,0600|IPC_CREAT);
//创建一个共享内存  然后返回标示符

char buf[]={"I am your father\n"};
char s[123];
if(fork()!=0)
{//父进程完毕对共享内存的写
char *f;
f=(char *)shmat(flag,NULL,0);//连接了父进程和共享内存 返回指针 指向
//内存的第一个字节
memset(f,'\0',4096);//这时候能够操作f
strncpy(f,"I am you father",16);//写入内容

printf("parent %d Write buf is %s\n",getpid(),f);
wait(NULL);//等待子进程
shmctl(flag,IPC_RMID,0);//删除共享 内存
exit(0);
}
else
{
char *fp;
sleep(3);//让父进程有时间往里面写
fp=(char *)shmat(flag,NULL,0);//子进程跟其连接 然后返回给字符指针
printf("child pid is %d,Read buf is %s\n",getpid(),fp);
exit(0);
}
}


---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

2.实现非亲缘关系的通信。(两个进程对共享内存的值进行改动)

思路分析:1)首先我们须要创建一个共享内存。

2)两个进程要採取锁的方式訪问共享内存的值。

效果展示:








代码展示:

#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>

//写进程不断的写 然后要推断当前内存中的锁是开闭状态
struct t
{
int user_now;//定义一个锁
int val;
};

int main()
{
int flag;
flag=shmget((key_t)1234,4096,0600|IPC_CREAT);

struct t *tt;
tt=(struct t*)shmat(flag,NULL,0);//拿到内存
tt->user_now=0;
while(1)
{
if(tt->user_now==0)
{//假设0 锁开了 那么设置值
tt->val=1;
printf("I am write, the value is %d\n",tt->val);                                        tt->user_now=1;//加上锁。

}
sleep(1);
}
shmdt((void *)tt);
return 0;

}
写进程2:

<pre name="code" class="objc">#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>

//写进程不断的写 然后要推断当前内存中的锁是开闭状态
struct t
{
int user_now;//定义一个锁
int val;
};

int main()
{
int flag;
flag=shmget((key_t)1234,4096,0600|IPC_CREAT);

struct t *tt;
tt=(struct t*)shmat(flag,NULL,0);//拿到内存
tt->user_now=0;
while(1)
{
if(tt->user_now==1)
{//假设0 锁开了 那么设置
tt->val=2;
printf("I am write2, the value is %d\n",tt->val);                               tt->user_now=0;//加上锁。
}
sleep(1);
}
shmdt((void *)tt);
shmctl(flag,IPC_RMID,0);
return 0;

}



-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------





3.程序间的对话(AB进程能够实现对话 仅仅能够实现A进程不断写 B进程不断读)





思路分析:1)首先我们须要创建一个共享内存。

2)建立两个进程,A,B。

A进程完毕接受键盘的输入,然后放在共享内存中。

3)B进程拿出共享内存的数据。然后显示出来。

效果展示:













代码展示:

A进程

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>

//写进程不断的写 然后要推断当前内存中的锁是开闭状态
struct t
{
int user_now;//定义一个锁
char buf[1024];
};

int main()
{
int flag;
flag=shmget((key_t)1234,4096,0600|IPC_CREAT);

struct t *tt;
tt=(struct t*)shmat(flag,NULL,0);//拿到内存
tt->user_now=0;
while(1)
{
if(tt->user_now==0)
{//假设0 锁开了 那么设置值
read(STDIN_FILENO,tt->buf,1024);
//将键盘输入的放在共享内存的buf中
tt->user_now=1;
}
// memset(tt->buf,0,sizeof(tt->buf));
sleep(1);
}
shmdt((void *)tt);
return 0;

}<strong>
</strong>


B进程

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>

//写进程不断的写 然后要推断当前内存中的锁是开闭状态
struct t
{
int user_now;//定义一个锁
char buf[1024];
};

int main()
{
int flag;
flag=shmget((key_t)1234,4096,0600|IPC_CREAT);

struct t *tt;
tt=(struct t*)shmat(flag,NULL,0);//拿到内存
tt->user_now=0;
while(1)
{
if(tt->user_now==1)
{//假设0 锁开了 那么设置值
write(STDOUT_FILENO,tt->buf,strlen(tt->buf));
memset(tt->buf,0,sizeof(tt->buf));
tt->user_now=0;
}
sleep(1);
}
shmdt((void *)tt);
shmctl(flag,IPC_RMID,0);
return 0;

}






内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: