#include "cs241.c"
#define THREAD_COUNT 10
int list_s;
int connections[THREAD_COUNT];
char space[THREAD_COUNT];
int done = 0;
pthread_mutex_t muxlock = PTHREAD_MUTEX_INITIALIZER;
int *numbers;
int numbers_count;
void *listener(void *arg) {
int n = *(int *) arg;
FILE *f = fdopen(connections[n], "r");
if (f == NULL)
printf("Could not open file\n");
char *line = NULL;
size_t *len = malloc(sizeof(int));
while(getline(&line, len, f) != -1) {
printf("%s", line);
if (strcmp("END", line) == 0) {
pthread_mutex_lock(&muxlock);
done = 1;
pthread_mutex_unlock(&muxlock);
}
}
space[n] = 't';
fclose(f);
free(len);
close(connections[n]);
return NULL;
}
void initialize() {
int n;
for (n = 0; n < THREAD_COUNT; n++) {
space[n] = 't';
}
}
int check() {
int index;
for (index = 0; index < THREAD_COUNT; index++) {
if (space[index] == 't') {
space[index] = 'f';
return index;
}
}
return 0;
}
int main(int argc, char *argv[]) {
int port = 0;
int binder;
int lis;
int i = 0;
int *j = malloc(sizeof(int));
initialize();
pthread_t threads[THREAD_COUNT];
if ((argc != 2) || (sscanf(argv[1], "%d", &port) != 1)) {
fprintf(stderr, "Usage: %s [PORT]\n", argv[0]);
exit(1);
}
if (port < 1024) {
fprintf(stderr, "Port must be greater than 1024\n");
exit(1);
}
// set the initial conditions for the numbers array.
numbers = malloc(sizeof(int));
numbers_count = 0;
struct sockaddr_in servaddr; // socket address structure
// set all bytes in socket address structure to zero, and fill in the relevant data members
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(port);
list_s = socket(AF_INET, SOCK_STREAM, 0);
if (list_s < 0) {
printf("Could not create socket\n");
exit(EXIT_FAILURE);
}
binder = bind(list_s, (struct sockaddr *) &servaddr, sizeof(servaddr));
if (binder < 0) {
printf("Could not bind socket\n");
exit(EXIT_FAILURE);
}
lis = listen(list_s, SOMAXCONN);
if (lis < 0) {
printf("Could not listen on socket\n");
exit(EXIT_FAILURE);
}
SET_NON_BLOCKING(list_s);
while (done != 1) {
connections[i] = accept(list_s, NULL, NULL);
if (connections[i] < 0) {
if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
continue;
printf("Could not accept connection\n");
exit(EXIT_FAILURE);
}
i = check();
*j = i;
SET_BLOCKING(list_s);
pthread_create(&threads[i], NULL, listener, j);
}
// Verify the array.
//verify(numbers, numbers_count);
free(j);
close(list_s);
exit(EXIT_SUCCESS);
}
所以我有一個while循環在我的main(),它應該退出時,'完成'= 1。這是由偵聽器()設置,當它收到'結束'。第一個問題是,當我在第一次迭代中發送'END'時,只有在發送另一個'END'後,while循環纔會退出。POSIX線程在C插座
我有兩個宏SET_NON_BLOCKING和SET_BLOCKING在另一個文件中用於解鎖和阻塞套接字,因此如果沒有連接,它會等待連接。接下來的問題是,當我不使用這些宏時,listener()中的getline()無法讀取從流輸出的所有內容。當我使用它們時,它根本無法打開流。
我認爲一些問題在於將'j'傳遞給線程,因爲當第二個線程啓動時,它會在第一個線程可以讀取之前覆蓋'j'。然而,我已經在這裏呆了幾天,並且無法到達任何地方。任何幫助將不勝感激。謝謝!
另外我想問一下,我的套接字阻塞和線程鎖定是否在正確的位置?
「size_t * len = malloc(sizeof(int));」 – loreb
@loreb:我不確定你的評論指出了什麼。然而,分配是錯誤的('int'可能與size_t不一樣),但更重要的是它是不必要的。只需要'size_t len;'並傳遞len的地址:'getline(&line,&len,f)' –