C语言多线程输出不正常求高手指点
在linux下写的一个小练习,通过用pthread和popen管道代码如下:#include<stdio.h>#include<stdlib.h>#include<pth...
在linux下写的一个小练习,通过用pthread 和 popen管道
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <semaphore.h>
#include <unistd.h>
void *fun_ping(void *att);
sem_t sem;
char str[BUFSIZ];
int main(int argc, char **argv) {
int i = 1;
int res;
pthread_t thread;
sem_init(&sem, 0, 3);
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
for (; i <= 255; i++) {
sem_wait(&sem);
sprintf(str, "ping -c 1 117.88.34.%d", i);
res = pthread_create(&thread, &attr, fun_ping, str);
if (res != 0) {
fprintf(stderr, "create thread failed\n");
}
}
sem_wait(&sem);
sem_wait(&sem);
sem_wait(&sem);
sem_destroy(&sem);
exit(EXIT_SUCCESS);
}
void *fun_ping(void *att) {
int read_chars;
char buff[BUFSIZ + 1];
char *str_pt;
FILE *fp = popen((char *) att, "r");
if (fp != NULL) {
read_chars = fread(buff, sizeof(char), BUFSIZ, fp);
while (read_chars > 0) {
str_pt = strstr(buff, "100%");
if (str_pt) {
printf("%s Unreachable\n", (char *) att);
sem_post(&sem);
pthread_exit(NULL);
}
read_chars = fread(buff, sizeof(char), BUFSIZ, fp);
}
}
printf("%s host\n", (char *) att);
sem_post(&sem);
pthread_exit(NULL);
}
为什么输出结果老是不正常:
ping -c 1 117.88.34.3 host
ping -c 1 117.88.34.4 Unreachable
ping -c 1 117.88.34.5 Unreachable
ping -c 1 117.88.34.6 Unreachable
ping -c 1 117.88.34.7 host
循环中117.88.34.1,117.88.34.2为什么没有输出阿。我试着把信号的初始值
调整为1就可以正常但是那样线程就保持1个。我想实现多线程执行。请高手帮忙指点,谢谢了
在for循环内,输出打印,确实已经把117.88.34.1,117.88.34.2作为参数来创建线程了,望高手指教 展开
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <semaphore.h>
#include <unistd.h>
void *fun_ping(void *att);
sem_t sem;
char str[BUFSIZ];
int main(int argc, char **argv) {
int i = 1;
int res;
pthread_t thread;
sem_init(&sem, 0, 3);
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
for (; i <= 255; i++) {
sem_wait(&sem);
sprintf(str, "ping -c 1 117.88.34.%d", i);
res = pthread_create(&thread, &attr, fun_ping, str);
if (res != 0) {
fprintf(stderr, "create thread failed\n");
}
}
sem_wait(&sem);
sem_wait(&sem);
sem_wait(&sem);
sem_destroy(&sem);
exit(EXIT_SUCCESS);
}
void *fun_ping(void *att) {
int read_chars;
char buff[BUFSIZ + 1];
char *str_pt;
FILE *fp = popen((char *) att, "r");
if (fp != NULL) {
read_chars = fread(buff, sizeof(char), BUFSIZ, fp);
while (read_chars > 0) {
str_pt = strstr(buff, "100%");
if (str_pt) {
printf("%s Unreachable\n", (char *) att);
sem_post(&sem);
pthread_exit(NULL);
}
read_chars = fread(buff, sizeof(char), BUFSIZ, fp);
}
}
printf("%s host\n", (char *) att);
sem_post(&sem);
pthread_exit(NULL);
}
为什么输出结果老是不正常:
ping -c 1 117.88.34.3 host
ping -c 1 117.88.34.4 Unreachable
ping -c 1 117.88.34.5 Unreachable
ping -c 1 117.88.34.6 Unreachable
ping -c 1 117.88.34.7 host
循环中117.88.34.1,117.88.34.2为什么没有输出阿。我试着把信号的初始值
调整为1就可以正常但是那样线程就保持1个。我想实现多线程执行。请高手帮忙指点,谢谢了
在for循环内,输出打印,确实已经把117.88.34.1,117.88.34.2作为参数来创建线程了,望高手指教 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询