/***************************************************************************************************
* File Name: main.c
* Description: 绗?绔犲疄渚嬭缁?
* Reference book:銆奓inux搴旂敤涓庡紑鍙戝吀鍨嬪疄渚嬬簿璁层€嬮偙閾侊紝浜庣帀榫欙紝寰愬瓙宸?缂栬憲锛屾竻鍗庡ぇ瀛﹀嚭鐗堢ぞ.2010.5
* E_mail: [email protected]
*
****************************************************************************************************/
/***************************************************************************************************
* File Name: mysemop.c
* Description: 绗?绔犲疄渚嬭缁?
* Reference book:銆奓inux搴旂敤涓庡紑鍙戝吀鍨嬪疄渚嬬簿璁层€嬮偙閾侊紝浜庣帀榫欙紝寰愬瓙宸?缂栬憲锛屾竻鍗庡ぇ瀛﹀嚭鐗堢ぞ.2010.5
* E_mail: [email protected]
*
****************************************************************************************************/
/***************************************************************************************************
* File Name: mysemop.h
* Description: 绗?绔犲疄渚嬭缁?
* Reference book:銆奓inux搴旂敤涓庡紑鍙戝吀鍨嬪疄渚嬬簿璁层€嬮偙閾侊紝浜庣帀榫欙紝寰愬瓙宸?缂栬憲锛屾竻鍗庡ぇ瀛﹀嚭鐗堢ぞ.2010.5
* E_mail: [email protected]
*
****************************************************************************************************/
#ifndef MYSEMOP_H
#define MYSEMOP_H
#include
#include
#include
#include
#include
#include
#include
#include
int CreateSem(int value);
int SetSemValue(int sem_id, int value);
void DeleteSem(int sem_id);
int Psem(int sem_id);
int Vsem(int sem_id);
#endif
#include "mysemop.h"
int CreateSem(int value)
{
int sem_id;
sem_id = semget(IPC_PRIVATE, 1, 0666 | IPC_CREAT);
if (sem_id == -1) return -1;
if (SetSemValue(sem_id, value) == 0) return -1;
return sem_id;
}
int SetSemValue(int sem_id, int value)
{
if(semctl(sem_id, 0, SETVAL, value) == -1) return 0;
return 1;
}
void DeleteSem(int sem_id)
{
if(semctl(sem_id, 0, IPC_RMID) == -1)
fprintf(stderr, "Failed to delete semaphore\n");
}
int Psem(int sem_id)
{
struct sembuf sem_b;
sem_b.sem_num = 0;
sem_b.sem_op = -1;
sem_b.sem_flg = SEM_UNDO;
if(semop(sem_id,&sem_b,1) == -1){
fprintf(stderr, "P failed\n");
return 0;
}
return 1;
}
int Vsem(int sem_id)
{
struct sembuf sem_b;
sem_b.sem_num = 0;
sem_b.sem_op = 1;
sem_b.sem_flg = SEM_UNDO;
if(semop(sem_id,&sem_b,1) == -1){
fprintf(stderr, "V failed\n");
return 0;
}
return 1;
}
#include "mysemop.h"
#include
#include
#include
int root_sem_id = 0;
int chopsticks_sem_id[5];
int main(int argc, char argv[])
{
pid_t pid;
pid_t chldpid[5];
char ch;
int i = 0;
int room_sem_id = CreateSem(4);
for (i = 0; i < 5; i++) {
chopsticks_sem_id[i] = CreateSem(1);
}
for (i = 0; i < 5; i++) {
pid = fork();
if (pid < 0) {
fprintf(stderr,"When fork philosopher %d Failed!\n", i);
exit(-1);
}
if (pid == 0) {
while (1) {
printf("Philopher %d is thinking,No disterb!\n", i);
sleep(1);
Psem(room_sem_id);
printf("Philopher %d is hungry,so he entered the room.\n", i);
Psem(chopsticks_sem_id[i]);
printf("Philopher %d pick up left chopstick\n", i);
Psem(chopsticks_sem_id[(i + 1) % 5]);
printf("Philopher %d pick up right chopstick\n", i);
printf("Philopher %d begins to eat!\n", i);
sleep(5 - i);
printf("Philopher %d ends to eat!\n", i);
Vsem(chopsticks_sem_id[(i + 1) % 5]);
printf("Philopher %d put down right chopstick\n", i);
Vsem(chopsticks_sem_id[i]);
printf("Philopher %d put down left chopstick\n", i);
Vsem(room_sem_id);
printf("Philopher %d left the room!\n",i);
printf("Philopher %d begins thinking!\n",i);
sleep(1);
}
} else
{
chldpid[i] = pid;
}
}
do
{
ch = getchar();
if (ch == 'q')
{
for (i = 0; i < 5; i++)
kill(chldpid[i], SIGTERM);
}
} while (ch != 'q');
}
/***************************************************************************************************
* File Name: mysemop.h
* Description: 绗?绔犲疄渚嬭缁?
* Reference book:銆奓inux搴旂敤涓庡紑鍙戝吀鍨嬪疄渚嬬簿璁层€嬮偙閾侊紝浜庣帀榫欙紝寰愬瓙宸?缂栬憲锛屾竻鍗庡ぇ瀛﹀嚭鐗堢ぞ.2010.5
* E_mail: [email protected]
*
****************************************************************************************************/
#ifndef MYSEMOP_H
#define MYSEMOP_H
#include
#include
#include
#include
#include
#include
#include
#include
int CreateSem(int value);
int SetSemValue(int sem_id, int value);
void DeleteSem(int sem_id);
int Psem(int sem_id);
int Vsem(int sem_id);
#endif
/***************************************************************************************************
* File Name: mysemop.c
* Description: 绗?绔犲疄渚嬭缁?
* Reference book:銆奓inux搴旂敤涓庡紑鍙戝吀鍨嬪疄渚嬬簿璁层€嬮偙閾侊紝浜庣帀榫欙紝寰愬瓙宸?缂栬憲锛屾竻鍗庡ぇ瀛﹀嚭鐗堢ぞ.2010.5
* E_mail: [email protected]
*
****************************************************************************************************/
#include "mysemop.h"
int CreateSem(int value)
{
int sem_id;
sem_id = semget(IPC_PRIVATE, 1, 0666 | IPC_CREAT);
if (sem_id == -1) return -1;
if (SetSemValue(sem_id, value) == 0) return -1;
return sem_id;
}
int SetSemValue(int sem_id, int value)
{
if(semctl(sem_id, 0, SETVAL, value) == -1) return 0;
return 1;
}
void DeleteSem(int sem_id)
{
if(semctl(sem_id, 0, IPC_RMID) == -1)
fprintf(stderr, "Failed to delete semaphore\n");
}
int Psem(int sem_id)
{
struct sembuf sem_b;
sem_b.sem_num = 0;
sem_b.sem_op = -1;
sem_b.sem_flg = SEM_UNDO;
if(semop(sem_id,&sem_b,1) == -1){
fprintf(stderr, "P failed\n");
return 0;
}
return 1;
}
int Vsem(int sem_id)
{
struct sembuf sem_b;
sem_b.sem_num = 0;
sem_b.sem_op = 1;
sem_b.sem_flg = SEM_UNDO;
if(semop(sem_id,&sem_b,1) == -1){
fprintf(stderr, "V failed\n");
return 0;
}
return 1;
}