Practical Subject at SNJB's Late Sau K B J College of Engineering
Practical subject SL-II (OS) at SNJB's Late Sau K B J College of Engineering involves implementing programs in C, shell programming, multithreading with pthreads, and demonstrating thread synchronization concepts like counting semaphores, mutex, and deadlock avoidance. The practical list includes tasks like creating an address book, sorting integers, implementing the producer-consumer problem, and showcasing solutions to deadlock scenarios. Explore the hands-on approach to software laboratory assignments in computer engineering at SNJB's College of Engineering."
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
SNJBs Late Sau. K. B. J. College of Engineering Practical Subject: SL-II(OS) Class: TE IT Teacher Name: Ms.Madhuri D.Kawade TE IT Practical Material Available on https://madhurikawade.wordpress.com/te-it-practical-sl-ii/ SNJB s KBJ CoE | Civil | Computer | E&TC | IT | Mechanical | AIDS | MBA | Visit Us @: www.snjb.org TE IT:SL-II 1
SNJBs Late Sau. K. B. J. College of Engineering SOFTWARE LABORATORY II:314447 Practical/Examination Scheme Teaching Scheme: Credit Examination Scheme: Practical: 04 Hours/Week 02 Term Work: 25 Marks Practical: 50 Marks Prerequisites: 1.C programming.2.Fundamental of Data Structure Companion Course: : OS SNJB s KBJ CoE | Civil | Computer | E&TC | IT | Mechanical | AIDS | MBA | Visit Us @: www.snjb.org TE IT:SL-II 2
SNJBs Late Sau. K. B. J. College of Engineering For Attendance Marking Type in Chat window Rno Batch FirstName LastName Batch 03 T2 Madhuri Kawade Department of Computer Engineering TE IT:SL-II 3
SOFTWARE LABORATORY II:314447 Practical List Practi cal No Title of Practical Shell programming. Write a program to implement an address book with options given below: a) Create address book. b) View address book. c) Insert a record. d) Delete a record. e) Modify a record.f) Exit. 1 Process control system calls: The demonstration of FORK, EXECVEand WAITsystem calls along with zombie and orphan states. a.Implement the C program in which main program accepts the integers to be sorted. Main program uses the FORKsystem call to create a new process called a child process. Parent process sorts the integers using sorting algorithm and waits for child process using WAITsystem call to sort the integers using any sorting algorithm. Also demonstrate zombie and orphan states b.Implement the C program in which main program accepts an integer array. Main program uses the FORKsystem call to create a new process called a child process. Parent process sorts an integer array and passes the sorted array to child process through the command line arguments of EXECVEsystem call. The child process uses EXECVEsystem call to load new program that uses this sorted array for performing the binary search to search the particular item in the array. 2 TE IT:SL-II 4
SOFTWARE LABORATORY II:314447 Practical List Ass. No Title of Practical Implement multithreading for Matrix Multiplication using pthreads 3 Thread synchronization using counting semaphores. Application to demonstrate: producer-consumer problem with counting semaphores and mutex. Thread synchronization and mutual exclusion using mutex. Application to demonstrate: Reader-Writer problem with reader priority Deadlock Avoidance Using Semaphores: Implement the deadlock-free solution to Dining Philosophers problem to illustrate the problem of deadlock and/or starvation that can occur when many synchronized threads are competing for limited resources. 4 5 6 TE IT:SL-II 5
? Thread synchronization using counting semaphores. Application to demonstrate: producer-consumer problem with counting semaphores and mutex. TE IT:SL-II 6
Basic Concept What is a Thread? What is Process? What is Synchronization? Critical Section Problem Semaphore Producer Consumer Problem TE IT:SL-II 7
Basic Concept What is a Thread? A thread is a path of execution within a process. A process can contain multiple threads. What is Process? ? In computing, a process is the instance of a computer program that is being executed by one or many threads. It contains the program code and its activity. ? Depending on the operating system (OS), a process may be made up of multiple threads of execution that execute instructions concurrently. ? TE IT:SL-II 8
Basic Concept What is Synchronization ? ? Process Synchronization means sharing system resources by processes in a such a way that, Concurrent access to shared data is handled thereby minimizing the chance of inconsistent data. ? Maintaining data consistency demands mechanisms to ensure synchronized execution of cooperating processes. TE IT:SL-II 9
Basic Concept: Critical Section Problem ? A Critical Section is a code segment that accesses shared variables and has to be executed as an atomic action. ? It means that in a group of cooperating processes, at a given point of time, only one process must be executing its critical section. ? If any other process also wants to execute its critical section, it must wait until the first one finishes. TE IT:SL-II 10
Basic Concept : Critical Section Problem TE IT:SL-II 11
Basic Concept : Critical Section Problem ? The main blocks of process are ? Entry Section To enter the critical section code, a process must request permission. Entry Section code implements this request. ? Critical Section This is the segment of code where process changes common variables, updates a table, writes to a file and so on. When 1 process is executing in its critical section, no other process is allowed to execute in its critical section. ? Exit Section After the critical section is executed , this is followed by exit section code which marks the end of critical section code. ? Remainder Section The remaining code of the process is known as remaining section. TE IT:SL-II 12
Basic Concept: Semaphore Semaphore is also an entity devised by Edsger W. Dijkstra, to solve Process Synchronization problem in OS. Its most popular use is it solve Critical Section algorithm. It uses signalling mechanism to allow access to shared resource namely by two 1.Wait 2.Signal TE IT:SL-II 13
Basic Concept: Semaphore Wait p(s) or wait(s) Wait decrements the value of semaphore by 1 Signal v(s) or signal(s) Signal increments the value of semaphore by 1 Semaphore Semaphore can only have non negative values Before start of program it is always initialised to 1 TE IT:SL-II 14
Semaphore TE IT:SL-II 15
Semaphore ? There are two types of Semaphores ? Binary Semaphore Only True/False or 0/1 values ? Counting Semaphore Non-negative value TE IT:SL-II 16
? For Binary Semaphore ? Let us try to understand the above code with an example ? Imagine that there are two processes A and B. ? At the beginning the value of semaphore is initialised as 1. ? Imagine process A wants to enter the critical section Before it can do that it checks the value of semaphore which is 1 thus, it can enter the CS and semaphore value is turned to 0 ? Now imagine that process B wants to enter too It checks the semaphore value which is 0 thus it can t enter and waits until the value is non zero non negative value ? Now, Process A finishes and signals semaphore which in turns changes semaphore value to 1 ? Thus, now process B can enter Critical section ? In this way mutual exclusion was achieved. TE IT:SL-II 17
? For Counting Semaphore ? For Counting Semaphore we initialise the value of semaphore as the number of concurrent access of critical sections we want to allow. ? For example Let us assume that the value is 3. ? Process 1 enters Critical section and semaphore value is changed to 2 ? Process 2 also enters critical section and semaphore value is changed to 1 ? Process 2 signals semaphore and comes out of critical section and Semaphore value is 2 ? Note at this moment only 1 process that is process 1 is in critical section ? Process 3 and 4 also enter critical section simultaneously and semaphore value is 0 ? At this moment there are three processes in Critical section which are process Process 1, 3, 4 ? Now imagine that process 5 wants to enter the CS. It would not be able to enter as semaphore value is 0 ? It can only enter once any of the process 1, 3, 4 signals out of the critical section. ? Thus value of semaphore in counting semaphore is equal to number of simultaneous processes allowed to access the critical section TE IT:SL-II 18
Producer Consumer Problem In computing, the producer consumer problem (also known as the bounded-buffer problem) is a classic example of a multi-process synchronization problem, proposed by Edsger W. Dijkstra. The problem describes two processes, the producer and the consumer, who share a common, fixed-size buffer used as a queue. The producer's job is to generate data, put it into the buffer, and start again. At the same time, the consumer is consuming the data (i.e., removing it from the buffer), one piece at a time. The problem is to make sure that the producer won't try to add data into the buffer if it's full and that the consumer won't try to remove data from an empty buffer. TE IT:SL-II 19
TE IT:SL-II 20
<semaphore.h> ? The <semaphore.h> header defines the sem_t type, used in performing semaphore operations. ? The following are declared as functions and may also be declared as macros. Function prototypes must be provided for use with an ISO C compiler. TE IT:SL-II 21
Basic Knowledge for Program code ? In the code, mutex, empty and full are semaphores. ? Here mutex is initialized to 1, ? empty is initialized to n (maximum size of the buffer) and full is initialized to 0. ? sem_t full; /* keep track of the number of full spots */ sem_t empty; /* keep track of the number of empty spots */ sem_t mutex; /* enforce mutual exclusion to shared data */ TE IT:SL-II 22
#include<stdio.h> #include<pthread.h> #include<semaphore.h> int buf[5],f=-1,r=-1; sem_t mutex,full,empty; void *produce(void *arg) { int i; /*Produce item */ for(i=0;i<5;i++) { /* Prepare to write item to buf */ /* If there are no empty slots, wait */ sem_wait(&empty); /* If another thread uses the buffer, wait */ sem_wait(&mutex); /*Producer start*/ printf("produced item is %d\n",i); buf[(++r)%5]=i; sleep(1); /* Release the buffer */ sem_post(&mutex); /* Increment the number of full slots */ sem_post(&full); } } Producer code TE IT:SL-II 23
Consumer code main() { pthread_t tid1,tid2; sem_init(&mutex,0,1); sem_init(&full,0,1); sem_init(&empty,0,5); void *consume(void *arg) { int item,i; for(i=0;i<5;i++) { sem_wait(&full); sem_wait(&mutex); item=buf[(++f)%5]; printf("consumed item is %d\n",item); sleep(1); sem_post(&mutex); sem_post(&empty); } } pthread_create(&tid1,NULL,produce,NULL); pthread_create(&tid2,NULL,consume,NULL) ; pthread_join(tid1,NULL); pthread_join(tid2,NULL); } 24 TE IT:SL-II
How to run program? And output ? ~$ gcc ass4.c lpthread ? ~$ ./a.out produced item is 0 produced item is 1 produced item is 2 produced item is 3 produced item is 4 consumed item is 0 consumed item is 1 consumed item is 2 consumed item is 3 consumed item is 4 ? ? ? ? ? ? ? ? ? ? Note: lpthread:: Points to a function that notifies the host that a thread has started to execute. TE IT:SL-II 25
Run Using Online shell terminal: https://cocalc.com/app TE IT:SL-II 26
TE IT:SL-II 27
Reference ? All concept: https://youtu.be/InW7IioWWjc ? Semaphore:https://youtu.be/LRiN3DJdskA ? Thread: https://www.geeksforgeeks.org/thread-in-operating-system/ ? Semaphore: https://prepinsta.com/operating-systems/semaphore/ ? Critical Section problem: https://www.studytonight.com/operating-system/process- synchronization ? difference between Mutex and Semaphore: https://prepinsta.com/operating- systems/mutex-vs-semaphore/ ? Pthread: https://www.man7.org/linux/man-pages/man3/pthread_create.3.html ? Semaphore : https://docs.oracle.com/cd/E19120-01/open.solaris/816-5137/sync- 11157/index.html ? Code releated: http://www.csc.villanova.edu/~mdamian/threads/PC.htm ? https://www.geeksforgeeks.org/semaphores-in-process-synchronization/ ? https://www.geeksforgeeks.org/producer-consumer-problem-using-semaphores-set-1/ ? https://www.geeksforgeeks.org/mutex-vs-semaphore/ ? https://www.geeksforgeeks.org/introduction-of-process-synchronization/ TE IT:SL-II 28
TE IT:SL-II 29
TE IT:SL-II 30