Professional Documents
Culture Documents
Fatima Qaiser OS LAB 8
Fatima Qaiser OS LAB 8
Activities:
1. Develop a solution program for a Dining-Philosopher problem.
1. //dining_philosophers.c
2. #include <pthread.h>
3. #include <semaphore.h>
4. #include <stdio.h>
5.
6. #define N 5
7. #define THINKING 2
8. #define HUNGRY 1
9. #define EATING 0
10. #define LEFT (phnum + 4) % N
11. #define RIGHT (phnum + 1) % N
12.
13. int state[N];
14. int phil[N] = { 0, 1, 2, 3, 4 };
15.
16. sem_t mutex;
17. sem_t S[N];
18.
19. void test(int phnum)
20. {
21. if (state[phnum] == HUNGRY
22. && state[LEFT] != EATING
23. && state[RIGHT] != EATING) {
24. // state that eating
25. state[phnum] = EATING;
26.
27. sleep(2);
28.
29. printf("Philosopher %d takes fork %d and %d\n",
30. phnum + 1, LEFT + 1, phnum + 1);
31.
32. printf("Philosopher %d is Eating\n", phnum + 1);
33.
34. // sem_post(&S[phnum]) has no effect
35. // during takefork
36. // used to wake up hungry philosophers
37. // during putfork
38. sem_post(&S[phnum]);
39. }
40. }
41.
42. // take up chopsticks
43. void take_fork(int phnum)
44. {
45.
46. sem_wait(&mutex);
47.
48. // state that hungry
49. state[phnum] = HUNGRY;
50.
51. printf("Philosopher %d is Hungry\n", phnum + 1);
OS LAB 8 4th Semester
52.
53. // eat if neighbours are not eating
54. test(phnum);
55.
56. sem_post(&mutex);
57.
58. // if unable to eat wait to be signalled
59. sem_wait(&S[phnum]);
60.
61. sleep(1);
62. }
63.
64. // put down chopsticks
65. void put_fork(int phnum)
66. {
67.
68. sem_wait(&mutex);
69.
70. // state that thinking
71. state[phnum] = THINKING;
72.
73. printf("Philosopher %d putting fork %d and %d down\n",
74. phnum + 1, LEFT + 1, phnum + 1);
75. printf("Philosopher %d is thinking\n", phnum + 1);
76.
77. test(LEFT);
78. test(RIGHT);
79.
80. sem_post(&mutex);
81. }
82.
83. void* philosopher(void* num)
84. {
85.
86. while (1) {
87.
88. int* i = num;
89.
90. sleep(1);
91.
92. take_fork(*i);
93.
94. sleep(0);
95.
96. put_fork(*i);
97. }
98. }
99.
100. int main()
101. {
102.
103. int i;
104. pthread_t thread_id[N];
105.
106. // initialize the semaphores
107. sem_init(&mutex, 0, 1);
108.
109. for (i = 0; i < N; i++)
110.
OS LAB 8 4th Semester
52.
53. /* Lock mutex "srvCust" - protect service by the same barber from other threads */
54. pthread_mutex_lock(&srvCust);
55.
56. /* Serve customer */
57. serve_customer();
58.
59. /* Unlock mutex "srvCust" - finished service */
60. pthread_mutex_unlock(&srvCust);
61.
62. printf("Customer was served.\n");
63.
64. counter++;
65. if (counter == (total_custs - no_served_custs))
66. break;
67. }
68. pthread_exit(NULL);
69. }
70.
71. void *customer_function(void *idp)
72. {
73. struct timeval start, stop;
74.
75. /* Lock semaphore "modifySeats" */
76. sem_wait(&modifySeats);
77.
78. /* If there is available seat */
79. if (available_seats >= 1)
80. {
81. /* Occupy a seat */
82. available_seats--;
83.
84. printf("Customer[pid = %lu] is waiting.\n", pthread_self());
85. printf("Available seats: %d\n", available_seats);
86.
87. /* Start waiting-time counter */
88. gettimeofday(&start, NULL);
89.
90. /* Unlock semaphore "customer_ready" - set the customer ready to be served */
91. sem_post(&customer_ready);
92.
93. /* Unlock semaphore "modifySeats" */
94. sem_post(&modifySeats);
95.
96. /* Lock semaphore "barber_ready" - wait for barber to get ready */
97. sem_wait(&barber_ready);
98.
99. /* Stop waiting-time counter */
100. gettimeofday(&stop, NULL);
101.
102. double sec = (double)(stop.tv_usec - start.tv_usec) / 1000000 + (double)(stop.tv_sec -
start.tv_sec);
103.
104. /* Assign the time spent to global variable (ms) */
105. waiting_time_sum += 1000 * sec;
106. printf("Customer[pid = %lu] is being served. \n", pthread_self());
107. }
108. else
109. {
OS LAB 8 4th Semester
169.
170. printf("Please enter the number of seats: \n");
171. scanf("%d", &chair_cnt);
172.
173. printf("Please enter the total customers: \n");
174. scanf("%d", &total_custs);
175.
176. available_seats = chair_cnt;
177.
178. /* Create barber thread */
179. tmp = pthread_create(&barber_1, NULL, (void *)barber_function, NULL);
180.
181. if (tmp)
182. printf("Failed to create thread.");
183.
184. /* Create customer_maker thread */
185. tmp = pthread_create(&customer_maker, NULL, (void *)make_customer_function, NULL);
186.
187. if (tmp)
188. printf("Failed to create thread.");
189.
190. /* Wait for threads to finish */
191. pthread_join(barber_1, NULL);
192. pthread_join(customer_maker, NULL);
193.
194. printf("\n------------------------------------------------\n");
195. printf("Average customers' waiting time: %f ms.\n", (waiting_time_sum / (double) (total_custs -
no_served_custs)));
196. printf("Number of customers that were forced to leave: %d\n", no_served_custs);
197. }
198.
OS LAB 8 4th Semester
1. //reader_writer.c
2. #include <stdio.h>
3. #include <stdlib.h>
4. #include <pthread.h>
5.
6. #define MAX_READERS 16 //lasttwo didts of rollno
7.
8. pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
9. pthread_cond_t read_phase = PTHREAD_COND_INITIALIZER;
10. pthread_cond_t write_phase = PTHREAD_COND_INITIALIZER;
11.
12. int readers_count = 0;
13.
14. void *reader(void *arg) {
15. int id = *(int*)arg;
16. FILE *file = fopen("example.txt", "r");
17.
18. if (file == NULL) {
19. perror("Failed to open file");
20. exit(EXIT_FAILURE);
21. }
22.
23. // Reader enters critical section
24. pthread_mutex_lock(&mutex);
25. while (readers_count == MAX_READERS) {
26. pthread_cond_wait(&read_phase, &mutex);
27. }
28. readers_count++;
29. pthread_mutex_unlock(&mutex);
30.
31. // Reading the file
32. printf("Reader %d is reading...\n", id);
33. char buffer[1024]; // Buffer to hold the read content
34. while (fgets(buffer, sizeof(buffer), file) != NULL) {
35. printf("Reader %d read: %s", id, buffer);
36. }
37.
38. // Exiting critical section
39. pthread_mutex_lock(&mutex);
40. readers_count--;
41. if (readers_count == 0) {
42. pthread_cond_signal(&write_phase);
43. }
44. pthread_mutex_unlock(&mutex);
45.
46. fclose(file);
47. pthread_exit(NULL);
48. }
49.
50.
51. void *writer(void *arg) {
52. FILE *file = fopen("example.txt", "a");
53.
54. if (file == NULL) {
55. perror("Failed to open file");
56. exit(EXIT_FAILURE);
57. }
58.
OS LAB 8 4th Semester