Minor lab4 changes

This commit is contained in:
Isaac Shoebottom 2023-10-25 11:27:26 -03:00
parent 25ac7bc195
commit bcd7364d14

View File

@ -6,6 +6,8 @@
* Question 6: You see the number 1 almost never. The number of loops at this point almost guarantees that the threads will be reading/writing at the same time, and the counter missing increments/decrements. * Question 6: You see the number 1 almost never. The number of loops at this point almost guarantees that the threads will be reading/writing at the same time, and the counter missing increments/decrements.
* *
* Question 8: The number 1 is the only thing displayed. This is because the mutex lock ensures that the threads will not be reading/writing at the same time, and the counter will not miss increments/decrements. When another thread tried to lock the mutex, it waits for it to first become unlocked, ensuring proper thread synchronization. * Question 8: The number 1 is the only thing displayed. This is because the mutex lock ensures that the threads will not be reading/writing at the same time, and the counter will not miss increments/decrements. When another thread tried to lock the mutex, it waits for it to first become unlocked, ensuring proper thread synchronization.
*
* Question 13:
*/ */
@ -29,6 +31,7 @@ void *inc() {
gv++; gv++;
pthread_mutex_unlock(&mutex); pthread_mutex_unlock(&mutex);
} }
return NULL;
} }
void *dec() { void *dec() {
@ -37,6 +40,7 @@ void *dec() {
gv--; gv--;
pthread_mutex_unlock(&mutex); pthread_mutex_unlock(&mutex);
} }
return NULL;
} }
void *minus() { void *minus() {
@ -55,7 +59,7 @@ void *plus(void *argg) {
for (i = 0; i < 10; i++) { for (i = 0; i < 10; i++) {
printf("+"); printf("+");
//usleep(interval); usleep(interval);
sem_post(&sem); sem_post(&sem);
} }
return NULL; return NULL;
@ -85,8 +89,8 @@ int main(int argc, char **argv) {
unsigned int *t = (unsigned int *) malloc(sizeof(unsigned int)); unsigned int *t = (unsigned int *) malloc(sizeof(unsigned int));
*t = rand(); *t = rand();
pthread_create(&(thread[i]), NULL, &plus, (void *) t); pthread_create(&thread[i], NULL, &plus, (void *) t);
pthread_create(&(thread[i + 1]), NULL, &minus, NULL); pthread_create(&thread[i + 1], NULL, &minus, NULL);
pthread_join(thread[i], NULL); pthread_join(thread[i], NULL);
pthread_join(thread[i + 1], NULL); pthread_join(thread[i + 1], NULL);