欢迎光临散文网 会员登陆 & 注册

QY

2023-03-10 21:56 作者:神隐的千寻バ  | 我要投稿

//two
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <unistd.h>
 #include"P.h"
 #include"P2.h"
#define SEM_READ 0
#define SEM_WRITE 1

union semun
{
    int val;
};
 
void P_operation(int index, int semId);

 
void V_operation(int index, int semId);

 
 

void P2_operation(int index, int semId);

 
void V2_operation(int index, int semId);

 
int main()
{
    key_t key;
    key = ftok("b.c", 123); // 获取 key 值
    pid_t pid;
    int semId; // 信号灯 ID
    int shmId; // 共享内存 ID
    int semId2; // 信号灯 ID2

 
    char *shamaddr;
 
     semId2 = semget(key, 2, IPC_CREAT | 0755); // 创建信号量
    if (semId < 0)
    {
        perror("semget error");
        return -1;
    }
    semId = semget(key, 2, IPC_CREAT | 0755); // 创建信号量
    if (semId < 0)
    {
        perror("semget error");
        return -1;
    }
    shmId = shmget(key, 128, IPC_CREAT | 0755); // 创建共享内存
    if (shmId < 0)
    {
        perror("shmget error");
        return -1;
    }
 
    // Init semaphore
    union semun myun;
    // Init semaphore read
    myun.val = 0;
    semctl(semId, SEM_READ, SETVAL, myun); // 对 SEM_READ 信号量设置初始值
    // Init semaphore write
    myun.val = 1;
    semctl(semId, SEM_WRITE, SETVAL, myun); // 对 SEM_WRITE 信号量设置初始值
 
    pid = fork();
 
    // child process
    if (pid == 0)
    {
        while (1)
        {
            shamaddr = (char *)shmat(shmId, NULL, 0);
            //printf("Na Hua Yan Dan Ji Xu Kan Bing %s\n", shamaddr); // 操作对共享资源    
            //printf("Hua Yan Wan\n");
            //printf("Qing Xian Hua Yan\n");
            //    printf("Kan Wan Le Xia Yi Wei\n");
            //V2_operation(SEM_WRITE, semId2);        // 共享内存映射
            P_operation(SEM_READ, semId);                    // P 操作
            //printf("XIAN HUA YAN: %s\n", shamaddr); // 操作对共享资源
            printf("Hua Yan Wan\n");
        V2_operation(SEM_WRITE, semId2);
        printf("Na Hua Yan Dan Ji Xu Kan Bing\n");     
                        // V 操作
                        printf("Kan Wan Le Xia Yi Wei %s\n", shamaddr);
                    
        }
    }
 
    // parent process
    else if (pid > 0)
    {
        while (1)
        {
            shamaddr = (char *)shmat(shmId, NULL, 0);
            
            V_operation(SEM_READ, semId);
            fgets(shamaddr, 32, stdin);
            printf("Qing Xian Hua Yan\n");
            //P_operation(SEM_WRITE, semId);
            //printf("HUA YAN le,kan YI SHENG\n");
            //fgets(shamaddr, 32, stdin);
            
                P2_operation(SEM_WRITE, semId2);
                    
        }
    }
 
    return 0;
}






//ww
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <unistd.h>
void P_operation(int index, int semId)
{
    struct sembuf sop;
    sop.sem_num = index; //信号灯编号
    sop.sem_op = -1;     // P 操作
    sop.sem_flg = 0;     // 阻塞
    semop(semId, &sop, 1);
}
 
void V_operation(int index, int semId)
{
    struct sembuf sop;
    sop.sem_num = index; //信号灯编号
    sop.sem_op = 1;         // V 操作
    sop.sem_flg = 0;     // 阻塞
    semop(semId, &sop, 1);
}
 




//WW:
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <unistd.h>
void P2_operation(int index, int semId)
{
    struct sembuf sop2;
    sop2.sem_num = index; //信号灯编号
    sop2.sem_op = -1;     // P 操作
    sop2.sem_flg = 0;     // 阻塞
    semop(semId, &sop2, 1);
}
 
void V2_operation(int index, int semId)
{
    struct sembuf sop2;
    sop2.sem_num = index; //信号灯编号
    sop2.sem_op = 1;         // V 操作
    sop2.sem_flg = 0;     // 阻塞
    semop(semId, &sop2, 1);
}

QY的评论 (共 条)

分享到微博请遵守国家法律