2,共享内存。
进程间传递数据,如果数据量比较大的话,使用共享内存,共享内存在进程间通信中效率比较高。
共享内存的操作函数:
int shmget(key_tkey,size_t size,int shmflg ); //创建或者获取共享内存标识码。
key_t共享内存键值。可以随意选。特殊键值IPC_PRIVATE,创建只能有本进程使用的共享内存,一般不用。
size创建获得共享内存大小。shmflg共享内存访问权限。和信号量权限一样。
void *shmat(intshm_id,const void *shm_addr,int shmflg); //共享内存与进程进行关联。
共享内存创建后,不能被任何进程使用,必须要与具体使用的进程进行关联到进程空间后才可以使用。
shmid共享内存标示符。shm_addr进程空间的关联地址,一般为(void*)0,有系统自动关联,返回关联后的地址。
shmflg设置共享内存在本进程的访问权限。如果为0就和创建时的访问权限一直。可能取值SHM_RNDD(一般不用)和SHM_RDONLY。
shmdl((void*)shm);解除共享内存与进程的关联,参数为shmat返回的关联地址。只是解除的关联,共享内存还存在。成功返回0.
失败返回-1。
int shmctl(intshm_id,int command,struct shmid_ds *buf);
struct shmid_ds
{
uid_t shm_perm.uid;
uid_t shm_perm.gid;
mode_tshm_perm.mode;
};
shm_id 共享内存标示符。
command将要采取的动作,有三个可能取值。
IPC_STAT 获取当前共享内存的状态信息,放到shmid_ds结构中。
IPC_SET 把shmid_ds结构中的数据设置为共享内存的当前关联值。
IPC_RMID 删除共享内存段。
share_memory1
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/shm.h>
#include "shm_com.h"
int main()
{
int running = 1;
void*shared_memory = (void *)0;
structshared_use_st *shared_stuff;
int shmid;
srand((unsignedint)getpid());
shmid =shmget((key_t)1234, sizeof(struct shared_use_st), 0666 | IPC_CREAT);
if (shmid == -1){
fprintf(stderr, "shmget failed\n");
exit(EXIT_FAILURE);
}
shared_memory =shmat(shmid, (void *)0, 0);
if(shared_memory == (void *)-1) {
fprintf(stderr, "shmat failed\n");
exit(EXIT_FAILURE);
}
printf("Memoryattached at %X\n", (int)shared_memory);
shared_stuff =(struct shared_use_st *)shared_memory;
shared_stuff->written_by_you = 0;
while(running) {
if(shared_stuff->written_by_you) {
printf("You wrote: %s", shared_stuff->some_text);
sleep(rand() % 4 ); /* make the other process wait for us ! */
shared_stuff->written_by_you = 0;
if(strncmp(shared_stuff->some_text, "end", 3) == 0) {
running = 0;
}
}
}
if(shmdt(shared_memory) == -1) {
fprintf(stderr, "shmdt failed\n");
exit(EXIT_FAILURE);
}
if(shmctl(shmid, IPC_RMID, 0) == -1) {
fprintf(stderr, "shmctl(IPC_RMID) failed\n");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
share_memory 2
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/shm.h>
#include "shm_com.h"
int main()
{
int running = 1;
void*shared_memory = (void *)0;
structshared_use_st *shared_stuff;
charbuffer[BUFSIZ];
int shmid;
shmid =shmget((key_t)1234, sizeof(struct shared_use_st), 0666 | IPC_CREAT);
if (shmid == -1){
fprintf(stderr, "shmget failed\n");
exit(EXIT_FAILURE);
}
shared_memory =shmat(shmid, (void *)0, 0);
if(shared_memory == (void *)-1) {
fprintf(stderr, "shmat failed\n");
exit(EXIT_FAILURE);
}
printf("Memoryattached at %X\n", (int)shared_memory);
shared_stuff =(struct shared_use_st *)shared_memory;
while(running) {
while(shared_stuff->written_by_you == 1) {
sleep(1);
printf("waiting for client...\n");
}
printf("Enter some text: ");
fgets(buffer, BUFSIZ, stdin);
strncpy(shared_stuff->some_text, buffer, TEXT_SZ);
shared_stuff->written_by_you = 1;
if(strncmp(buffer, "end", 3) == 0) {
running = 0;
}
}
if(shmdt(shared_memory) == -1) {
fprintf(stderr, "shmdt failed\n");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}