I have a client app implemented with OpenMP. This program terminate when it receives a SIGINT
signal, but in that case it should first send a message to the server indicating that the user logged out. I have implemented the messaging successfully, but I have been unable to make the program terminate afterward.
Here is the parallel section of my code:
#pragma omp parallel num_threads(4)
{
#pragma omp sections
{
#pragma omp section
{
while(1)
{
char pom[1000];
fgets(pom, 1000, stdin);
if (strlen(pom) != 1)
{
char * buffer;
buffer = message(username, pom);
if (send(client_socket, buffer, strlen(buffer), 0) < 0)
{
callError("ERROR: cannot send socked");
}
bzero(pom, 1000);
free(buffer);
}
}
}
#pragma omp section
{
char buffer[4096];
char * data;
ssize_t length;
int received = 0;
int data_cap = 4096;
while(1)
{
data = calloc(BUFFER_LEN, sizeof(char));
while ((length = read(client_socket, buffer, BUFFER_LEN)) > 0)
{
received += length;
if (received > data_cap)
{
data = realloc(data, sizeof(char) * data_cap * 2);
data_cap = data_cap * 2;
}
strcat(data, buffer);
if (!isEnough(data))
{
break;
}
}
printf("%s", data);
free(data);
bzero(buffer, BUFFER_LEN);
data_cap = 4096;
received = 0;
length = 0;
}
}
#pragma omp section
{
void sig_handler(int signo)
{
char * welcome1 = calloc(strlen(username) + 13, sizeof(char));
strcat(welcome1, username);
strcat(welcome1, " logged out\r\n");
if (send(client_socket, welcome1, strlen(welcome1), 0) < 0)
{
callError("ERROR: cannot send socked");
}
free(welcome1);
}
while (1)
{
if (signal(SIGINT, sig_handler) == SIG_ERR)
printf("\ncan't catch SIGINT\n");
while (1)
sleep(1);
}
}
}
How can I make the program terminate after catching the signal?