linux achieve three processes (A, B, C) sequentially prints A, B, C

linux achieve three processes (A, B, C) sequentially prints A, B, C

The key is how to make the subject three sequentially ordered print out the program A, B, C.
Here, I realized by lights

explain lights: https://blog.csdn.net/wfea_lff/article/details/104093277
have said before the lights, do not say here
how to make three unrelated processes have Print sequence: like in turn reported the same number than three people will be only the second person to hear his shouted figures represent the number of the first person after the report, analogies come back, how do we know that the order between processes, namely sequential operation

Here we can think of, which is operated by semaphore lights
semaphore can be simply understood as a resource
when a signal when a value of 1 is considered a resource is available at this time, that is, 0 no resources are available

We give an example
, when two users of a resource semaphore value 1 is operated, there will always be two users to operate a user, the other will be blocked because of a number of resources, at this time when the occupation when users leave the resource, the resource is let out another user begins blocked state to the operating state, in fact, a semaphore / light in the P / V operations

Having said that, this problem will be easily solved

1. The first preparatory work

Suppose we create a light, its interior contains three semaphore, for example: sem [0], sem [1], sem [2], and it in turn is initialized to 0, 0

2. A program for defining

A print of our program, before the print statement, let A program to acquire resources sem [0] (P operation), access to resources will run successfully, coupled to the second semaphore sem after the print statement [ operation 1] to V

3. The program defined for B

We print the B program, before the print statement, let B program to acquire resources sem [1] (P operation), access to resources will run successfully, coupled with the third semaphore sem after the print statement [ 2] for V operation.

4. The program defined for C

We print in the C program, before the print statement, let C program to acquire resources sem [2] (P operation), access to resources will run successfully, coupled to the first semaphore sem after the print statement [ 0] to V operation.

At this point the entire programming logic has come out

Reference Code:

Result of the program:

Here Insert Picture Description

A program

#include <stdio.h>
#include <unistd.h>
#include <sys/sem.h>
#include <sys/ipc.h>
#include <sys/types.h>

union semun
{
    int val;
    struct semid_ds *buf;
    unsigned short  *array;
    struct seminfo  *__buf;
};

int main()
{
    key_t key = ftok(".",123);
    if(key<0)
    {
        printf("creat semaphore's key fail\n");
        return -1;
    }
    
    int semid = semget(key,3,IPC_CREAT|0777);
    if(semid<0)
    {
        printf("creat semaphore fail\n");
        return -2;
    }

    union semun mysem[3];
    mysem[0].val = 1;
    semctl(semid,0,SETVAL,mysem[0]);

    mysem[1].val = 0;
    semctl(semid,1,SETVAL,mysem[1]);

    mysem[2].val = 0;
    semctl(semid,2,SETVAL,mysem[2]);
    
    struct sembuf mybuf[2];
    mybuf[0].sem_op = -1;
    mybuf[0].sem_num = 0;
    mybuf[0].sem_flg = 0;

    mybuf[1].sem_op = 1;
    mybuf[1].sem_num = 1;
    mybuf[1].sem_flg = 0;
    
    int i = 5;
    while(i)
    {
        semop(semid,&mybuf[0],1);
        sleep(1);
        printf("A\n");
        i--;
        semop(semid,&mybuf[1],1);
    }
    return 0;
}

B program

#include <stdio.h>
#include <unistd.h>
#include <sys/sem.h>
#include <sys/ipc.h>
#include <sys/types.h>

union semun
{
    int val;
    struct semid_ds *buf;
    unsigned short  *array;
    struct seminfo  *__buf;
};

int main()
{
    key_t key = ftok(".",123);
    if(key<0)
    {
        printf("creat semaphore's key fail\n");
        return -1;
    }
    
    int semid = semget(key,3,IPC_CREAT|0777);
    if(semid<0)
    {
        printf("creat semaphore fail\n");
        return -2;
    }
    
    struct sembuf mybuf[2];
    mybuf[0].sem_op = -1;
    mybuf[0].sem_num = 1;
    mybuf[0].sem_flg = 0;

    mybuf[1].sem_op = 1;
    mybuf[1].sem_num = 2;
    mybuf[1].sem_flg = 0;
    
    int i = 5;
    while(i)
    {
        semop(semid,&mybuf[0],1);
        sleep(1);
        printf("B\n");
        i--;
        semop(semid,&mybuf[1],1);
    }
    return 0;
}

C program

#include <stdio.h>
#include <unistd.h>
#include <sys/sem.h>
#include <sys/ipc.h>
#include <sys/types.h>

union semun
{
    int val;
    struct semid_ds *buf;
    unsigned short  *array;
    struct seminfo  *__buf;
};

int main()
{
    key_t key = ftok(".",123);
    if(key<0)
    {
        printf("creat semaphore's key fail\n");
        return -1;
    }
    
    int semid = semget(key,3,IPC_CREAT|0777);
    if(semid<0)
    {
        printf("creat semaphore fail\n");
        return -2;
    }
    
    struct sembuf mybuf[2];
    mybuf[0].sem_op = -1;
    mybuf[0].sem_num = 2;
    mybuf[0].sem_flg = 0;

    mybuf[1].sem_op = 1;
    mybuf[1].sem_num = 0;
    mybuf[1].sem_flg = 0;
    
    int i = 5;
    while(i)
    {
        semop(semid,&mybuf[0],1);
        sleep(1);
        printf("C\n");
        i--;
        semop(semid,&mybuf[1],1);
    }
    return 0;
}
Published 28 original articles · won praise 0 · Views 993

Guess you like

Origin blog.csdn.net/wfea_lff/article/details/104094251