学习过程中写的一些测试程序,能让自己很快的了解多线程和共享内存的一些操作,下下来大家共享,欢迎拍砖!
编译多线程时加上:-lpthread,如gcc -lpthread -o selfMultiThread selMultiThread.c
1、多线程编程
-
/*
-
是否熟悉POSIX多线程编程技术?如熟悉,编写程序完成如下功能:
-
1)有一int型全局变量g_Flag初始值为0;
-
2)在主线称中起动线程1,打印“this is thread1”,并将g_Flag设置为1
-
3)在主线称中启动线程2,打印“this is thread2”,并将g_Flag设置为2
-
4)线程序1需要在线程2退出后才能退出
-
5)主线程在检测到g_Flag从1变为2,或者从2变为1的时候退出
-
*/
-
-
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <pthread.h> //多线程要用到的头文件
-
#include <errno.h>
-
#include <unistd.h>
-
-
typedef void* (*fun)(void*);
-
-
int g_Flag=0;
-
static pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
-
static pthread_cond_t cond=PTHREAD_COND_INITIALIZER;
-
-
void* thread1(void*);
-
void* thread2(void*);
-
-
int main(int argc,char** argv)
-
{
-
int rc1,rc2;
-
pthread_t tid1,tid2;
-
printf("enter main\n");
-
rc2=pthread_create(&tid2,NULL,thread2,NULL);
-
if(rc2!=0)
-
printf("thread2 create error!\n");
-
rc1=pthread_create(&tid1,NULL,thread1,&tid2);
-
if(rc1!=0)
-
printf("thread1 create error!\n");
-
pthread_cond_wait(&cond,&mutex); //等待条件满足
-
printf("leave main\n");
-
exit(0);
-
-
}
-
-
void* thread1(void* arg)
-
{
-
printf("enter thread1\n");
-
printf("this is thread1,g_Flag:%d,tid:%u\n",g_Flag,(unsigned int)pthread_self());
-
pthread_mutex_lock(&mutex);
-
if(g_Flag==2)
-
pthread_cond_signal(&cond);//使条件满足
-
g_Flag=1;
-
printf("this is thread1,g_Flag:%d,tid:%u\n",g_Flag,(unsigned int)pthread_self());
-
pthread_mutex_unlock(&mutex);
-
pthread_join(*(pthread_t*)arg,NULL);
-
printf("leave thread1\n");
-
pthread_exit(0);
-
}
-
-
void* thread2(void* arg)
-
{
-
printf("enter thread2\n");
-
printf("this is thread2,g_Flag:%d,tid:%u\n",g_Flag,(unsigned int)pthread_self());
-
pthread_mutex_lock(&mutex);
-
if(g_Flag==1)
-
pthread_cond_signal(&cond);
-
g_Flag=2;
-
printf("this is thread2,g_Flag:%d,tid:%u\n",g_Flag,(unsigned int)pthread_self());
-
pthread_mutex_unlock(&mutex);
-
printf("leave thread2\n");
-
pthread_exit(0);
-
}
2、共享内存
-
/*向共享内存写数据*/
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
#include <errno.h>
-
#include <sys/ipc.h>
-
#include <sys/shm.h>
-
-
void get_buf(char *buf)
-
{
-
int i=0;
-
while((buf[i]=getchar())!='\n'&&i<1024)
-
i++;
-
}
-
-
int main(void)
-
{
-
int shmid;
-
shmid=shmget(IPC_PRIVATE,sizeof(char)*1024,IPC_CREAT|0666);
-
if(shmid==-1)
-
{
-
perror("shmget");
-
}
-
char *buf;
-
if((int)(buf=shmat(shmid,NULL,0))==-1)
-
{
-
perror("shmat");
-
exit(1);
-
}
-
get_buf(buf);
-
printf("%d\n",shmid);
-
return 0;
-
}
-
/*从共享内存读取数据*/
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <errno.h>
-
#include <sys/ipc.h>
-
#include <sys/types.h>
-
#include <sys/shm.h>
-
-
int main(int argc,char** argv)
-
{
-
int shmid;
-
shmid=atoi(argv[1]);
-
char *buf;
-
if((int)(buf=shmat(shmid,NULL,0))==-1)
-
{
-
perror("shmat");
-
exit(1);
-
}
-
printf("%s\n",buf);
-
shmdt(buf);
-
return 0;
-
}
3、多线程共享内存混合编程
-
/*threadWrite线程向共享内存写数据,threadRead线程从共享内存读数据*/
-
/*读线程必须等待写线程执行完才开始*/
-
#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);
-
}
阅读(580) | 评论(0) | 转发(0) |