How to implement secure threading

I used a multi-threaded library before in Python, but this is the first time I try to use threading in C. I want to create a worker pool. In turn, these workers had to push or jump out of the queue. The coding of the code is not quite right yet, but this is what I have done so far:

#include <stdio.h> #include <stdlib.h> #include <pthread.h> #define NUMTHREADS 20 /* number of threads to create */ typedef struct node node; typedef struct queue queue; struct node { char *name; node *next; }; struct queue { node *head; node *tail; }; /* pop: remove and return first name from a queue */ char *pop(queue *q) { if (q->head == NULL) return NULL; char *name = q->head->name; node *tmp = q->head; q->head = q->head->next; free(tmp); return name; } /* push: add name to the end of the queue */ int push(queue *q, char *name) { node *new = malloc(sizeof(node)); if (new == NULL) return -1; new->name = name; new->next = NULL; if (q->tail != NULL) q->tail->next = new; q->tail = new; if (q->head == NULL) /* first value */ q->head = new; return 0; } /* printname: get a name from the queue, and print it. */ void *printname(void *sharedQ) { queue *q = (queue *) sharedQ; char *name = pop(q); if (name == NULL) pthread_exit(NULL); printf("%s\n",name); pthread_exit(NULL); } int main() { size_t i; int rc; pthread_t threads[NUMTHREADS]; char *names[] = { "yasar", "arabaci", "osman", "ahmet", "mehmet", "zeliha" }; queue *q = malloc(sizeof(queue)); q->head = NULL; q->tail = NULL; /* number of elements in the array */ size_t numelems = sizeof(names) / sizeof(char *); for (i = 0; i < numelems; i++) /* push each name */ push(q, names[i]); for (i = 0; i < NUMTHREADS; i++) { /* fire up threads */ rc = pthread_create(&threads[i], NULL, printname, (void *)q); if (rc) { printf("Error, return code from pthread is %d\n", rc); exit(-1); } } pthread_exit(NULL); } 

I tried the code and it always printed each name exactly once. He did not skip any names or print the same name twice. On the other hand, I'm not sure how much streaming security is implemented in this queue. So my question is: is this a streaming queue? If not, why not? And how to make it thread safe?

+6
source share
2 answers

The code is not thread safe.

The push and pop functions are not thread safe. In the code, pressing is only performed by one thread, so it does not matter, but pops are executed by several threads.

 1. char *name = q->head->name; 2. node *tmp = q->head; 3. q->head = q->head->next; 4. free(tmp); 

Imagine that thread A runs before and including line 2. Then thread B executes and includes line 4. Thread A resumes execution. He found that q-> head is already free () ed.

Now it discusses logical issues.

However, there are physical problems that need to be considered.

Imagine that we had a locking mechanism in which threads could synchronize their behavior, so that only one thread at a time could execute code in lines 1 through 4, for example. a mutex, which is the object of only one thread, can โ€œholdโ€ at a time, and when an attempt to get a mutex blocks the thread until the current thread is released.

 0. get mutex 1. char *name = q->head->name; 2. node *tmp = q->head; 3. q->head = q->head->next; 4. free(tmp); 5. release mutex 

We would still have a problem: records executed by any CPU core (not a thread) are immediately visible only to threads on that core; not for threads on other cores.

Simply synchronizing execution is not enough; at the same time, we must also ensure that the entries made by the kernel become visible to other kernels.

(Un), fortunately, all modern synchronization methods also perform this flushing of a record (for example, when you get a mutex, you also clear all records in memory). I say, unfortunately, because you - never need this behavior, and it is bad for performance.

+5
source

It is not thread safe, as multiple threads can change pointers in a linked list at the same time, which can damage it.

Here you have an answer to a very similar question: Multi-user streaming queue in C

Here you can see how to queue in streaming mode.

+3
source

Source: https://habr.com/ru/post/916457/


All Articles