Thread of LINUX system programming

Thread of LINUX system programming

scene:

In a dual-core virtual machine there are two thread functions that perform the following functions:

Thread 1: printf("hello\n");

Thread 2: printf("world\n");

When the program is running, the execution order of the two threads is different in the single-core state and the dual-core state. What rules are they scheduled according to?


The process has its own data segment, code segment, and stack, which occupies a lot of resources, has high overhead, and is inconvenient to communicate.

In order to reduce system overhead, threads are evolved from processes

Threads exist in the process and use the resources of the process


I. Overview

Thread is the basic unit of CPU scheduling and allocation, exists in the process, and is an independent control flow in the process

A process is the basic unit of program execution and resource allocation in a system

The thread does not own the resource itself

Processes have one thread of control (main thread) by default

Threads depend on the existence of a process, and when the process ends, the thread also ends

Thread takes up less space

Purpose:

multitasking programming

concurrent programming

network program

data sharing

Multi-CPU Parallelism


2. Operation

void *fun(void *arg)

Pay attention to thread function parameters and return value types

pthread_t pth;

Create a thread pthread_create(&pth, NULL, fun, (void *)arg); (multiple parameters can be passed in a structure or array)

Wait for the thread to finish recycling its resources pthread_join(pth, NULL);

Detach thread pthread_detach(pth);

Exit the thread pthread_exit();

Cancel thread pthread_cancle();

Cancel state pthread_setcancelstate();

cancel type pthread_setcanceltype();

Set the cancellation point pthread_testcancel();

Clean up pthread_cleanup_push(); pthread_cleanup_pop(); The two functions must exist in pairs

Compile gcc ac plus -lpthread


In gtk programming, multiple threads may use the same resource to freeze the interface, so threads are mutually exclusive

Can be implemented using gtk_threads_enter(); and gtk_threads_leave();


3. Thread synchronization and mutual exclusion

Mutual exclusion: Multiple tasks access the same common resource, and only one task can access it at the same time

Mutexes and Semaphores

1. Mutex lock: mutex, lock and unlock two states, unlocking must be completed by the locker

Apply for mutex, block the applicant if locked

pthread_mutex_t mutex;

pthread_mutex_lock(&mutex);

pthread_mutex_trylock(&mutex);

pthread_mutex_unlock(&mutex);

pthread_mutex_destroy(&mutex);


2. Semaphore

non-negative integer counter

Subtract the semaphore, blocking if it is 0

PV primitive, P minus, V plus

sem_t sem;

sem_init(&sem, 0, 1);

sem_wait (& sem); sem_trywait (& sem);

sem_post (& sem);

int val;

sem_getvalue(&sem, &val);

sem_destroy(&sem);


Through semaphore synchronization operation, multiple tasks can run in sequence

thread: unnamed semaphore, process: named semaphore

One task one semaphore

named semaphore

sem_t *sem_open("sem", O_RDWR);

sem_close(sem);

sem_unlink("sem");

Named semaphores have different names in the program and in the file system

The named semaphore will save the previous value, so it should be deleted and created before use


Example:

There is a warehouse producer who is responsible for producing the product and putting it into the warehouse, and the consumer takes the product from the warehouse

request warehouse

Only one person can enter the library at a time

A maximum of 10 products can be stored in the warehouse, and no more products can be placed in the warehouse when the warehouse is full

Products can no longer be taken from the warehouse when it is empty

The speed of production and consumption is different

Ideas:

One thread for production and one for consumption, the warehouse is mutually exclusive, assuming a capacity of 10 and an inventory of 3

Assume production is faster than consumption

The value of the semaphore is equal to the remaining product

#include<stdio.h>

#include<sys/stat.h>

#include<fcntl.h>

#include<semaphore.h>

int total=10;//Total

int last=7;//Remaining amount

sem_t sem_p;

sem_t sem_c;

void *produce(void *arg)

{

// sem_t *temp_semp=(sem_t *)arg;

while(1)

{

// sem_p = total-last;

if(9 >= last)

{

sleep(2);

sem_wait(&sem_p);

last++;

printf("in!last=%d\n",last);

sem_post (& sem_c);

}

}

}

void *cost(void *arg)

{

// sem_t *temp_semp=(sem_t *)arg;

while(1)

{

// sem_c = last;

if(1 <= last)

{

sem_wait(&sem_c);

last--;

printf("out!last=%d\n",last);

sem_post (& sem_p);

sleep(3);

}

}

}

intmain()

{

pthread_t pth_p,pth_c;

sem_init(&sem_p,0,total-last);

sem_init(&sem_c,0,last);

printf("init_last=%d\n",last);

pthread_create(&pth_p,NULL,produce,NULL);

pthread_create(&pth_c,NULL,cost,NULL);

while(1);

return 0;

}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324655598&siteId=291194637