Arlon's CSUMB Intro to Operating Systems CST-334 Module 6 Learning Journal #6/#30 for the week Wed 07/28-Tues 08/03, year 2021

Arlon's CSUMB Intro to Operating Systems CST-334 Module 6 Learning Journal #6/#30 for the week Wed 07/28-Tues 08/03, year 2021

Journal spec: What to Write in your Journal Entry Write a 1 - 2 paragraph description of what you learned this fifth week in CST 334 Topic: semaphores.

I think I could sum it up really quick actually - aside from all the theory - which is great - I really want to make sure I know what to put in a C program. So I sum up the theory with "use semaphores - see examples from Lab 6" and I sum up the code by saying, pass copies of the data to the threads by making a struct and mallocing for a new struct, sending the struct in to the thread as a copy of the data. The producer has 2 waits and 2 posts in the order of empty - mutex - mutex - full and consumer is full - mutex - mutex - empty and more specifically those look like

sem_wait(&mutex);

In between the waits and posts are puts and gets depending if it's the producer (put) or the consumer (get). from the Operating Systems, three easy peices Arpaci-Dusseau book, pg. 376, the producer and consumer functions (or presumably any thread function) looks like

void *consumer(void *arg){...}

You pass it a void pointer, and you can make the void pointer by defining a struct somewhere, something like:

typedef struct p_c_data_passer{  //  passing data through a struct to the threads is methodology learned from Lab 5
  int i ,j, k;  //  put what ever you want, int's chars, probably other structs I think.
  char c;  //  a character
}p_c_data_passer;    // a structure type called p_c_data_passer
and then instantiating instances of it with something like:
  //... thread coming up, need to copy data:
  p_c_data_passer * a_void_pointer;  //    pointer to a struct to pass a character in to the thread
  a_void_pointer=malloc(sizeof(struct p_c_data_passer));  // this is how you malloc for a new struct
  a_void_pointer->j=an_integer;// etc - I learned this from lab 5 - copy the data to a struct to send in to the thread
and then the thread function can be called with the thread with something like:
pthread_create(&threadList[counter], NULL, threadFunction, (void *)a_void_pointer_variable);

On the topic of preventing no preemption:
I am not sure if I understand this correctly because I would say to avoid not having preemption, allow preemption. But in lecture, as well as in the book, it is stated that with regards to avoiding no preemption that if you can't get all locks, to relinquish all locks, otherwise locks could be affecting other locks. The book says something similar - multiple locks cause trouble, top of page 393. Preemption is when the processor can interrupt processes if they go too long. No preemption allowed is one of the four conditions that would be required for deadlock to occur (pg 391, OS book) In other words, if preemption is not allowed that means the scheduler is not allowed to interrupt processes and that could be a contributing factor that could allow deadlock to occur. Preemption allowed alleviates deadlock possibility because then the scheduler can interrupt processes periodically. Also somehow related, according to lecture and also mentioned in the book under avoiding no preemption on page 393 is pthread_mutex_trylock() for only getting available locks - reversing the order for alternate access in a different thread - but which causes the possibility of livelock - but which can be dodged by staggering the execution a microsecond or two with random delays. Reversing the order reminds me of the standard dining philosophers solution.

According to our book, this free semaphore (pdf) book Little Book Of Semaphores https://greenteapress.com/semaphores/LittleBookOfSemaphores.pdf has semaphore based puzzles in it, which could possibly be helpful for learning more in-depth about practical semaphore usage, although I haven't tried any yet, alluding to thinking of them as puzzles sounds interesting.

Atomicity: Order can't matter, to eliminate atomicity bugs? The terminology all seems to make it more confusing than the reality of how it all really works I think - I hope.

I think Lab 6 was a good semaphore example which I will save and study.

Comments

Popular posts from this blog

Module 2 Learning Journal 1-19-21

Arlon's CSUMB ProSeminar CST300 Module 4 Learning Journal for the week Wed 1/27-Tues 2/2, year 2021

Arlon's CSUMB ProSeminar CST300 Module 8 Learning Journal for the week Wed 02/24-Saturday 02/27, year 2021 - Journal 8