0% found this document useful (0 votes)
61 views

Operating System Laboratory Assignment

This document contains 4 questions from an Operating System Laboratory worksheet. Question 1 involves implementing Peterson's algorithm in C to synchronize access to a shared variable between two threads. Question 2 simulates a deadlock scenario with 3 threads competing for 3 shared resources. Question 3 displays the last 5 characters of a file by seeking to the end of the file, getting the length, and then seeking back 5 characters from the end to read and display the characters.

Uploaded by

Hitesh Sahani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Operating System Laboratory Assignment

This document contains 4 questions from an Operating System Laboratory worksheet. Question 1 involves implementing Peterson's algorithm in C to synchronize access to a shared variable between two threads. Question 2 simulates a deadlock scenario with 3 threads competing for 3 shared resources. Question 3 displays the last 5 characters of a file by seeking to the end of the file, getting the length, and then seeking back 5 characters from the end to read and display the characters.

Uploaded by

Hitesh Sahani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

OPERATING SYSTEM LABORATORY WORKSHEET

NAME: HITESH SAHANI

REGISTRATION NUMBER:11816118

ROLL NO: 18

SECTION - MS

QUESTION NO- 4

QUESTION 1

C PROGRAM TO IMPLEMENT PETERSON ALGORITHM

#include <stdio.h>
#include <pthread.h>
#include"mythreads.h"

int flag[2]; int


turn; const int MAX
= 1e9;
int ans = 0;

void lock_init()
{
flag[0] = flag[1] = 0;
turn = 0;
}
void lock(int self)
{ flag[self] =
1;

turn = 1-self;

while (flag[1-self]==1 && turn==1-self) ;


}

void unlock(int self)


{
flag[self] = 0;
}

void* func(void *s)


{ int i = 0;
int self = (int *)s;
printf("Thread Entered: %d ", self);

lock(self);

for (i=0; i<MAX; i++)


ans++;

unlock(self); }

int main() {
pthread_t p1, p2;
lock_init();

pthread_create(&p1, NULL, func, (void*)0);


pthread_create(&p2, NULL, func, (void*)1);

pthread_join(p1, NULL);
pthread_join(p2, NULL);

printf("Actual Count: %d | Expected Count: %d ",


ans, MAX*2);

return 0;
void Pthread_mutex_lock(pthread_mutex_t *m)
{
int rc = pthread_mutex_lock(m);
assert(rc == 0);
}

void Pthread_mutex_unlock(pthread_mutex_t *m)


{
int rc = pthread_mutex_unlock(m);
assert(rc == 0);
}

void Pthread_create(pthread_t *thread, const pthread_attr_t *attr,


void *(*start_routine)(void*), void *arg)
{
int rc = pthread_create(thread, attr, start_routine, arg);
assert(rc == 0);
}

void Pthread_join(pthread_t thread, void **value_ptr) {


int rc = pthread_join(thread, value_ptr);
assert(rc == 0); }
}

OUTPUT

QUESTION NO 3

CODE: PROGRAM TO STIMULATE DEADLOCK WITH 3 THREADS AND 3 RESOURCES.

#include<iostr
eam>
#include<thread>
#include<mutex>
using namespace
std; std::mutex m1;
std::mutex m2;
std::mutex m3; void
thread1()
{ m1.lock();
m2.lock(); m3.lock();
cout<<"Critical section of Thread Thread
One\n"; m1.unlock();
m2.unlock();m3.unlock();
}
void thread2()
{ m2.lock();
m1.lock(); m3.lock();
cout<<"Critical section of Thread Thread
Two\n"; m2.unlock(); m1.unlock();
m3.unlock();
}
void thread3()
{ m3.lock();
m1.lock(); m2.lock();
cout<<"Critical section of Thread Thread
Three\n"; m3.unlock(); m1.unlock();
m2.unlock(); }
int main() {
thread
t1(thread1);
thread
t2(thread2);
thread
t3(thread3);
t1.join(); t2.join();
t3.join(); return 0;
}

QUESTION NO 4

CODE: PROGRAM TO DISPLAY LAST 5 CHARACTERS. #include<unistd.h>


#include<fcntl.h>
#include<stdio.h>
#include<sys/types.h
> #include<stlib.h> int
main() { FILE *fp; char
ch; long length;

fp = fopen(“test.txt”,”r”); if (fp==
NULL) { puts(“cannot open this
file”); exit(1); } feek(fp, 0,
SEEK_END); length = ftell(fp);
fseek(fp, (length-5), SEEK_SET);

do{
ch = fgetc(fp);
putchar(ch); }while
(ch !=EOF);

fclose(fp);
return(0);
}

You might also like