/* Edited */
my Cmakefile.txt file is:
cmake_minimum_required(VERSION 3.3)
project(WebServer)
set(CMAKE_C_COMPILER "/usr/bin/gcc")
set(SOURCE_FILES io.c server.c lock_fcntl.c sig_handler.c thread_job.c msg_parser.c)
set(LIB http-parser-master/http_parser.c)
set(CMAKE_USE_PTHREADS_INIT true)
set(CMAKE_USE_PTHREADS_INIT ON)
find_package(Threads REQUIRED)
add_executable(server ${SOURCE_FILES} ${LIB})
target_link_libraries(server Threads::Threads)
add_executable(client client.c io.c)
include_directories(header)
include_directories(http-parser-master)
In this way, I can compile and link my code. However pthread_create seems not work.
server.c:
void create_thread(int socket) {
data_t *data;
data = malloc(sizeof(data_t));
if (data == NULL) {
fprintf(stderr, "error in memory allocation");
exit(EXIT_FAILURE);
}
data->sock = socket;
/* debug */
fprintf(stdout, "[*] Creating thread\n");
fflush(stdout);
if (pthread_create(data->tid, NULL, job_t, data) != 0) {
fprintf(stderr, "Error returned by pthread_create()\n");
exit(EXIT_FAILURE);
}
/* debug */
fprintf(stdout, "[*] Thread created\n");
fflush(stdout);
}
thread_job.c:
void *job_t(void *arg) {
data_t *data = arg;
char *http_msg_h;
int nread;
/* debug */
fprintf(stdout, "start listening\n");
fflush(stdout);
/* read http header from connsd socket and store it in msg buffer */
nread = receive_msg_h(data->sock, (void **) &http_msg_h);
/* debug */
fprintf(stdout, "[+] parsing completed");
fflush(stdout);
}
Neither "start listening nor "Thread creation" is printed in output. it is as if the function does not return but it doesn't work.
CMakeLists.txt is correct. My error was in code.
I change
pthread_create(data->tid, NULL, job_t, data) != 0
in pthread_create(&data->tid, NULL, job_t, data) != 0.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With