日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時間:2025/3/15 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 共享内存进程线程混合通信 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

多線程共享內(nèi)存混合編程

  • /*threadWrite線程向共享內(nèi)存寫數(shù)據(jù),threadRead線程從共享內(nèi)存讀數(shù)據(jù)*/
  • /*讀線程必須等待寫線程執(zhí)行完才開始*/
  • #include?<stdio.h>
  • #include?<stdlib.h>
  • #include?<sys/types.h>
  • #include?<sys/unistd.h>
  • #include?<sys/ipc.h>
  • #include?<sys/shm.h>
  • #include?<pthread.h>
  • #include?<errno.h>
  • ?
  • static pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
  • static pthread_cond_t cond=PTHREAD_COND_INITIALIZER;
  • int?g_Flag=0;
  • void*?threadRead(void*);
  • void*?threadWrite(void*);
  • ?
  • int?shmid;
  • char?*buf[4092];
  • ?
  • int?main(int?argc,char**?argv)
  • {
  • ????printf("Enter main\n");
  • ????
  • ????printf("Create shared memory id\n");
  • ????shmid=shmget(IPC_PRIVATE,sizeof(char)*4092,IPC_CREAT|0666);
  • ????printf("shmid:%d\n",shmid);
  • ????
  • ????int?rt=0,wt=0;
  • ????pthread_t rtid,wtid;
  • ????wt=pthread_create(&wtid,NULL,threadWrite,NULL);
  • ????if(wt!=0)
  • ????{
  • ????????printf("create Write Thread error:%s,errno:%d\n",strerror(errno),errno);
  • ????????exit(0);
  • ????}
  • ????rt=pthread_create(&rtid,NULL,threadRead,&wtid);
  • ????if(rt!=0)
  • ????{
  • ????????printf("create Read Thread error:%s,errno:%d\n",strerror(errno),errno);
  • ????????exit(0);
  • ????}
  • ????pthread_cond_wait(&cond,&mutex);
  • ????printf("Leave main\n");
  • ????exit(0);
  • }
  • ?
  • void*?threadRead(void*?arg)
  • {
  • ????printf("Enter Read thread,start reading after Write thread finished!\n");
  • ????
  • ????char?*shmptr;
  • ????int?i=0;
  • ????char word;
  • ????if((int)(shmptr=shmat(shmid,NULL,0))==-1)
  • ????{
  • ????????printf("The Read thread attach the shared memory failed!\n");
  • ????????exit(0);
  • ????}
  • ????printf("Attached successfully!\n");
  • ????
  • ????pthread_join(*(pthread_t*)arg,NULL);
  • ????printf("this is Read thread,thread id is %u,g_Flag:%d\n",(unsigned?int)pthread_self(),g_Flag);
  • ????pthread_mutex_lock(&mutex);
  • ????g_Flag=1;
  • ????printf("Reading shared memory...\n");
  • ????printf("Content:%s\n",shmptr);
  • ????shmdt(shmptr);
  • ????pthread_mutex_unlock(&mutex);
  • ????printf("Read over,thread id is %u,g_Flag:%d\n",(unsigned?int)pthread_self(),g_Flag);
  • ????printf("Leave Read thread!\n");
  • ????pthread_cond_signal(&cond);
  • ????pthread_exit(0);
  • }
  • ?
  • void*?threadWrite(void*?arg)
  • {
  • ????printf("Enter Write thread,we'll write something\n");
  • ?
  • ????
  • ????char?*shmptr;
  • ????int?i=0;
  • ????char word;
  • ????if((int)(shmptr=shmat(shmid,NULL,0))==-1)
  • ????{
  • ????????printf("The Write thread attach the shared memory failed!\n");
  • ????????exit(0);
  • ????}
  • ????printf("Attached successfully!\n");
  • ????
  • ????printf("this is Write thread,thread id is %u,g_Flag:%d\n",(unsigned?int)pthread_self(),g_Flag);?
  • ????pthread_mutex_lock(&mutex);
  • ????g_Flag=2;
  • ????
  • ????printf("write a string into shared memory\n");
  • ????while((word=getchar())!='\n')
  • ????????shmptr[i++]=word;
  • ????pthread_mutex_unlock(&mutex);
  • ????printf("Write over,thread id is %u,g_Flag:%d\n",(unsigned?int)pthread_self(),g_Flag);
  • ????printf("Leave Write thread!\n");
  • ????pthread_exit(0);
  • }
  • ?

    兩進程通信

    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,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,BUF_SIZE,IPC_CREAT)) ==-1) ?
    ? ? { ?
    ?? ??? ?printf("shmget error \n"); ?
    ?? ??? ?exit(1); ?
    ? ? } ?
    ??
    ? ? if((shmptr =shmat(shmid,0,0))==(void *)-1) ?
    ? ? { ?
    ?? ??? ?printf("shmat error!\n"); ?
    ?? ??? ?exit(1); ?
    ? ? } ?
    ??
    ? ? while(1) ?
    ? ? { ?
    ?? ??? ?printf("input:"); ?
    ?? ??? ?scanf("%s",shmptr); ?
    ? ? } ?
    ??
    ? ? exit(0); ?
    }?

    ?

    ?

    ?

    ?

    總結(jié)

    以上是生活随笔為你收集整理的共享内存进程线程混合通信的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。