Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C linux中的Posix线程信号量_C_Linux_Multithreading_Posix - Fatal编程技术网

C linux中的Posix线程信号量

C linux中的Posix线程信号量,c,linux,multithreading,posix,C,Linux,Multithreading,Posix,我正在尝试编写一个简单的多线程程序,其中有6个线程执行 1.从用户处读取输入 2.查找字符串的长度 3.查找字符的出现 4.元音计数 5.计数特殊字符 6.换箱 除了出现字符线程外,它正在按预期运行。但它不允许我输入要查看的字符并向前移动到下一部分 output- Converted Text:HIMANSHUSOURAV //user input thread working correctly length of text:15 //l

我正在尝试编写一个简单的多线程程序,其中有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
代码:

#包括
#包括
#包括
#包括
#包括
void*read();
void*changecase();
空隙*长度();
void*元音();
void*splchar();
void*findchar();
#定义大小1024
字符缓冲区[大小];
sem_t sem1、sem2、sem3、sem4、sem5、sem6;
void main(){
国际关系;
无效*结果;
pthread_t casethread、readthread、lengthread、元音thread、splchart线程、findchart线程;
//信号量创建
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);
//线程创建
res=pthread_create(&readthread,NULL,read,NULL);
如果(res!=0){
perror(“创建线程时出错”);
退出(退出失败);
}
res=pthread_create(&casethread,NULL,changecase,NULL);
如果(res!=0){
perror(“创建线程时出错”);
退出(退出失败);
}
res=pthread_create(&lengthread,NULL,length,NULL);
如果(res!=0){
perror(“创建线程时出错”);
退出(退出失败);
}
res=pthread_create(&vuelthread,NULL,元音,NULL);
如果(res!=0){
perror(“创建线程时出错”);
退出(退出失败);
}
res=pthread_create(&splcharthread,NULL,splchar,NULL);
如果(res!=0){
perror(“创建线程时出错”);
退出(退出失败);
}
res=pthread_create(&findcharthread,NULL,findchar,NULL);
如果(res!=0){
perror(“创建线程时出错”);
退出(退出失败);
}
//螺纹连接
res=pthread_join(readthread,&threadresult);
res=pthread_join(casethread和threadresult);
res=pthread_join(lengthread和threadresult);
res=pthread_join(splcharthread和threadresult);
res=pthread_join(元音读取和线程结果);
//sem_post(和sem6);
}
void*read(){
while(strncmp(“退出”,缓冲区,4)!=0){
printf(“\n\n输入文本:”);
fgets(缓冲区、大小、标准输入);
sem_post(&sem1);
睡眠(1);
//sem_wait(&sem6);
}
}
void*changecase(){
int i;
while(strncmp(“退出”,缓冲区,4)!=0){
sem_wait(&sem1);
printf(“\n转换文本:”);

对于(i=0;i在这种情况下,对大小为1的缓冲区的fgets调用没有意义,因为缓冲区中只会存储一个空字节。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");
}
和变量声明为

char ch[5];

让你的问题更清楚。程序的实际输出不会帮助我们解决你的问题。@sujin做了一些更改,它有帮助吗…?绝对正确:)另外,在比较时,我使用的是ch[1],这没有意义。经过很多努力,我发现了自己…Austin-你对bug有很好的眼光:)
printf("enter the character whose occurence is to be counted in entered string:");
        fgets(ch,3,stdin);
char ch[5];