虎贲小队程序猿

get along

导航

linux C学习笔记02--共享内存(进程同步)

 system V下3中进程同步:共享内存(shared memory),信号量(semaphore)和消息队列(message queue)

调试了下午,终于调通啦! 运行./c.out 输出共享内存中的内容,运行 ./c.out arg1 对共享内存区进行修改,shell下输入ipcs -m 可以查看共享内存情况 ,-s 是信号量,-q 是消息队列

下面先贴上main的代码: 

#include <signal.h>        //head file of define signal
#include <pthread.h>
#include <static_lib.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/mman.h>

extern int create_shm(char **shmptr);
int main(int argc,char* argv[])
{
   //IPC share memory
    char *shmptr;
    if(create_shm(&shmptr) == -1)
    {
        printf("create_shm error \n");
        return -1;        
    }
    if(argc > 1)
    {
    //do the second thing for share memory
        while(1)
        {
            printf("input str to share memory:");
            gets(shmptr);
        }
    }
    else
    {
        memcpy(shmptr,"hello",5);
        while(1)
        {
            sleep(2);
            printf("share memory is %s \n",shmptr);
       }
    }   
    return 0;
}

下面是共享内存创建的代码:

/*************************************************************************
    > File Name: share_memory.c
    > Author: hailin.ma
    > Mail: mhl2018@126.com 
    > Created Time: Wed 27 May 2015 11:19:26 PM CST
 ************************************************************************/

#include<stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <errno.h>

#define SHARE_MEM_KEY 26

int create_shm(char **shmptr)
{
    int shmid;

    //if((shmid = shmget(IPC_PRIVATE,200,IPC_CREAT|IPC_EXCL|0666)) == -1)
    if((shmid = shmget(SHARE_MEM_KEY,200,IPC_CREAT|IPC_EXCL|0666)) == -1)
    {
        perror("shmget");
        if(errno == EEXIST)
        {
            shmid = shmget(SHARE_MEM_KEY,0,0);
            if(shmid == -1)
            {
                perror("shmget2");
                return -1;
            }
            else
            {
                if((*shmptr = shmat(shmid,0,0666)) == (void*)-1)
                {
                    perror("shmat2");
                    return -1;
                }
                else
                {
                    return shmid;
                }
            }
        }
        else
        {
            return -1;
        }
    }
    
    if((*shmptr = shmat(shmid,0,0)) == (void*)-1)
    {
        perror("shmat");
        return -1;
    }
    
    return shmid;
}

 

 运行效果:

 

05.29日增加了共享内存释放和删除:

int main(int argc,char* argv[])
{
    
    //IPC share memory
    char *shmptr;
    int shmid;
    if((shmid =create_shm(&shmptr)) == -1)
    {
        printf("create_shm error \n");
        return -1;        
    }
    if(argc > 1)
    {
    //do the second thing for share memory
        while(1)
        {
            printf("input str to share memory:");
            gets(shmptr);
            if(shmptr[0] == 'q')        //quit
            {
                shmdt(shmptr);            //disconnect to the share memory but will not dellect the memery
                break;
            }
            
        }
    }
    else
    {
        while(1)
        {
            sleep(2);
            printf("share memory is: %s \n",shmptr);
            if(shmptr[0] == 'q')
            {
                shmdt(shmptr);
                shmctl(shmid,IPC_RMID,NULL);        //delete the share memery
                break;
            }
        }
    }
}

 

 

posted on 2015-05-28 17:49  jjssl  阅读(2155)  评论(0编辑  收藏  举报