簡體   English   中英

Linux中的Posix線程信號量

[英]Posix thread semaphore in linux

我正在嘗試編寫一個簡單的多線程程序,該程序具有6個線程,它們執行1.從用戶那里讀取輸入2.查找字符串的長度3.查找出現的字符4.計算編號。 元音5.計算特殊字符6.改變大小寫

除了出現字符線程外,它按預期運行。但是它不允許我輸入要查找的字符並前進到下一部分。

output-
      Converted Text:HIMANSHUSOURAV   //user input thread working correctly 
    length of text:15                //length working fine 
    Number of Vowels: 6              //count of vowels fine
     Number of Special characters: 0 // spl chars fine

但是,當控件達到計數時,在現有輸入字符串(在這種情況下為himanshusourav)中沒有出現特定字符(由用戶輸入)的情況,此處程序無需等待用戶輸入要查找的字符就向前移動並打印出現為0

o/p where error
 enter code here`enter the character whose occurence is
    to be counted in entered string:no of occurences of : 0

碼:

#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<semaphore.h>
#include<string.h>

void *read();
void *changecase();
void *length();
void *vowel();
void *splchar();
void *findchar();

#define size 1024
 char buffer[size];

sem_t sem1,sem2,sem3,sem4,sem5,sem6;

void main(){
    int res;
    void *threadresult;
    pthread_t casethread,readthread,lengththread,vowelthread,splcharthread,findcharthread;
//Semaphore Creation
    res=sem_init(&sem5,0,0);
    res=sem_init(&sem1,0,0);
    res=sem_init(&sem2,0,0);
    res=sem_init(&sem3,0,0);
    res=sem_init(&sem4,0,0);
    res=sem_init(&sem6,0,0);


//Thread creation
    res=pthread_create(&readthread,NULL,read,NULL);
    if(res!=0){
        perror("Error While creating thread");
        exit(EXIT_FAILURE);
    }

    res=pthread_create(&casethread,NULL,changecase,NULL);
    if(res!=0){
        perror("Error While creating thread");
        exit(EXIT_FAILURE);
    }

    res=pthread_create(&lengththread,NULL,length,NULL);
    if(res!=0){
        perror("Error While creating thread");
        exit(EXIT_FAILURE);
    }

    res=pthread_create(&vowelthread,NULL,vowel,NULL);
    if(res!=0){
        perror("Error While creating thread");
        exit(EXIT_FAILURE);
    }

    res=pthread_create(&splcharthread,NULL,splchar,NULL);
    if(res!=0){
        perror("Error While creating thread");
        exit(EXIT_FAILURE);
    }

    res=pthread_create(&findcharthread,NULL,findchar,NULL);
    if(res!=0){
        perror("Error While creating thread");
        exit(EXIT_FAILURE);
    }

//Thread joining

    res=pthread_join(readthread,&threadresult);

    res=pthread_join(casethread,&threadresult);

    res=pthread_join(lengththread,&threadresult);

    res=pthread_join(splcharthread,&threadresult);

    res=pthread_join(vowelthread,&threadresult);

    //sem_post(&sem6);
}

void *read(){

    while(strncmp("quit",buffer,4)!=0){
        printf("\n\nEnter Text:");
        fgets(buffer,size,stdin);
        sem_post(&sem1);
        sleep(1);
//      sem_wait(&sem6);
    }
}


void *changecase(){
    int i;

    while(strncmp("quit",buffer,4)!=0){
        sem_wait(&sem1);    
        printf("\nConverted Text:");
        for(i=0;i<strlen(buffer);i++)
            printf("%c",toupper(buffer[i]));
        sem_post(&sem2);
        sleep(1);

    }
}

void *length(){
    int i;
    while(strncmp("quit",buffer,4)!=0){
        sem_wait(&sem2);
        printf("length of text:%d",strlen(buffer));
        sem_post(&sem3);
        sleep(1);
    }
}

void *vowel(){
    int i,vowels=0;
    while(strncmp("quit",buffer,4)!=0){
        sem_wait(&sem3);
        for(i=0;i<strlen(buffer);i++){
            if (buffer[i]=='a'|| buffer[i]=='e' || buffer[i]=='i' || buffer[i]=='o' || buffer[i]=='u')
                vowels++;
        }
        printf("\nNumber of Vowels: %d", vowels);
        vowels=0;
        sem_post(&sem4);
        sleep(1);
    }
}

void *splchar(){
    int i,splch=0;
    while(strncmp("quit",buffer,4)!=0){
        sem_wait(&sem4);
        for(i=0;i<strlen(buffer);i++){
            if(buffer[i]== '_' ||buffer[i]=='@' ||buffer[i]=='#' || buffer[i]=='*' || buffer[i]=='.')
                splch++;
        }
        printf("\nNumber of Special characters: %d\n",splch);
        splch=0;
        sem_post(&sem5);
        sleep(1);
    }
}


void *findchar(){
    int i,count;
 char ch[1];
    while(strncmp("quit",buffer,4)!=0){
        sem_wait(&sem5);
        count=0;
        printf("enter the character whose occurence is to be counted in entered string:");
        fgets(ch,1,stdin);
        for(i=0;i<strlen(buffer);i++){
            if(ch[1]==buffer[i])
                count++;
        }
    printf("\nno of occurences of %s: %d",ch,count);
//  sem_post(&sem6);
    sleep(1);
    }
}

端子輸出

root @ h2o-Vostro-1015:〜/ C / threads#gcc -o sem semsixthreads.c -lpthread root @ h2o-Vostro-1015:〜/ C / threads#./sem

輸入文字:himanshusourav

轉換后的文本:HIMANSHUSOURAV文本長度:15元音數量:6特殊字符數量:0輸入要在輸入的字符串中計算其出現次數的字符:沒有出現次數:0

輸入文字:himanshu_sourav

轉換后的文本:HIMANSHU_SOURAV文本的長度:16元音的數量:6特殊字符的數量:1輸入要在輸入的字符串中計算其出現次數的字符:沒有出現的次數:0

好心提醒...

在這種情況下,對大小為1的緩沖區的fgets調用沒有意義,因為緩沖區中僅存儲一個NULL字節。 ch數組應至少包含兩個字符,並且緩沖區內容是根據ch [0](而非ch [1])檢查的。

正確的代碼是

void *findchar(){
    int i,num;
    while(strncmp("quit",buffer,4)!=0){
        sem_wait(&sem5);            
        for(i=0;i<strlen(buffer);i++){
            if(ch[0]==buffer[i])
                num++;
        }
        printf("\nno. of occurences of %s: %d",ch,num);
        num=0;
        sleep(1);

    }
    pthread_exit("find char occurrence thread completed");
}

用戶輸入部分為

printf("enter the character whose occurence is to be counted in entered string:");
        fgets(ch,3,stdin);

和變量聲明為

char ch[5];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM