-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmh.c
169 lines (146 loc) · 5.03 KB
/
mh.c
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
/*****************************************************************************************************************
*
* NAME : VINU CHARANYA ATHANGUDI PURUSHOTHAMAN
* PERSON # : 37648013
* PROGRAM 2 : mh.c
* HEADER FILE : osheaders.h
*
* DESCRIPTION : The program gets one input. The number of days the mother hubbard program should run
* Based on the number of days, the mother wakes up the child does various tasks and once she
* finishes with bathing a child,she wakes up the father to read a book and make the child sleep.
*
* EXECUTION : ./mh <Days>
*
* <Days> is the # of days to run the mother Hubbard function on.
*
*
*
* DATE : 29 Mar 2011
*
******************************************************************************************************************/
#include "osheaders.h"
int days;
int beg=1,day=1;
sem_t moth;
sem_t fath;
pthread_attr_t attr;
/*****************************************************************************************************************
*
* Function : init
*
Parameters : nil
*
* Description : This function initializes the various semaphores
*
******************************************************************************************************************/
void init()
{
sem_init(&moth,0,1);
sem_init(&fath,0,0);
pthread_attr_init(&attr);
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
}
/*****************************************************************************************************************
*
* Function : mother
*
Parameters : nil
*
* Description : This function performs various tasks on 12 children and when it comes to last task it will wake
* up father and goes back to sleep after finishing 12 children. It sleeps till it is woken up by father
* for the next day. It exits when the number of days are over
*
******************************************************************************************************************/
void *mother (void *args){
int i;
while(beg<=days)
{
printf("\n\nMother Woke up for day :\t%d\n\n",beg);
for(i =0; i < 12 ; i++)
{
printf(" Mother wakes the child #%d up\n",i+1);
}
for(i =0; i < 12 ; i++)
{
printf("Mother gives breakfast to child #%d \n",i+1);
}
for(i =0; i < 12 ; i++)
{
printf("Mother sends the child #%d to school\n",i+1);
}
for(i =0; i < 12 ; i++)
{
printf("Mother gives dinner to child #%d\n",i+1);
}
for(i =0; i < 12 ; i++)
{
printf("Mother gives bath to child #%d\n",i+1);
usleep(100);
sem_post(&fath);//Mother wakes the father up
}
beg++;
}
}
/*****************************************************************************************************************
*
* Function : father
*
Parameters : nil
*
* Description : This function performs various tasks on 12 children after it is woken up by mother and when he puts
* all the children to sleep he will wake the mother. The father waits on every child to be bathed by mother.
* he sleeps after wakin up the mother after he finishes it with all the 12 children.
*
******************************************************************************************************************/
void *father(void *args){
int i;
while(day<=days){
printf("\n\nFather has woken up for day :\t%d\n\n",day);
for(i =0; i < 12 ; i++)
{
sem_wait(&fath); //Father is waiting to be woken up
printf("Father reads the child #%d a book\n",i+1);
usleep(100);
}
for(i =0; i < 12 ; i++)
{
printf("Father makes the child #%d sleep\n",i+1);
}
printf("\n\nFather wakes the Mother and goes to sleep\n");
day++;
sem_post(&moth);
}
}
/*****************************************************************************************************************
*
* Function : Main
*
Parameters : <# of days>
*
*
* Description : The program gets one input. The number of days the mother hubbard program should run
* Based on the number of days, the mother wakes up the child does various tasks and once she
* finishes with bathing a child,she wakes up the father to read a book and make the child sleep.
* The threads are created and they are waited by join to perform the given tasks for the number of days
*
******************************************************************************************************************/
int main(int argc, char **argv) {
if (argc != 2)//checking for number of arguments
{
printf("\nHey argument must be 2!!");
printf("\nThe correct format is ./mh <# of Days>\n");
} else {
printf("\nYou have given correct number of Arguments\n");
days = atoi(argv[1]);
printf("\n Number of Days\t =\t%d\n\n", days);
init();
pthread_t m;
pthread_t f;
pthread_create(&m,&attr,mother,NULL);
pthread_create(&f,&attr,father,NULL);
pthread_join(m,NULL);
pthread_join(f,NULL);
exit(0);
}
}