共享内存进程线程混合通信

多线程共享内存混合编程

  1. /*threadWrite线程向共享内存写数据,threadRead线程从共享内存读数据*/
  2. /*读线程必须等待写线程执行完才开始*/
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <sys/types.h>
  6. #include <sys/unistd.h>
  7. #include <sys/ipc.h>
  8. #include <sys/shm.h>
  9. #include <pthread.h>
  10. #include <errno.h>
  11. static pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
  12. static pthread_cond_t cond=PTHREAD_COND_INITIALIZER;
  13. int g_Flag=0;
  14. void* threadRead(void*);
  15. void* threadWrite(void*);
  16. int shmid;
  17. char *buf[4092];
  18. int main(int argc,char** argv)
  19. {
  20.     printf("Enter main\n");
  21.     
  22.     printf("Create shared memory id\n");
  23.     shmid=shmget(IPC_PRIVATE,sizeof(char)*4092,IPC_CREAT|0666);
  24.     printf("shmid:%d\n",shmid);
  25.     
  26.     int rt=0,wt=0;
  27.     pthread_t rtid,wtid;
  28.     wt=pthread_create(&wtid,NULL,threadWrite,NULL);
  29.     if(wt!=0)
  30.     {
  31.         printf("create Write Thread error:%s,errno:%d\n",strerror(errno),errno);
  32.         exit(0);
  33.     }
  34.     rt=pthread_create(&rtid,threadRead,&wtid);
  35.     if(rt!=0)
  36.     {
  37.         printf("create Read Thread error:%s,errno);
  38.         exit(0);
  39.     }
  40.     pthread_cond_wait(&cond,&mutex);
  41.     printf("Leave main\n");
  42.     exit(0);
  43. }
  44. void* threadRead(void* arg)
  45. {
  46.     printf("Enter Read thread,start reading after Write thread finished!\n");
  47.     
  48.     char *shmptr;
  49.     int i=0;
  50.     char word;
  51.     if((int)(shmptr=shmat(shmid,0))==-1)
  52.     {
  53.         printf("The Read thread attach the shared memory failed!\n");
  54.         exit(0);
  55.     }
  56.     printf("Attached successfully!\n");
  57.     
  58.     pthread_join(*(pthread_t*)arg,NULL);
  59.     printf("this is Read thread,thread id is %u,g_Flag:%d\n",(unsigned int)pthread_self(),g_Flag);
  60.     pthread_mutex_lock(&mutex);
  61.     g_Flag=1;
  62.     printf("Reading shared memory...\n");
  63.     printf("Content:%s\n",shmptr);
  64.     shmdt(shmptr);
  65.     pthread_mutex_unlock(&mutex);
  66.     printf("Read over,g_Flag);
  67.     printf("Leave Read thread!\n");
  68.     pthread_cond_signal(&cond);
  69.     pthread_exit(0);
  70. }
  71. void* threadWrite(void* arg)
  72. {
  73.     printf("Enter Write thread,we'll write something\n");
  74.     
  75.     char *shmptr;
  76.     int i=0;
  77.     char word;
  78.     if((int)(shmptr=shmat(shmid,0))==-1)
  79.     {
  80.         printf("The Write thread attach the shared memory failed!\n");
  81.         exit(0);
  82.     }
  83.     printf("Attached successfully!\n");
  84.     
  85.     printf("this is Write thread,g_Flag); 
  86.     pthread_mutex_lock(&mutex);
  87.     g_Flag=2;
  88.     
  89.     printf("write a string into shared memory\n");
  90.     while((word=getchar())!='\n')
  91.         shmptr[i++]=word;
  92.     pthread_mutex_unlock(&mutex);
  93.     printf("Write over,g_Flag);
  94.     printf("Leave Write thread!\n");
  95.     pthread_exit(0);
  96. }

两进程通信

service

#include <stdio.h>   
#include <sys/types.h>   
#include <sys/ipc.h>   
#include <sys/shm.h>   
  
#define BUF_SIZE 1024   
#define MYKEY 25   
int  main()  
{  
    int shmid;  
    char * shmptr;  
  
    if((shmid = shmget(MYKEY,BUF_SIZE,IPC_CREAT)) ==-1)  
    {  
        printf("shmget error!\n");  
        exit(1);  
    }  
  
    if((shmptr = shmat(shmid,0)) == (void *)-1)  
    {  
        printf("shmat error!\n");  
        exit(1);  
    }  
  
    while(1)  
    {  
        printf("string :%s\n",shmptr);  
        sleep(3);  
    }  
  
    exit(0);  
}  

client

#include <sys/types.h>   
#include <sys/ipc.h>   
#include <sys/shm.h>   
#include <stdio.h>   
  
#define BUF_SIZE 1024   
#define MYKEY 25   
int main()  
{  
    int shmid;  
    char *shmptr;  
  
    if((shmid = shmget(MYKEY,IPC_CREAT)) ==-1)  
    {  
        printf("shmget error \n");  
        exit(1);  
    }  
  
    if((shmptr =shmat(shmid,0))==(void *)-1)  
    {  
        printf("shmat error!\n");  
        exit(1);  
    }  
  
    while(1)  
    {  
        printf("input:");  
        scanf("%s",shmptr);  
    }  
  
    exit(0);  

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐