Linux system programming (2)

Source: Internet
Author: User

An EXEC function

E:env
int execle (const char *path, const char *ARG,..., char * const envp[]);

An array of environment variable pointers:
Char *envp[] = {"Environment variable name = Content", NULL};

Two multi-threading

1. Threads are the smallest execution unit in a process, and multithreading shares the address space of the same process
2.Linux Kernel Scheduler is a thread, so multiple threads in a process participate in operating system unified scheduling

Using Threading Benefits:
<1> High Efficiency
Easy communication between <2> threads (global variables)

Use thread Disadvantage:
Poor security

Thread API

1. Thread creation
int Pthread_create (pthread_t *thread, const pthread_attr_t *attr,
void * (*start_routine) (void *), void *arg);

Parameters:
@thread Get thread ID
@attr Set thread Properties NULL: Default Property
Functions that are executed @start_routine thread
@arg parameters passed to the thread execution function

return value:
Successful return 0, failure to return error code

Practice:
Create two sub-threads, child 1 thread output data value, child 2 thread data + + operation, main thread will data-operation (data is defined in main thread)

2. Thread exit

(1) Thread function returns (return)
(2) Pthread_exit
(3) Pthread_cancel
(4) End of process, all threads in this process are exited

void Pthread_exit (void *retval);
Function: Used to exit a thread
Parameters:
@retval returns an address value


int Pthread_join (pthread_t thread, void **retval);
Function: Waits for a thread to exit, releasing resources that are not freed from the thread
Parameters:
@thread need to wait for the ID of the exit thread
@retval get the value returned by thread exit


void *thread_function (void *arg)
{
int data = 100;

Pthread_exit (&data);
}

int main ()
{
int *p;

Pthread_join (TID,&AMP;P);
return 0;
}

3. Separating the thread marks

The separation state of the thread at the end of the time, the system automatically reclaims its resources

int Pthread_detach (pthread_t thread);
@thread thread ID


Three-thread mutual exclusion lock

Function: Mutually exclusive access to shared resources to ensure the integrity of access

If a mutex is used, each thread accesses the shared resource, must first obtain the mutex, and then access the shared resource if the mutex cannot be obtained
, it means that another thread is accessing the shared resource, and the thread that does not have the lock is blocked until the other thread releases the lock and can get the lock again

1. Define the mutex [global so that each thread can access it]
pthread_mutex_t lock;

2. Initialize the mutex lock

Dynamic initialization of [before thread creation], property using default NULL
int Pthread_mutex_init (pthread_mutex_t *restrict mutex,const pthread_mutexattr_t *restrict attr);

Static initialization definition Initialization
pthread_mutex_t mutex = Pthread_mutex_initializer;

3. Obtain the Mutex lock
int Pthread_mutex_lock (pthread_mutex_t *mutex);//lock before operating a shared resource
int Pthread_mutex_trylock (pthread_mutex_t *mutex);

4. Release the lock
int Pthread_mutex_unlock (pthread_mutex_t *mutex);//When the Operation sharing resource ends

5. Destroying the lock
int Pthread_mutex_destroy (pthread_mutex_t *mutex);//No need to use the lock again


Linux system Programming (2)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.