Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Threads of LINUX system programming

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Threads of LINUX system programming

Scenario:

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 in the single-core state is different from that in the dual-core state. What rules are they scheduled according to?

The process has its own data segment, code segment and stack, which takes up a lot of resources, high overhead and inconvenient communication.

In order to reduce system overhead, threads are evolved from the process

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

I. Overview

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

Process is the basic unit of program execution and resource allocation in the system.

The thread does not own the resource.

The process has a control thread (the main thread) by default

The thread depends on the existence of the process, and the process ends the thread.

Thread takes up less space

Objective:

Multitasking programming

Concurrent programming

Network program

Data sharing

Multi-CPU parallel

II. Operation

Void * fun (void * arg)

Pay attention to thread function parameters and return value types

Pthread_t pth

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

Wait for the thread to finish reclaiming its resource pthread_join (pth, NULL)

Detach thread pthread_detach (pth)

Exit thread pthread_exit ()

Cancel thread pthread_cancle ()

Cancel status pthread_setcancelstate ()

Cancel type pthread_setcanceltype ()

Set the cancellation point pthread_testcancel ()

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

Compile gcc a.c plus-lpthread

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

You can use gtk_threads_enter (); and gtk_threads_leave (); to implement

Third, synchronization and mutual exclusion of threads

Mutual exclusion: multiple tasks access the same public resource, and only one task can access it at a time

Mutexes and semaphores

1. Mutex: mutex. There are two states of locking and unlocking. Unlocking must be done by the locking person.

Apply for mutex, if lock, block the applicant

Pthread_mutex_t mutex

Pthread_mutex_lock & mutex)

Pthread_mutex_trylock & mutex)

Pthread_mutex_unlock & mutex)

Pthread_mutex_destroy & mutex)

two。 Semaphore

Non-negative integer counter

Subtract the semaphore and block 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)

Multi-tasks run sequentially through semaphore synchronization operation

Thread: anonymous semaphore, process: named semaphore

One task, one semaphore.

Famous semaphore

Sem_t * sem_open ("sem", O_RDWR)

Sem_close (sem)

Sem_unlink ("sem")

The name of a named semaphore is different from that in the file system.

A named semaphore will save the previous value, so you should delete it before you use it.

Example:

There is a warehouse producer who produces the product and puts it in the warehouse, and the consumer takes the product from the warehouse.

Required warehouse

Only one person can enter the library at a time.

A maximum of 10 products are stored in the warehouse, and no more products can be put in the warehouse when it is full.

Products can no longer be taken out of the warehouse when it is empty.

The speed of production and consumption is different

Train of thought:

Production and consumption each have a thread, and the warehouse is mutually exclusive, assuming that the capacity is 10 and the inventory is 3.

Assume that the speed of production is faster than that of consumption

The value of the semaphore is equal to the surplus product.

# include

# include

# include

# include

Total int total=10;//

Int last=7;// surplus

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 ("inactive lastworthy% 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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report