最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網 會員登陸 & 注冊

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); // 創(chuàng)建信號量
?? ?if (semId < 0)
?? ?{
?? ??? ?perror("semget error");
?? ??? ?return -1;
?? ?}
?? ?semId = semget(key, 2, IPC_CREAT | 0755); // 創(chuàng)建信號量
?? ?if (semId < 0)
?? ?{
?? ??? ?perror("semget error");
?? ??? ?return -1;
?? ?}
?? ?shmId = shmget(key, 128, IPC_CREAT | 0755); // 創(chuàng)建共享內存
?? ?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的評論 (共 條)

分享到微博請遵守國家法律
阿克苏市| 盐山县| 广丰县| 腾冲县| 乌苏市| 岫岩| 长武县| 玛纳斯县| 泰来县| 兰考县| 拜城县| 称多县| 云龙县| 兴仁县| 潼南县| 乐至县| 宜章县| 拉萨市| 化州市| 阳新县| 长兴县| 临西县| 天峻县| 田东县| 南召县| 定襄县| 杂多县| 辉县市| 苏尼特右旗| 长岛县| 获嘉县| 贡嘎县| 遂昌县| 延安市| 弋阳县| 南投市| 吉木萨尔县| 宁陵县| 观塘区| 启东市| 武宣县|