这是我用c语言编写的代码和pthread:
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <time.h>
#define BUFFER_SIZE 5
#define TRUE 1
int numberOfLines ;
int sumCounter, subCounter, mulCounter, divCounter ;
int sumFileEnd, subFileEnd, mulFileEnd, divFileEnd ;
pthread_t *tid ;
clock_t begin, end;
double time_spent;
sem_t sumfull, sumEmpty ;
sem_t subfull, subEmpty ;
sem_t divfull, divEmpty ;
sem_t mulfull, mulEmpty ;
char sumBuff [5][25] ;
char subBuff [5][25] ;
char divBuff [5][25] ;
char mulBuff [5][25] ;
pthread_mutex_t sumMutex ;
pthread_mutex_t subMutex ;
pthread_mutex_t divMutex ;
pthread_mutex_t mulMutex ;
void initData(){
pthread_mutex_init(&sumMutex, NULL) ;
pthread_mutex_init(&subMutex, NULL) ;
pthread_mutex_init(&divMutex, NULL) ;
pthread_mutex_init(&mulMutex, NULL) ;
sem_init(&sumfull, 0, 0) ;
sem_init(&sumEmpty, 0, BUFFER_SIZE) ;
sem_init(&subfull, 0, 0) ;
sem_init(&subEmpty, 0, BUFFER_SIZE) ;
sem_init(&divfull, 0, 0) ;
sem_init(&divEmpty, 0, BUFFER_SIZE) ;
sem_init(&mulfull, 0, 0) ;
sem_init(&mulEmpty, 0, BUFFER_SIZE) ;
sumCounter = subCounter = mulCounter = divCounter = 0 ;
sumFileEnd = subFileEnd = mulFileEnd = divFileEnd = 0 ;
}
void *plusReadThread(){
printf("Enter to the PRT\n");
FILE *sumfp = fopen("sample.txt","r");
sumFileEnd = 0;
char * line = NULL;
size_t len = 0;
ssize_t read;
char *isSum ;
int lineNumber = 1 ;
if (lineNumber == 1){
read = getline(&line, &len, sumfp) ;
lineNumber ++ ;
}
while (!feof(sumfp)){
printf("Enter to the PRTW\n");
read = getline(&line, &len, sumfp) ;
isSum = strchr(line, '+') ;
if (isSum != NULL){
sem_wait(&sumEmpty);
printf("wait on sumEmpty\n");
pthread_mutex_lock(&sumMutex) ;
printf("Lock sumMutex\n");
if(sumCounter < BUFFER_SIZE){
char temp[20] ;
sprintf(temp, "%d", lineNumber);
strcat(temp, ".");
strcat (temp, line);
strcpy(sumBuff[sumCounter], temp);
sumCounter ++;
}
pthread_mutex_unlock(&sumMutex);
printf("Unlock sumMutex\n");
sem_post(&sumfull);
printf("Post on sumfull\n");
}
lineNumber ++;
}
if (feof(sumfp))
{
printf("SumFileEnd\n");
sumFileEnd = 1;
}
fclose(sumfp);
}
void *plusExec(){
printf("Enter to th PE\n");
char *search = "+" ;
char *temp1;
char *temp2;
char *saveptr1, *saveptr2 ;
int operand1 ;
int operand2 ;
int result ;
while (!sumFileEnd || sumCounter!=0){
printf("Enter to the PEW\n");
sem_wait(&sumfull);
printf("Wait on sumfull\n");
pthread_mutex_lock(&sumMutex);
printf("Lock sumMutex\n");
temp1 = strtok_r(sumBuff[sumCounter-1], search, &saveptr1) ;
operand2 = atoi(strtok_r(NULL, search, &saveptr1));
temp2 = strtok_r(temp1, ".", &saveptr2) ;
operand1 = atoi(strtok_r(NULL, ".", &saveptr2)) ;
result = operand1 + operand2 ;
printf("Exex Sum result\n");
printf("%d %d\n", atoi(temp2), result);
sumCounter --;
sem_post(&sumEmpty);
printf("Post on sumEmpty\n");
pthread_mutex_unlock(&sumMutex);
printf("Unlock sumMutex\n");
}
if(sumFileEnd && sumCounter==0)
printf("Returned for end of work sum\n");
}
void *subReadThread(){
printf("Enter to the SRT\n");
FILE *subfp = fopen("sample.txt","r");
char * line = NULL;
size_t len = 0;
ssize_t read;
char *isSub ;
int lineNumber = 1 ;
if (lineNumber == 1){
read = getline(&line, &len, subfp) ;
lineNumber ++ ;
}
while (!feof(subfp)){
printf("Enter to the SRTW\n");
read = getline(&line, &len, subfp) ;
isSub = strchr(line, '-') ;
if (isSub != NULL){
sem_wait(&subEmpty);
printf("Wait subEmpty\n");
pthread_mutex_lock(&subMutex) ;
printf("Lock subMutex\n");
if(subCounter < BUFFER_SIZE){
char temp[20] ;
sprintf(temp, "%d", lineNumber);
strcat(temp, ".");
strcat (temp, line);
strcpy(subBuff[subCounter], temp);
subCounter ++;
}
pthread_mutex_unlock(&subMutex);
printf("Unlock subMutex\n");
sem_post(&subfull);
printf("Post on subfull\n");
}
lineNumber ++;
}
if (feof(subfp))
{
printf("SubFileEnd\n");
subFileEnd = 1;
}
fclose(subfp);
}
void *subExec(){
printf("Enter to the SE\n");
char *search = "-" ;
char *temp1;
char *temp2;
char *saveptr1, *saveptr2 ;
int operand1 ;
int operand2 ;
int result ;
while (!subFileEnd || subCounter!=0){
printf("Enter to SEW \n");
sem_wait(&subfull);
pthread_mutex_lock(&subMutex);
temp1 = strtok_r(subBuff[subCounter-1], search, &saveptr1) ;
operand2 = atoi(strtok_r(NULL, search, &saveptr1));
temp2 = strtok_r(temp1, ".", &saveptr2) ;
operand1 = atoi(strtok_r(NULL, ".", &saveptr2)) ;
result = operand1 - operand2 ;
printf("Exec sub result\n");
printf("%d %d\n", atoi(temp2), result);
subCounter --;
sem_post(&subEmpty);
printf("Post on subEmpty\n");
pthread_mutex_unlock(&subMutex);
printf("Unlock subMutex\n");
}
if(subFileEnd && subCounter==0)
printf("Returned for end of work sub\n");
}
void *mulReadThread(){
printf("Enter to the MRT\n");
FILE *mulfp = fopen("sample.txt","r");
char * line = NULL;
size_t len = 0;
ssize_t read;
char *isMul ;
int lineNumber = 1 ;
if (lineNumber == 1){
read = getline(&line, &len, mulfp) ;
lineNumber ++ ;
}
while (!feof(mulfp)){
printf("Enter to the MRTW\n");
read = getline(&line, &len, mulfp) ;
isMul = strchr(line, '*') ;
if (isMul != NULL){
sem_wait(&mulEmpty);
printf("Wait on mulEmpty\n");
pthread_mutex_lock(&mulMutex) ;
printf("Lock mulMutex\n");
if(mulCounter < BUFFER_SIZE){
char temp[20] ;
sprintf(temp, "%d", lineNumber);
strcat(temp, ".");
strcat (temp, line);
strcpy(mulBuff[mulCounter], temp);
mulCounter ++;
}
pthread_mutex_unlock(&mulMutex);
printf("Unlock mulMutex\n");
sem_post(&mulfull);
printf("Post on mulfull\n");
}
lineNumber ++;
}
if (feof(mulfp))
{
printf("MulFileEnd\n");
mulFileEnd = 1;
}
fclose(mulfp);
}
void *mulExec(){
printf("Enter to the ME\n");
char *search = "*" ;
char *temp1;
char *temp2;
char *saveptr1, *saveptr2 ;
int operand1 ;
int operand2 ;
int result ;
while (!mulFileEnd || mulCounter!=0){
printf("Enter to the MEW\n");
sem_wait(&mulfull);
pthread_mutex_lock(&mulMutex);
temp1 = strtok_r(mulBuff[mulCounter-1], search, &saveptr1) ;
operand2 = atoi(strtok_r(NULL, search, &saveptr1));
temp2 = strtok_r(temp1, ".", &saveptr2) ;
operand1 = atoi(strtok_r(NULL, ".", &saveptr2)) ;
result = operand1 * operand2 ;
printf("Exec mul result\n");
printf("%d %d\n", atoi(temp2), result);
mulCounter --;
sem_post(&mulEmpty);
printf("Post on mulEmpty\n");
pthread_mutex_unlock(&mulMutex);
printf("Unlock mulMutex\n");
}
if(mulFileEnd && mulCounter==0)
printf("Returned for end of work mul\n");
}
void *divReadThread(){
printf("Enter to the DRT\n");
FILE *divfp = fopen("sample.txt","r");
char * line = NULL;
size_t len = 0;
ssize_t read;
char *isDiv ;
int lineNumber = 1 ;
if (lineNumber == 1){
read = getline(&line, &len, divfp) ;
lineNumber ++ ;
}
while (!feof(divfp)){
printf("Enter to the DRTW\n");
read = getline(&line, &len, divfp) ;
isDiv = strchr(line, '/') ;
if (isDiv != NULL){
sem_wait(&divEmpty);
printf("Wait on divEmpty\n");
pthread_mutex_lock(&divMutex) ;
printf("Lock divMutex\n");
if(divCounter < BUFFER_SIZE){
char temp[20] ;
sprintf(temp, "%d", lineNumber);
strcat(temp, ".");
strcat (temp, line);
strcpy(divBuff[divCounter], temp);
divCounter ++;
}
pthread_mutex_unlock(&divMutex);
printf("Unlock divMutex\n");
sem_post(&divfull);
printf("Post on divfull\n");
}
lineNumber ++;
}
if (feof(divfp))
{
printf("DivFileEnd\n");
divFileEnd = 1;
}
fclose(divfp);
}
void *divExec(){
printf("Enter to the DE\n");
char *search = "/" ;
char *temp1;
char *temp2;
char *saveptr1, *saveptr2 ;
float operand1 ;
float operand2 ;
float result ;
while (!divFileEnd || divCounter!=0){
printf("Enter to the DEW\n");
sem_wait(&divfull);
pthread_mutex_lock(&divMutex);
temp1 = strtok_r(divBuff[divCounter-1], search, &saveptr1) ;
operand2 = atof(strtok_r(NULL, search, &saveptr1));
temp2 = strtok_r(temp1, ".", &saveptr2) ;
operand1 = atof(strtok_r(NULL, ".", &saveptr2)) ;
result = operand1 / operand2 ;
printf("Exec div result\n");
printf("%d %f\n", atoi(temp2), result);
divCounter --;
sem_post(&divEmpty);
printf("Post on divEmpty\n");
pthread_mutex_unlock(&divMutex);
printf("Unlock divMutex\n");
}
if(divFileEnd && divCounter==0)
printf("Returned for end of work div\n");
}
int main (int argc, char* argv []){
initData() ;
tid = (pthread_t*)malloc(8*sizeof(pthread_t)) ;
pthread_create(&tid[0],NULL,plusReadThread,NULL);
pthread_create(&tid[1],NULL,plusExec,NULL);
pthread_create(&tid[2],NULL,subReadThread,NULL);
pthread_create(&tid[3],NULL,subExec,NULL);
pthread_create(&tid[4],NULL,divReadThread,NULL);
pthread_create(&tid[5],NULL,divExec,NULL);
pthread_create(&tid[6],NULL,mulReadThread,NULL);
pthread_create(&tid[7],NULL,mulExec,NULL);
pthread_join(tid[0] , NULL);
pthread_join(tid[1] , NULL);
pthread_join(tid[2] , NULL);
pthread_join(tid[3] , NULL);
pthread_join(tid[4] , NULL);
pthread_join(tid[5] , NULL);
pthread_join(tid[6] , NULL);
pthread_join(tid[7] , NULL);
printf("Hi\n");
exit(0);
}
它为每个操作符创建4个线程。当我执行add
,div
和mul
个主题时,对于示例文件,例如:
24
3707 + 1056
3707 + 1056
3707 + 1056
3707 + 1056
3707 + 1056
3707 + 1056
4460 - 2396
4460 - 2396
4460 - 2396
4460 - 2396
4460 - 2396
4460 - 2396
2088 / 1958
2682 / 3028
1809 / 4601
4297 / 2841
2222 / 2551
4459 / 1466
1520 * 1189
4544 * 777
2987 * 2748
3028 * 3328
633 * 270
633 * 270
彼此之后超过5 mul
,它并没有完全结束。
这是跟踪堆栈:
Starting program: /home/emadhelmi/Desktop/OSPthread/Final/a
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7ffff77f6700 (LWP 18953)]
Enter to the PRT
Enter to the PRTW
[New Thread 0x7ffff6ff5700 (LWP 18954)]
.
.
.
[Thread 0x7fffef7fe700 (LWP 18961) exited]
^C
Program received signal SIGINT, Interrupt.
0x00007ffff7bc566b in pthread_join ()
from /lib/x86_64-linux-gnu/libpthread.so.0
(gdb) bt
#0 0x00007ffff7bc566b in pthread_join ()
from /lib/x86_64-linux-gnu/libpthread.so.0
#1 0x0000000000401e73 in main (argc=1, argv=0x7fffffffdff8) at cm.c:363
(gdb)
但它打印了所有结果。