Chinaunix首页 | 论坛 | 博客
  • 博客访问: 20936
  • 博文数量: 8
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2015-01-14 13:30
文章分类
文章存档

2016年(8)

我的朋友

分类: C/C++

2016-03-18 10:47:33

学习过程中写的一些测试程序,能让自己很快的了解多线程和共享内存的一些操作,下下来大家共享,欢迎拍砖!
编译多线程时加上:-lpthread,如gcc -lpthread -o selfMultiThread selMultiThread.c
1、多线程编程

selfMultiThread.c(点此)

  1. /*
  2.  是否熟悉POSIX多线程编程技术?如熟悉,编写程序完成如下功能:
  3.   1)有一int型全局变量g_Flag初始值为0;
  4.   2)在主线称中起动线程1,打印“this is thread1”,并将g_Flag设置为1
  5.   3)在主线称中启动线程2,打印“this is thread2”,并将g_Flag设置为2
  6.   4)线程序1需要在线程2退出后才能退出
  7.   5)主线程在检测到g_Flag从1变为2,或者从2变为1的时候退出
  8.    */


  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <pthread.h> //多线程要用到的头文件
  12. #include <errno.h>
  13. #include <unistd.h>

  14. typedef void* (*fun)(void*);

  15. int g_Flag=0;
  16. static pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
  17. static pthread_cond_t cond=PTHREAD_COND_INITIALIZER;

  18. void* thread1(void*);
  19. void* thread2(void*);

  20. int main(int argc,char** argv)
  21. {
  22.     int rc1,rc2;
  23.     pthread_t tid1,tid2;
  24.     printf("enter main\n");
  25.     rc2=pthread_create(&tid2,NULL,thread2,NULL);
  26.     if(rc2!=0)
  27.         printf("thread2 create error!\n");
  28.     rc1=pthread_create(&tid1,NULL,thread1,&tid2);
  29.     if(rc1!=0)
  30.         printf("thread1 create error!\n");
  31.     pthread_cond_wait(&cond,&mutex); //等待条件满足
  32.     printf("leave main\n");
  33.     exit(0);
  34.     
  35. }

  36. void* thread1(void* arg)
  37. {
  38.     printf("enter thread1\n");
  39.     printf("this is thread1,g_Flag:%d,tid:%u\n",g_Flag,(unsigned int)pthread_self());
  40.     pthread_mutex_lock(&mutex);
  41.     if(g_Flag==2)
  42.         pthread_cond_signal(&cond);//使条件满足
  43.     g_Flag=1;
  44.     printf("this is thread1,g_Flag:%d,tid:%u\n",g_Flag,(unsigned int)pthread_self());
  45.     pthread_mutex_unlock(&mutex);
  46.     pthread_join(*(pthread_t*)arg,NULL);
  47.     printf("leave thread1\n");
  48.     pthread_exit(0);
  49. }

  50. void* thread2(void* arg)
  51. {
  52.     printf("enter thread2\n");
  53.     printf("this is thread2,g_Flag:%d,tid:%u\n",g_Flag,(unsigned int)pthread_self());
  54.     pthread_mutex_lock(&mutex);
  55.     if(g_Flag==1)
  56.         pthread_cond_signal(&cond);
  57.     g_Flag=2;
  58.     printf("this is thread2,g_Flag:%d,tid:%u\n",g_Flag,(unsigned int)pthread_self());
  59.     pthread_mutex_unlock(&mutex);
  60.     printf("leave thread2\n");
  61.     pthread_exit(0);
  62. }
2、共享内存

sharedMemoryWrite.c(此处)

  1. /*向共享内存写数据*/
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <errno.h>
  6. #include <sys/ipc.h>
  7. #include <sys/shm.h>

  8. void get_buf(char *buf)
  9. {
  10.     int i=0;
  11.     while((buf[i]=getchar())!='\n'&&i<1024)
  12.         i++;
  13. }

  14. int main(void)
  15. {
  16.     int shmid;
  17.     shmid=shmget(IPC_PRIVATE,sizeof(char)*1024,IPC_CREAT|0666);
  18.     if(shmid==-1)
  19.     {
  20.         perror("shmget");
  21.     }
  22.     char *buf;
  23.     if((int)(buf=shmat(shmid,NULL,0))==-1)
  24.     {
  25.         perror("shmat");
  26.         exit(1);
  27.     }
  28.     get_buf(buf);
  29.     printf("%d\n",shmid);
  30.     return 0;
  31. }

sharedMemoryRead.c(此处)

  1. /*从共享内存读取数据*/
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <errno.h>
  5. #include <sys/ipc.h>
  6. #include <sys/types.h>
  7. #include <sys/shm.h>

  8. int main(int argc,char** argv)
  9. {
  10.     int shmid;
  11.     shmid=atoi(argv[1]);
  12.     char *buf;
  13.     if((int)(buf=shmat(shmid,NULL,0))==-1)
  14.     {
  15.         perror("shmat");
  16.         exit(1);
  17.     }
  18.     printf("%s\n",buf);
  19.     shmdt(buf);
  20.     return 0;
  21. }
3、多线程共享内存混合编程

selfThreadAndShm(此处)

  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,NULL,threadRead,&wtid);
  35.     if(rt!=0)
  36.     {
  37.         printf("create Read Thread error:%s,errno:%d\n",strerror(errno),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,NULL,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,thread id is %u,g_Flag:%d\n",(unsigned int)pthread_self(),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,NULL,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,thread id is %u,g_Flag:%d\n",(unsigned int)pthread_self(),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,thread id is %u,g_Flag:%d\n",(unsigned int)pthread_self(),g_Flag);
  94.     printf("Leave Write thread!\n");
  95.     pthread_exit(0);
  96. }




阅读(1008) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~