Assignment 4 008

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

OS LAB ASSIGNMENT 4

SYED ALI MUZAHIR


FA21-BSE-008
Explain basics of pthread library?
The pthread library, short for POSIX threads, is a library in C and C++ programming languages that
provides a standardized interface for creating and managing threads. Threads are lightweight processes
that execute independently within a program.

Explain Following code snipet?(pthread_join( thread1, NULL);


The code pthread_join(thread1, NULL); waits for the thread with identifier thread1 to finish its
execution before allowing the program to proceed. The second argument (NULL) indicates that the exit
status of the thread is not needed. This is a common synchronization mechanism in multi-threaded
programs.

Explain following function, How it works?


pthread_create (&thread_a, NULL, (void *) &handler, (void *) &i[0])
The pthread_create function creates a new thread. In this case:

• &thread_a: Pointer to store the new thread's identifier.


• NULL: Default thread attributes.
• (void *) &handler : Function the new thread will execute.
• (void *) &i[0]: Data passed to the thread's function ( handler ).

Explain following function, How it works?


sem_init(&mutex, 0, 1);

The sem_init function initializes a semaphore. In this case:

• &mutex: Pointer to the semaphore variable.


• 0: Indicates the semaphore is shared between threads in the same process.
• 1: Initial value of the semaphore (in this case, 1). It's often used for simple binary
(mutex) semaphores.

So, sem_init(&mutex, 0, 1); initializes a semaphore named mutex with an initial value of 1,
making it suitable for basic mutual exclusion (mutex) purposes in multithreaded
programs.

Explain following function,

How it works? sem_wait(&mutex)


The sem_wait(&mutex) function decrements the value of the semaphore pointed to by &mutex. If the
semaphore's value becomes negative, the calling thread is blocked until the semaphore becomes non-
negative. It's commonly used for synchronization, such as implementing a critical section or protecting
shared resources in a multithreaded program.

sem_post(&mutex);
The sem_post(&mutex) function increments the value of the semaphore pointed to by &mutex. If
any threads were blocked waiting for this semaphore, one of them is allowed to proceed. It's
often used to signal the availability of a resource or to release a lock in a multithreaded program.
Explain what following code snippet does?
sem_t mutex,writelock
The code snippet declares two semaphores: `mutex` and `writelock`. Semaphores are synchronization
mechanisms in multithreaded programs. `mutex` is likely used for basic mutual exclusion, while
`writelock` is potentially employed to control write access to a shared resource, allowing only one thread
to write at a time.

rcount = 0;
This line initializes the `rcount` variable to 0

rcount is used to keep track of the number of active readers. Initializing it to 0 ensures no readers are
initially accessing the shared resource.

Explain what following code snippet does?


sem_wait(&mutex); rcount = rcount + 1; if(rcount = =1) sem_wait(&writeblock);
sem_post(&mutex);
This code snippet is likely part of a reader-writer problem solution using semaphores:

1. sem_wait(&mutex): Decrements a semaphore ( mutex) to enter the critical section, ensuring


exclusive access for updating shared variables.
2. rcount = rcount + 1;: Increments a variable (rcount) representing the number of active
readers.
3. if(rcount == 1) sem_wait(&writeblock); : If it's the first reader, this locks the writeblock
semaphore, preventing writers from entering.
4. sem_post(&mutex): Releases the lock on the critical section, allowing other readers to
enter.

This arrangement ensures that multiple readers can access the critical section simultaneously, but
if a writer is present (locked writeblock), new readers are temporarily blocked.
Explain what following code snippet does?
void writer(void *arg) { int f; f = *((int *)arg); sem_wait(&writeblock); data++;
printf("Data writen by the writer%d is %d\n",f,data); sleep(1); sem_post(&writeblock); }
This code represents a writer function in a reader-writer problem using semaphores:

1. sem_wait(&writeblock) : Waits for the writeblock semaphore, ensuring exclusive access for
writing.
2. data++: Increments a shared variable data, representing the data being written.
3. printf("Data written by the writer%d is %d\n", f, data): Prints the updated data along with
the writer's identifier ( f).
4. sleep(1): Simulates some processing time.
5. sem_post(&writeblock) : Releases the lock, allowing other writers or readers to access the
shared data.

Explain functions of following semaphore in a program?


sem_t mutex, full, empty
• mutex: Used for mutual exclusion, ensuring exclusive access to critical sections to
prevent race conditions.
• full : Manages the number of filled slots or items in a buffer. It helps coordinate producers
and consumers, allowing producers to signal when the buffer is full.
• empty: Manages the number of empty slots or available space in a buffer. It helps
coordinate producers and consumers, allowing consumers to signal when the buffer is
empty.

Explain why two nested semaphores have been used in following code snippet?
sem_wait(&empty); sem_wait(&mutex); //code here sem_post(&mutex); sem_post(&full);

The nested semaphores are likely used in a producer-consumer scenario to control access to a
shared buffer.

• sem_wait(&empty) : Ensures there is space in the buffer for a produced item.


• sem_wait(&mutex): Acquires a lock for the critical section to update the shared buffer.
• sem_post(&mutex): Releases the lock after updating the buffer.
• sem_post(&full): Signals that the buffer is no longer empty, allowing consumers to
proceed.

You might also like