Operating system Ubuntu (experiment three or four)

3._Experiment 3: Asynchronous and concurrent execution of Linux processes/threads

3.1_fork() function creates a child process

Fork() function usage explains
personal views :

#include <unistd.h>  
#include <stdio.h>   
int main ()   
{
    
       
    pid_t fpid; //fpid表示fork函数返回的值  
    int count=0;  
    fpid=fork();   
    if (fpid < 0)   
        printf("error in fork!");   
    else if (fpid == 0) {
    
      
        printf("i am the child process, my process id is %d/n",getpid());   
        printf("我是爹的儿子/n");//对某些人来说中文看着更直白。  
        count++;  
    }  
    else {
    
      
        printf("i am the parent process, my process id is %d/n",getpid());   
        printf("我是孩子他爹/n");  
        count++;  
    }  
    printf("统计结果是: %d/n",count);  
    return 0;  
}  

When the execution reaches the code segmentfpid=fork(); 时, A new sub-process is created at this time, and its intuitive diagram is as follows:
Insert picture description here
Exercise:
draw a flowchart

#include <unistd.h>
#include <stdio.h>
int main(){
    
    
        pid_t son_pid,daughter_pid;
        int count = 1;
        son_pid = fork();
        if(son_pid == 0){
    
    
                count++;
                printf("i am son,count = %d\n",count);
        }else{
    
    
                daughter_pid = fork();
                if(daughter_pid == 0){
    
    
                        count++;
                        printf("i am daughter, count = %d\n",count);
                }else{
    
    
                        count++;
                        printf("i am father,ID = %d, count = %d\n",daughter_pid,count);
                        waitpid(son_pid,NULL,0);
                        waitpid(daughter_pid,NULL,0);
                }
        }
}

Insert picture description here

3.2_ Create thread

Use of pthread.h

pthread_create();

Insert picture description here
Create two threads, function son() and function daughter(); the execution order of the two is random and synchronized

4._Experiment 4: Using semaphores for mutual exclusion and synchronization

4.1_ Initial use of semaphore

(1) Brief introduction of semaphore

Linux provides two semaphores:
kernel semaphore: used for resource sharing control in the kernel.
User mode semaphore: mainly includes POSIX semaphore and SYSTEM V semaphore. Among them, POSIX semaphores are divided into two categories.
Unnamed semaphore: Mainly used for synchronization between threads, but also for synchronization between processes (generated by fork).
Well-known semaphore: It can be used for inter-process synchronization and inter-thread synchronization.

(2) How to use semaphore and P and V operations

POSIX famous semaphores mainly include:

sem_t* sem_open(const char *name, int oflag, mode_t mode, int value);
//例如:创建一个互斥信号量mutex,初值为1即mutex = 1,访问权限为0666(我也不知道啥意思),
//如信号量存在,则打开之,如不存在,则创建  
sem_t* mutex("mutex",O_CREATE,0666,1);

①name : file name path, such as'mysem', /dev/shm/sem.mysem will be created
②oflag :O_CREATEorO_CREATE | O_EXCL
O_CREATE: If the semaphore exists, open it, if it does not exist, create it
O_CREATE | O_EXCL:If the semaphore already exists, return error
③mode : semaphore access permission, such as0666
④value : semaphore initialization value

int sem_wait(sem_t *sem)

Test the value of the specified semaphore, which is equivalent toP operation
Ifsem> 0,thenMinus 1Return immediately;
ifsem = 0,thenSleep until sem> 0, Then immediately subtract 1 and then return

int sem_post(sem_t *sem)

Release resources, equivalent toV operation, The value of the semaphore sem is increased by 1, to wake up the process/thread that is waiting for the semaphore

int sem_close(sem_t *sem)

Close the famous semaphore
In the process, if the semaphore is used up, this function should be used to close the famous semaphore

int sem_unlink(const char *name)

Delete the semaphore in the system

(2) Use semaphore to achieve synchronous mutual exclusion

Guess you like

Origin blog.csdn.net/qq_43907296/article/details/106249994