多线程应用程序为了处理请求
这里有一个思想如果我需要写的时候用多线程,也就是在我需要打开输入的文本文件需要按照行读的时候执行线程.想提供一个例子读十行并且执行十个线程并且等待十个线程结束,然后再读十行反复操作.我需要这个作为一个例子,启动线程的时候5个线程,并且这些线程比其他的线程完成的非常的快,因为它们必须解析域名,连接地址,并且发送信息.所以必须在开始5个线程,在3秒内一个线程完成,立即开始一个新的线程在读第六行的时候,下面是写代码的时候需要注意的:
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/errno.h>
#include <errno.h>
#include <ctype.h>
#include <pthread.h>
#include <string.h>
#include <strings.h>
void * submitter( void *arg);
int activethreads=0;//variable for that how many threads are active at every moment of the execution process
int numthreads=5;//variable for that how many threads will be doing at once.Currently i have defined it 5, but it will be user selected(5,10,20 ...max.40)
main()
{
FILE *fp; //variable for the input file
pthread_t *threads[41]; //the maximum threads allowed
pthread_t tid; //variable for the threads
char buffer[2001]; //temporary variable
char newline[2001];
int status;
fp=fopen(".temp","r"); //open the file
while((fgets(buffer,sizeof(buffer),fp))!=NULL)
{
bzero(newline,sizeof(newline));
strcpy(newline,buffer);
status=pthread_create(&tid, NULL, submitter, newline);
//pthread_join(tid,NULL);
if(status == 0)
activethreads++ ;
if(activethreads==numthreads)
WaitFreeThread();
}
pthread_exit((void * ) (pthread_self()));
return 1;
}
void * submitter( void * arg)
{
struct hostent *host;
struct in_addr addr;
struct sockaddr_in sin;
int sock;
int sts;
pthread_t mid;
char msg[200];
char * str = (char *)arg;
printf("Thread %d join -> connecting to %s",mid,str);
sleep(1);
if((host=gethostbyname(str))==NULL)
pthread_exit((void *)1);
sock=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
bzero((char *) &sin, sizeof(sin));
sin.sin_family=AF_INET;
sin.sin_port=htons(80);
bcopy(host->h_addr, (char *)&sin.sin_addr, host->h_length);
sts=connect(sock, (struct sockaddr*)&sin, sizeof(sin));
if((sts==-1)||(errno == ECONNREFUSED)) pthread_exit((void *)1);
sprintf(msg,"GET HTTP/1.0\r\n");
send(sock,msg,strlen(msg),0);
close(sock);
activethreads--;
return NULL;
}
int WaitFreeThread()
{
while(activethreads>numthreads)
sleep(1);
return NULL;
}
这个程序有一些错误,如果多线程同时写同一个变量,而且另一个线程正在读这个变量
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/errno.h>
#include <errno.h>
#include <ctype.h>
#include <pthread.h>
static pthread_mutex_t thread_exit_mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t thread_exit_cnd = PTHREAD_COND_INITIALIZER;
void * submitter( void *arg);
int WaitFreeThread();
static int activethreads = 0;
static int maxthreads = 5;
int main()
{
FILE *fp;
pthread_t tid;
char buffer[2001];
char *newline;
int status;
int checker=0;
char temp[2000];
int i;
fp = fopen(".temp","r");
while((fgets(buffer,sizeof(buffer),fp))!=NULL)
{
newline = (char * ) malloc (strlen(buffer) + 1);
strcpy(temp,buffer);
checker=strchr(temp,'\n');
bzero(checker,1);
strcat(temp,checker);
strcpy(newline,temp);
status = pthread_create(&tid, NULL, submitter,newline);
if(status == 0) {
pthread_mutex_lock(&thread_exit_mtx);
activethreads++ ;
pthread_cond_signal(&thread_exit_cnd);
pthread_mutex_unlock(&thread_exit_mtx);
}
if(activethreads >= maxthreads) {
WaitFreeThread();
}
}
pthread_exit((void * ) (pthread_self()));
return 1;
}
void * submitter( void * arg)
{
struct hostent *host;
struct in_addr addr;
struct sockaddr_in sin;
int sock;
int sts;
char msg[2000];
int n;
printf("%d -> %s\n",pthread_self(),(char*) arg);
if((host=gethostbyname((char*)arg))==NULL)
{
printf("error: %s\n",(char*)arg);
return NULL;
}
sock=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
bzero((char *) &sin, sizeof(sin));
sin.sin_family=AF_INET;
sin.sin_port=htons(80);
bcopy(host->h_addr, (char *)&sin.sin_addr, host->h_length);
sts=connect(sock, (struct sockaddr*)&sin, sizeof(sin));
if((sts==-1)||(errno == ECONNREFUSED)) pthread_exit((void *)1);
sprintf(msg,"GET /\r\n\r\n");
send(sock,msg,strlen(msg),0);
printf("%s\n\n\n\n",msg);
close(sock);
printf("Thread %d done\n",pthread_self());
pthread_mutex_lock(&thread_exit_mtx);
activethreads--;
pthread_cond_signal(&thread_exit_cnd);
pthread_mutex_unlock(&thread_exit_mtx);
free(arg); arg = NULL;
return NULL;
}
int WaitFreeThread(){
while(activethreads > maxthreads) {
printf("Waiting\n");
sleep(1);
}
printf("A thread has exited, ready to start a new one\n");
return 1;
}
--------------------------------------------------------------------------------
浙公网安备 33010602011771号