Reputation: 710
I am trying to write a simple multithreaded program which has 6 threads performing 1. to read input from user 2. find length of string 3. find occurrence of character 4. count no. of vowels 5. count special characters 6. change case
except for the occurrence of character thread it is running as expected.but it doesnt allow me to enter the character to be looked and moves ahead to next part.
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
but when the control reaches the counting no of occurences of a particular character( to be entered by user) in the existing input string (himanshusourav in this case) here the program without waiting for user to input the char to be found moves ahead and prints occurence as 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
code:
#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);
}
}
Output on terminal
root@h2o-Vostro-1015:~/C/threads# gcc -o sem semsixthreads.c -lpthread root@h2o-Vostro-1015:~/C/threads# ./sem
Enter Text:himanshusourav
Converted Text:HIMANSHUSOURAV length of text:15 Number of Vowels: 6 Number of Special characters: 0 enter the character whose occurence is to be counted in entered string:no of occurences of : 0
Enter Text:himanshu_sourav
Converted Text:HIMANSHU_SOURAV length of text:16 Number of Vowels: 6 Number of Special characters: 1 enter the character whose occurence is to be counted in entered string:no of occurences of : 0
kindly advise...
Upvotes: 0
Views: 463
Reputation: 710
correct code would be
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");
}
with user input part as
printf("enter the character whose occurence is to be counted in entered string:");
fgets(ch,3,stdin);
and variable declaration as
char ch[5];
Upvotes: 0
Reputation: 15776
A fgets call into a buffer with size 1 doesn't make sense in this case as only a NULL byte will be stored inside the buffer. The ch array should be at least two characters, and the the buffer contents checked against ch[0], not ch[1].
Upvotes: 1