Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ============================================================================
- Name : lab4b.c
- Author :
- Version :
- Copyright : Your copyright notice
- Description : Hello World in C, Ansi-style
- ============================================================================
- */
- #include <stdio.h> // needed for printf, sprintf, ...
- #include <stdlib.h> // needed for exit()
- #include <unistd.h> // needed for sleep, fork, waitpid, _exit, ftruncate
- #include <sys/wait.h> // needed for waitpid
- #include <fcntl.h> // For O_* constants
- #include <sys/mman.h> // needed for mmap, munmap and associated constants
- #include <semaphore.h>
- void philosopher(int i);
- int parent(void);
- void take_forks(int i);
- void put_forks(int i);
- void test (int i);
- void think(int i);
- void eat(int i);
- #define N 5 /* number of philosophers */
- #define THINKING 0 /* philosopher is thinking */
- #define HUNGRY 1 /* philosopher is trying to get for ks */
- #define EATING 2 /* philosopher is eating */
- #define FINISH 3
- #define LEFT (arga+4)%N
- #define RIGHT (arga+1)%N
- struct shared_memory {
- int k;
- int state[N];
- sem_t s[N];
- sem_t mutex;
- };
- struct shared_memory *shared;
- int fd_shm, fd_log;
- int main(void) {
- if ((fd_shm = shm_open ("/test", O_RDWR | O_CREAT, 0660)) == -1)
- printf("error");
- if (ftruncate (fd_shm, sizeof (struct shared_memory)) == -1)
- printf("error");
- if ((shared = mmap (NULL, sizeof (struct shared_memory), PROT_READ | PROT_WRITE, MAP_SHARED,fd_shm, 0)) == MAP_FAILED)
- printf("error");
- // Initialize the shared memory
- shared -> k = 0;
- for (int i=0; i<N;i++){
- shared -> state[i] = 0;
- sem_init(&shared->s[i],0,1);
- }
- sem_init(&shared->mutex,1,1);
- pid_t pids[N];
- // At the beginning every philosopher is thinking
- for (int l = 0; l < N; l++){
- shared ->state[l]=THINKING;
- }
- printf("Start");
- for(int j=0; j< N; j++){
- if((pids[j] = fork()) < 0){
- printf("failed");
- perror("fork");
- }else if (pids[j] == 0 ){
- int pid = getpid();
- printf("pids[%d] = %d\n",j,pid);
- philosopher(j);
- exit(0);
- }
- }
- parent();
- for (int m=0; m<N;m++){
- waitpid(pids[m],NULL,0);
- }
- printf("This is final k %d\n",shared->k);
- if (munmap(shared, sizeof(struct shared_memory)) == -1) {
- printf("Function munmap() failed\n");
- exit(-1);
- }
- if ((shm_unlink("/test")) == -1){
- printf("Function shm_unlink() failed\n");
- exit(-1);
- }
- return EXIT_SUCCESS;
- }
- void philosopher(int arga) {
- //int count =0;
- //while(count<5){
- while(1){
- //sleep(1);
- //think(i);
- take_forks(arga);
- sleep(2);
- //eat(i);
- //sleep(1);
- put_forks(arga);
- //count++;
- }
- shared->state[arga] = FINISH;
- }
- void take_forks(int arga) /* i: philosopher number, from 0 to Nā1 */
- {
- //printf("arga take_fork: %d\n", arga);
- sem_wait(&shared->mutex); /* enter critical region, down mutex */
- shared->state[arga] = HUNGRY; /* record fact that philosopher i is hungry */
- //while (shared->state[i] == HUNGRY){
- test(arga); /* try to acquire 2 forks */
- sem_post(&shared->mutex); /* exit critical region, up mutex */
- //sem_wait(&shared->s[arga]); /* block if for ks were not acquired, down philosopher*/
- //}
- //sleep(1);
- }
- void put_forks(int arga) /* i: philosopher number, from 0 to Nā1 */
- {
- //printf("arga put_fork: %d\n", arga);
- sem_wait(&shared->mutex); /* enter critical region, down mutes */
- //test(LEFT); /* see if left neighbor can now eat */
- //test(RIGHT); /* see if right neighbor can now eat */
- shared->state[arga] = THINKING; /* philosopher has finished eating */
- sem_post(&shared->mutex); /* exit critical region, up mutex */
- }
- void test(int arga) /* i: philosopher number, from 0 to Nā1 */
- {
- sem_wait(&shared->s[LEFT]);
- sem_wait(&shared->s[RIGHT]);
- shared->state[arga]= EATING;
- sleep(1);
- sem_post(&shared->s[RIGHT]);
- sem_post(&shared->s[LEFT]);
- //if (shared->state[arga] == HUNGRY && shared->state[LEFT] != EATING && shared->state[RIGHT] != EATING)
- //{
- //printf("Philosopher: %d (i+N-1)%N - %d", i, ((i+N-1)%N));
- //shared->state[arga] = EATING;
- //sleep(2);
- //sem_post(&shared->s[arga]); //Up philosopher
- //}
- }
- void think(int arga){
- //printf("I am philosopher %d i am thinking\n", i);
- sleep(2);
- }
- void eat(int arga){
- //printf("I am philosopher %d i am eating\n", i);
- sleep(3);
- }
- int parent(void){
- puts("This is the Parent Process");
- int philosopherState;
- char* philprint;
- int count=0;
- int c;
- while (1){
- sleep(1);
- //sem_wait(&shared->mutex);
- for(int o = 0; o<N;o++ ){
- /*philosopherState = shared->state[o];
- if (philosopherState == 0) {
- philprint = "Thinking";
- }else if(philosopherState == 1){
- philprint = "Eating";
- }else if(philosopherState == 2){
- philprint = "Hungry";
- }else if(philosopherState == 3){
- philprint = "Gone";
- count++;
- }*/
- sem_getvalue(&shared->s[o], &c);
- //printf("P%d %s \t",o, philprint);
- if (c==0){
- printf("P%d Thinking\t",o);
- }
- else if(c==1){
- printf("P%d Eating\t",o);
- }
- else if (c==2){
- printf("P%d Hungry\t",o);
- }
- else if (c==3){
- printf("P%d Gone\t",o);
- }
- if (count >4){
- break;
- }
- }
- //sem_post(&shared->mutex);
- printf("\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement