- 牛云
-
/*
* File: PV.h
* Author: Caspar Zhang
*/
#ifndef _PV_H
#define _PV_H
#define SEM_KEY 0x12345678
#define ERR_AND_EXIT(arg) do {
perror(arg);
exit(1);
} while(0)
int P(int semid, int sem_index);
int V(int semid, int sem_index);
#endif /* _PV_H */
/*
* File: PV.c
* Author: Caspar Zhang
*/
#include "PV.h"
#include <stdio.h>
#include <unistd.h>
#include <sys/sem.h>
int P(int semid, int sem_index)
{
struct sembuf buf;
buf.sem_num = sem_index;
buf.sem_op = -1;
buf.sem_flg = SEM_UNDO;
if (semop(semid, &buf, 1) == -1)
return -1;
return 0;
}
int V(int semid, int sem_index)
{
struct sembuf buf;
buf.sem_num = sem_index;
buf.sem_op = 1;
buf.sem_flg = SEM_UNDO;
if (semop(semid, &buf, 1) == -1)
return -1;
return 0;
}
/*
* File: Agent.c
* Author: Caspar Zhang
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/sem.h>
#include "PV.h"
int main(int argc, char *argv[])
{
/*
* 0 - Paper and matches
* 1 - Matches and tobacco
* 2 - Tobacco and paper
*/
char *ingredient[3] = {
"Paper and matches",
"Matches and tobacco",
"Tobacco and paper"};
int ingred_type;
int semid, i;
srand((unsigned)time(NULL));
/* Puts out the Agent"s information */
fprintf(stdout, "I"m the Agent.n" );
/*
* Create a new semaphore set which has 4 semaphores in it.
* The first 3 semaphores are the Smokers" state,
* the last one is the Agent"s state
*/
if ((semid = semget(SEM_KEY, 4, IPC_CREAT|0660)) < 0)
ERR_AND_EXIT("semget");
for (i = 0; i < 4; ++i)
if (semctl(semid, i, SETVAL, 0) < 0)
ERR_AND_EXIT("semctl");
/*
* The procedures are shown in below:
* 1. Offer Ingredients
* 2. V(Smoker_i)
* 3. P(Agent)
* 4. Go to 1 and repeat
*/
while (1)
{
/* Offer the ingredients randomly*/
ingred_type = rand() % 3;
fprintf(stdout, "Agent%d: I offered %s, waiting for the smoker.n",
ingred_type, ingredient[ingred_type]);
sleep(5);
/* Wake up the specified smoker */
if (V(semid, ingred_type) < 0) ERR_AND_EXIT("V failed");
/* Wait for smoker to roll and smoke */
if (P(semid, 3) < 0) ERR_AND_EXIT("P failed");
}
return 0;
}
/*
* File: Smoker.c
* Author: Caspar Zhang
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/sem.h>
#include "PV.h"
int main(int argc, char *argv[])
{
/*
* 0 - Tobacco Smoker
* 1 - Paper Smoker
* 2 - Matches Smoker
*/
char *ingredient[3] = {"Tobacco", "Paper", "Matches"};
int smoker = argv[1][0] - "0";
int semid;
/* Puts out the Smoker"s information */
fprintf(stdout, "I"m a smoker. I have %sn", ingredient[smoker]);
/* Get the existed semaphore set */
/* TODO How to create semaphores if smoker process executed first? */
if ((semid = semget(SEM_KEY, 0, 0)) < 0)
ERR_AND_EXIT("semget");
if (semctl(semid, smoker, GETVAL, 0) < 0)
ERR_AND_EXIT("semctl");
/*
* The procedures are shown in below:
* 1. P(Smoker_i)
* 2. Get Ingredients, Roll and Smoke
* 3. V(Agent)
* 4. Go to 1 and repeat
*/
while (1)
{
/* Wait for the Agent*/
fprintf(stdout, "%s: I"m waiting for the agent.n", ingredient[smoker]);
sleep(5);
if (P(semid, smoker) < 0) ERR_AND_EXIT("P failed");
/* Roll and smoke */
fprintf(stdout, "%s: I get the ingredients, I"m rolling and smoking now.n",
ingredient[smoker]);
sleep(5);
/* Wake up Agent */
if (V(semid, 3) < 0) ERR_AND_EXIT("V failed");
}
return 0;
}
嘿嘿,你的分数能不能再高点呢
- LuckySXyd
-
vnsf
- meira
-
哇,好久没有做过了,但是你可以看看书上有个例子和这个挺像的。