Linux System Programming Threads
Scene:
There are two thread functions in a dual-core virtual machine that perform the following functions:
Line Cheng: printf ("hello\n");
Thread Two: printf ("world\n");
When the program runs in a single-core state and a dual-core state, the execution sequence of two threads is different, how are they scheduled according to the rules?
The process has its own data segments, code snippets, stacks, resource-intensive, overhead, communication inconvenient
To reduce system overhead, threads are evolved from the process
Threads exist in the process, using the resources of the process
I. Overview
Threads are the basic unit of CPU scheduling and allocation, exist in the process, and are independent control flows in the process
Process is the basic unit of program execution and resource allocation in the system
Threads do not own resources themselves
Process has a control thread by default (main thread)
Thread relies on process existence, process end thread also ends
The thread takes up less space
Objective:
Multi-tasking programming
Concurrent programming
Network program
Data sharing
Multi-CPU Parallelism
Second, the operation
void *fun (void *arg)
Note Thread function parameters and return value types
pthread_t PTH;
Create Thread Pthread_create (&pth, NULL, fun, (void *) arg); (Multiple parameters can be passed by struct or array)
Waits for the thread to end reclaiming its resource pthread_join (PTH, NULL);
Detach thread Pthread_detach (PTH);
Exit thread Pthread_exit ();
Cancels the thread pthread_cancle ();
Cancel status pthread_setcancelstate ();
Cancellation type Pthread_setcanceltype ();
Set Cancel point pthread_testcancel ();
Clean Pthread_cleanup_push ();p thread_cleanup_pop (); Two functions must exist in pairs
Compiling gcc A.C plus-lpthread
Multiple threads in GTK programming may freeze using the same resource as the interface, so the thread is mutually exclusive
can use Gtk_threads_enter (), and Gtk_threads_leave ();
Third, synchronization and mutual exclusion of threads
Mutual exclusion: Multiple tasks access the same public resource at the same time only one task can access
Mutual exclusion Locks and semaphores
1. Mutex: Mutex, lock unlock two states, unlock must be done by the lock
Apply for a mutex if lock blocks the applicant
pthread_mutex_t Mutex;
Pthread_mutex_lock (&mutex);
Pthread_mutex_trylock (&mutex);
Pthread_mutex_unlock (&mutex);
Pthread_mutex_destroy (&mutex);
2. Signal Volume
Non-negative integer counter
Reduce the amount of semaphore, if 0 is blocked
PV Primitives, 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);
Sequential operation of multiple tasks with semaphore synchronization
Threads: Nameless semaphore, process: known semaphore
A task a semaphore
Well-known signal volume
sem_t *sem_open ("sem", O_RDWR);
Sem_close (SEM);
Sem_unlink ("sem");
The name of a well-known semaphore is different in the program from the file system.
A well-known semaphore will save the previous value so it should be deleted before it is created.
Instance:
A warehouse producer is responsible for producing products and putting them in warehouses where consumers take their products from the warehouse.
Request Warehouse
Library can only enter one person at a time
Store up to 10 products in the warehouse, no more when the warehouse is full
The warehouse can no longer be removed from the product when it is empty
Different production and consumption speed
Ideas:
Production and consumption of each thread, the warehouse is mutually exclusive, assuming a capacity of 10, inventory of 3
Assuming production speed is faster than consumption
The value of the semaphore equals the surplus.
#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);
}
}
}
int main ()
{
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;
}
Linux System Programming Threads