1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
#include<errno.h>
#include<math.h>
#include<signal.h>
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#include<sys/time.h>
#define FALSE 0
#define TRUE 1
#define NPHIL 4
#define NCHOPSTICKS NPHIL
#define AVG_THINK 2.5
#define AVG_EAT 1.5
#define DURATION 30
typedef union semun{
int val;
struct semid_ds *buf;
ushort *array;
}SEM_UNION;
static int alarm_flag=FALSE;
/*------print error mesage and terminate a process------*/
void error(const char *msg)
{
fflush(stdout);
printf( "%s %d %s\n", msg ,errno,strerror(errno));
exit(0);
}
/*******signal the semaphore*******/
void my_signal(int s)
{
int status;
struct sembuf sb;
sb.sem_num=0;
sb.sem_op=1;
sb.sem_flg=SEM_UNDO;
status=semop(s,&sb,1);//s=semid
if(status<0)
{ error("In my wait,semaphore failed");}
}
/********wait on semaphore*******/
void my_wait(int s)
{
int status;
struct sembuf sb;
sb.sem_num=0;
sb.sem_op=-1;//wait1
sb.sem_flg=SEM_UNDO;
status=semop(s,&sb,1);
if(status<0)
{ error("In my wait,semaphore failed");}
}
/********think *******/
void think(int phil)
{
int duration=drand48()*1000000.0*AVG_THINK;
printf("phil %d (proc %d) thinking %d microseconds\n",phil,getpid(), duration);
usleep(duration);
}
void eat(int phil)
{
int duration=drand48()*1000000.0*AVG_EAT;
printf("phil %d (proc %d) eating %d microseconds\n",phil,getpid(), duration);
usleep(duration);
}
/*****timer*****/
void alarm_handler(sig,code,scp)
int sig;
int code;
struct sigcontext *scp;
{
alarm_flag=TRUE;
}
void set_sig_handler()
{
struct sigaction act;
act.sa_handler=alarm_handler;
#if defined_FreeBSD_
act.sa_mask=0;
#endif
act.sa_flags=SA_RESTART|SA_RESETHAND;
sigaction(SIGUSR1, &act,(struct sigaction *) NULL);
}
/******creat process*********/
int create_philososphers(const int n,int child[])
{
int i,status;
for (i=0;i<n;++i)
{
child[i]=0;
}
printf("creating %d philosophers\n",n);
for(i=0;i<n;++i)
{
status=fork();
if(status<0)
{
error("failed to creat philosopher \n");}
else if(status==0)
{return i-1;
}
else{ child[i-1]=status;}
}
return -1;
}
/**********Main program*********/
int main( int argc,char *argv[])
{
int child[NPHIL],
chopstick[NCHOPSTICKS],i,status, philosopher_id=0 , left,right;
SEM_UNION arg;
key_t k;
for(i=0;i<=NCHOPSTICKS;++i)
{
k=ftok(argv[0],i);
printf("\n",argv[0],k);
chopstick[i]=semget(k,1,IPC_CREAT);
printf("chopstick[%d] is %d\n",i,chopstick[i]);
if(chopstick[i]<0)
{error("bad sem creat");
}
arg.val=1;
status=semctl(chopstick[i],0,SETVAL,arg);
if(status<0)
{error("couldnt itinalize sem counter to 1");}
}
/*********creat semaphores******/
philosopher_id=create_philososphers(NPHIL,child);
if(philosopher_id==-1)
{
sleep(DURATION);
for(i=0;i<NPHIL;++i)
{
kill(child[i],SIGUSR1);
}
for(i=0;i<NPHIL;++i)
{
status=wait((int *)0);
if(status<0)
{
error("wait erorr detected");
}
}
for(i=0;i<NCHOPSTICKS;i++)
{
status=semctl(chopstick[i],0,IPC_RMID,0);
if(status<0)
{error("bad remove sem");}
}
}
else
{
left=philosopher_id;
right=(philosopher_id +1)%NPHIL;
set_sig_handler();
while(!alarm_flag)
{
think(philosopher_id);
if(philosopher_id & 0x1)
{
my_wait(chopstick[left]);
printf("phil %d picked up left chopstick %d\n",philosopher_id,getpid(),left);
my_wait(chopstick[right]);
printf("phil %d picked up right chopstick %d\n",philosopher_id,getpid(),right);
}
else
{
my_wait(chopstick[right]);
printf("phil %d picked up right chopstick %d\n",philosopher_id,getpid(),right);
my_wait(chopstick[left]);
printf("phil %d picked up left chopstick %d\n",philosopher_id,getpid(),left);
}
eat(philosopher_id);
if(philosopher_id & 0x1)
{
my_signal(chopstick[left]);
printf("phil %d put down left chopstick %d\n",philosopher_id,getpid(),left);
my_signal(chopstick[right]);
printf("phil %d put down right chopstick %d\n",philosopher_id,getpid(),right);
}
else
{
my_signal(chopstick[right]);
printf("phil %d put down right chopstick %d\n",philosopher_id,getpid(),right);
my_signal(chopstick[left]);
printf("phil %d put down left chopstick %d\n",philosopher_id,getpid(),left);
}
}
}
}
|